-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathport_scanner_banner.py
60 lines (50 loc) · 2.45 KB
/
port_scanner_banner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import argparse
import socket
import threading
def connection_scan(target_ip, target_port):
"""Attempts to create a socket connection with the given IP address and port.
If successful, the port is open. If not, the port is closed. In addition, a byte literal is sent to the target; the
result is captured and printed as a system banner.
"""
try:
conn_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn_socket.connect((target_ip, target_port))
conn_socket.send(b'Banner_query\r\n')
results = conn_socket.recv(100)
print("[+] {}/tcp open".format(target_port))
print("[+] {}".format(str(results))) # Print banner grab results
except OSError:
print("[-] {}/tcp closed".format(target_port))
finally:
conn_socket.close() # Ensure the connection is closed
def port_scan(target, port_num):
"""Scan indicated ports for status.
First, it attempts to resolve the IP address of a provided hostname, then enumerates through the ports. Threads are
used to call the connection_scan() function.
"""
try:
target_ip = socket.gethostbyname(target)
except OSError:
print("[^] Cannot resolve {}: Unknown host".format(target))
return # Exit scan if target IP is not resolved
try:
target_name = socket.gethostbyaddr(target_ip)
print('[*] Scan Results for: {}'.format(target_name[0]))
except OSError:
print('[*] Scan Results for: {}'.format(target_ip))
t = threading.Thread(target=connection_scan, args=(target_ip, int(port_num)))
t.start()
def argument_parser():
"""Allow user to specify target host and port."""
parser = argparse.ArgumentParser(description="TCP port scanner. Accepts a hostname/IP address and list of ports to "
"scan. Attempts to identify the service running on a port.")
parser.add_argument("-o", "--host", required=True, help="Host IP address")
parser.add_argument("-p", "--ports", required=True, help="Comma-separated port list, such as '25,80,8080'")
var_args = vars(parser.parse_args()) # Convert argument namespace to dictionary
return var_args
if __name__ == '__main__':
user_args = argument_parser()
host = user_args["host"]
port_list = user_args["ports"].split(",") # Make a list from port numbers
for port in port_list:
port_scan(host, port)