-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStinger.py
285 lines (247 loc) · 11.8 KB
/
Stinger.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
# -*- coding: utf-8 -*-
import logging
import socket
import threading
import argparse
import time
import sys
import traceback
import paramiko
import colors
import os
from twisted.python import log
from commands import __all__
from pynput import keyboard
from commands.adduser import Command_adduser
from commands.apt import APTCommand
from StingerServer import StingerServer
from commands.ls import LSCommand
print(
colors.bcolors.COLOR['YELLOW'] +
'============================================================================\n' +
' /$$$$$$\ $$$$$$$$\ $$$$$$\ |$$\ $$\ /$$$$$$\ |$$$$$$$$\ |$$$$$$$\\ \n' +
'|$$ __$$\ \__$$ __|\_$$ _| |$$$\ $$ | |$$ __$$\ |$$ _____| |$$ __$$\\\n' +
'|$$ / \__| |$$ | |$$ | |$$$$\ $$ | |$$ / \__| |$$ | |$$ | $$ |\n' +
'\$$$$$$\ |$$ | |$$ | |$$ $$\$$ | |$$ |$$$$\ |$$$$$\ |$$$$$$$ |\n' +
' \____$$\ |$$ | |$$ | |$$ \$$$$ | |$$ |\_$$ | |$$ __| |$$ __$$< \n' +
'|$$\ $$ | |$$ | |$$ | |$$ |\$$$ | |$$ | $$ | |$$ | |$$ | $$ |\n' +
'\$$$$$$ | |$$ | $$$$$$\ |$$ | \$$ | \$$$$$$ | |$$$$$$$$\ |$$ | $$ |\n' +
'\_______/ \___| \_____| \__| \__| \_______/ \________| \__| \__|\n' +
'\n' +
' \ / \n' +
' \ o ^ o / \n' +
' \ ( ) / \n' +
' ____________(%%%%%%%)____________ \n' +
' ( / / )%%%%%%%( \ \ ) \n' +
' (___/___/__/ \__\___\___) \n' +
' ( / /(%%%%%%%)\ \ ) \n' +
' (__/___/ (%%%%%%%) \___\__) \n' +
' /( )\\ \n' +
' / (%%%%%) \\ \n' +
' (%%%) \n' +
' \ / \n' +
' V \n' +
' | \n' +
'============================================================================\n' +
colors.bcolors.COLOR['RESET_ALL']
)
HOST_KEY = paramiko.RSAKey(filename='server.key')
UP_KEY = '\x1b[A'.encode()
DOWN_KEY = '\x1b[B'.encode()
RIGHT_KEY = '\x1b[C'.encode()
LEFT_KEY = '\x1b[D'.encode()
BACK_KEY = '\x7f'.encode()
COMMANDS = {}
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)s %(message)s',
datefmt='%m-%d-%Y %H:%M:%S',
filename='stinger.log',
filemode='a')
def handle_command(cmd, chan,transport):
response = ''
if cmd.startswith('ls'):
x = cmd.split(' ')
y = LSCommand()
y.start(x, chan)
elif cmd.startswith('pwd'):
response = '/home/root/'
elif cmd.startswith('adduser'):
y = cmd.split(' ')
t = Command_adduser()
l = t.start(y[1], chan, transport)
elif cmd.startswith('apt'):
y = cmd.split(' ')
t = APTCommand()
l = t.start(y, chan)
if response != '':
response = response + '\r\n'
chan.send(response)
def handle_connection(client, addr, password):
client_ip = addr[0]
logging.info('New connection from: {}'.format(client_ip))
print('[*] New connection from: {}'.format(client_ip))
try:
transport = paramiko.Transport(client)
transport.add_server_key(HOST_KEY)
server = StingerServer(client_ip)
try:
transport.start_server(server=server)
except paramiko.SSHException:
print('*** SSH negotiation failed.')
raise Exception("SSH negotiation failed")
# wait for auth
chan = transport.accept(10)
if chan is None:
print('*** No channel (from ' + client_ip + ').')
raise Exception("No channel")
chan.settimeout(10800)
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:
chan.send('[email protected]\'s password: ')
if password != "":
passwd = ''
count = 0
check = True
while check:
while not passwd.endswith('\r'):
p = chan.recv(1024)
if (
transport != UP_KEY
and transport != DOWN_KEY
and transport != LEFT_KEY
and transport != RIGHT_KEY
and transport != BACK_KEY
):
passwd += p.decode("utf-8")
if count == 2:
check = False
chan.send("root@localhost: Permission denied (publickey,password).\r\n")
chan.close()
return
if passwd.rstrip() != password:
chan.send("\r\nPermission denied, please try again.\r\n")
chan.send('[email protected]\'s password: ')
passwd = ""
count += 1
print('[-] Incorrect password from ({}): {}'.format(client_ip, passwd.rstrip()))
elif passwd.rstrip() == password:
break
else:
passwd = ''
while not passwd.endswith('\r'):
p = chan.recv(1024)
if (
transport != UP_KEY
and transport != DOWN_KEY
and transport != LEFT_KEY
and transport != RIGHT_KEY
and transport != BACK_KEY
):
passwd += p.decode("utf-8")
date = time.ctime()
chan.send("\r\n\r\nLinux kali 4.19.0-kali4-amd64 #1 SMP Debian 4.19.28-2kali1 (2019-03-18) x86_64\r\n\r\n")
chan.send("The programs included with the Kali GNU/Linux system are free software;\r\n" +
"the exact distribution terms for each program are described in the\r\n" +
"individual files in /usr/share/doc/*/copyright.\r\n\r\n" +
"Kali GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent\r\n" +
"permitted by applicable law.\r\n" +
"Last login: " + date + " from " + str(client_ip) + "\r\n" +
"┏━(" + colors.bcolors.COLOR["RED"] + "Message from Kali developers" +
colors.bcolors.COLOR["RESET_ALL"] + ")\r\n" +
"┃\r\n" +
"┃ This is a minimal installation of Kali Linux, you likely\r\n" +
"┃ want to install supplementary tools. Learn how:\r\n" +
"┃ ⇒ https://www.kali.org/docs/troubleshooting/common-minimum-setup/\r\n" +
"┃\r\n" +
"┗━(" + colors.bcolors.COLOR['GREY'] + "Run “touch ~/.hushlogin” to hide this message)" +
colors.bcolors.COLOR['RESET_ALL'] + "\r\n")
run = True
while run:
chan.send(colors.bcolors.COLOR['RED'] + "root@kali" + colors.bcolors.COLOR['RESET_ALL'] + ':' +
colors.bcolors.COLOR['BLUE'] + '~' + colors.bcolors.COLOR['RESET_ALL'] + '# ')
command = ""
while not command.endswith("\r"):
transport = chan.recv(1024)
# 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")
elif 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))
print('[*] Command received ({}): {}'.format(client_ip, command))
# detect_url(command, client_ip)
if command == "exit":
chan.send('logout\r\n')
run = False
else:
handle_command(command, chan, transport)
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
def start_server(port, bind, password):
"""Init and run the ssh server"""
try:
print('Listening for connection ...\r\n')
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)
threads = []
while True:
try:
sock.listen(100)
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, password))
new_thread.start()
threads.append(new_thread)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Stinger SSH Honeypot")
parser.add_argument("--port", "-p", help="The port to bind the ssh server to (default 22)", default=2222, type=int,
action="store")
parser.add_argument("--bind", "-b", help="The address to bind the ssh server to", default="", type=str,
action="store")
parser.add_argument("--password", "-k", help="The password that client will have to input to gain access",
default="", type=str, action="store")
args = parser.parse_args()
connections = []
connections.append(os.system("who"))
command = 'iptables -A PREROUTING -t nat -p tcp --dport 22 -j REDIRECT --to-port {}'.format(args.port)
os.system(command)
start_server(args.port, args.bind, args.password)