def generate_script(header_dict, details_dict, searchString=None): """Generate the ruby script for the passed request. :param dict header_dict: Header dictionary containing fields like 'Host','User-Agent'. :param dict details_dict: Request specific details like body and method for the request. :param str searchString: String to search for in the response to the request. By default remains None. :return: None """ method = details_dict['method'].strip() url = get_url(header_dict['Host']) path = details_dict['path'] if path != "": url += path encoding_list = ['HEAD', 'OPTIONS', 'GET'] if details_dict['data'] and (details_dict['method'].strip() in encoding_list): encoded_data = quote(details_dict['data'], '') url = url + encoded_data if not check_valid_url(url): print("Please enter a valid URL with correct domain name and try again ") sys.exit(-1) skeleton_code = """ require 'net/http' require 'uri' uri = URI('%s')""" % (url) if method == "GET": skeleton_code += """ req = Net::HTTP::Get.new(uri.request_uri) %s %s %s response = http.request(req)""" % (generate_request_headers(header_dict), is_proxy(details_dict), is_https(url)) elif method == "POST": body = details_dict['data'] skeleton_code += """ req = Net::HTTP::Post.new(uri.request_uri) %s req.body = %s\n %s %s response = http.request(req) """ % (generate_request_headers(header_dict), str(body), is_proxy(details_dict), is_https(url)) if searchString: skeleton_code += """ puts 'Response #{response.code} #{response.message}:' begin require 'colorize' rescue LoadError puts "search option will need colorize to work properly" puts "You can install it by gem install colorize" end matched = response.body.match /%s/ original = response.body if matched then for i in 0..matched.length original.gsub! /#{matched[i]}/, "#{matched[i]}".green end end puts original """ % (searchString) else: skeleton_code += """ puts "Response #{response.code} #{response.message}: #{response.body}" """ print(skeleton_code)
def generate_script(header_dict, details_dict, searchString=None): url = get_url(header_dict['Host']) if not check_valid_url(url): print( "Please enter a valid URL with correct domain name and try again ") sys.exit(0) if details_dict['data']: details_dict['data'] = '"' + str(details_dict['data']) + '"' encoding_list = ['HEAD', 'OPTIONS', 'GET'] if details_dict['data'] and (details_dict['method'].strip() in encoding_list): print "Encoding URL" encoded_data = quote(details_dict['data'], '') url = url + encoded_data header_dict['Host'] = url details_dict['data'] = None if searchString: try: if 'proxy' not in details_dict: skeleton_code = '''\ #!/usr/bin/python from tornado.httpclient import HTTPRequest, HTTPClient from termcolor import colored import re def main(): headers, url, method = ''' + str(header_dict) + ''', "''' + url + '''" , "''' + details_dict['method'].strip() + '''" body = ''' + str(details_dict['data']) + ''' request_object = HTTPRequest(url, method=method,headers=headers, body=body, allow_nonstandard_methods=True) response_header = HTTPClient().fetch(request_object).headers match = re.findall(r"''' + searchString + '''", str(response_header)) for x in range(0, len(match)) : replace_string = colored(match[x], 'green') response_header = re.sub(match[x], replace_string, str(response_header)) print response_header if __name__ == '__main__': main() ''' else: skeleton_code = '''\ #!/usr/bin/python from tornado.httpclient import HTTPRequest, HTTPClient from termcolor import colored import re def main(): headers, url, method = ''' + str(header_dict) + ''', "''' + url +\ '''" , "''' + details_dict['method'].strip() + '''" proxy_host, proxy_port = "''' + details_dict['proxy'].split(':')[0].strip() + \ '''", "''' + details_dict['proxy'].split(':')[1].strip() + '''" body = ''' + str(details_dict['data']) + ''' request_object = HTTPRequest(url, method=method, headers=headers, proxy_host=proxy_host,\ proxy_port=proxy_port, body=body, allow_nonstandard_methods=True) response_header = HTTPClient().fetch(request_object).headers for x in range(0, len(match)) : replace_string = colored(match[x], 'green') response_header = re.sub(match[x], replace_string, str(response_header)) print response_header if __name__ == '__main__': main() ''' except IndexError: print "You haven't given the port Number" else: print skeleton_code else: try: if 'proxy' not in details_dict: skeleton_code = '''\ #!/usr/bin/python from tornado.httpclient import HTTPRequest, HTTPClient def main(): headers, url, method = ''' + str(header_dict) + ''', "''' + url + '''" , "''' + details_dict['method'].strip() + '''" body = ''' + str(details_dict['data']) + ''' request_object = HTTPRequest(url, method=method,headers=headers, body=body, allow_nonstandard_methods=True) response_header = HTTPClient().fetch(request_object).headers print response_header if __name__ == '__main__': main() ''' else: skeleton_code = '''\ #!/usr/bin/python from tornado.httpclient import HTTPRequest, HTTPClient def main(): headers, url, method = ''' + str(header_dict) + ''', "''' + url +\ '''" , "''' + details_dict['method'].strip() + '''" proxy_host, proxy_port = "''' + details_dict['proxy'].split(':')[0].strip() +\ '''", "''' + details_dict['proxy'].split(':')[1].strip() + '''" body = ''' + str(details_dict['data']) + ''' request_object = HTTPRequest(url, method=method, headers=headers,\ proxy_host=proxy_host, proxy_port=proxy_port, body=body, allow_nonstandard_methods=True) return HTTPClient().fetch(request_object).headers if __name__ == '__main__': main() ''' except IndexError: print "You haven't given the port Number" else: print(skeleton_code)