我正在python3中创建一个Honeypot项目。所以我正在使用python3套接字和_thread库来实现我的目标。这是我的代码:
import _thread
import time
from socket import socket,AF_INET,SOCK_STREAM
def OpenPort23():
while(1>0):
print("Telnet port opened on 23 port")
port=23
sk=socket(AF_INET,SOCK_STREAM)
sk.bind(('127.0.0.1',port))
sk.listen(5)
conn,addr = sk.accept()
print('alert '+addr[0]+' has connected with us on port '+str(port))
sk.close()
print("Telnet Port 23 closed")
time.sleep(3)
try:
_thread.start_new_thread(OpenPort23,())
except:
pass
while 1:
pass
因此,当我运行上述脚本时,该程序使用nmap命令正常响应,如下所示:
**Nmap Command: nmap localhost -p 23**
~/mypro $ sudo python3 port23.py
Telnet port opened on 23 port
alert 127.0.0.1 has connected with us on port 23
Telnet Port 23 closed
Telnet port opened on 23 port
alert 127.0.0.1 has connected with us on port 23
Telnet Port 23 closed
Telnet port opened on 23 port
alert 127.0.0.1 has connected with us on port 23
Telnet Port 23 closed
Telnet port opened on 23 port
alert 127.0.0.1 has connected with us on port 23
Telnet Port 23 closed
Telnet port opened on 23 port
alert 127.0.0.1 has connected with us on port 23
Telnet Port 23 closed
Telnet port opened on 23 port
现在,当我尝试在端口23上与 netcat 连接时,它崩溃了,下面是输出:
Telnet port opened on 23 port
alert 127.0.0.1 has connected with us on port 23
Telnet Port 23 closed
Telnet port opened on 23 port
Unhandled exception in thread started by <function OpenPort23 at 0x7f8b6c9dc6a8>
Traceback (most recent call last):
File "port23.py", line 12, in OpenPort23
sk.bind(('127.0.0.1',port))
OSError: [Errno 98] Address already in use
所以我应该怎么做才能消除此错误,我希望我的套接字以与nmap命令响应相同的方式进行响应。
答案 0 :(得分:1)
通常,系统将端口保留一段时间(在关闭套接字之后),然后才能继续使用。
您可以在bind()
之前使用此选项以一次使用端口。
sk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
import socket
sk = socket.socket(AF_INET,SOCK_STREAM)
sk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sk.bind(('127.0.0.1', port))
最终在大多数教程中都喜欢。创建套接字并在循环之前绑定它,仅在listen()
,accept()
内循环并使用accept()
返回的套接字,并且主套接字仅在退出循环时在程序结束时关闭。>
编辑:类似的操作(它可以同时获得5个客户)
import socket
import threading
import time
# --- functions ---
def client_handler(conn, addr):
print('connection', addr[0], addr[1])
conn.close()
# --- main ---
port = 23
sk = socket.socket()
sk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sk.bind(('127.0.0.1', port))
print('Telnet opened on port', port)
sk.listen(5)
try:
while True:
conn, addr = sk.accept()
t = threading.Thread(target=client_handler, args=(conn, addr))
t.start()
except KeyboardInterrupt as ex:
print('KeyboardInterrupt')
sk.close()
print('Telnet closed on port', port)
EIDT :更复杂的版本,可从客户端接收数据,直到客户端重置连接(nmap
)或您使用Ctr+C
(netcat
)
import socket
import threading
import time
# --- functions ---
def client_handler(conn, addr):
print('connection:', addr[0], addr[1])
try:
while True:
data = conn.recv(100)
if not data:
print('exit:', addr[0], addr[1])
break
print(data)
except Exception as ex:
print('Exception:', addr[0], addr[1], ex)
finally:
print('close:', addr[0], addr[1])
conn.close()
# --- main ---
port = 23
sk = socket.socket()
sk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sk.bind(('127.0.0.1', port))
print('Telnet opened on port', port)
sk.listen(5)
try:
while True:
conn, addr = sk.accept()
t = threading.Thread(target=client_handler, args=(conn, addr))
t.start()
except KeyboardInterrupt as ex:
print('KeyboardInterrupt')
sk.close()
print('Telnet closed on port', port)