Update setup.py

This commit is contained in:
snfsx 2024-11-19 19:49:21 +00:00
parent f63495093e
commit 2ac6be7b73

240
setup.py
View file

@ -1,120 +1,120 @@
import sqlite3 import sqlite3
import os import os
import paramiko import paramiko
from typing import Callable from typing import Callable
def create_database(): def create_database():
conn = sqlite3.connect("config.db") conn = sqlite3.connect("config.db")
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(""" cursor.execute("""
CREATE TABLE IF NOT EXISTS config ( CREATE TABLE IF NOT EXISTS config (
tg_bot_token TEXT NOT NULL, tg_bot_token TEXT NOT NULL,
chat_id TEXT NOT NULL, chat_id TEXT NOT NULL,
cb TEXT NOT NULL, cb TEXT NOT NULL,
ma INTEGER NOT NULL, ma INTEGER NOT NULL,
delay INTEGER NOT NULL, delay INTEGER NOT NULL,
port INTEGER NOT NULL port INTEGER NOT NULL
) )
""") """)
conn.commit() conn.commit()
conn.close() conn.close()
kf = "server_key.pem" kf = "server_key.pem"
def generate_rsa_key(key_filename=kf, key_size=2048): def generate_rsa_key(key_filename=kf, key_size=2048):
key = paramiko.RSAKey.generate(key_size) key = paramiko.RSAKey.generate(key_size)
key.write_private_key_file(key_filename) key.write_private_key_file(key_filename)
print(f"Key generated: {key_filename}") print(f"Key generated: {key_filename}")
print("Goodbye!") print("Goodbye!")
def delete_existing_key(): def delete_existing_key():
delete_input = input( delete_input = input(
f"Key {kf} exists. Delete it? (y/n): ").strip().lower() f"Key {kf} exists. Delete it? (y/n): ").strip().lower()
if delete_input == 'y': if delete_input == 'y':
os.remove(kf) os.remove(kf)
print(f"Key {kf} deleted.") print(f"Key {kf} deleted.")
generate_rsa_key() generate_rsa_key()
else: else:
print('Okay, goodbye!') print('Okay, goodbye!')
def prompt_for_key_generation(): def prompt_for_key_generation():
if os.path.exists(kf): if os.path.exists(kf):
delete_existing_key() delete_existing_key()
else: else:
generate_rsa_key() generate_rsa_key()
def handle_user_input(): def handle_user_input():
user_input = input("Enter 1 to generate the key or 0 to exit: ").strip() user_input = input("Enter 1 to generate the key or 0 to exit: ").strip()
if user_input == "1": if user_input == "1":
prompt_for_key_generation() prompt_for_key_generation()
elif user_input == "0": elif user_input == "0":
print("Bye") print("Bye")
else: else:
print("Invalid input. Please try again.") print("Invalid input. Please try again.")
def main1(): def main1():
handle_user_input() handle_user_input()
def save_config(tg_bot_token, chat_id, cb, ma, delay, port): def save_config(tg_bot_token, chat_id, cb, ma, delay, port):
conn = sqlite3.connect("config.db") conn = sqlite3.connect("config.db")
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute("DELETE FROM config") cursor.execute("DELETE FROM config")
cursor.execute(""" cursor.execute("""
INSERT INTO config (tg_bot_token, chat_id, cb, ma, delay, port) INSERT INTO config (tg_bot_token, chat_id, cb, ma, delay, port)
VALUES (?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?)
""", (tg_bot_token, chat_id, cb, ma, delay, port)) """, (tg_bot_token, chat_id, cb, ma, delay, port))
conn.commit() conn.commit()
conn.close() conn.close()
def check(prompt: str, condition: Callable[[int], bool], default=None) -> int: def check(prompt: str, condition: Callable[[int], bool], default=None) -> int:
while True: while True:
try: try:
raw = input(prompt + ": ").strip() raw = input(prompt + ": ").strip()
if not raw and default is not None: if not raw and default is not None:
return default return default
value = int(raw) value = int(raw)
if condition(value): if condition(value):
return value return value
print("Incorrect value, please try again..") print("Incorrect value, please try again..")
except ValueError: except ValueError:
print("You must enter an integer value.") print("You must enter an integer value.")
def main(): def main():
print("Сonfigure honeypot for yourself:") print("Configure honeypot for yourself:")
while True: while True:
tg_bot_token = input("Telegram Bot Token: ").strip() tg_bot_token = input("Telegram Bot Token: ").strip()
if not tg_bot_token: if not tg_bot_token:
print("Token can't be blank") print("Token can't be blank")
else: else:
break break
chat_id = check("Telegram Chat ID (botapi) ", lambda x: x < 0) chat_id = check("Telegram Chat ID (botapi) ", lambda x: x < 0)
cb = "SSH-2.0-OpenSSH_8.9p1 Ubuntu-3ubuntu0.10" cb = "SSH-2.0-OpenSSH_8.9p1 Ubuntu-3ubuntu0.10"
ma = check("Max Attempts (leave blank for default value of 3)", ma = check("Max Attempts (leave blank for default value of 3)",
lambda x: x > 0, default=3) lambda x: x > 0, default=3)
delay = check("Delay (in ms(leave blank for default value of 929)) ", delay = check("Delay (in ms(leave blank for default value of 929)) ",
lambda x: x >= 0, default=929) lambda x: x >= 0, default=929)
port = check("Port (leave blank for default value of 22)", port = check("Port (leave blank for default value of 22)",
lambda x: 0 < x < 65536, default=22) lambda x: 0 < x < 65536, default=22)
save_config(tg_bot_token, chat_id, cb, ma, delay, port) save_config(tg_bot_token, chat_id, cb, ma, delay, port)
print("The configuration is saved, now you can run honeypot") print("The configuration is saved, now you can run honeypot")
if __name__ == "__main__": if __name__ == "__main__":
create_database() create_database()
main() main()
main1() main1()