-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSentinelPot.py
More file actions
170 lines (141 loc) · 5.69 KB
/
SentinelPot.py
File metadata and controls
170 lines (141 loc) · 5.69 KB
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
# --------------------
# Dependencies
# --------------------
import socketserver
import threading
import requests
import logging
import datetime
import json
import time
import random
import os
# --------------------
# Variables
# --------------------
WEBHOOK_URL = "webhook here"
HOST = "0.0.0.0" # ip here
PORT = 2222 # you can change port if you want
MAX_EMBED = 2048
SESSION_LOG = "honeypot_sessions.log"
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S"
)
VIRTUAL_FS = {
"/": {
"type": "dir",
"stuff": {
"Documents": {"type": "dir", "stuff": {"resume.txt": {"type": "file", "data": ""}}},
"Downloads": {"type": "dir", "stuff": {}},
"secret.txt": {"type": "file", "data": "database"},
"readme.md": {"type": "file", "data": "# Goof"}
}
}
}
# --------------------
# Main
# --------------------
def shout_to_discord(ip, when, what_they_did):
cmd_log = "\n".join(what_they_did) if what_they_did else "Nothin' happened"
desc = (f"**IP:** {ip}\n"
f"**When:** {when}\n"
f"**What they tried:**\n```\n{cmd_log}\n```")
if len(desc) > MAX_EMBED:
desc = desc[:MAX_EMBED - 3] + "..."
discord_msg = {
"title": "🚨 Honeypot Activity!",
"description": desc,
"color": 15158332,
"timestamp": datetime.datetime.now(datetime.UTC).isoformat()
}
payload = {"embeds": [discord_msg]}
try:
response = requests.post(WEBHOOK_URL, data=json.dumps(payload), headers={"Content-Type": "application/json"})
if response.status_code != 204:
logging.error("Discord hook failed: " + response.text)
except Exception as e:
logging.error("Discord oopsie: " + str(e))
def keep_record(ip, timestamp, commands):
try:
with open(SESSION_LOG, "a") as log:
log.write(f"\n--- New session @ {timestamp} ---\n")
log.write(f"IP: {ip}\nCommands:\n - " + "\n - ".join(commands) + "\n")
except Exception as e:
logging.error("Log file trouble: " + str(e))
def cook_response(cmd, filesystem, current_path):
cmd = cmd.strip()
if not cmd:
return "", current_path, filesystem
if cmd.lower() in ["exit", "quit"]:
return "Connection closed!", current_path, filesystem
if cmd == "ls":
current = filesystem.get(current_path, {})
return " ".join(current.get("stuff", {}).keys()) or "", current_path, filesystem
if cmd.startswith("pwd"):
return current_path, current_path, filesystem
if cmd.startswith("cd "):
target = cmd.split(maxsplit=1)[1]
if target == "..":
current_path = os.path.dirname(current_path.rstrip("/")) or "/"
else:
current = filesystem.get(current_path, {})
if target in current.get("stuff", {}):
if current["stuff"][target]["type"] == "dir":
current_path = os.path.join(current_path, target)
else:
return f"Not a folder: {target}", current_path, filesystem
else:
return f"cd: {target} not found", current_path, filesystem
return "", current_path, filesystem
if cmd.startswith("cat "):
filename = cmd.split(maxsplit=1)[1]
current = filesystem.get(current_path, {})
if filename in current.get("stuff", {}):
return current["stuff"][filename].get("data", ""), current_path, filesystem
return f"cat: {filename} MIA", current_path, filesystem
if cmd == "whoami":
return "root", current_path, filesystem
if cmd.startswith("uname"):
return "Linux 4.15.0-72-generic #81-Ubuntu SMP 2025 x86_64 GNU/Linux", current_path, filesystem
return f"Unknown command: {cmd}", current_path, filesystem
class ConnectionHandler(socketserver.BaseRequestHandler):
def handle(self):
client_ip = self.client_address[0]
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
cmd_history = []
filesystem = {"/": VIRTUAL_FS["/"].copy()}
current_path = "/"
self.request.sendall(b"SSH-2.0-OpenSSH_7.2p2\r\n")
self.request.sendall(b"login: ")
username = self.request.recv(1024).decode().strip()
self.request.sendall(b"Password: ")
self.request.recv(1024)
self.request.sendall(f"Hey {username}! Type 'exit' to bail\r\n".encode())
while True:
self.request.sendall(b"Goof$ ")
try:
cmd = self.request.recv(1024).decode().strip()
if not cmd:
break
cmd_history.append(cmd)
time.sleep(random.uniform(0.1, 0.5))
response, current_path, filesystem = cook_response(cmd, filesystem, current_path)
self.request.sendall(response.encode() + b"\r\n")
if cmd.lower() in ["exit", "quit"]:
break
except:
break
keep_record(client_ip, timestamp, cmd_history)
shout_to_discord(client_ip, timestamp, cmd_history)
def fire_up():
logging.info(f"Pot's hot on {HOST}:{PORT}")
server = socketserver.ThreadingTCPServer((HOST, PORT), ConnectionHandler)
try:
server.serve_forever()
except KeyboardInterrupt:
logging.info("Powering down...")
server.shutdown()
if __name__ == "__main__":
fire_up()