I have this basic port scanner script which looks very similar to the code from this site
#!/bin/python3
import socket
import sys
from datetime import datetime
#Define your target
if len(sys.argv) == 2:
target = socket.gethostbyname(sys.argv[1]) #Translate hostname to IPv4
else:
print("Invalid amount of arguments")
print("Syntax: python3 scanner.py <ip>")
sys.exit()
#Add pretty banner
print("-" * 50)
print("Scanning target " + target)
print("Time started: " + str(datetime.now()))
print("-" * 50)
try:
for port in range(50,85):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setdefaulttimeout(1)
print("Time starting port {}: ".format(port) + str(datetime.now()))
result = s.connect_ex((target, port)) #Returns an error indicator
if (result == 0):
print("Port {} is open".format(port))
s.close()
except KeyboardInterrupt:
print("Exiting program")
sys.exit()
except socket.gaierror:
print("Hostname could not be resolved")
sys.exit()
except socket.error:
print("Couldn't connect to server.")
sys.exit()
Here's the output of this script:
--------------------------------------------------
Scanning target 142.251.32.110
Time started: 2022-06-11 12:33:43.256562
--------------------------------------------------
Time starting port 50: 2022-06-11 12:33:43.256649
Time starting port 51: 2022-06-11 12:35:53.061330
Time starting port 52: 2022-06-11 12:35:54.063653
Time starting port 53: 2022-06-11 12:35:55.065565
Time starting port 54: 2022-06-11 12:35:56.067881
Time starting port 55: 2022-06-11 12:35:57.084706
Time starting port 56: 2022-06-11 12:35:58.089606
Time starting port 57: 2022-06-11 12:35:59.090590
Time starting port 58: 2022-06-11 12:36:00.485674
Time starting port 59: 2022-06-11 12:36:02.077196
Time starting port 60: 2022-06-11 12:36:03.078435
Time starting port 61: 2022-06-11 12:36:04.147746
Time starting port 62: 2022-06-11 12:36:05.148526
Time starting port 63: 2022-06-11 12:36:06.159334
Time starting port 64: 2022-06-11 12:36:07.161831
Time starting port 65: 2022-06-11 12:36:08.229387
Time starting port 66: 2022-06-11 12:36:09.340348
Time starting port 67: 2022-06-11 12:36:10.427094
Time starting port 68: 2022-06-11 12:36:11.434983
Time starting port 69: 2022-06-11 12:36:12.466602
Time starting port 70: 2022-06-11 12:36:13.879363
Time starting port 71: 2022-06-11 12:36:14.948457
Time starting port 72: 2022-06-11 12:36:16.176371
Time starting port 73: 2022-06-11 12:36:17.211928
Time starting port 74: 2022-06-11 12:36:18.319003
Time starting port 75: 2022-06-11 12:36:19.321525
Time starting port 76: 2022-06-11 12:36:20.323844
Time starting port 77: 2022-06-11 12:36:21.325633
Time starting port 78: 2022-06-11 12:36:22.338753
Time starting port 79: 2022-06-11 12:36:23.340196
Time starting port 80: 2022-06-11 12:36:24.343861
Port 80 is open
Time starting port 81: 2022-06-11 12:36:24.365744
Time starting port 82: 2022-06-11 12:36:25.372192
Time starting port 83: 2022-06-11 12:36:26.382037
Time starting port 84: 2022-06-11 12:36:27.387056
Note: Normally I would port scan my own network, but for stackover, I didn't want that to be another factor so I port scanned google instead. It has the same affect on my own network though.
As you can see, the first connection took 2 minutes and every other connection took a second. From the videos I've seen, it should be able to scan ports 50-84 in almost an instant.
I'm running this program through a Kali Virtual Machine on a NAT network using VirtualBox. I've been following the instructions from this ethical hacking video and this has been my first major roadblock.
Is there a way I can fix this issue?
EDIT: Not an OS issue
I have tested this program on some of my other computers on the local network and they all have the same issue. If it's not an issue with the code, it leads me to believe that this is some sort of router issue. I'm not quite sure what it could be though, as we leave most things on their default settings. My ISP is FibrOp if that helps at all.
EDIT: Temporary work-around but still not great
This is not the best fix but this is my best result so far.
I changed the following lines
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setdefaulttimeout(1)
to
socket.setdefaulttimeout(0.01)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Simply switching the order so the lines so that the timeout applies to the first packet as well as the others, and then decreasing the timeout delay further so that it doesn't take a whole second. You might be able to decrease it further depending on what you're connecting to.
I'm not going to make this an answer, as it is still quite slow compared to what I've seen it take in other videos.
EDIT: Might just be for direct connections
I was testing out the script with some other IPs and I found that with 127.0.0.1/localhost, it happened at the same speed as in the video. This leads me to believe that the people demonstrating this code must of had a direct connection to their router when testing it with their router (via ethernet). If this is the case, not sure why this wasn't mentioned in the videos and websites teaching about python port scanning but regardless, this is my conclusion to anyone reading this until someone else comes along. It's fast for them because they have a direct connection to the device they are port scanning, WIFI is much too slow to port scan that fast synchronously
I had the same issue as your, but I have come to bless you with the gift of threading.
this will allow your scan to be insanely fast while still being on wireless wifi AND you wont have to make the UNGODLY sacrifice of setting defaulttimeout to less than 1 or 2 seconds
integreate this into your code, made my runtime go from 1 min for range(50,85) port scan to like less than 1 second
may threading set you free