EAHonepot/setup.py

121 lines
3.1 KiB
Python
Raw Normal View History

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