-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheka.py
93 lines (71 loc) · 2.68 KB
/
heka.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python2.7
import socket, sys, threading, thread
import paramiko
# Heka - A lightweight SSH honeypot for Unix/Linux
# Keys can be generated by running the below command:
# 'ssh-keygen -t rsa -f server.key'
# Variables
HOST_KEY = paramiko.RSAKey(filename='server.key')
LOGINS = 'heka-logins.log'
LOGFILE = 'heka-ssh.log'
LOG_LOCK = threading.Lock()
# Banner that displays when Heka is run
BANNER = '''[*] Heka - A lightweight SSH Honeypot'''
print BANNER
# Requests user input on what port to run SSH service on
PORT = input('[*] Specify what port to listen on: ')
# Errors & Messages
def socketerror():
print("[!] ERROR: Failed to create socket.")
def clienterror():
print("[!] ERROR: Client handling error occurred.")
# Connection logging
class SSHServerHandler (paramiko.ServerInterface):
def __init__(self):
self.event = threading.Event()
def check_auth_password(self, username, password):
LOG_LOCK.acquire()
try:
log_handle = open(LOGINS,"a")
print("[#] Login attempt received: " + username + ":" + password)
log_handle.write(username + ":" + password + "\n")
log_handle.close()
finally:
LOG_LOCK.release()
return paramiko.AUTH_FAILED
def get_allowed_auths(self, username):
return 'password'
# Transport
def handleConnection(client):
transport = paramiko.Transport(client)
transport.add_server_key(HOST_KEY)
server_handler = SSHServerHandler()
transport.start_server(server=server_handler)
channel = transport.accept(1)
if not channel is None:
channel.close()
# Starts SSH server and provides error messages if the connection can't
# be opened on the specified port
def main():
try:
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(('', PORT))
server_socket.listen(100)
print "[*] Heka now listening on port %s" % PORT
paramiko.util.log_to_file (LOGFILE)
# Attempts to open socket
while(True):
try:
client_socket, client_addr = server_socket.accept()
thread.start_new_thread(handleConnection,(client_socket,))
# Print error and exit if socket cannot be opened
except Exception as systemerror:
clienterror()
#print (systemerror) ## Uncomment to print system provided error
# Print error and exit if socket cannot be opened
except Exception as systemerror:
socketerror()
#print(systemerror) ## Uncomment to print system provided error
sys.exit(1)
main()