![]() |
![]() |
![]() | #1 |
Editör ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | ![]() Socket Koruma Botu - IP Bağlantı Sınırı ve Engelleme Bu Python ile yazılmış socket koruma botu, belirli IP adreslerinden gelen bağlantıları sınırlar, istenmeyen IP'leri engeller ve tüm bağlantı hareketlerini loglar. Çoklu bağlantı saldırılarını engellemek için ideal basit bir örnektir. 1. Temel Socket Koruma Botu (Bağlantı Sınırı) [code=python] import socket import threading # Maksimum bağlantı sayısı (aynı IP'den) MAX_CONNECTIONS_PER_IP = 5 # Her IP için aktif bağlantıları tutuyorum connections_per_ip = {} # Thread-safe işlemler için kilit lock = threading.Lock() def handle_client(client_socket, client_address): ip = client_address[0] print(f"[BAĞLANTI] {ip} bağlandı.") try: while True: data = client_socket.recv(1024) if not data: break # Basit echo servisi client_socket.sendall(data) except: pass finally: with lock: connections_per_ip[ip] -= 1 if connections_per_ip[ip] == 0: del connections_per_ip[ip] client_socket.close() print(f"[KAPANDI] {ip} bağlantısı kapandı.") def start_server(host='0.0.0.0', port=12345): server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((host, port)) server.listen(100) print(f"[BAŞLADI] Sunucu {host}:{port} adresinde dinliyor...") while True: client_socket, client_address = server.accept() ip = client_address[0] with lock: if ip not in connections_per_ip: connections_per_ip[ip] = 0 if connections_per_ip[ip] >= MAX_CONNECTIONS_PER_IP: print(f"[ENGELLENDİ] {ip} bağlantı sınırını aştı.") client_socket.sendall(b"Çok fazla bağlantı, lütfen daha sonra tekrar deneyin.\n") client_socket.close() continue else: connections_per_ip[ip] += 1 threading.Thread(target=handle_client, args=(client_socket, client_address)).start() if __name__ == "__main__": start_server() [/code] 2. Gelişmiş Versiyon - IP Engelleme ve Loglama [code=python] import socket import threading import datetime MAX_CONNECTIONS_PER_IP = 5 BLACKLIST = {"192.168.1.100"} # Engellenen IP'leri buraya ekle connections_per_ip = {} lock = threading.Lock() LOG_FILE = "connections.log" def log_event(message: str): timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") log_message = f"[{timestamp}] {message}" print(log_message) with open(LOG_FILE, "a") as f: f.write(log_message + "\n") def handle_client(client_socket, client_address): ip = client_address[0] log_event(f"{ip} bağlandı.") try: while True: data = client_socket.recv(1024) if not data: break client_socket.sendall(data) except Exception as e: log_event(f"{ip} bağlantısında hata: {e}") finally: with lock: connections_per_ip[ip] -= 1 if connections_per_ip[ip] == 0: del connections_per_ip[ip] client_socket.close() log_event(f"{ip} bağlantısı kapandı.") def start_server(host='0.0.0.0', port=12345): server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((host, port)) server.listen(100) log_event(f"Sunucu {host}:{port} adresinde başlatıldı.") while True: client_socket, client_address = server.accept() ip = client_address[0] if ip in BLACKLIST: log_event(f"{ip} engellendi (blacklist).") client_socket.sendall(b"IP adresiniz engellenmiştir.\n") client_socket.close() continue with lock: if ip not in connections_per_ip: connections_per_ip[ip] = 0 if connections_per_ip[ip] >= MAX_CONNECTIONS_PER_IP: log_event(f"{ip} bağlantı sınırını aştı, bağlantı reddedildi.") client_socket.sendall(b"Çok fazla bağlantı, daha sonra tekrar deneyin.\n") client_socket.close() continue else: connections_per_ip[ip] += 1 threading.Thread(target=handle_client, args=(client_socket, client_address)).start() if __name__ == "__main__": start_server() [/code]
__________________ ![]() |
![]() |
Konuyu yanıtla |
Seçenekler | |
Stil | |
| |
Forum | Bilgilendirme | Sosyal Medya |
Powered by vBulletin® Version 3.8.11 Copyright ©2000 - 2025, Jelsoft Enterprises Ltd. Bu Forum Lisanslı Vbulletin Ürünü Kullanmaktadır. | Sitemiz bir " paylaşım " sitesidir. Bu yüzden sitemize kayıt olan herkes kontrol edilmeksizin mesaj/konu/resim paylaşabilmektedir. Bu sebepten ötürü, sitemiz üzerinden paylaşılan mesajlar, konular ve resimlerden doğabilecek olan yasal sorumluluklar paylaşan kullanıcıya aittir. Web sitemiz hiçbir yasal sorumluluk kabul etmemektedir. Illegal herhangi bir faaliyet görülmesi durumunda Yöneticilere adresine mail atıldığı taktirde mesaj, konu ya da resim en fazla 24 saat içerisinde silinecektir. | BeSte |