This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.py
More file actions
executable file
·355 lines (304 loc) · 11.3 KB
/
bot.py
File metadata and controls
executable file
·355 lines (304 loc) · 11.3 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env python3
"""Python repl bot for irc using re-ircbot.
Indented to be executed on a docker container for better OPSEC
"""
import json
import logging
import os
import multiprocess
import requests
import RestrictedPython
from dotenv import load_dotenv
from ircbot import Color, IrcBot, utils
from ircbot.message import Message
from pathos.multiprocessing import ProcessPool
from RestrictedPython import compile_restricted, limited_builtins, safe_builtins, utility_builtins
from RestrictedPython.PrintCollector import PrintCollector
load_dotenv()
##################################################
# SETTINGS #
##################################################
LOGFILE = None
LEVEL = int(os.getenv("LOG_LEVEL", logging.INFO))
HOST = os.getenv("IRC_HOST")
PORT = int(os.getenv("IRC_PORT") or 6667)
SSL = os.getenv("IRC_SSL") == "true"
NICK = os.getenv("NICK") or "_pybot"
PASSWORD = os.getenv("PASSWORD") or ""
USERNAME = NICK
REALNAME = NICK
CHANNELS = json.loads(os.getenv("CHANNELS") or "[]")
assert HOST, "IRC_HOST must be set in the environment"
bot = (
IrcBot(HOST, PORT, NICK, CHANNELS, PASSWORD, strip_messages=False, use_ssl=SSL)
.set_prefix("`")
.set_parser_order(False)
)
MODULES_WHITELIST = [
"numpy",
"IrcBot",
"math",
"itertools",
"socket",
"time",
"re",
"random",
"collections",
"datetime",
"requests",
"http",
"hashlib",
"json",
"copy",
"functools",
"secrets",
"string",
]
TIMEOUT = 5
##################################################
# LIB
##################################################
pool = ProcessPool(nodes=4)
user_source = {}
user_env = {}
user_state = {}
user_multiline = {}
def debug(*args, **kwargs):
"""Debug function to print debug messages."""
logging.debug(*args, **kwargs)
def log(*args, **kwargs):
"""Log function to print log messages."""
logging.info(*args, **kwargs)
def interpret(code, env):
"""Interprets the given python code inside a safe execution environment."""
def guarded_import(mname, globals={}, locals={}, fromlist=(), level=0): # noqa A002
if mname in MODULES_WHITELIST or ("." in mname and mname.split(".")[0] in MODULES_WHITELIST):
return __import__(mname, globals, locals, fromlist)
else:
raise Exception("This module is not whitelisted")
code += "\n_ = printed"
byte_code = compile_restricted(
code,
filename="<string>",
mode="exec",
)
data = {
"_print_": PrintCollector,
"__builtins__": {
**safe_builtins,
**limited_builtins,
**utility_builtins,
"_getiter_": RestrictedPython.Eval.default_guarded_getiter,
"_iter_unpack_sequence_": RestrictedPython.Guards.guarded_iter_unpack_sequence,
"__import__": guarded_import,
"abs": __builtins__.abs,
"all": __builtins__.all,
"any": __builtins__.any,
"ascii": __builtins__.ascii,
"bin": __builtins__.bin,
"bool": __builtins__.bool,
"bytearray": __builtins__.bytearray,
"bytes": __builtins__.bytes,
"callable": __builtins__.callable,
"chr": __builtins__.chr,
"classmethod": __builtins__.classmethod,
"complex": __builtins__.complex,
"delattr": __builtins__.delattr,
"dict": __builtins__.dict,
"dir": __builtins__.dir,
"divmod": __builtins__.divmod,
"enumerate": __builtins__.enumerate,
"filter": __builtins__.filter,
"float": __builtins__.float,
"format": __builtins__.format,
"frozenset": __builtins__.frozenset,
"getattr": __builtins__.getattr,
"globals": __builtins__.globals,
"hasattr": __builtins__.hasattr,
"hash": __builtins__.hash,
"help": __builtins__.help,
"hex": __builtins__.hex,
"id": __builtins__.id,
"int": __builtins__.int,
"isinstance": __builtins__.isinstance,
"issubclass": __builtins__.issubclass,
"iter": __builtins__.iter,
"len": __builtins__.len,
"list": __builtins__.list,
"locals": __builtins__.locals,
"map": __builtins__.map,
"max": __builtins__.max,
"min": __builtins__.min,
"next": __builtins__.next,
"object": __builtins__.object,
"oct": __builtins__.oct,
"ord": __builtins__.ord,
"pow": __builtins__.pow,
"print": __builtins__.print,
"property": __builtins__.property,
"range": __builtins__.range,
"repr": __builtins__.repr,
"reversed": __builtins__.reversed,
"round": __builtins__.round,
"set": __builtins__.set,
"setattr": __builtins__.setattr,
"slice": __builtins__.slice,
"sorted": __builtins__.sorted,
"staticmethod": __builtins__.staticmethod,
"str": __builtins__.str,
"sum": __builtins__.sum,
"super": __builtins__.super,
"tuple": __builtins__.tuple,
"type": __builtins__.type,
"vars": __builtins__.vars,
"zip": __builtins__.zip,
},
"_getattr_": RestrictedPython.Guards.safer_getattr,
"_write_": lambda x: x,
"_getitem_": lambda obj, key: obj[key],
}
exec(byte_code, data, env)
return env
def process_source(nick, source):
global user_source
debug("Starting process")
if nick not in user_env:
user_env[nick] = {}
if nick not in user_source:
user_source[nick] = ""
debug("Checked user env")
result = pool.apipe(interpret, source, user_env[nick])
output = None
debug("Launched interpret process")
try:
debug("Collecting output")
env = result.get(timeout=TIMEOUT)
output = env["_"]
user_env[nick] = env
user_source[nick] += "\n" + source
debug("Output collected")
except multiprocess.context.TimeoutError:
output = Color("Timeout error - do you have an infinite loop?", fg=Color.red).str
except Exception as e:
output = Color("Runtime error: {}".format(e), fg=Color.red).str
return output or "(no output to stdout)"
##################################################
# RUNTIME & HANDLERS
##################################################
@bot.arg_command("clear", "Clear environment and history")
def clear(args, message):
if message.nick in user_source:
log("Clearing ", message.nick)
user_source[message.nick] = ""
user_env[message.nick] = {}
return f"<{message.nick}> Environment and history cleared!"
@bot.regex_cmd_with_message("^`(.+)`$")
async def run(m, message):
global pool
global user_source
source = m[1]
debug("Executing {}".format(repr(source)))
output = process_source(message.nick, source)
await bot.send_message(f"<{message.nick}> " + output, message.channel)
@bot.regex_cmd_with_message("^(.+)$")
def multiline_capture(m, message):
global user_multiline
debug(f"RECEIVED: {message.text=}")
if message.nick in user_state and user_state[message.nick]:
if message.nick not in user_multiline:
user_multiline[message.nick] = ""
user_multiline[message.nick] += message.text + "\n"
@bot.regex_cmd_with_message("^```$")
def start_multiline(m, message):
global user_state
if message.nick not in user_state or not user_state[message.nick]:
user_state[message.nick] = True
user_multiline[message.nick] = ""
return f"<{message.nick}> Waiting for lines, type ``` to finish and execute"
else:
user_state[message.nick] = False
if message.nick not in user_multiline or (
message.nick in user_multiline and len(user_multiline[message.nick]) == 0
):
return f"<{message.nick}> Ignoring empty multiline code"
output = process_source(message.nick, user_multiline[message.nick])
return f"<{message.nick}>> " + output
@bot.arg_command("lsmod", "List available whitelisted modules")
def lsmod(args, message):
return f"<{message.nick}> Available modules are: " + ", ".join(MODULES_WHITELIST)
def pastebin(text) -> str:
url = "https://s.h4ks.com/api/"
with tempfile.NamedTemporaryFile(suffix=".txt") as f:
with open(f.name, "wb") as file:
file.write(text.encode("utf-8"))
response = requests.post(
url,
files={"file": open(file.name, "rb")},
)
try:
obj = response.json()
except json.JSONDecodeError:
response.raise_for_status()
return response.text
if "url" in obj:
return obj["url"]
if "error" in obj:
return f"error: {obj['error']}"
return f"error: {obj}"
@bot.arg_command("paste", "Paste the code history to ix.io")
def paste(args, message):
if message.nick not in user_source:
return
log("Pasting ", message.nick)
url = pastebin(user_source[message.nick])
return f"<{message.nick}> " + url
@bot.arg_command("show", "Sends the code history over private messages")
def show(args, message):
if message.nick not in user_source:
return
log("Showing in PM ", message.nick)
return [Message(message=ln, channel=message.nick) for ln in user_source[message.nick].split("\n")]
@bot.arg_command("run", "Runs code from a url, e.g. ix.io")
def paste_run(args, message):
if not args[1]:
return f"<{message.nick}> This commands requires an argument!"
log("Running thing", message.nick)
response = requests.get(args[1])
if response.status_code != 200:
return f"<{message.nick}> Failed to fetch this url!"
source = response.content.decode().strip()
debug("Executing {}".format(repr(source)))
output = process_source(message.nick, source)
return f"<{message.nick}>> " + output
@bot.arg_command("get", "Gets the environmental variables from another user in the chat")
async def transfer(args, message):
if not args[1]:
await bot.send_message(f"<{message.nick}> This commands requires an argument!", message.channel)
return
names = await bot.list_names(message.channel)
if args[1] not in names:
await bot.send_message(f"<{message.nick}> This user is not on this channel", message.channel)
return
if args[1] not in user_env:
await bot.send_message(f"<{message.nick}> This user does not have an environment started", message.channel)
return
nick = message.nick
if nick not in user_env:
user_env[nick] = {}
user_env[nick].update(user_env[args[1]])
await bot.send_message(f"<{message.nick}> Environment imported!", message.channel)
async def check_no_bot(bot: IrcBot, message: Message):
await bot.send_raw("WHO {}".format(message.nick))
print("11111111111111111111111111111")
resp = await bot.wait_for("who", message.nick, timeout=10, cache_ttl=60)
print("22222222222222222222222222222")
modes = resp.get("modes")
if modes is None or "B" in modes:
return False
return True
async def on_connect():
await bot.send_raw(f"MODE {bot.nick} +B")
if __name__ == "__main__":
utils.set_loglevel(LEVEL, LOGFILE)
bot.add_middleware(check_no_bot)
bot.run_with_callback(on_connect)