Python for循环减慢甚至挂起

Tom*_*Tom 5 python for-loop hang

我是Python的新手(截至半小时前),并尝试编写一个简单的脚本来枚举SMTP服务器上的用户.

用户文件是一个简单的列表(每行一个)用户名.

脚本运行正常,但循环的每次迭代都会减慢,直到第14循环,它似乎完全挂起.没有错误 - 我必须^ c.

有人能解决这个问题吗?

TIA,汤姆

#!/usr/bin/python

import socket
import sys

if len(sys.argv) != 2:
        print "Usage: vrfy.py <username file>"
        sys.exit(0)

#open user file
file=open(sys.argv[1], 'r')
users=[x.strip() for x in file.readlines()]
file.close

#Just for debugging
print users

# Create a Socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to the Server
connect=s.connect(('192.168.13.222',25))

for x in users:
        # VRFY a user
        s.send('VRFY ' + x + '\r\n')
        result=s.recv(1024)
        print result

# Close the socket
s.close()
Run Code Online (Sandbox Code Playgroud)

sam*_*ias 8

您的SMTP服务器很可能是对您的客户端连接进行tarpitting.这是对失控客户端或提交大量"垃圾"命令的客户端的防御.从Postfix smtpd的联机帮助页:

   smtpd_junk_command_limit (normal: 100, stress: 1)
          The number of junk commands (NOOP, VRFY, ETRN or  RSET)  that  a
          remote  SMTP  client  can  send  before  the Postfix SMTP server
          starts to increment the error counter with each junk command.
Run Code Online (Sandbox Code Playgroud)

在看到一定量的垃圾后,smtpd守护程序将在回复之前插入1秒的延迟.如果您对相关的smtp服务器具有root访问权限,请尝试使用strace来查看服务器是否正在发出nanosleep系统调用.

以下是针对本地服务器运行脚本的跟踪.在100个VRFY命令之后,它开始在命令之间休眠.您的服务器可能有~15个垃圾命令的下限:

nanosleep({1, 0}, 0x7fffda9a67a0)       = 0
poll([{fd=9, events=POLLOUT}], 1, 300000) = 1 ([{fd=9, revents=POLLOUT}])
write(9, "252 2.0.0 pat\r\n", 15)       = 15
poll([{fd=9, events=POLLIN}], 1, 300000) = 1 ([{fd=9, revents=POLLIN}])
read(9, "VRFY pat\r\n", 4096)           = 10
Run Code Online (Sandbox Code Playgroud)

  • 别客气.通过telnet到smtpd端口并手动发出"VRFY user"命令,可以轻松确认.如果您发现响应延迟,这肯定表明您的服务器是安全漏洞.祝好运! (2认同)