-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhoneypot.py
291 lines (241 loc) · 9.36 KB
/
honeypot.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python
# importing libraries
import argparse
import threading
import socket
import sys
from os.path import isfile
import traceback
import logging
import json
import paramiko
from paramiko.rsakey import RSAKey
# init SSH banner
SSH_BANNER = "SSH-2.0-OpenSSH_8.2p1 Ubuntu-Not-a-Honeypot-4ubuntu0.1"
HOST_KEY = None
# init arrow keys character sequence, to filter them out
UP_KEY = "\x1b[A".encode()
DOWN_KEY = "\x1b[B".encode()
RIGHT_KEY = "\x1b[C".encode()
LEFT_KEY = "\x1b[D".encode()
BACK_KEY = "\x7f".encode()
# init logger for logging all activity
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO,
filename="ssh-honeypot.log",
)
def handle_cmd(cmd, chan, ip):
"""the function handling the different commands sent to the SSH server"""
response = ""
cmd = cmd.strip()
cmds = []
if isfile("cmd-directory.json"):
file = open("cmd-directory.json")
cmds = json.load(file)["commands"]
else:
raise Exception("Command directory file not found!")
if cmd in cmds:
response = cmds[cmd]
else:
response = f"sh: 1: {cmd.split()[0]}: not found"
if response != "":
logging.info("Response from honeypot ({}): ".format(ip, response))
response = response + "\r\n"
chan.send(response)
class SshHoneypot(paramiko.ServerInterface):
"""the custom implementation of the Paramiko SSH server"""
client_ip = None
def __init__(self, client_ip):
self.client_ip = client_ip
self.event = threading.Event()
def check_channel_request(self, kind, chanid):
"""determine if a channel request of a given type will be granted, called in server when the client requests a channel, after authentication is complete"""
logging.info(
"client called check_channel_request ({}): {}".format(self.client_ip, kind)
)
if kind == "session":
return paramiko.OPEN_SUCCEEDED
def get_allowed_auths(self, username):
"""return a list of authentication methods supported by the server"""
logging.info(
"client called get_allowed_auths ({}) with username {}".format(
self.client_ip, username
)
)
return "password"
def check_auth_password(self, username, password):
"""determine if a given username and password supplied by the client is acceptable for use in authentication"""
# Accept all passwords as valid by default
logging.info(
"new client credentials ({}): username: {}, password: {}".format(
self.client_ip, username, password
)
)
return paramiko.AUTH_SUCCESSFUL
def check_channel_shell_request(self, channel):
"""determine if a shell will be provided to the client on the given channel"""
self.event.set()
return True
def check_channel_pty_request(
self, channel, term, width, height, pixelwidth, pixelheight, modes
):
"""determine if a pseudo-terminal of the given dimensions (usually requested for shell access) can be provided on the given channel"""
return True
def check_channel_exec_request(self, channel, command):
"""determine if a shell command will be executed for the client"""
command_text = str(command.decode("utf-8"))
logging.info(
"client sent command via check_channel_exec_request ({}): {}".format(
self.client_ip, command_text
)
)
return True
def handle_connection(client, addr):
"""the function handling the new client connections to the SSH server"""
client_ip = addr[0]
logging.info("New connection from: {}".format(client_ip))
print("New connection is here from: {}".format(client_ip))
try:
transport = paramiko.Transport(client)
transport.add_server_key(HOST_KEY)
# changing banner to appear more convincing
transport.local_version = SSH_BANNER
server = SshHoneypot(client_ip)
try:
transport.start_server(server=server)
except paramiko.SSHException:
print("*** SSH negotiation failed.")
raise Exception("SSH negotiation failed")
# waiting 30 seconds for auth to complete
chan = transport.accept(60)
if chan is None:
print("*** No channel (from " + client_ip + ").")
raise Exception("No channel")
chan.settimeout(None)
if transport.remote_mac != "":
logging.info("Client mac ({}): {}".format(client_ip, transport.remote_mac))
if transport.remote_compression != "":
logging.info(
"Client compression ({}): {}".format(
client_ip, transport.remote_compression
)
)
if transport.remote_version != "":
logging.info(
"Client SSH version ({}): {}".format(
client_ip, transport.remote_version
)
)
if transport.remote_cipher != "":
logging.info(
"Client SSH cipher ({}): {}".format(client_ip, transport.remote_cipher)
)
server.event.wait(10)
if not server.event.is_set():
logging.info("** Client ({}): never asked for a shell".format(client_ip))
raise Exception("No shell request")
try:
# sending a convincing MOTD to the client
chan.send(
f"{'*'*25}\n\rWelcome to Ubuntu 18.04.4 LTS (GNU/Linux 4.15.0-128-generic x86_64)\n\rPlease rest assured you are NOT in a Honeypot Server :)\n\r{'*'*25}\r\n\r\n"
)
run = True
while run:
chan.send("$ ")
command = ""
while not command.endswith("\r"):
transport = chan.recv(1024)
print(client_ip + "- received:", transport)
# echo input to pseudo-simulate a basic terminal
if (
transport != UP_KEY
and transport != DOWN_KEY
and transport != LEFT_KEY
and transport != RIGHT_KEY
and transport != BACK_KEY
):
chan.send(transport)
command += transport.decode("utf-8")
chan.send("\r\n")
command = command.rstrip()
logging.info("Command received ({}): {}".format(client_ip, command))
# handling commands to the SSH server
if command == "exit":
print(f"Connection closed (via exit command) from: {client_ip}")
logging.info(
"Connection closed (via exit command): " + client_ip + "\n"
)
run = False
else:
handle_cmd(command, chan, client_ip)
except Exception as err:
print("!!! Exception: {}: {}".format(err.__class__, err))
try:
transport.close()
except Exception:
pass
chan.close()
except Exception as err:
print("!!! Exception: {}: {}".format(err.__class__, err))
try:
transport.close()
except Exception:
pass
threads = []
def start_server(port, bind):
"""init and run the ssh server"""
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((bind, port))
except Exception as err:
print("*** Bind failed: {}".format(err))
traceback.print_exc()
sys.exit(1)
# using multi-threading to handle parallel connections
while True:
try:
sock.listen(100)
print("Listening for connection on port {} ...".format(port))
client, addr = sock.accept()
except Exception as err:
print("*** Listen/accept failed: {}".format(err))
traceback.print_exc()
new_thread = threading.Thread(target=handle_connection, args=(client, addr))
new_thread.daemon = True
new_thread.start()
threads.append(new_thread)
if __name__ == "__main__":
# parse arguments passed to the executable
parser = argparse.ArgumentParser(description="Run an SSH Honeypot Server")
parser.add_argument(
"--port",
"-p",
help="The port to bind the ssh server to (default: 2222)",
default=2222,
type=int,
action="store",
)
parser.add_argument(
"--bind",
"-b",
help="The address to bind the ssh server to (default: 0.0.0.0)",
default="0.0.0.0",
type=str,
action="store",
)
args = parser.parse_args()
# check for a server private key, if not generate a new one
try:
if isfile("server.key"):
print("Server RSA key found")
HOST_KEY = RSAKey(filename="server.key")
else:
print("No RSA key found, creating one")
HOST_KEY = RSAKey.generate(bits=2048)
HOST_KEY.write_private_key_file(filename="server.key")
start_server(args.port, args.bind)
except KeyboardInterrupt as e:
print("\r\nExiting program through keyboard interrupt.")
sys.exit(0)