|
| 1 | +import socket |
| 2 | +import paramiko |
| 3 | +import threading |
| 4 | +import logging |
| 5 | +import os |
| 6 | +import time |
| 7 | +import json |
| 8 | +import argparse |
| 9 | +from scapy.all import sniff, IP, TCP |
| 10 | +import smtplib |
| 11 | +from email.mime.text import MIMEText |
| 12 | + |
| 13 | +def parse_args(): |
| 14 | + parser = argparse.ArgumentParser(description='SSH Honeypot') |
| 15 | + parser.add_argument('--host', type=str, default='0.0.0.0', help='IP address to bind the honeypot') |
| 16 | + parser.add_argument('--port', type=int, default=2222, help='Port to bind the honeypot') |
| 17 | + parser.add_argument('--downloads_dir', type=str, default='downloads', help='Directory to save downloaded files') |
| 18 | + parser.add_argument('--log_file', type=str, default='honeypot.log', help='Log file path') |
| 19 | + parser.add_argument('--commands_log', type=str, default='commands.log', help='Commands log file path') |
| 20 | + parser.add_argument('--downloads_log', type=str, default='downloads.log', help='Downloads log file path') |
| 21 | + parser.add_argument('--smtp_server', type=str, default='smtp.gmail.com', help='SMTP server for sending alerts') |
| 22 | + parser.add_argument('--smtp_port', type=int, default=587, help='SMTP server port') |
| 23 | + parser.add_argument('--email_from', type=str, required=True, help='Email address to send alerts from') |
| 24 | + parser.add_argument('--email_to', type=str, required=True, help='Email address to send alerts to') |
| 25 | + parser.add_argument('--email_subject', type=str, default='Honeypot Alert', help='Subject of alert emails') |
| 26 | + parser.add_argument('--email_username', type=str, required=True, help='Username for the SMTP server') |
| 27 | + parser.add_argument('--email_password', type=str, required=True, help='Password for the SMTP server') |
| 28 | + return parser.parse_args() |
| 29 | + |
| 30 | +# Configuración de registro |
| 31 | +def setup_logging(log_file): |
| 32 | + logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(message)s') |
| 33 | + |
| 34 | +# Generar la clave RSA si no existe |
| 35 | +def generate_rsa_key(key_filename): |
| 36 | + if not os.path.exists(key_filename): |
| 37 | + os.system(f'ssh-keygen -t rsa -b 2048 -f {key_filename} -N ""') |
| 38 | + |
| 39 | +class Server(paramiko.ServerInterface): |
| 40 | + def __init__(self): |
| 41 | + self.event = threading.Event() |
| 42 | + |
| 43 | + def check_channel_request(self, kind, chanid): |
| 44 | + if kind == 'session': |
| 45 | + return paramiko.OPEN_SUCCEEDED |
| 46 | + return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED |
| 47 | + |
| 48 | + def check_auth_password(self, username, password): |
| 49 | + logging.info(f"Login attempt with username: {username} and password: {password}") |
| 50 | + alert_admin(f"Login attempt with username: {username} and password: {password}") |
| 51 | + return paramiko.AUTH_FAILED |
| 52 | + |
| 53 | +def handle_connection(client_socket, host_key, commands_log, downloads_log, downloads_dir): |
| 54 | + try: |
| 55 | + transport = paramiko.Transport(client_socket) |
| 56 | + transport.add_server_key(host_key) |
| 57 | + |
| 58 | + server = Server() |
| 59 | + try: |
| 60 | + transport.start_server(server=server) |
| 61 | + except paramiko.SSHException: |
| 62 | + logging.error("SSH negotiation failed") |
| 63 | + return |
| 64 | + |
| 65 | + chan = transport.accept(20) |
| 66 | + if chan is None: |
| 67 | + logging.error("No channel") |
| 68 | + return |
| 69 | + |
| 70 | + chan.send("Welcome to the SSH honeypot!\n") |
| 71 | + |
| 72 | + while True: |
| 73 | + command = chan.recv(1024).decode('utf-8') |
| 74 | + if not command: |
| 75 | + break |
| 76 | + logging.info(f"Command received: {command}") |
| 77 | + log_command(command, commands_log) |
| 78 | + chan.send(f"Command '{command}' received.\n") |
| 79 | + |
| 80 | + if command.startswith('wget') or command.startswith('curl'): |
| 81 | + handle_file_download(command, downloads_dir, downloads_log) |
| 82 | + |
| 83 | + chan.close() |
| 84 | + except Exception as e: |
| 85 | + logging.error(f"Exception: {str(e)}") |
| 86 | + finally: |
| 87 | + client_socket.close() |
| 88 | + |
| 89 | +def handle_file_download(command, downloads_dir, downloads_log): |
| 90 | + try: |
| 91 | + if 'wget' in command: |
| 92 | + url = command.split(' ')[1] |
| 93 | + elif 'curl' in command: |
| 94 | + url = command.split(' ')[2] |
| 95 | + |
| 96 | + filename = url.split('/')[-1] |
| 97 | + os.system(f"wget {url} -O {downloads_dir}/{filename}") |
| 98 | + logging.info(f"File downloaded: {filename}") |
| 99 | + log_downloaded_file(filename, url, downloads_log) |
| 100 | + except Exception as e: |
| 101 | + logging.error(f"Error downloading file: {str(e)}") |
| 102 | + |
| 103 | +def log_command(command, commands_log): |
| 104 | + with open(commands_log, 'a') as f: |
| 105 | + f.write(f"{time.ctime()} - Command: {command}\n") |
| 106 | + |
| 107 | +def log_downloaded_file(filename, url, downloads_log): |
| 108 | + with open(downloads_log, 'a') as f: |
| 109 | + f.write(f"{time.ctime()} - File: {filename}, URL: {url}\n") |
| 110 | + |
| 111 | +def analyze_traffic(): |
| 112 | + def process_packet(packet): |
| 113 | + if packet.haslayer(TCP) and packet.haslayer(IP): |
| 114 | + ip_src = packet[IP].src |
| 115 | + ip_dst = packet[IP].dst |
| 116 | + tcp_sport = packet[TCP].sport |
| 117 | + tcp_dport = packet[TCP].dport |
| 118 | + logging.info(f"Traffic - SRC: {ip_src}:{tcp_sport} DST: {ip_dst}:{tcp_dport}") |
| 119 | + |
| 120 | + sniff(prn=process_packet, filter="tcp", store=0) |
| 121 | + |
| 122 | +def alert_admin(message): |
| 123 | + args = parse_args() |
| 124 | + try: |
| 125 | + msg = MIMEText(message) |
| 126 | + msg['Subject'] = args.email_subject |
| 127 | + msg['From'] = args.email_from |
| 128 | + msg['To'] = args.email_to |
| 129 | + |
| 130 | + server = smtplib.SMTP(args.smtp_server, args.smtp_port) |
| 131 | + server.starttls() |
| 132 | + server.login(args.email_username, args.email_password) |
| 133 | + server.sendmail(args.email_from, [args.email_to], msg.as_string()) |
| 134 | + server.quit() |
| 135 | + |
| 136 | + logging.info(f"Alert sent: {message}") |
| 137 | + except Exception as e: |
| 138 | + logging.error(f"Failed to send alert: {str(e)}") |
| 139 | + |
| 140 | +def main(): |
| 141 | + args = parse_args() |
| 142 | + |
| 143 | + setup_logging(args.log_file) |
| 144 | + generate_rsa_key('test_rsa.key') |
| 145 | + host_key = paramiko.RSAKey(filename='test_rsa.key') |
| 146 | + |
| 147 | + if not os.path.exists(args.downloads_dir): |
| 148 | + os.makedirs(args.downloads_dir) |
| 149 | + |
| 150 | + server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 151 | + server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 152 | + server.bind((args.host, args.port)) |
| 153 | + server.listen(100) |
| 154 | + |
| 155 | + logging.info("Honeypot started and listening for connections...") |
| 156 | + |
| 157 | + threading.Thread(target=analyze_traffic, daemon=True).start() |
| 158 | + |
| 159 | + while True: |
| 160 | + client_socket, addr = server.accept() |
| 161 | + logging.info(f"Connection from {addr}") |
| 162 | + threading.Thread(target=handle_connection, args=(client_socket, host_key, args.commands_log, args.downloads_log, args.downloads_dir)).start() |
| 163 | + |
| 164 | +if __name__ == "__main__": |
| 165 | + BANNER = """ |
| 166 | + ██╗ █████╗ ███████╗██╗ ██╗ ██████╗ ██╗ ██╗███╗ ██╗ |
| 167 | + ██║ ██╔══██╗╚══███╔╝╚██╗ ██╔╝██╔═══██╗██║ ██║████╗ ██║ |
| 168 | + ██║ ███████║ ███╔╝ ╚████╔╝ ██║ ██║██║ █╗ ██║██╔██╗ ██║ |
| 169 | + ██║ ██╔══██║ ███╔╝ ╚██╔╝ ██║ ██║██║███╗██║██║╚██╗██║ |
| 170 | + ███████╗██║ ██║███████╗ ██║ ╚██████╔╝╚███╔███╔╝██║ ╚████║ |
| 171 | + ╚══════╝╚═╝ ╚═╝╚══════╝ ╚═╝ ╚═════╝ ╚══╝╚══╝ ╚═╝ ╚═══╝ |
| 172 | + [*] Iniciando: LazyOwn Honeypot [;,;] |
| 173 | + """ |
| 174 | + print(BANNER) |
| 175 | + main() |
0 commit comments