标签:
搬了一年的砖,手糙得不会写代码了。闲来无聊写了个WEB目录文件扫描的小py,实现延迟随机时间,单线程,有WAF时挂个字典随机延迟扫着就行。
后期准备持续完善功能,集成一些常用的测试项,代码多了改成OO。
功能单一,主要练手,欢迎指正,代码如下:
1 # -*- coding: iso-8859-1 -*- 2 __author__ = ‘BT‘ 3 4 import urllib2 5 import sys 6 import getopt 7 import time 8 import random 9 10 global dic_file #dictionary file 11 global res_file # result file 12 global test_url # test url 13 global tar_lan # target language 14 global sca_delay # scanner delay 15 global sca_depth # scanner depth 16 global random_time_upper_limit # upper limit of random delay 17 global res_file_fp # result file point 18 dic_file = ‘‘ 19 res_file = ‘‘ 20 test_url = ‘‘ 21 tar_lan = ‘‘ 22 sca_delay = 0 23 sca_depth = 1 24 random_time_upper_limit = 0 25 res_file_fp = None 26 27 def Logo(): 28 print 29 print ‘ |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|‘ 30 print ‘ | |‘ 31 print ‘ | ==== @-**-@ |‘ 32 print ‘ | || >> || ! ! |‘ 33 print ‘ | || * || ! + + ! * * |‘ 34 print ‘ | || >> || ! + + ! * * |‘ 35 print ‘ | ==== \ ___ \ * # \ * $ |‘ 36 print ‘ | |‘ 37 print ‘ |__________________________________________|‘ 38 print 39 40 def Usage(): 41 print ‘WebFucking.py usage\nVersion 1.0‘ 42 print ‘-h, --help: print help message‘ 43 print ‘-f, --file: choose dir file‘ 44 print ‘-o, --output: output the result to a file‘ 45 print ‘-u, --url: type test url‘ 46 print ‘-l, --lan: type the target site lan, such as php,asp,aspx,jsp ‘ 47 print ‘-d, --delay: scanner delay, x seconds‘ 48 print ‘-r, --random: upper limit of random delay‘ 49 print ‘-p, --dePth: scanner depth‘ 50 51 def main(argv): 52 global dic_file 53 global res_file 54 global test_url 55 global tar_lan 56 global sca_delay 57 global sca_depth 58 global random_time_upper_limit 59 global res_file_fp 60 61 Logo() 62 63 try: 64 opts, args = getopt.getopt(argv[1:], ‘hf:u:l:o:d:r:p:‘, [‘help‘, ‘file=‘, ‘url=‘, ‘lan=‘, ‘output=‘, ‘delay=‘, ‘random=‘, ‘depth=‘]) 65 except getopt.GetoptError, err: 66 print str(err) 67 Usage() 68 sys.exit(2) 69 70 for o, a in opts: 71 if o in (‘-h‘, ‘--help‘): 72 Usage() 73 sys.exit(1) 74 elif o in (‘-f‘, ‘--file‘): 75 dic_file = a 76 elif o in (‘-o‘, ‘--output‘): 77 res_file = a 78 try: 79 res_file_fp = open(res_file, "a") 80 except IOError: 81 print >> sys.stderr, "File could not be opened" 82 sys.exit(1) 83 elif o in (‘-u‘, ‘--url‘): 84 # 判断URL是否结尾为/ 85 if a[len(a)-1] != ‘/‘: 86 a += ‘/‘ 87 test_url = a 88 elif o in (‘-l‘, ‘--lan‘): 89 tar_lan = a 90 elif o in (‘-d‘, ‘--delay‘): 91 sca_delay = float(a) 92 elif o in (‘-p‘, ‘--depth‘): 93 sca_depth = int(a) 94 elif o in (‘-r‘, ‘--random‘): 95 random_time_upper_limit = float(a) 96 else: 97 print ‘unhandled option‘ 98 sys.exit(3) 99 100 do_scanner(test_url, sca_depth) 101 102 def do_scanner(url = ‘‘, depth = 1): 103 ‘‘‘ 104 the main method to scanner dir and file 105 :param url: final test url 106 :param depth: scanner depth 107 :return: no return 108 ‘‘‘ 109 global res_file_fp 110 global tar_lan 111 if depth < 1: 112 return 113 114 # open file 115 try: 116 fp = open(dic_file, "r") 117 except IOError: 118 print >> sys.stderr, "File could not be opened" 119 sys.exit(1) 120 key = fp.readline() 121 while key: 122 if key[len(key)-1] == ‘\n‘: # 判斷最後一位是否爲回車 123 key = key[:len(key)-1] 124 125 # dir scanner 126 fin_url1 = url + key + ‘/‘ 127 rsp_code1 = get_response_code(fin_url1) 128 print fin_url1 + ‘--------‘ + str(rsp_code1) 129 if res_file_fp != None: 130 res_file_fp.write(fin_url1 + ‘--------‘ + str(rsp_code1) + ‘\n‘) 131 res_file_fp.flush() 132 # file scanner 133 if tar_lan != ‘‘: 134 fin_url2 = url + key + ‘.‘ + tar_lan 135 rsp_code2 = get_response_code(fin_url2) 136 print fin_url2 + ‘--------‘ + str(rsp_code2) 137 if res_file_fp != None: 138 res_file_fp.write(fin_url2 + ‘--------‘ + str(rsp_code2) + ‘\n‘) 139 res_file_fp.flush() 140 141 # recursion the next scanner 142 if rsp_code1 != 404: # when dir response code not equals 404 143 do_scanner(fin_url1, depth-1) 144 145 key = fp.readline() 146 fp.close() 147 148 def get_response_code(url = ‘‘): 149 ‘‘‘ 150 input url, return response code, and delay x seconds 151 :param url: final test url 152 :return: response code 153 ‘‘‘ 154 global sca_delay 155 global random_time_upper_limit 156 157 # sleep random time 158 sleep_time = sca_delay + random.uniform(0,random_time_upper_limit) 159 time.sleep(sleep_time) 160 161 response = None 162 try: 163 response = urllib2.urlopen(url) 164 except urllib2.URLError as e: 165 if response: 166 response.close() 167 return e.code 168 if response: 169 response.close() 170 return 200 171 172 if __name__ == ‘__main__‘: 173 main(sys.argv)
标签:
原文地址:http://www.cnblogs.com/btlulu/p/4631645.html