扫描IP的端口是否开放:Porttest.py
1 # -*- coding: utf-8 -*- 2 import sys 3 import os 4 import socket 5 6 #扫描 7 def scanport(ip,port): 8 try: 9 socket.setdefaulttimeout(2) 10 s=socket.socket() 11 s.connect((ip,port)) 12 portrecv=s.recv(1024) 13 return portrecv 14 except Exception as e: 15 print(e) 16 17 ‘‘‘ 18 检测ip的合法性 19 filter(fuction, list)检查list中符合fuction的元素 20 ‘‘‘ 21 def ip_check(ip): 22 q = ip.split(‘.‘) 23 return len(q)==4 and len(list(filter(lambda x: x>=0 and x<= 255, list(map(int, filter(lambda x: x.isdigit(), q))))))==4 24 25 def main(): 26 if len(sys.argv)==2: 27 ip=sys.argv[1] 28 portlist=[21,22,25,80,110,443] 29 if ip_check(ip)==0: 30 print("输入的不是一个合法的ip") 31 return 32 for port in portlist: 33 portcheck=scanport(ip,port) 34 if portcheck: 35 print(str(ip)+‘:‘+str(portchec1)) 36 else: 37 print("Tips:python3 Porttest.py 目标ip\n") 38 39 if __name__==‘__main__‘: 40 main()
生成各种进制的IP:IP Converter.py
1 # -*- coding: utf-8 -*- 2 import sys 3 import socket 4 import struct 5 import itertools 6 7 #纯8进制 8 def ip_split_by_comma_oct(ip): 9 """ 10 set函数是一个无序不重复的元素集,用于关系测试和去重 11 print ip_split_oct -> [‘0177‘, ‘0‘, ‘0‘, ‘01‘] 12 print parsed_result -> set([‘0177.0.0.01‘]) 13 """ 14 parsed_result = set() 15 ip_split = str(ip).split(‘.‘) 16 ip_split_oct = [oct(int(_)) for _ in ip_split] 17 parsed_result.add(‘.‘.join(ip_split_oct)) 18 return parsed_result 19 20 #纯16进制 21 def ip_split_by_comma_hex(ip): 22 """ 23 print ip_split_hex -> [‘0x7f‘, ‘0x0‘, ‘0x0‘, ‘0x1‘] 24 print parsed_result -> set([‘0x7f.0x0.0x0.0x1‘]) 25 """ 26 parsed_result = set() 27 ip_split = str(ip).split(‘.‘) 28 ip_split_hex = [hex(int(_)) for _ in ip_split] 29 parsed_result.add(‘.‘.join(ip_split_hex)) 30 return parsed_result 31 32 #10进制,8进制 33 def combination_oct_int_ip(ip): 34 """ 35 itertools.combinations(iterable,r) 36 创建一个迭代器,返回iterable中长度为r的序列。 37 print oct_2 -> [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)] 38 print oct_3 -> [(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)] 39 enumerate用来枚举函数 40 tuple表示元组 41 """ 42 result = set() 43 parsed_result = set() 44 ip_split = str(ip).split(‘.‘) 45 oct_2 = list(itertools.combinations([0, 1, 2, 3], 2)) 46 oct_3 = list(itertools.combinations([0, 1, 2, 3], 3)) 47 #变化ip的一段 48 for n, _ in enumerate(ip_split): 49 _tmp = oct(int(_)) 50 #ip_split[:n] -> []读取前面的数 ip_split[n+1:]-> [‘0‘, ‘0‘, ‘1‘]读取后面的数 51 _delete = ip_split[:n] + ip_split[n+1:] 52 _delete.insert(n, _tmp) 53 result.add(tuple(_delete)) 54 #变化ip的两段 55 for _ in oct_2: 56 _tmp_ip = ip_split[:] 57 _tmp1 = oct(int(ip_split[_[0]])) 58 _tmp2 = oct(int(ip_split[_[1]])) 59 del _tmp_ip[_[0]] 60 del _tmp_ip[_[1]-1] 61 _tmp_ip.insert(_[0], _tmp1) 62 _tmp_ip.insert(_[1], _tmp2) 63 result.add(tuple(_tmp_ip)) 64 #变化ip的三段 65 for _ in oct_3: 66 _tmp_ip = ip_split[:] 67 _tmp1 = oct(int(ip_split[_[0]])) 68 _tmp2 = oct(int(ip_split[_[1]])) 69 _tmp3 = oct(int(ip_split[_[2]])) 70 del _tmp_ip[_[0]] 71 del _tmp_ip[_[1] - 1] 72 del _tmp_ip[_[2] - 2] 73 _tmp_ip.insert(_[0], _tmp1) 74 _tmp_ip.insert(_[1], _tmp2) 75 _tmp_ip.insert(_[2], _tmp3) 76 result.add(tuple(_tmp_ip)) 77 for _ in result: 78 parsed_result.add(‘.‘.join(_)) 79 return parsed_result 80 81 #16进制,10进制 82 def combination_hex_int_ip(ip): 83 """ 84 :param ip: 85 :return: 86 """ 87 result = set() 88 parsed_result = set() 89 ip_split = str(ip).split(‘.‘) 90 hex_2 = list(itertools.combinations([0, 1, 2, 3], 2)) 91 hex_3 = list(itertools.combinations([0, 1, 2, 3], 3)) 92 for n, _ in enumerate(ip_split): 93 _tmp = hex(int(_)) 94 _delete = ip_split[:n] + ip_split[n+1:] 95 _delete.insert(n, _tmp) 96 result.add(tuple(_delete)) 97 for _ in hex_2: 98 _tmp_ip = ip_split[:] 99 _tmp1 = hex(int(ip_split[_[0]])) 100 _tmp2 = hex(int(ip_split[_[1]])) 101 del _tmp_ip[_[0]] 102 del _tmp_ip[_[1] - 1] 103 _tmp_ip.insert(_[0], _tmp1) 104 _tmp_ip.insert(_[1], _tmp2) 105 result.add(tuple(_tmp_ip)) 106 for _ in hex_3: 107 _tmp_ip = ip_split[:] 108 _tmp1 = hex(int(ip_split[_[0]])) 109 _tmp2 = hex(int(ip_split[_[1]])) 110 _tmp3 = hex(int(ip_split[_[2]])) 111 del _tmp_ip[_[0]] 112 del _tmp_ip[_[1] - 1] 113 del _tmp_ip[_[2] - 2] 114 _tmp_ip.insert(_[0], _tmp1) 115 _tmp_ip.insert(_[1], _tmp2) 116 _tmp_ip.insert(_[2], _tmp3) 117 result.add(tuple(_tmp_ip)) 118 for _ in result: 119 parsed_result.add(‘.‘.join(_)) 120 return parsed_result 121 122 #10进制,16进制,8进制 123 def combination_hex_int_oct_ip(ip): 124 """ 125 :param ip: 126 :return: 127 """ 128 result = set() 129 parsed_result = set() 130 ip_split = str(ip).split(‘.‘) 131 hex_3 = list(itertools.combinations([0, 1, 2, 3], 3)) 132 for n1, n2, n3 in hex_3: 133 _tmp_ip = ip_split[:] 134 _tmp_2 = oct(int(_tmp_ip[n2])) 135 _tmp_3 = hex(int(_tmp_ip[n3])) 136 del _tmp_ip[n2] 137 del _tmp_ip[n3 - 1] 138 _tmp_ip.insert(n2, _tmp_2) 139 _tmp_ip.insert(n3, _tmp_3) 140 result.add(tuple(_tmp_ip)) 141 for _ in result: 142 parsed_result.add(‘.‘.join(_)) 143 return parsed_result 144 145 ‘‘‘ 146 socket.inet_aton() 把IPV4地址转化为32位打包的二进制格式 -> 检查是否为ipv4 147 struct.unpack(fmt,string) 按照给定的格式(fmt)解析字节流string,返回解析出来的tuple 148 !L: ! = network(=big-endian) L = unsigned long 149 ‘‘‘ 150 if __name__ == ‘__main__‘: 151 if len(sys.argv)==2: 152 ip = sys.argv[1] 153 ip_int = struct.unpack(‘!L‘, socket.inet_aton(ip))[0] 154 ip_oct_no_comma = oct(ip_int) 155 ip_hex_no_comma = hex(ip_int) 156 ip_oct_by_comma = ip_split_by_comma_oct(ip) 157 ip_hex_by_comma = ip_split_by_comma_hex(ip) 158 all_result = ip_oct_by_comma | ip_hex_by_comma | combination_oct_int_ip(ip) | combination_hex_int_ip(ip) | combination_hex_int_oct_ip(ip) 159 print ip_int 160 print ip_oct_no_comma 161 print ip_hex_no_comma 162 for _ip in all_result: 163 print _ip 164 else: 165 print("Tips: IP.py 127.0.0.1 \n") 166
爆破解压zip文件:ZIP.py
1 # -*- coding:utf-8 -*- 2 import sys 3 import zipfile 4 import threading 5 6 def extractFile(zFile,password): 7 try: 8 zFile.extractall(pwd=password.encode("utf-8")) 9 print("Password is: "+password) 10 except Exception as e: 11 print(str(e)) 12 13 def main(): 14 if len(sys.argv) == 3: 15 zFile = zipfile.ZipFile(sys.argv[1]) 16 passFile = open(sys.argv[2]) 17 for line in passFile.readlines(): 18 password = line.strip(‘\n‘) 19 t = threading.Thread(target=extractFile,args=(zFile,password)) 20 t.start() 21 else: 22 print("Tips:python3 ZIP.py 要爆破的文件 字典") 23 24 if __name__ ==‘__main__‘: 25 main()
学习和总结
python绝技:运用python成为顶级黑客
一个生成各种进制格式IP的小工具:http://www.freebuf.com/sectool/140982.html