IRCat/modules/botnet_protect.py

44 lines
2.2 KiB
Python
Raw Normal View History

2025-01-10 23:43:23 -08:00
import threading
__ircat_type__ = "allsocket"
2025-01-11 00:08:29 -08:00
__ircat_requires__ = ["ban-provider"]
__ircat_giveme__ = ["sql"] # Only command and allsocket have these.
2025-01-26 20:24:58 -08:00
__ircat_fakechannels__ = {"#IRCATSUCKS": "B0tn3t pr0t3ct10n, do not join."}
2025-01-10 23:43:23 -08:00
class IRCatModule:
sus_strings = [
2025-01-26 20:24:33 -08:00
" .''.", # 2025 new year
"⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣤⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀" # "The United States of America" LATEST
2025-01-10 23:43:23 -08:00
]
memory = {} # {nick: {channel: trustlevel}} one can also be {nick: True} if it is whitelisted for the session.
2025-01-11 00:58:30 -08:00
useSQLengine = False
2025-01-11 00:08:29 -08:00
def __init__(self, ban_provider, sql):
self.ban_provider = ban_provider
if ban_provider == "sql":
self.useSQLengine = True
self.SQLengine = sql
def ban(self, ip):
2025-01-26 20:24:33 -08:00
del self.memory[ip] # Forget this all happened
# Add the ban
2025-01-11 00:08:29 -08:00
if self.useSQLengine:
2025-01-26 20:24:33 -08:00
self.SQLengine.addban(ip, "Botnet detected!") # Use the SQL provider if {'ban-provider': 'sql'}
2025-01-11 00:08:29 -08:00
else:
2025-01-26 20:24:33 -08:00
open(self.ban_provider, "a").write(f"\n{ip} Botnet detected!") # Else, write on the banfile.
raise Exception("Botnet detected!") # Kill the connection
2025-01-10 23:43:23 -08:00
def onSocket(self, ip, socket, value, cachedNick=None, validated=False):
2025-01-11 00:52:10 -08:00
if cachedNick != None:
2025-01-11 01:04:11 -08:00
print(value)
if "JOIN" in value:
target = value.split(" ")[1]
self.memory[ip] = 1 # 1: Just joined the channel, continue observing.
print("Autoban> Somebody joined " + target)
if target.lower() == "#ircatsucks":
self.ban(ip)
elif "PRIVMSG" in value:
if not (ip in self.memory and self.memory[ip] == 0):
2025-01-10 23:43:23 -08:00
target = value.split(" ")[1]
content = " ".join(value.split(" ")[2:])[1:]
2025-01-11 00:57:49 -08:00
if content in self.sus_strings:
2025-01-11 00:57:36 -08:00
if ip in self.memory:
if self.memory[ip] == 1:
self.ban(ip)
2025-01-10 23:43:23 -08:00
else:
2025-01-11 00:50:26 -08:00
self.memory[ip] = 0 # 0: Trust the connection :3