Python????Nmap???????
???????????? ???????[ 2016/7/29 11:31:22 ] ?????????????????? Nmap Python
1 #coding=utf-8
2
3 import nmap
4 import optparse
5 import threading
6 import sys
7 import re
8 '''
9 ?譎?python_nmap???????2.x???3.x
10 python_nmap??????python????nmap?????????
11
12 ????????????????
13 1.????nmap?????
14 class PortScanner()
15 __init__(self?? nmap_search_path=('nmap'?? '/usr/bin/nmap'?? '/usr/local/bin/nmap'?? '/sw/bin/nmap'?? '/opt/local/bin/nmap'))
16 Initialize PortScanner module
17
18 * detects nmap on the system and nmap version
19 * may raise PortScannerError exception if nmap is not found in the path
20
21 :param nmap_search_path: tupple of string where to search for nmap executable. Change this if you want to use a specific version of nmap.
22 :returns: nothing
23 2.?????????
24 scan(self?? hosts='127.0.0.1'?? ports=None?? arguments='-sV'?? sudo=False)
25 Scan given hosts
26
27 May raise PortScannerError exception if nmap output was not xml
28
29 Test existance of the following key to know if something went wrong : ['nmap']['scaninfo']['error']
30 If not present?? everything was ok.
31
32 :param hosts: string for hosts as nmap use it 'scanme.nmap.org' or '198.116.0-255.1-127' or '216.163.128.20/20'
33 :param ports: string for ports as nmap use it '22??53??110??143-4564'
34 :param arguments: string of arguments for nmap '-sU -sX -sC'
35 :param sudo: launch nmap with sudo if True
36
37 :returns: scan_result as dictionnary
38
39 ??????????
40 import nmap
41 scanner = nmap.PortScanner() #nmap_search_path???????nmap????·?????????·???????nmap?????????
42 results = scanner.scan(hosts='192.168.2.1'??ports='80')
43 pprint.pprint(results)
44 {'nmap': {'command_line': 'nmap -oX - -p 80 -sV 192.168.2.1'??
45 'scaninfo': {'tcp': {'method': 'syn'?? 'services': '80'}}??
46 'scanstats': {'downhosts': '0'??
47 'elapsed': '11.59'??
48 'timestr': 'Thu Jul 21 10:08:34 2016'??
49 'totalhosts': '1'??
50 'uphosts': '1'}}??
51 'scan': {'192.168.2.1': {'addresses': {'ipv4': '192.168.2.1'??
52 'mac': 'D0:C7:C0:6A:F6:A0'}??
53 'hostnames': []??
54 'status': {'reason': 'arp-response'??
55 'state': 'up'}??
56 'tcp': {80: {'conf': '3'??
57 'cpe': ''??
58 'extrainfo': ''??
59 'name': 'http'??
60 'product': ''??
61 'reason': 'no-response'??
62 'state': 'filtered'??
63 'version': ''}}??
64 'vendor': {'D0:C7:C0:6A:F6:A0': 'Tp-link '
65 'Technologies'}}}}
66
67 '''
68 def anlyze_port(target_port):
69 #????-p???????????????????б?
70 try:
71 pattern = re.compile(r'(d+)-(d+)') #?????????-??
72 match = pattern.match(target_port)
73 if match:
74 start_port = int(match.group(1))
75 end_port = int(match.group(2))
76 return([x for x in range(start_port??end_port + 1)])
77 else:
78 return([int(x) for x in target_port.split('??')])
79 except Exception as err:
80 print('????????1:'??sys.exc_info()[0]??err)
81 print(parser.usage)
82 exit(0)
83
84 def portscanner(target_host??target_port):
85 scanner = nmap.PortScanner()
86 results = scanner.scan(hosts=target_host??ports=target_port??arguments='-T4 -A -v -Pn ') #??ping????????
87 print('?????????'??results['nmap']['command_line'])
88 print('[*]????' + target_host + '??' + str(target_port) + '????????' + results['scan'][target_host]['tcp'][int(target_port)]['state'])
89
90 def main():
91 usage = 'Usage:%prog --host <target_host> --port <target_port>'
92 parser = optparse.OptionParser(usage??version='v1.0')
93 parser.add_option('--host'??dest='target_host'??type='string'??
94 help='???????????????????IP')
95 parser.add_option('--port'??dest='target_port'??type='string'??
96 help='??????????????????1-100??21??53??80???????')
97 (options??args) = parser.parse_args()
98 if options.target_host == None or options.target_port == None:
99 print(parser.usage)
100 exit(0)
101 else:
102 target_host = options.target_host
103 target_port = options.target_port
104
105 target_port = anlyze_port(target_port)
106 for port in target_port:
107 t = threading.Thread(target=portscanner??args=(target_host??str(port)))
108 t.start()
109
110 if __name__ == '__main__':
111 main()
???????к???????
????1 c:python34python.exe NmapScanner.py --host 192.168.1.1 --port 80
????2 ????????? nmap -oX - -p 80 -T4 -A -v -Pn 192.168.1.1
????3 [*]????192.168.1.1??80????????filtered
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11