I am creating a Honeypot project in python3. So I am using python3 socket and _thread library in order to accomplish my goal. Here is my code:
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
So when i run the above script, the program is responding normal with nmap command as below:
**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
Now when I have tried to connect with netcat on port 23 then its crashed and below is the output:
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
So what should I do to remove this error and I want my socket to respond the same way as it is responding with nmap command.
while 1: passis really bad. That thread will be completely consumed by doing nothing at all while wanting to use as much CPU time as it can. At least usewhile 1: time.sleep(1)or something along those lines.