-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
380 lines (272 loc) · 15.7 KB
/
bot.py
File metadata and controls
380 lines (272 loc) · 15.7 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
import logging
import re
from telegram import Update
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext, ConversationHandler
import os
import paramiko
from dotenv import load_dotenv
import psycopg2
from psycopg2 import Error
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) #filename='logsfile.txt',
logger = logging.getLogger(__name__)
load_dotenv()
TOKEN = os.getenv('TOKEN')
def start(update: Update, context: CallbackContext):
user = update.effective_user
update.message.reply_text(f'Привет, {user.full_name}! Я бот для поиска email и номеров телефонов и проверки паролей на сложность, а также мониторинга linux системы.')
def help_command(update: Update, context: CallbackContext):
update.message.reply_text('Используйте /find_email для поиска email адресов, /find_phone для поиска номеров телефонов и /verify_password для проверки сложности пароля или /monitor для мониторинга Linux системы. \n Также можно вывести информацию о всех имеющихся почтах и номерах телефона с помощью /get_emails и /get_phone_numbers соответственно')
def check_email(text):
email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b'
emails_found = re.findall(email_pattern, text)
return emails_found
def check_phone_numbers(text):
phone_pattern = r'(\+7|8)[\s(]?(\d{3})[\s)]?\s?(\d{3})[\s-]?(\d{2})[\s-]?(\d{2})'
phones_found = re.findall(phone_pattern, text)
formatted_numbers = [''.join(p) for p in phones_found]
return formatted_numbers
def check_password_complexity(password):
strong_password_regex = r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()]).{8,}$'
if re.match(strong_password_regex, password):
return "Пароль сложный."
else:
return "Пароль простой."
def find_email(update: Update, context: CallbackContext):
update.message.reply_text("Отправьте текст для поиска email-адресов.")
context.chat_data['command'] = 'find_email'
def find_phone_number(update: Update, context: CallbackContext):
update.message.reply_text("Отправьте текст для поиска номеров телефонов.")
context.chat_data['command'] = 'find_phone_number'
def verify_password(update: Update, context: CallbackContext):
update.message.reply_text("Отправьте пароль для проверки его сложности.")
context.chat_data['command'] = 'verify_password'
def ssh_command(command):
host = os.getenv('RM_HOST')
port = os.getenv('RM_PORT')
username = os.getenv('RM_USER')
password = os.getenv('RM_PASSWORD')
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
client.connect(hostname=host, username=username, password=password, port=port)
stdin, stdout, stderr = client.exec_command(command)
data = stdout.read()
client.close()
return data
except paramiko.AuthenticationException as e:
logger.error(f"SSH authentication failed: {e}")
return "Authentication failed."
except paramiko.SSHException as e:
logger.error(f"SSH connection failed: {e}")
return "SSH connection failed."
def get_release(update: Update, context: CallbackContext):
result = ssh_command('lsb_release -a')
update.message.reply_text(f"Результат выполнения команды просмотра информации о релизе:\n{result}")
def get_uname(update: Update, context: CallbackContext):
result = ssh_command('uname -a')
update.message.reply_text(f"Результат выполнения команды просмотра информации о системе:\n{result}")
def get_uptime(update: Update, context: CallbackContext):
result = ssh_command('uptime')
update.message.reply_text(f"Результат выполнения команды просмотра времени работы:\n{result}")
def get_df(update: Update, context: CallbackContext):
result = ssh_command('df -h')
update.message.reply_text(f"Результат выполнения команды просмотра состояния файловой системы:\n{result}")
def get_free(update: Update, context: CallbackContext):
result = ssh_command('free -h')
update.message.reply_text(f"Результат выполнения команды просмотра состояния оперативной памяти:\n{result}")
def get_mpstat(update: Update, context: CallbackContext):
result = ssh_command('mpstat')
update.message.reply_text(f"Результат выполнения команды просмотра производительности системы:\n{result}")
def get_w(update: Update, context: CallbackContext):
result = ssh_command('w')
update.message.reply_text(f"Результат выполнения команды просмотра работающих пользователей:\n{result}")
def get_auths(update: Update, context: CallbackContext):
result = ssh_command('last')
update.message.reply_text(f"Результат выполнения команды просмотра последних входов:\n{result[-500:]}")
def get_critical(update: Update, context: CallbackContext):
result = ssh_command('journalctl -p 0..2')
update.message.reply_text(f"Результат выполнения команды просмотра последних критических событий:\n{result}")
def get_ps(update: Update, context: CallbackContext):
result = ssh_command('ps aux')[:1000]
update.message.reply_text(f"Результат выполнения команды просмотра запущенных процессов:\n{result}")
def get_ss(update: Update, context: CallbackContext):
result = ssh_command('netstat -tuln')
update.message.reply_text(f"Результат выполнения команды просмотра используемых портов:\n{result}")
def apt_list(service):
if service == 'all':
result = ssh_command('dpkg -l')[:1000]
return result
else:
result = ssh_command(f'dpkg -l | grep {service}')
if result:
return result
else:
return ''
def get_apt_list(update: Update, context: CallbackContext):
update.message.reply_text("Отправьте название сервиса (all для всех).")
context.chat_data['command'] = 'get_apt_list'
def get_services(update: Update, context: CallbackContext):
result = ssh_command('systemctl list-units --type=service --state=running')
update.message.reply_text(f"Результат выполнения команды просмотра запущенных сервисов:\n{result}")
def get_repl_logs(update: Update, context: CallbackContext):
result = ssh_command('cat /var/log/postgresql/postgresql-14-main.log')[-1500:]
update.message.reply_text(f"Результат выполнения команды просмотра логов репликации :\n{result}")
def bd_command(command):
hostbd = os.getenv('DB_HOST')
portbd = os.getenv('DB_PORT')
userbd = os.getenv('DB_USER')
passwordbd = os.getenv('DB_PASSWORD')
db = os.getenv('DB_DATABASE')
connection = None
try:
connection = psycopg2.connect(user=userbd,
password=passwordbd,
host=hostbd,
port=portbd,
database=db)
cursor = connection.cursor()
cursor.execute(command)
connection.commit()
data = cursor.fetchall()
logging.info("Команда успешно выполнена")
return data
except (Exception, Error) as error:
logging.error("Ошибка при работе с PostgreSQL: %s", error)
finally:
if connection is not None:
cursor.close()
connection.close()
def get_emails(update: Update, context: CallbackContext):
result = bd_command("SELECT * FROM emails;")
update.message.reply_text(f"Все email:\n{result}")
def get_phone_numbers(update: Update, context: CallbackContext):
result = bd_command("SELECT * FROM phone_numbers;")
update.message.reply_text(f"Все номера телефона:\n{result}")
def save(update: Update, context: CallbackContext):
found = context.user_data['found']
data = context.user_data['data']
if data == 'email':
for email in found:
bd_command(f"INSERT INTO emails (email) VALUES ('{email}');")
update.message.reply_text(f"Найденные email-адреса сохранены в базе данных.{context.user_data['found']}")
elif data == 'phone':
for phone in found:
bd_command(f"INSERT INTO phone_numbers (phone_number) VALUES ('{phone}');")
update.message.reply_text(f"Найденные номера сохранены в базе данных.{context.user_data['found']}")
def discard(update: Update, context: CallbackContext):
update.message.reply_text("Сохранение отменено.")
def handle_text_message(update: Update, context: CallbackContext) -> None:
command = context.chat_data.get('command')
text = update.message.text
if command == 'find_email':
emails_found = check_email(text)
if emails_found:
update.message.reply_text("Найденные email-адреса:\n" + "\n".join(emails_found))
update.message.reply_text("Хотите сохранить найденные email-адреса в базе данных? \n ( /save или /discard )")
context.user_data['data'] = 'email'
context.user_data['found'] = emails_found
else:
update.message.reply_text("Email-адреса не найдены.")
elif command == 'find_phone_number':
phone_numbers_found = check_phone_numbers(text)
if phone_numbers_found:
update.message.reply_text("Найденные номера телефонов:\n" + "\n".join(phone_numbers_found))
update.message.reply_text("Хотите сохранить найденные номера в базе данных? \n ( /save или /discard )")
context.user_data['data'] = 'phone'
context.user_data['found'] = phone_numbers_found
else:
update.message.reply_text("Номера телефонов не найдены.")
elif command == 'save':
save()
elif command == 'discard':
discard()
elif command == 'verify_password':
password_result = check_password_complexity(text)
update.message.reply_text(password_result)
elif command == 'get_uname':
get_uname()
elif command == 'monitor':
monitor()
elif command == 'get_release':
get_release()
elif command == 'get_uptime':
get_uptime()
elif command == 'get_df':
get_df()
elif command == 'get_free':
get_free()
elif command == 'get_mpstat':
get_mpstat()
elif command == 'get_w':
get_w()
elif command == 'get_auths':
get_auths()
elif command == 'get_critical':
get_critical()
elif command == 'get_ps':
get_ps()
elif command == 'get_ss':
get_ss()
elif command == 'get_apt_list':
info = apt_list(text)
if info:
update.message.reply_text(f"Результат выполнения команды просмотра установленных пакетов :\n{info}")
else:
update.message.reply_text('Сервис не найден')
elif command == 'get_services':
get_services()
elif command == 'get_repl_logs':
get_repl_logs()
elif command == 'get_emails':
get_emails()
elif command == 'get_phone_numbers':
get_phone_numbers()
def monitor(update: Update, context: CallbackContext):
update.message.reply_text('Выберите действие для мониторинга Linux системы:'
'\n/get_release - информация о релизе'
'\n/get_uname - информация о системе'
'\n/get_uptime - время работы'
'\n/get_df - состояние файловой системы'
'\n/get_free - состояние оперативной памяти'
'\n/get_mpstat - производительность системы'
'\n/get_w - работающие пользователи'
'\n/get_auths - последние входы в систему'
'\n/get_critical - последние критические события'
'\n/get_ps - запущенные процессы'
'\n/get_ss - используемые порты'
'\n/get_apt_list - установленные пакеты (all/service)'
'\n/get_services - запущенные сервисы'
'\n/get_repl_logs - логи бд')
def main() -> None:
updater = Updater(TOKEN)
dispatcher = updater.dispatcher
# Добавляем обработчики команд
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(CommandHandler("help", help_command))
dispatcher.add_handler(CommandHandler("find_email", find_email))
dispatcher.add_handler(CommandHandler("find_phone", find_phone_number))
dispatcher.add_handler(CommandHandler("verify_password", verify_password))
dispatcher.add_handler(CommandHandler("get_uname", get_uname))
dispatcher.add_handler(CommandHandler("monitor", monitor))
dispatcher.add_handler(CommandHandler("get_release", get_release))
dispatcher.add_handler(CommandHandler("get_uptime", get_uptime))
dispatcher.add_handler(CommandHandler("get_df", get_df))
dispatcher.add_handler(CommandHandler("get_free", get_free))
dispatcher.add_handler(CommandHandler("get_mpstat", get_mpstat))
dispatcher.add_handler(CommandHandler("get_w", get_w))
dispatcher.add_handler(CommandHandler("get_auths", get_auths))
dispatcher.add_handler(CommandHandler("get_critical", get_critical))
dispatcher.add_handler(CommandHandler("get_ps", get_ps))
dispatcher.add_handler(CommandHandler("get_ss", get_ss))
dispatcher.add_handler(CommandHandler("get_apt_list", get_apt_list)) #get_apt_list
dispatcher.add_handler(CommandHandler("get_services", get_services))
dispatcher.add_handler(CommandHandler("get_repl_logs", get_repl_logs))
dispatcher.add_handler(CommandHandler("get_emails", get_emails))
dispatcher.add_handler(CommandHandler("get_phone_numbers", get_phone_numbers))
dispatcher.add_handler(CommandHandler("save", save))
dispatcher.add_handler(CommandHandler("discard", discard))
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_text_message))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()