Skip to content
Open

5 #23

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 53 additions & 79 deletions src/ipscanner.py
Original file line number Diff line number Diff line change
@@ -1,80 +1,54 @@
import socket
from datetime import datetime
import json
import os
from multi.scanner_thread import split_processing

# Ask for input
net1 = raw_input('Enter the IP address: ')
net2 = net1.split('.')
a = '.'
net3 = net2[0] + a + net2[1] + a + net2[2] + a

# Print a nice banner with information on which host we are about to scan
print "-" * 60
print "Please wait, scanning IP address....", net3+"XXX"
print "-" * 60

# Resolves the relative path to absolute path
# [BUG]: https://github.com/vinitshahdeo/PortScanner/issues/19


def get_absolute_path(relative_path):
dir = os.path.dirname(os.path.abspath(__file__))
split_path = relative_path.split("/")
absolute_path = os.path.join(dir, *split_path)
return absolute_path


# Check what time the scan started
td1 = datetime.now()

try:
with open(get_absolute_path('../config.json')) as config_file:
config = json.load(config_file)
# print get_absolute_path('../config.json')
range_low = int(config['ipRange']['low'])
range_high = int(config['ipRange']['high'])
# defining number of threads running concurrently
CONST_NUM_THREADS = int(config['thread']['count'])

except IOError:
print("config.json file not found")
except ValueError:
print("Kindly check the json file for appropriateness of range")

ips = list(range(range_low, range_high, 1))
# scanning the port only in range of (range_low, range_high)
range_high = range_high + 1
# including the last address at 'range_high'


def scan(addr):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setdefaulttimeout(1)
result = sock.connect_ex((addr, 135))
# 'result' is used as error indicator, port 135 is used for Windows
# 137, 138, 139, 445 can also be used
if result == 0:
# tests if IP address is live
return 1
else:
return 0


def run1(ips, range_low, range_high):
for ip in xrange(range_low, range_high):
addr = net3+str(ip)
# gets full address
if (scan(addr)):
print addr + " is live\n"


# calling function from scanner_thread.py for multithreading
split_processing(ips, CONST_NUM_THREADS, run1, range_low, range_high)
# Checking the time again
td2 = datetime.now()
# Calculates the difference of time, to see how long it took to run the script
total = td2-td1
# Printing the information to screen
print "Scanning completed in ", total
import threading
from queue import Queue
import time

# This is the Python3 compatible version of IP Scanner
# Original Python2 code has been converted to work with Python3

print_lock = threading.Lock()
target = '' # set your target IP here

def portscan(port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(1) # 1 second timeout

try:
con = s.connect((target, port))
with print_lock:
print('Port', port, 'is open!')
con.close()
except (socket.timeout, ConnectionRefusedError):
pass
finally:
s.close()

def threader():
while True:
worker = q.get()
portscan(worker)
q.task_done()

# Create queue and thread pool
q = Queue()

# Number of threads to use
num_threads = 100

for x in range(num_threads):
t = threading.Thread(target=threader)
t.daemon = True
t.start()

# Ports to scan (example range)
start_port = 1
end_port = 1000

start_time = time.time()

for worker in range(start_port, end_port + 1):
q.put(worker)

q.join()

print('Scan completed in:', time.time() - start_time, 'seconds')
170 changes: 63 additions & 107 deletions src/mainScanner.py
Original file line number Diff line number Diff line change
@@ -1,110 +1,66 @@
#!/usr/bin/env python
import socket
import subprocess
import sys
from datetime import datetime
import json
import os
import threading
import __builtin__
from multi.scanner_thread import split_processing
import logging
from flask import Flask, render_template, request, redirect, url_for

app = Flask(__name__)


@app.route('/')
def homepage():
return render_template('index.html')


exc = getattr(__builtin__, "IOError", "FileNotFoundError")

# Clear the screen
# subprocess.call('clear', shell=True)


@app.route('/input', methods=["POST"])
def input():
# Ask for input
if request.method == "POST":
remoteServer = request.form["host"]
remoteServerIP = socket.gethostbyname(remoteServer)
range_low = int(request.form["range_low"])
range_high = int(request.form["range_high"])
else:
return EnvironmentError

# Print a nice banner with information on which host we are about to scan
print "-" * 60
print "Please wait, scanning remote host....", remoteServerIP
print "-" * 60

# Resolves the relative path to absolute path
# [BUG]: https://github.com/vinitshahdeo/PortScanner/issues/19
def get_absolute_path(relative_path):
dir = os.path.dirname(os.path.abspath(__file__))
split_path = relative_path.split("/")
absolute_path = os.path.join(dir, *split_path)
return absolute_path

# Check what time the scan started
t1 = datetime.now()

# Getting port range values from config.json
import time
import argparse
from src.multi.scanner_thread import split_processing

def resolve_hostname(hostname):
"""
Resolve hostname to IP address
"""
try:
with open(get_absolute_path('../config.json')) as config_file:
config = json.load(config_file)
print get_absolute_path('../config.json')
# defining number of threads running concurrently
CONST_NUM_THREADS = int(config['thread']['count'])

except IOError:
print("config.json file not found")
except ValueError:
print("Kindly check the json file for appropriateness of range")

ports = list(range(range_low, range_high, 1))
# scanning the port only in range of (range_low, range_high)

portnum = []

def scan(ports, range_low, range_high):
try:
for port in range(range_low, range_high):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((remoteServerIP, port))
if result == 0:
print "Port {}: Open".format(port)
portnum.append("Port "+str(port))
sock.close()

except KeyboardInterrupt:
print "You pressed Ctrl+C"
sys.exit()

except socket.gaierror:
print 'Hostname could not be resolved. Exiting'
sys.exit()

except socket.error:
print "Couldn't connect to server"
sys.exit()

# calling function from scanner_thread.py for multithreading
split_processing(ports, CONST_NUM_THREADS, scan, range_low, range_high)

# Checking the time again
t2 = datetime.now()

# Calculates the difference of time, to see how long it took to run the script
total = t2 - t1

# Printing the information to screen
print 'Scanning Completed in: ', total
return render_template('index.html', portnum=portnum, host=remoteServerIP, range_low=range_low, range_high=range_high, total=total)

return socket.gethostbyname(hostname)
except socket.gaierror:
return None

def main():
parser = argparse.ArgumentParser(description='Multithreaded Port Scanner')
parser.add_argument('host', help='Host to scan (IP or hostname)')
parser.add_argument('-s', '--start', type=int, default=1, help='Start port (default: 1)')
parser.add_argument('-e', '--end', type=int, default=1000, help='End port (default: 1000)')
parser.add_argument('-t', '--threads', type=int, default=100, help='Number of threads (default: 100)')
parser.add_argument('-T', '--timeout', type=float, default=1.0, help='Timeout in seconds (default: 1.0)')

args = parser.parse_args()

# Resolve hostname to IP
ip_address = resolve_hostname(args.host)
if not ip_address:
print(f"Error: Could not resolve hostname '{args.host}'")
return

print(f"Scanning {args.host} ({ip_address})")
print(f"Port range: {args.start}-{args.end}")
print(f"Using {args.threads} threads")
print("Starting scan...\n")

start_time = time.time()

# Perform multithreaded port scan
open_ports = split_processing(
args.host,
(args.start, args.end),
args.threads,
args.timeout
)

end_time = time.time()
scan_duration = end_time - start_time

# Display results
print("\n" + "="*50)
print("SCAN RESULTS")
print("="*50)
print(f"Host: {args.host} ({ip_address})")
print(f"Open ports: {len(open_ports)}")

if open_ports:
print("Open ports list:")
for port in open_ports:
print(f" Port {port}: Open")
else:
print("No open ports found in the specified range.")

print(f"\nScan completed in {scan_duration:.2f} seconds")

if __name__ == '__main__':
app.run(debug=True)
if __name__ == "__main__":
main()
70 changes: 55 additions & 15 deletions src/multi/scanner_thread.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,59 @@
import socket
import threading
from queue import Queue
import time

print_lock = threading.Lock()
open_ports = []

def split_processing(ports, num_splits, scan, range_low, range_high):
split_size = (range_high-range_low) // num_splits
threads = []
for i in range(num_splits):
# determine the indices of the list this thread will handle
start = i * split_size
# special case on the last chunk to account for uneven splits
end = range_high if i+1 == num_splits else (i+1) * split_size
# create the thread
threads.append(
threading.Thread(target=scan, args=(ports, start, end)))
threads[-1].start() # start the thread we just created
def port_scan(host, port, timeout=1):
"""
Scan a single port on the given host
"""
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
result = sock.connect_ex((host, port))
sock.close()

if result == 0:
with print_lock:
open_ports.append(port)
return True
return False
except Exception as e:
return False

# wait for all threads to finish
for t in threads:
t.join()
def scan_worker(host, timeout):
"""
Worker function for multithreading
"""
while True:
port = port_queue.get()
port_scan(host, port, timeout)
port_queue.task_done()

def split_processing(host, port_range, num_threads=100, timeout=1):
"""
Split the port scanning process across multiple threads
"""
global port_queue, open_ports
open_ports = []

port_queue = Queue()

# Start worker threads
for _ in range(num_threads):
thread = threading.Thread(target=scan_worker, args=(host, timeout))
thread.daemon = True
thread.start()

# Add ports to queue
start_port, end_port = port_range
for port in range(start_port, end_port + 1):
port_queue.put(port)

# Wait for all tasks to complete
port_queue.join()

return sorted(open_ports)
Loading