Skip to content

Instantly share code, notes, and snippets.

@omnidan
Created December 10, 2011 22:09
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save omnidan/1456674 to your computer and use it in GitHub Desktop.
Save omnidan/1456674 to your computer and use it in GitHub Desktop.
HONEYPOT.PY | A simple honeypot written in python.
#!/usr/bin/env python
"""
Copyright (c) 2011, Daniel Bugl
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software developed by Daniel Bugl.
4. Neither the name of Daniel Bugl nor the names
of its other contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY Daniel Bugl ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import time
import socket
def getInput():
motd = raw_input('MOTD: ')
host = raw_input('IP Address: ')
while True:
try:
port = int(raw_input('Port: '))
except TypeError:
print 'Error: Invalid port number.'
continue
else:
if (port < 1) or (port > 65535):
print 'Error: Invalid port number.'
continue
else:
return (host, port, motd)
def writeLog(client, data=''):
separator = '='*50
fopen = open('./honey.mmh', 'a')
fopen.write('Time: %s\nIP: %s\nPort: %d\nData: %s\n%s\n\n'%(time.ctime(), client[0], client[1], data, separator))
fopen.close()
def main(host, port, motd):
print 'Starting honeypot!'
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(100)
while True:
(insock, address) = s.accept()
print 'Connection from: %s:%d' % (address[0], address[1])
try:
insock.send('%s\n'%(motd))
data = insock.recv(1024)
insock.close()
except socket.error, e:
writeLog(address)
else:
writeLog(address, data)
if __name__=='__main__':
try:
stuff = getInput()
main(stuff[0], stuff[1], stuff[2])
except KeyboardInterrupt:
print 'Bye!'
exit(0)
except BaseException, e:
print 'Error: %s' % (e)
exit(1)
@mayur9011
Copy link

what this honeypot do?

@ecwright3
Copy link

Lets take a second to read it.

It takes an IP address, Port, and MOTD (message of the day) as inputs. then it opens a listener. when a device makes a connection to the IP and port, they are presented with the MOTD. At the same time, the listener appends "honey.mmh" with the connecting device's details to include the first 1024 bytes of data, then it closes the connection and starts listening again. The listener can be stopped at any by typing "CTRL + C"

That's about it.

Copy link

ghost commented Dec 26, 2018

That's very intressting

@florinstefan4197
Copy link

I am a beginner in python programming and i really want to know if you can help me to run this code: when i'm executing honeypot.py, i meet the following error "Invalid syntax for except socket.error, e:"..
I cleared "e" from the syntax and now is going into BaseException
I am running like this: python3 honeypot.py 10.10.5.221 80 Message -> Is the correct type for the arguments?
Thank you

@alexa984
Copy link

alexa984 commented May 15, 2019

I am a beginner in python programming and i really want to know if you can help me to run this code: when i'm executing honeypot.py, i meet the following error "Invalid syntax for except socket.error, e:"..
I cleared "e" from the syntax and now is going into BaseException
I am running like this: python3 honeypot.py 10.10.5.221 80 Message -> Is the correct type for the arguments?
Thank you

Hi, @florinstefan4197 !
The errors you see are because of the python version. If you run the honeypot code on python3, you should replace
except socket.error, e:
with
except socket.error as e:
on both places.
Try this code instead:

import time
import socket

def getInput():
	motd = input('MOTD: ')
	host = input('IP Address: ')
	while True:
		try:
			port = int(input('Port: '))
		except TypeError:
			print ('Error: Invalid port number.')
			continue
		else:
			if (port < 1) or (port > 65535):
				print ('Error: Invalid port number.')
				continue
			else:
				return (host, port, motd)

def writeLog(client, data=''):
	separator = '='*50
	fopen = open('./honey.mmh', 'a')
	fopen.write('Time: %s\nIP: %s\nPort: %d\nData: %s\n%s\n\n'%(time.ctime(), client[0], client[1], data, separator))
	fopen.close()

def main(host, port, motd):
	print ('Starting honeypot!')
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.bind((host, port))
	s.listen(100)
	while True:
		(insock, address) = s.accept()
		print ('Connection from:{1}:{2}'.format(address[0], address[1]))
		try:
			insock.send('%s\n'%(motd))
			data = insock.recv(1024)
			insock.close()
		except socket.error as e:
			writeLog(address)
		else:
			writeLog(address, data)
        
if __name__=='__main__':
	try:
		stuff = getInput()
		main(stuff[0], stuff[1], stuff[2])
	except KeyboardInterrupt:
		print ('Bye!')
		exit(0)
	except BaseException as e:
		print('Error: {0}'.format(str(e)))
exit(1)

@florinstefan4197
Copy link

florinstefan4197 commented May 23, 2019 via email

@carnage99
Copy link

@florinstefan4197
I'm very new to python and i stumbled upon honeypots and I'm trying to learn it . I wanted to know the entire execution process of this. Did you connect your phone to the laptop hotspot? And how did you get that IP that you enter in?

@Njikamcode
Copy link

Hey ! How did i run the file please ?

@Hodgegoblin
Copy link

Hi! Thank you very much for your time and for this code. I have to face another little error -> For Example: MOTD: MessageOFTheDAY IP Address: 192.168.100.112 Port: 80 Starting honeypot! Connection from 192.168.100.112:42482 Error: a bytes-like object is required, not 'str' (This happens when i try to access the ip of my laptop from another device - my mobile phone - ).. It breaks in main at the line: try: insock,send('%s\n%(motd)) I am sorry for wasting your time! All the best!

Change line to this to make it a bytes-like object.

insock.send(f"{motd}\n".encode("UTF-8"))

@marwankh
Copy link

marwankh commented Dec 1, 2021

How to run the file please answer ASAP

@marwankh
Copy link

marwankh commented Dec 1, 2021

???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment