cyber-security-resources/python_ruby_and_bash/quick_scanner.py

63 lines
1.6 KiB
Python
Raw Normal View History

2018-03-13 19:11:29 -04:00
#!/usr/bin/python
# Author: Omar Santos @santosomar
# version 1.0
# This is a quick demonstration on how to create a
# basic TCP port scanner using python.
#####################################################################
2018-06-02 09:34:19 -04:00
from __future__ import print_function
2018-03-13 19:11:29 -04:00
2018-06-02 09:34:19 -04:00
import socket, subprocess, sys
2018-03-13 19:11:29 -04:00
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
2018-06-02 09:34:19 -04:00
subprocess.call('clear', shell=True)
2018-03-13 19:11:29 -04:00
2018-06-02 09:34:19 -04:00
print('''\t
2018-03-13 19:11:29 -04:00
___ __ __ _ ___ _ ___
/ _ \| \/ | /_\ | _ ( ) __|
| (_) | |\/| |/ _ \| //\__ \
\___/|_| _|_/_/_\_\_|_\ |___/
| | |_ _|_ _|_ _| | | __|
| |__ | | | | | | | |__| _|
|____|___| |_| _|_| |____|___|__
/ __|/ __| /_\ | \| | \| | __| _ \\
\__ \ (__ / _ \| .` | .` | _|| /
|___/\___/_/ \_\_|\_|_|\_|___|_|_\\
2018-06-02 09:34:19 -04:00
''')
2018-03-13 19:11:29 -04:00
target_ip = raw_input("\t Please enter the IP address of the target host:").strip()
port_1 = int(raw_input("\t Enter the first port to scan:\t").strip())
port_2 = int(raw_input("\t Enter the last port to scan:\t").strip())
2018-06-02 09:34:19 -04:00
print("~"*50)
print("\n ...scanning target now. ", target_ip)
print("~"*50)
2018-03-13 19:11:29 -04:00
try:
2018-06-02 09:34:19 -04:00
for port in range(port_1, port_2):
sock= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
2018-03-13 19:11:29 -04:00
socket.setdefaulttimeout(1)
2018-06-02 09:34:19 -04:00
result = sock.connect_ex((target_ip, port))
2018-03-13 19:11:29 -04:00
if result==0:
2018-06-02 09:34:19 -04:00
print("Found open port:\t", port)
2018-03-13 19:11:29 -04:00
sock.close()
except KeyboardInterrupt:
2018-06-02 09:34:19 -04:00
print("[!] Scan stopped by user... ")
2018-03-13 19:11:29 -04:00
sys.exit()
except socket.gaierror:
2018-06-02 09:34:19 -04:00
print("[!] The target's hostname could not be resolved...")
2018-03-13 19:11:29 -04:00
sys.exit()
except socket.error:
2018-06-02 09:34:19 -04:00
print("[!] Target is unreachable...")
2018-03-13 19:11:29 -04:00
sys.exit()
2018-06-02 09:34:19 -04:00
print("The scan is complete. Happy hacking!")