annotate port_scan.py @ 38:16840a2375ae

Adapt to CentOS7
author atton
date Fri, 06 Nov 2015 21:51:43 +0900
parents 83d66786f576
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
33
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
1 #!/usr/bin/python
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
2
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
3
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
4 from optparse import OptionParser
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
5
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
6 import socket
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
7
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
8 def h2ip(host):
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
9 try:
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
10 ip = socket.gethostbyname(host)
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
11 return ip
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
12 except:
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
13 return None
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
14
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
15 def connect_to(host, port):
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
16 try:
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
17 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
18 s.connect((host,port))
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
19 return s
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
20 except:
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
21 s.close()
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
22 return None
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
23
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
24 def bgrabber(sock):
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
25 try:
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
26 sock.send("port scan now!")
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
27 banner = sock.recv(1024)
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
28 return banner
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
29 except:
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
30 return None
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
31
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
32 def scan(host, port):
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
33 sock = connect_to(host, port)
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
34 socket.setdefaulttimeout(5)
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
35 if sock:
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
36 print("++ Connect %s:\t%d" %(host, port))
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
37 banner = bgrabber(sock)
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
38 if banner:
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
39 print("++ Grab banner :\n\t%s" %banner)
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
40 else:
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
41 print("-- Can't grab the target banner")
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
42 sock.close()
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
43 else:
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
44 print("-- Not connect %s:\t%d" % (host, port))
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
45
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
46
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
47
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
48 if __name__=="__main__":
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
49 parser=OptionParser()
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
50 parser.add_option("-t", "--target", dest="host", type="string",
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
51 help="enter host name", metavar="exemple.com")
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
52 parser.add_option("-p", "--port", dest="ports", type="string",
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
53 help="port you want to scan separated by comma", metavar="PORT")
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
54
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
55 (options, args) = parser.parse_args()
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
56
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
57 if options.host == None or options.ports == None:
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
58 parser.print_help()
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
59 else:
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
60 host = options.host
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
61 ports = (options.ports).split(",")
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
62 try:
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
63 ports = list(filter(int, ports))
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
64 ip = h2ip(host)
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
65 if ip:
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
66 print("++ Running scan on %s"%host)
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
67 print("++ Target IP: %s"%ip)
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
68 for port in ports:
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
69 scan(host, int(port))
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
70 else:
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
71 print("-- Invalid host")
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
72 except:
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
73 print("Invalid port list (e.g: -p 21,22,53,..)")
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
74
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
75
83d66786f576 add port scanner that was written in python.
taiki
parents:
diff changeset
76