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