与Python插座编程建立连接,花了很长时间

发布于 2025-02-06 10:13:28 字数 4456 浏览 2 评论 0原文

我有这个基本的端口扫描仪脚本,看起来与

#!/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()

这是此脚本的输出:

--------------------------------------------------
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

注意:通常我会端口扫描自己的网络,但是对于Stackover,我不希望这是另一个因素,所以我将端口扫描了Google。不过,它对我自己的网络具有相同的影响。

如您所见,第一个连接花费了2分钟,其他连接花费了一秒钟。从我看过的视频中,它几乎可以立即扫描50-84。

我正在使用VirtualBox上的NAT网络上的Kali虚拟机运行此程序。我一直在按照这是我的第一个主要障碍。

有什么方法可以解决此问题?

编辑:不是OS问题

我已经在本地网络上的其他一些计算机上测试了该程序,它们都有相同的问题。如果代码不是问题,它会使我相信这是某种路由器问题。我不太确定会发生什么,因为我们将大多数内容留在他们的默认设置上。我的ISP是 fibrop 如果这完全有帮助。

编辑:临时工作,但仍然不是很好

这不是最好的解决方案,但这是我到目前为止的最佳结果。

我将以下行更改

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setdefaulttimeout(1)

socket.setdefaulttimeout(0.01)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

简单地切换订单,以便行以使超时适用于第一个数据包以及其他数据包,然后进一步减少超时延迟,以免整整一秒钟。您可能会根据与您的连接相关的方式进一步降低它。

我不会为此做出答案,因为与我在其他视频中看到的相比,它仍然很慢。

编辑:可能只是用于直接连接

我正在与其他一些IP一起测试脚本,发现使用127.0.0.1/localhost,它以与视频中相同的速度发生。这使我相信,证明这一代码的人们在使用路由器(通过以太网)进行测试时必须直接与路由器联系。如果是这种情况,则不确定为什么在有关Python Port扫描的视频和网站中没有提到这一点,但是无论如何,这是我对任何阅读此书的结论,直到其他人出现为止。 对他们来说很快,因为他们与端口扫描的设备有直接连接,WiFi太慢,无法同步 syport scan

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她比我温柔 2025-02-13 10:13:31

我遇到了与您相同的问题,但是我来祝福您的礼物。

这将使您的扫描能够快速快速,同时仍在无线wifi 上,您不必将 ungodly 牺牲设置 defaulttime out 超过1或2秒

将其整合到您的代码中,使我的运行时从 1分钟范围(50,85)端口扫描到喜欢 少于1秒五月

import threading

#check for sys.argv and whatever your doign before the for loop for socket connect code

starttime = str(datetime.now())

def checkport(port):
    ...
    # your socket connect code here using port arg as the port

threads = []

start = 50 #begining of list of ports to scan

end = 85 #end of list of ports to scan

ltype = list(range(start, end))

for x in ltype:
    t = threading.Thread(target=checkport, args=(x,))
    t.daemon = True
    threads.append(t)
for x in range(len(threads)):
    threads[x].start()
for x in range(len(threads)):
    threads[x].join()

print(starttime)
print(datetime.now())

五月螺纹集你有空

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

import threading

#check for sys.argv and whatever your doign before the for loop for socket connect code

starttime = str(datetime.now())

def checkport(port):
    ...
    # your socket connect code here using port arg as the port

threads = []

start = 50 #begining of list of ports to scan

end = 85 #end of list of ports to scan

ltype = list(range(start, end))

for x in ltype:
    t = threading.Thread(target=checkport, args=(x,))
    t.daemon = True
    threads.append(t)
for x in range(len(threads)):
    threads[x].start()
for x in range(len(threads)):
    threads[x].join()

print(starttime)
print(datetime.now())

may threading set you free

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文