-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
558 lines (489 loc) · 19.4 KB
/
bot.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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import telebot
from telebot import types
import time
import os
import serial
import pygame
TOKEN = "TOKEN" # Cambiar por el token
ncid = "Nº CID" # Cambiar por el numero cid
userStep = {}
knownUsers = []
arduino = serial.Serial('/dev/ttyUSB0', 9600)
commands = {'start': 'Arranca el bot',
'ayuda': 'Comandos disponibles',
'exec': 'Ejecuta un comando'}
menu = types.ReplyKeyboardMarkup()
menu.add("RPinfo", "Camara")
menu.add("Arduino", "Seguridad")
cam_menu = types.ReplyKeyboardMarkup()
cam_menu.add("Foto", "Timelapse")
cam_menu.add("Atras")
info_menu = types.ReplyKeyboardMarkup()
info_menu.add("TEMP", "HD")
info_menu.add("RAM", "CPU")
info_menu.add("Status", "Logs")
info_menu.add("Atras")
ard_menu = types.ReplyKeyboardMarkup()
ard_menu.add("LED ON", "LED OFF")
ard_menu.add("Efectos Sonido")
ard_menu.add("Mensaje Voz")
ard_menu.add("Atras")
snd_menu = types.ReplyKeyboardMarkup()
snd_menu.add("Sicofonia", "Risa Malvada")
snd_menu.add("Alarma", "Mario")
snd_menu.add("Atras")
sec_menu = types.ReplyKeyboardMarkup()
sec_menu.add("LiveCam ON", "LiveCam OFF")
sec_menu.add("Move ON", "Move OFF")
sec_menu.add("Alarma ON", "Alarma OFF")
sec_menu.add("Descargar", "Atras")
# COLOR TEXTO
class color:
RED = '\033[91m'
BLUE = '\033[94m'
GREEN = '\033[32m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
# USER STEP
def get_user_step(uid):
if uid in userStep:
return userStep[uid]
else:
knownUsers.append(uid)
userStep[uid] = 0
print(color.RED + " [¡] ¡¡NUEVO USUARIO!!" + color.ENDC)
# LISTENER
def listener(messages):
for m in messages:
if m.content_type == 'text':
print(time.strftime("%d/%m/%y-%H:%M:%S") + color.GREEN + " [" + str(m.chat.id) + "] " + str(m.chat.first_name) + ": " + color.ENDC + m.text)
# PLAY SOUND
def play(fname, seg):
pygame.mixer.init()
pygame.mixer.init(44100)
pygame.mixer.music.load(fname)
pygame.mixer.music.play()
time.sleep(seg)
pygame.mixer.music.stop()
# INFO HD
def diskSpace():
p = os.popen("df -h /")
i = 0
while 1:
i += 1
line = p.readline()
if i == 2:
return(line.split()[1:5])
# INFO RAM
def ramInfo():
p = os.popen('free -o -h')
i = 0
while 1:
i += 1
line = p.readline()
if i == 2:
return(line.split()[1:4])
# TEXTO A VOZ
def txt2snd(m):
cid = m.chat.id
tipo = m.text
with open('/tmp/msg.txt', 'r') as f:
msg = f.readline()
if tipo == "Hombre":
os.system('espeak -ves+m7 -p30 -a150 \"%s\" 2> /dev/null' % msg)
elif tipo == "Mujer":
os.system('espeak -ves+f5 -a150 \"%s\" 2> /dev/null' % msg)
elif tipo == "Fantasma":
os.system('espeak -ves+whisper -p5 -s140 -a40 \"%s\" 2> /dev/null' % msg)
bot.send_message(cid, "Mensaje reproducido")
userStep[cid] = 3
bot.send_message(cid, "Menu Arduino:", reply_markup=ard_menu)
def mensaje(m):
msg = m.text
with open('/tmp/msg.txt', 'w') as f:
f.write(msg)
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
markup.add('Hombre', 'Mujer')
markup.add('Fantasma')
tipo = bot.reply_to(m, "Tipo de voz?: ", reply_markup=markup)
bot.register_next_step_handler(tipo, txt2snd)
# TIMELAPSE
def timelapse(m):
cid = m.chat.id
start = 0
end = m.text
print(color.BLUE + "Nº FOTOS: " + str(end) + color.ENDC)
if end.isdigit():
bot.send_message(cid, "Comienza la captura de fotos...")
print(color.BLUE + "[+] Comienza la captura de fotos..." + color.ENDC)
while start < int(end):
print(color.BLUE + " [i] Capturando imagen %i" % start + color.ENDC)
bot.send_chat_action(cid, 'typing')
os.system("fswebcam -i 0 -d /dev/video0 -r 640x480 -q --no-banner fotos/%d%m%y_%H%M%S.jpg")
start = start + 1
time.sleep(10)
print(color.BLUE + "[-] Proceso TIMELAPSE finalizado!!" + color.ENDC)
bot.send_message(cid, "Proceso TIMELAPSE finalizado!!")
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
markup.add('SI', 'NO')
msg = bot.reply_to(m, "Enviar fotos? ", reply_markup=markup)
bot.register_next_step_handler(msg, tarFotos)
else:
bot.send_message(cid, "Introduce numero de fotos")
bot.register_next_step_handler(m, timelapse)
return
# TAR FOTOS
def tarFotos(m):
cid = m.chat.id
msg = m.text
if msg == "SI":
bot.send_message(cid, "Comprimiendo fotos...")
print(color.BLUE + "[+] Comprimiendo fotos..." + color.ENDC)
bot.send_chat_action(cid, 'typing')
if userStep[cid] == 2:
os.system("tar -cvf /tmp/fotos.tar fotos/*.jpg")
bot.send_message(cid, "Fotos comprimidas. Enviando...")
bot.send_chat_action(cid, 'upload_document')
tar = open('/tmp/fotos.tar', 'rb')
bot.send_document(cid, tar)
elif userStep[cid] == 4:
os.system("sudo tar -cvf /tmp/motion.tar motion/*.*")
bot.send_message(cid, "Fotos comprimidas. Enviando...")
bot.send_chat_action(cid, 'upload_document')
tar = open('/tmp/motion.tar', 'rb')
bot.send_document(cid, tar)
print(color.BLUE + " [+] Fotos Enviadas!!" + color.ENDC)
userStep[cid] = 0
bot.send_message(cid, "Menu Principal:", reply_markup=menu)
else:
userStep[cid] = 0
bot.send_message(cid, "Menu Principal:", reply_markup=menu)
# LOGS
def logs(m):
cid = m.chat.id
msg = m.text
if msg == "Fail2Ban":
f2blog()
log = open('/tmp/f2b.log', 'rb')
elif msg == "OpenVPN":
os.system('sudo cp /var/log/openvpn.log /tmp/')
os.system('sudo chmod 777 /tmp/openvpn.log')
log = open('/tmp/openvpn.log', 'rb')
elif msg == "Logins":
authlog()
log = open('/tmp/login.log', 'rb')
elif msg == "Atras":
userStep[cid] = 1
bot.send_message(cid, "Menu Info:", reply_markup=info_menu)
if msg == "Fail2Ban" or msg == "OpenVPN" or msg == "Logins":
bot.send_chat_action(cid, 'upload_document')
bot.send_document(cid, log)
print(color.BLUE + " [+] Log Enviado!!" + color.ENDC)
userStep[cid] = 0
bot.send_message(cid, "Menu Principal:", reply_markup=menu)
def f2blog():
bans = {}
os.system('sudo cp /var/log/fail2ban.log /tmp/')
os.system('sudo chmod 777 /tmp/fail2ban.log')
with open('/tmp/fail2ban.log', 'r') as log:
for line in log.readlines():
if "[ssh] Ban" in line:
ip = line.split()[6]
if ip not in bans:
bans[ip] = 1
else:
bans[ip] = bans[ip] + 1
with open('/tmp/f2b.log', 'w') as f2b:
f2b.write("\n [i] Número de IPs: %i" % len(bans) + "\n")
for ip, i in bans.iteritems():
f2b.write('[-] IP: %s \t : %i\n' % (ip, i))
def authlog():
os.system('sudo cp /var/log/auth.log /tmp/')
os.system('sudo chmod 777 /tmp/auth.log')
with open('/var/log/auth.log', 'r') as log:
for line in log.readlines():
if "Failed password" in line:
array = line.split()
if len(array) == 16:
datetime = time.strftime("%Y") + '-' + '-'.join(array[:3])
user = array[10]
ip = array[12]
port = array[14]
service = array[15]
else:
datetime = time.strftime("%Y") + '-' + '-'.join(array[:3])
user = array[8]
ip = array[10]
port = array[12]
service = array[13]
with open('/tmp/login.log', 'a+') as f:
f.write(" %s %s @ %s:%s - %s\n" % (datetime, user, ip, port, service))
# STATUS SERVER
def statusServer(m):
cid = m.chat.id
msg = m.text
if msg == "ON":
bot.send_message(cid, "[i] Status Server ON")
print(color.BLUE + "[i] Status Server ON" + color.ENDC)
os.system('nodejs Raspberry-Pi-Status/server.js &')
elif msg == "OFF":
bot.send_message(cid, "[i] Status Server OFF")
print(color.BLUE + "[i] Status Server OFF" + color.ENDC)
f = os.popen('ps -e | grep nodejs')
pid = f.read().split()[0]
os.system('sudo kill %s' % pid)
userStep[cid] = 1
bot.send_message(cid, "Menu Info:", reply_markup=info_menu)
bot = telebot.TeleBot(TOKEN)
bot.set_update_listener(listener)
# START
@bot.message_handler(commands=['start'])
def command_start(m):
cid = m.chat.id
userStep[cid] = 0
bot.send_message(cid, "Wake up " + str(m.chat.first_name) + "...")
time.sleep(1)
bot.send_message(cid, "Fwhibbit has you...")
time.sleep(1)
bot.send_message(cid, "Follow the white rabbit...\n", reply_markup=menu)
# AYUDA
@bot.message_handler(commands=['ayuda'])
def command_help(m):
cid = m.chat.id
if cid == ncid:
help_text = "Comandos disponibles: \n"
for key in commands:
help_text += "/" + key + ": "
help_text += commands[key] + "\n"
bot.send_message(cid, help_text)
else:
bot.send_message(cid, " ¡¡PERMISO DENEGADO!!")
print(color.RED + " ¡¡PERMISO DENEGADO!! " + color.ENDC)
# EXEC COMANDO
@bot.message_handler(commands=['exec'])
def command_exec(m):
cid = m.chat.id
if cid == ncid:
bot.send_message(cid, "Ejecutando: " + m.text[len("/exec"):])
bot.send_chat_action(cid, 'typing')
time.sleep(2)
f = os.popen(m.text[len("/exec"):])
result = f.read()
bot.send_message(cid, "Resultado: " + result)
else:
bot.send_message(cid, " ¡¡PERMISO DENEGADO!!")
print(color.RED + " ¡¡PERMISO DENEGADO!! " + color.ENDC)
# MENU PRINCIPAL
@bot.message_handler(func=lambda message: get_user_step(message.chat.id) == 0)
def main_menu(m):
cid = m.chat.id
text = m.text
if cid == ncid:
if text == "RPinfo": # RPINFO
bot.send_message(cid, "Informacion disponible:", reply_markup=info_menu)
userStep[cid] = 1
elif text == "Camara": # CAMARA
bot.send_message(cid, "Opciones de la camara:", reply_markup=cam_menu)
userStep[cid] = 2
elif text == "Arduino": # ARDUINO
bot.send_message(cid, "Opciones Arduino:", reply_markup=ard_menu)
userStep[cid] = 3
elif text == "Seguridad": # SEGURIDAD
bot.send_message(cid, "Opciones Seguridad:", reply_markup=sec_menu)
userStep[cid] = 4
else:
command_text(m)
else:
bot.send_message(cid, " ¡¡PERMISO DENEGADO!!")
print(color.RED + " ¡¡PERMISO DENEGADO!! " + color.ENDC)
# MENU INFO
@bot.message_handler(func=lambda message: get_user_step(message.chat.id) == 1)
def info_opt(m):
cid = m.chat.id
txt = m.text
if txt == "TEMP": # TEMP
bot.send_message(cid, "[+] TEMPERATURAS")
print(color.BLUE + "[+] TEMPERATURAS" + color.ENDC)
# cpu temp
tempFile = open("/sys/class/thermal/thermal_zone0/temp")
cpu_temp = tempFile.read()
tempFile.close()
cpu_temp = round(float(cpu_temp) / 1000)
bot.send_message(cid, " [i] CPU: %s" % cpu_temp)
print(color.GREEN + " [i] CPU: %s" % cpu_temp + color.ENDC)
# gpu temp
gpu_temp = os.popen('/opt/vc/bin/vcgencmd measure_temp').read().split("=")[1][:-3]
bot.send_message(cid, " [i] GPU: %s" % gpu_temp)
print(color.GREEN + " [i] GPU: %s" % gpu_temp + color.ENDC)
elif txt == "HD": # HD
bot.send_message(cid, "[+] DISCO DURO")
print(color.BLUE + "[+] DISCO DURO" + color.ENDC)
bot.send_message(cid, " [i] Total: %s" % diskSpace()[0])
print(color.GREEN + " [i] Total: %s" % diskSpace()[0] + color.ENDC)
bot.send_message(cid, " [i] Usado: %s" % diskSpace()[1])
print(color.GREEN + " [i] Usado: %s" % diskSpace()[1] + color.ENDC)
bot.send_message(cid, " [i] Disponible: %s" % diskSpace()[2])
print(color.GREEN + " [i] Disponible: %s" % diskSpace()[2] + color.ENDC)
elif txt == "RAM": # RAM
bot.send_message(cid, "[+] MEMORIA RAM")
print(color.BLUE + "[+] MEMORIA RAM" + color.ENDC)
bot.send_message(cid, " [i] Total: %s" % ramInfo()[0])
print(color.GREEN + " [i] Total: %s" % ramInfo()[0] + color.ENDC)
bot.send_message(cid, " [i] Usado: %s" % ramInfo()[1])
print(color.GREEN + " [i] Usado: %s" % ramInfo()[1] + color.ENDC)
bot.send_message(cid, " [i] Disponible: %s" % ramInfo()[2])
print(color.GREEN + " [i] Disponible: %s" % ramInfo()[2] + color.ENDC)
elif txt == "CPU": # CPU
bot.send_message(cid, "[+] CPU")
print(color.BLUE + "[+] CPU" + color.ENDC)
cpu = os.popen('mpstat | grep -A 5 "%idle" | tail -n 1 | awk -F " " \'{print 100 - $ 12}\'a').read()
bot.send_message(cid, " [i] Usado: %s" % cpu)
print(color.GREEN + " [i] Usado: %s" % cpu + color.ENDC)
elif txt == "Status": # STATUS SERVER
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
markup.add("ON", "OFF")
msg = bot.reply_to(m, "Accion: ", reply_markup=markup)
bot.register_next_step_handler(msg, statusServer)
elif txt == "Logs": # LOGS
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
markup.add("Fail2Ban", "Logins")
markup.add("OpenVPN", "Atras")
msg = bot.reply_to(m, "Elegir Log: ", reply_markup=markup)
bot.register_next_step_handler(msg, logs)
elif txt == "Atras": # ATRAS
userStep[cid] = 0
bot.send_message(cid, "Menu Principal:", reply_markup=menu)
else:
command_text(m)
# MENU CAMARA
@bot.message_handler(func=lambda message: get_user_step(message.chat.id) == 2)
def cam_opt(m):
cid = m.chat.id
text = m.text
if text == "Foto": # FOTO
bot.send_message(cid, "Tomando foto ...")
bot.send_chat_action(cid, 'upload_photo')
foto = "fotos/" + (time.strftime("%H%M%S-%d%m%y")) + ".jpg"
os.system('fswebcam -d /dev/video0 -r 640x480 --no-banner %s' % foto)
bot.send_photo(cid, open(foto, 'rb'))
print(color.BLUE + " [i] Foto enviada!!" + color.ENDC)
elif text == "Timelapse": # TIMELAPSE
bot.send_message(cid, "Nº Fotos?: ")
bot.register_next_step_handler(m, timelapse)
elif text == "Atras": # ATRAS
userStep[cid] = 0
bot.send_message(cid, "Menu Principal:", reply_markup=menu)
else:
command_text(m)
# MENU ARDUINO
@bot.message_handler(func=lambda message: get_user_step(message.chat.id) == 3)
def ard_opt(m):
cid = m.chat.id
text = m.text
if text == "LED ON": # LED ON
print(color.BLUE + " [i] LED ON" + color.ENDC)
arduino.write('1')
elif text == "LED OFF": # LED OFF
print(color.BLUE + " [i] LED OFF" + color.ENDC)
arduino.write('0')
elif text == "Efectos Sonido": # EFECTOS SONIDO
bot.send_message(cid, "Efectos de sonido:", reply_markup=snd_menu)
userStep[cid] = 5
elif text == "Mensaje Voz": # MENSAJE VOZ
bot.send_message(cid, "Escribir mensaje: ")
bot.register_next_step_handler(m, mensaje)
elif text == "Atras": # ATRAS
userStep[cid] = 0
bot.send_message(cid, "Menu Principal:", reply_markup=menu)
else:
command_text(m)
# MENU SEGURIDAD
@bot.message_handler(func=lambda message: get_user_step(message.chat.id) == 4)
def sec_opt(m):
cid = m.chat.id
text = m.text
if text == "Alarma ON": # ALARMA
print(color.BLUE + " [i] ALARMA ON" + color.ENDC)
bot.send_message(cid, "[i] Alarma ON")
with open('alarm.txt', 'w') as f:
f.write("1")
elif text == "Alarma OFF":
print(color.BLUE + " [i] ALARMA OFF" + color.ENDC)
bot.send_message(cid, "[i] Alarma OFF")
with open('alarm.txt', 'w') as f:
f.write("0")
elif text == "LiveCam ON": # LIVECAM
print(color.BLUE + " [i] Live Cam ON" + color.ENDC)
bot.send_message(cid, "[i] Live Cam ON")
os.system('sudo motion -m -n 2> /dev/null')
elif text == "LiveCam OFF":
f = os.popen('ps -e | grep motion')
pid = f.read().split()[0]
os.system('sudo kill %s' % pid)
os.system('sudo service motion stop')
print(color.BLUE + " [i] Live Cam OFF" + color.ENDC)
bot.send_message(cid, "[i] Live Cam OFF")
elif text == "Move ON": # MOVIMIENTO
print(color.BLUE + " [i] Detector Movimiento ON" + color.ENDC)
bot.send_message(cid, "[i] Detector Movimiento ON")
os.system('sudo motion -n 2> /dev/null')
elif text == "Move OFF":
f = os.popen('ps -e | grep motion')
pid = f.read().split()[0]
os.system('sudo kill %s' % pid)
os.system('sudo service motion stop')
print(color.BLUE + " [i] Detector Movimiento OFF" + color.ENDC)
bot.send_message(cid, "[i] Detector Movimiento OFF")
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
markup.add('SI', 'NO')
msg = bot.reply_to(m, "Enviar fotos?: ", reply_markup=markup)
bot.register_next_step_handler(msg, tarFotos)
elif text == "Descargar": # DESCARGAR FOTOS
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
markup.add('SI', 'NO')
msg = bot.reply_to(m, "Descargar fotos?: ", reply_markup=markup)
bot.register_next_step_handler(msg, tarFotos)
elif text == "Atras": # ATRAS
userStep[cid] = 0
bot.send_message(cid, "Menu Principal:", reply_markup=menu)
else:
command_text(m)
# MENU SONIDOS
@bot.message_handler(func=lambda message: get_user_step(message.chat.id) == 5)
def snd_opt(m):
cid = m.chat.id
text = m.text
if text == "Sicofonia": # SICOF
print(color.BLUE + " [i] Sicofonia" + color.ENDC)
bot.send_message(cid, "[i] Sicofonia")
play('Sonidos/sicofonia.mp3', 3)
elif text == "Risa Malvada": # RISA
print(color.BLUE + " [i] Risa Malvada" + color.ENDC)
bot.send_message(cid, "[i] Risa Malvada")
play('Sonidos/risa-malvada.mp3', 4)
elif text == "Alarma": # ALARMA
print(color.BLUE + " [i] Alarma" + color.ENDC)
bot.send_message(cid, "[i] Alarma")
play('Sonidos/alarma.mp3', 15)
elif text == "Mario":
print(color.BLUE + " [i] Super Mario" + color.ENDC)
bot.send_message(cid, "[i] Mario")
arduino.write('2')
elif text == "Atras": # ATRAS
userStep[cid] = 3
bot.send_message(cid, "Menu Arduino:", reply_markup=ard_menu)
else:
command_text(m)
# FILTRAR MENSAJES
@bot.message_handler(func=lambda message: True, content_types=['text'])
def command_text(m):
cid = m.chat.id
if (m.text.lower() in ['hola', 'hi', 'buenas', 'buenos dias']):
bot.send_message(cid, 'Muy buenas, ' + str(m.from_user.first_name) + '. Me alegra verte de nuevo.', parse_mode="Markdown")
elif (m.text.lower() in ['adios', 'aios', 'adeu', 'ciao']):
bot.send_message(cid, 'Hasta luego, ' + str(m.from_user.first_name) + '. Te echaré de menos.', parse_mode="Markdown")
print 'Corriendo...'
bot.polling(none_stop=True)