Linted code iwht black tool
This commit is contained in:
291
src/handler.py
291
src/handler.py
@@ -14,8 +14,13 @@ from analyzer import Analyzer
|
||||
from templates import html_templates
|
||||
from templates.dashboard_template import generate_dashboard
|
||||
from generators import (
|
||||
credentials_txt, passwords_txt, users_json, api_keys_json,
|
||||
api_response, directory_listing, random_server_header
|
||||
credentials_txt,
|
||||
passwords_txt,
|
||||
users_json,
|
||||
api_keys_json,
|
||||
api_response,
|
||||
directory_listing,
|
||||
random_server_header,
|
||||
)
|
||||
from wordlists import get_wordlists
|
||||
from sql_errors import generate_sql_error_response, get_sql_response_with_data
|
||||
@@ -25,6 +30,7 @@ from server_errors import generate_server_error
|
||||
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
"""HTTP request handler for the deception server"""
|
||||
|
||||
webpages: Optional[List[str]] = None
|
||||
config: Config = None
|
||||
tracker: AccessTracker = None
|
||||
@@ -37,15 +43,15 @@ class Handler(BaseHTTPRequestHandler):
|
||||
def _get_client_ip(self) -> str:
|
||||
"""Extract client IP address from request, checking proxy headers first"""
|
||||
# Headers might not be available during early error logging
|
||||
if hasattr(self, 'headers') and self.headers:
|
||||
if hasattr(self, "headers") and self.headers:
|
||||
# Check X-Forwarded-For header (set by load balancers/proxies)
|
||||
forwarded_for = self.headers.get('X-Forwarded-For')
|
||||
forwarded_for = self.headers.get("X-Forwarded-For")
|
||||
if forwarded_for:
|
||||
# X-Forwarded-For can contain multiple IPs, get the first (original client)
|
||||
return forwarded_for.split(',')[0].strip()
|
||||
return forwarded_for.split(",")[0].strip()
|
||||
|
||||
# Check X-Real-IP header (set by nginx and other proxies)
|
||||
real_ip = self.headers.get('X-Real-IP')
|
||||
real_ip = self.headers.get("X-Real-IP")
|
||||
if real_ip:
|
||||
return real_ip.strip()
|
||||
|
||||
@@ -54,7 +60,7 @@ class Handler(BaseHTTPRequestHandler):
|
||||
|
||||
def _get_user_agent(self) -> str:
|
||||
"""Extract user agent from request"""
|
||||
return self.headers.get('User-Agent', '')
|
||||
return self.headers.get("User-Agent", "")
|
||||
|
||||
def _get_category_by_ip(self, client_ip: str) -> str:
|
||||
"""Get the category of an IP from the database"""
|
||||
@@ -97,7 +103,7 @@ class Handler(BaseHTTPRequestHandler):
|
||||
Returns True if the path was handled, False otherwise.
|
||||
"""
|
||||
# SQL-vulnerable endpoints
|
||||
sql_endpoints = ['/api/search', '/api/sql', '/api/database']
|
||||
sql_endpoints = ["/api/search", "/api/sql", "/api/database"]
|
||||
|
||||
base_path = urlparse(path).path
|
||||
if base_path not in sql_endpoints:
|
||||
@@ -112,22 +118,30 @@ class Handler(BaseHTTPRequestHandler):
|
||||
user_agent = self._get_user_agent()
|
||||
|
||||
# Always check for SQL injection patterns
|
||||
error_msg, content_type, status_code = generate_sql_error_response(query_string or "")
|
||||
error_msg, content_type, status_code = generate_sql_error_response(
|
||||
query_string or ""
|
||||
)
|
||||
|
||||
if error_msg:
|
||||
# SQL injection detected - log and return error
|
||||
self.access_logger.warning(f"[SQL INJECTION DETECTED] {client_ip} - {base_path} - Query: {query_string[:100] if query_string else 'empty'}")
|
||||
self.access_logger.warning(
|
||||
f"[SQL INJECTION DETECTED] {client_ip} - {base_path} - Query: {query_string[:100] if query_string else 'empty'}"
|
||||
)
|
||||
self.send_response(status_code)
|
||||
self.send_header('Content-type', content_type)
|
||||
self.send_header("Content-type", content_type)
|
||||
self.end_headers()
|
||||
self.wfile.write(error_msg.encode())
|
||||
else:
|
||||
# No injection detected - return fake data
|
||||
self.access_logger.info(f"[SQL ENDPOINT] {client_ip} - {base_path} - Query: {query_string[:100] if query_string else 'empty'}")
|
||||
self.access_logger.info(
|
||||
f"[SQL ENDPOINT] {client_ip} - {base_path} - Query: {query_string[:100] if query_string else 'empty'}"
|
||||
)
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'application/json')
|
||||
self.send_header("Content-type", "application/json")
|
||||
self.end_headers()
|
||||
response_data = get_sql_response_with_data(base_path, query_string or "")
|
||||
response_data = get_sql_response_with_data(
|
||||
base_path, query_string or ""
|
||||
)
|
||||
self.wfile.write(response_data.encode())
|
||||
|
||||
return True
|
||||
@@ -140,7 +154,7 @@ class Handler(BaseHTTPRequestHandler):
|
||||
# Still send a response even on error
|
||||
try:
|
||||
self.send_response(500)
|
||||
self.send_header('Content-type', 'application/json')
|
||||
self.send_header("Content-type", "application/json")
|
||||
self.end_headers()
|
||||
self.wfile.write(b'{"error": "Internal server error"}')
|
||||
except:
|
||||
@@ -148,31 +162,35 @@ class Handler(BaseHTTPRequestHandler):
|
||||
return True
|
||||
|
||||
def generate_page(self, seed: str, page_visit_count: int) -> str:
|
||||
"""Generate a webpage containing random links or canary token"""
|
||||
"""Generate a webpage containing random links or canary token"""
|
||||
|
||||
random.seed(seed)
|
||||
num_pages = random.randint(*self.config.links_per_page_range)
|
||||
|
||||
|
||||
# Check if this is a good crawler by IP category from database
|
||||
ip_category = self._get_category_by_ip(self._get_client_ip())
|
||||
|
||||
|
||||
# Determine if we should apply crawler page limit based on config and IP category
|
||||
should_apply_crawler_limit = False
|
||||
if self.config.infinite_pages_for_malicious:
|
||||
if (ip_category == "good_crawler" or ip_category == "regular_user") and page_visit_count >= self.config.max_pages_limit:
|
||||
if (
|
||||
ip_category == "good_crawler" or ip_category == "regular_user"
|
||||
) and page_visit_count >= self.config.max_pages_limit:
|
||||
should_apply_crawler_limit = True
|
||||
else:
|
||||
if (ip_category == "good_crawler" or ip_category == "bad_crawler" or ip_category == "attacker") and page_visit_count >= self.config.max_pages_limit:
|
||||
if (
|
||||
ip_category == "good_crawler"
|
||||
or ip_category == "bad_crawler"
|
||||
or ip_category == "attacker"
|
||||
) and page_visit_count >= self.config.max_pages_limit:
|
||||
should_apply_crawler_limit = True
|
||||
|
||||
|
||||
# If good crawler reached max pages, return a simple page with no links
|
||||
if should_apply_crawler_limit:
|
||||
return html_templates.main_page(
|
||||
Handler.counter,
|
||||
'<p>Crawl limit reached.</p>'
|
||||
Handler.counter, "<p>Crawl limit reached.</p>"
|
||||
)
|
||||
|
||||
|
||||
num_pages = random.randint(*self.config.links_per_page_range)
|
||||
|
||||
# Build the content HTML
|
||||
@@ -189,10 +207,12 @@ class Handler(BaseHTTPRequestHandler):
|
||||
# Add links
|
||||
if self.webpages is None:
|
||||
for _ in range(num_pages):
|
||||
address = ''.join([
|
||||
random.choice(self.config.char_space)
|
||||
for _ in range(random.randint(*self.config.links_length_range))
|
||||
])
|
||||
address = "".join(
|
||||
[
|
||||
random.choice(self.config.char_space)
|
||||
for _ in range(random.randint(*self.config.links_length_range))
|
||||
]
|
||||
)
|
||||
content += f"""
|
||||
<div class="link-box">
|
||||
<a href="{address}">{address}</a>
|
||||
@@ -223,27 +243,36 @@ class Handler(BaseHTTPRequestHandler):
|
||||
post_data = ""
|
||||
|
||||
from urllib.parse import urlparse
|
||||
|
||||
base_path = urlparse(self.path).path
|
||||
|
||||
if base_path in ['/api/search', '/api/sql', '/api/database']:
|
||||
content_length = int(self.headers.get('Content-Length', 0))
|
||||
if base_path in ["/api/search", "/api/sql", "/api/database"]:
|
||||
content_length = int(self.headers.get("Content-Length", 0))
|
||||
if content_length > 0:
|
||||
post_data = self.rfile.read(content_length).decode('utf-8', errors="replace")
|
||||
post_data = self.rfile.read(content_length).decode(
|
||||
"utf-8", errors="replace"
|
||||
)
|
||||
|
||||
self.access_logger.info(f"[SQL ENDPOINT POST] {client_ip} - {base_path} - Data: {post_data[:100] if post_data else 'empty'}")
|
||||
self.access_logger.info(
|
||||
f"[SQL ENDPOINT POST] {client_ip} - {base_path} - Data: {post_data[:100] if post_data else 'empty'}"
|
||||
)
|
||||
|
||||
error_msg, content_type, status_code = generate_sql_error_response(post_data)
|
||||
error_msg, content_type, status_code = generate_sql_error_response(
|
||||
post_data
|
||||
)
|
||||
|
||||
try:
|
||||
if error_msg:
|
||||
self.access_logger.warning(f"[SQL INJECTION DETECTED POST] {client_ip} - {base_path}")
|
||||
self.access_logger.warning(
|
||||
f"[SQL INJECTION DETECTED POST] {client_ip} - {base_path}"
|
||||
)
|
||||
self.send_response(status_code)
|
||||
self.send_header('Content-type', content_type)
|
||||
self.send_header("Content-type", content_type)
|
||||
self.end_headers()
|
||||
self.wfile.write(error_msg.encode())
|
||||
else:
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'application/json')
|
||||
self.send_header("Content-type", "application/json")
|
||||
self.end_headers()
|
||||
response_data = get_sql_response_with_data(base_path, post_data)
|
||||
self.wfile.write(response_data.encode())
|
||||
@@ -253,28 +282,35 @@ class Handler(BaseHTTPRequestHandler):
|
||||
self.app_logger.error(f"Error in SQL POST handler: {str(e)}")
|
||||
return
|
||||
|
||||
if base_path == '/api/contact':
|
||||
content_length = int(self.headers.get('Content-Length', 0))
|
||||
if base_path == "/api/contact":
|
||||
content_length = int(self.headers.get("Content-Length", 0))
|
||||
if content_length > 0:
|
||||
post_data = self.rfile.read(content_length).decode('utf-8', errors="replace")
|
||||
post_data = self.rfile.read(content_length).decode(
|
||||
"utf-8", errors="replace"
|
||||
)
|
||||
|
||||
parsed_data = {}
|
||||
for pair in post_data.split('&'):
|
||||
if '=' in pair:
|
||||
key, value = pair.split('=', 1)
|
||||
for pair in post_data.split("&"):
|
||||
if "=" in pair:
|
||||
key, value = pair.split("=", 1)
|
||||
from urllib.parse import unquote_plus
|
||||
|
||||
parsed_data[unquote_plus(key)] = unquote_plus(value)
|
||||
|
||||
xss_detected = any(detect_xss_pattern(v) for v in parsed_data.values())
|
||||
|
||||
if xss_detected:
|
||||
self.access_logger.warning(f"[XSS ATTEMPT DETECTED] {client_ip} - {base_path} - Data: {post_data[:200]}")
|
||||
self.access_logger.warning(
|
||||
f"[XSS ATTEMPT DETECTED] {client_ip} - {base_path} - Data: {post_data[:200]}"
|
||||
)
|
||||
else:
|
||||
self.access_logger.info(f"[XSS ENDPOINT POST] {client_ip} - {base_path}")
|
||||
self.access_logger.info(
|
||||
f"[XSS ENDPOINT POST] {client_ip} - {base_path}"
|
||||
)
|
||||
|
||||
try:
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/html')
|
||||
self.send_header("Content-type", "text/html")
|
||||
self.end_headers()
|
||||
response_html = generate_xss_response(parsed_data)
|
||||
self.wfile.write(response_html.encode())
|
||||
@@ -284,11 +320,15 @@ class Handler(BaseHTTPRequestHandler):
|
||||
self.app_logger.error(f"Error in XSS POST handler: {str(e)}")
|
||||
return
|
||||
|
||||
self.access_logger.warning(f"[LOGIN ATTEMPT] {client_ip} - {self.path} - {user_agent[:50]}")
|
||||
self.access_logger.warning(
|
||||
f"[LOGIN ATTEMPT] {client_ip} - {self.path} - {user_agent[:50]}"
|
||||
)
|
||||
|
||||
content_length = int(self.headers.get('Content-Length', 0))
|
||||
content_length = int(self.headers.get("Content-Length", 0))
|
||||
if content_length > 0:
|
||||
post_data = self.rfile.read(content_length).decode('utf-8', errors="replace")
|
||||
post_data = self.rfile.read(content_length).decode(
|
||||
"utf-8", errors="replace"
|
||||
)
|
||||
|
||||
self.access_logger.warning(f"[POST DATA] {post_data[:200]}")
|
||||
|
||||
@@ -301,18 +341,24 @@ class Handler(BaseHTTPRequestHandler):
|
||||
self.credential_logger.info(credential_line)
|
||||
|
||||
# Also record in tracker for dashboard
|
||||
self.tracker.record_credential_attempt(client_ip, self.path, username or 'N/A', password or 'N/A')
|
||||
self.tracker.record_credential_attempt(
|
||||
client_ip, self.path, username or "N/A", password or "N/A"
|
||||
)
|
||||
|
||||
self.access_logger.warning(f"[CREDENTIALS CAPTURED] {client_ip} - Username: {username or 'N/A'} - Path: {self.path}")
|
||||
self.access_logger.warning(
|
||||
f"[CREDENTIALS CAPTURED] {client_ip} - Username: {username or 'N/A'} - Path: {self.path}"
|
||||
)
|
||||
|
||||
# send the post data (body) to the record_access function so the post data can be used to detect suspicious things.
|
||||
self.tracker.record_access(client_ip, self.path, user_agent, post_data, method='POST')
|
||||
self.tracker.record_access(
|
||||
client_ip, self.path, user_agent, post_data, method="POST"
|
||||
)
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
try:
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/html')
|
||||
self.send_header("Content-type", "text/html")
|
||||
self.end_headers()
|
||||
self.wfile.write(html_templates.login_error().encode())
|
||||
except BrokenPipeError:
|
||||
@@ -330,95 +376,102 @@ class Handler(BaseHTTPRequestHandler):
|
||||
return True
|
||||
|
||||
try:
|
||||
if path == '/robots.txt':
|
||||
if path == "/robots.txt":
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/plain')
|
||||
self.send_header("Content-type", "text/plain")
|
||||
self.end_headers()
|
||||
self.wfile.write(html_templates.robots_txt().encode())
|
||||
return True
|
||||
|
||||
if path in ['/credentials.txt', '/passwords.txt', '/admin_notes.txt']:
|
||||
if path in ["/credentials.txt", "/passwords.txt", "/admin_notes.txt"]:
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/plain')
|
||||
self.send_header("Content-type", "text/plain")
|
||||
self.end_headers()
|
||||
if 'credentials' in path:
|
||||
if "credentials" in path:
|
||||
self.wfile.write(credentials_txt().encode())
|
||||
else:
|
||||
self.wfile.write(passwords_txt().encode())
|
||||
return True
|
||||
|
||||
if path in ['/users.json', '/api_keys.json', '/config.json']:
|
||||
if path in ["/users.json", "/api_keys.json", "/config.json"]:
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'application/json')
|
||||
self.send_header("Content-type", "application/json")
|
||||
self.end_headers()
|
||||
if 'users' in path:
|
||||
if "users" in path:
|
||||
self.wfile.write(users_json().encode())
|
||||
elif 'api_keys' in path:
|
||||
elif "api_keys" in path:
|
||||
self.wfile.write(api_keys_json().encode())
|
||||
else:
|
||||
self.wfile.write(api_response('/api/config').encode())
|
||||
self.wfile.write(api_response("/api/config").encode())
|
||||
return True
|
||||
|
||||
if path in ['/admin', '/admin/', '/admin/login', '/login']:
|
||||
if path in ["/admin", "/admin/", "/admin/login", "/login"]:
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/html')
|
||||
self.send_header("Content-type", "text/html")
|
||||
self.end_headers()
|
||||
self.wfile.write(html_templates.login_form().encode())
|
||||
return True
|
||||
|
||||
if path in ['/users', '/user', '/database', '/db', '/search']:
|
||||
if path in ["/users", "/user", "/database", "/db", "/search"]:
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/html')
|
||||
self.send_header("Content-type", "text/html")
|
||||
self.end_headers()
|
||||
self.wfile.write(html_templates.product_search().encode())
|
||||
return True
|
||||
|
||||
if path in ['/info', '/input', '/contact', '/feedback', '/comment']:
|
||||
if path in ["/info", "/input", "/contact", "/feedback", "/comment"]:
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/html')
|
||||
self.send_header("Content-type", "text/html")
|
||||
self.end_headers()
|
||||
self.wfile.write(html_templates.input_form().encode())
|
||||
return True
|
||||
|
||||
if path == '/server':
|
||||
if path == "/server":
|
||||
error_html, content_type = generate_server_error()
|
||||
self.send_response(500)
|
||||
self.send_header('Content-type', content_type)
|
||||
self.send_header("Content-type", content_type)
|
||||
self.end_headers()
|
||||
self.wfile.write(error_html.encode())
|
||||
return True
|
||||
|
||||
if path in ['/wp-login.php', '/wp-login', '/wp-admin', '/wp-admin/']:
|
||||
if path in ["/wp-login.php", "/wp-login", "/wp-admin", "/wp-admin/"]:
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/html')
|
||||
self.send_header("Content-type", "text/html")
|
||||
self.end_headers()
|
||||
self.wfile.write(html_templates.wp_login().encode())
|
||||
return True
|
||||
|
||||
if path in ['/wp-content/', '/wp-includes/'] or 'wordpress' in path.lower():
|
||||
if path in ["/wp-content/", "/wp-includes/"] or "wordpress" in path.lower():
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/html')
|
||||
self.send_header("Content-type", "text/html")
|
||||
self.end_headers()
|
||||
self.wfile.write(html_templates.wordpress().encode())
|
||||
return True
|
||||
|
||||
if 'phpmyadmin' in path.lower() or path in ['/pma/', '/phpMyAdmin/']:
|
||||
if "phpmyadmin" in path.lower() or path in ["/pma/", "/phpMyAdmin/"]:
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/html')
|
||||
self.send_header("Content-type", "text/html")
|
||||
self.end_headers()
|
||||
self.wfile.write(html_templates.phpmyadmin().encode())
|
||||
return True
|
||||
|
||||
if path.startswith('/api/') or path.startswith('/api') or path in ['/.env']:
|
||||
if path.startswith("/api/") or path.startswith("/api") or path in ["/.env"]:
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'application/json')
|
||||
self.send_header("Content-type", "application/json")
|
||||
self.end_headers()
|
||||
self.wfile.write(api_response(path).encode())
|
||||
return True
|
||||
|
||||
if path in ['/backup/', '/uploads/', '/private/', '/admin/', '/config/', '/database/']:
|
||||
if path in [
|
||||
"/backup/",
|
||||
"/uploads/",
|
||||
"/private/",
|
||||
"/admin/",
|
||||
"/config/",
|
||||
"/database/",
|
||||
]:
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/html')
|
||||
self.send_header("Content-type", "text/html")
|
||||
self.end_headers()
|
||||
self.wfile.write(directory_listing(path).encode())
|
||||
return True
|
||||
@@ -440,9 +493,12 @@ class Handler(BaseHTTPRequestHandler):
|
||||
return
|
||||
user_agent = self._get_user_agent()
|
||||
|
||||
if self.config.dashboard_secret_path and self.path == self.config.dashboard_secret_path:
|
||||
if (
|
||||
self.config.dashboard_secret_path
|
||||
and self.path == self.config.dashboard_secret_path
|
||||
):
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/html')
|
||||
self.send_header("Content-type", "text/html")
|
||||
self.end_headers()
|
||||
try:
|
||||
stats = self.tracker.get_stats()
|
||||
@@ -455,72 +511,93 @@ class Handler(BaseHTTPRequestHandler):
|
||||
return
|
||||
|
||||
# API endpoint for fetching IP stats
|
||||
if self.config.dashboard_secret_path and self.path.startswith(f"{self.config.dashboard_secret_path}/api/ip-stats/"):
|
||||
ip_address = self.path.replace(f"{self.config.dashboard_secret_path}/api/ip-stats/", "")
|
||||
if self.config.dashboard_secret_path and self.path.startswith(
|
||||
f"{self.config.dashboard_secret_path}/api/ip-stats/"
|
||||
):
|
||||
ip_address = self.path.replace(
|
||||
f"{self.config.dashboard_secret_path}/api/ip-stats/", ""
|
||||
)
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'application/json')
|
||||
self.send_header('Access-Control-Allow-Origin', '*')
|
||||
self.send_header("Content-type", "application/json")
|
||||
self.send_header("Access-Control-Allow-Origin", "*")
|
||||
# Prevent browser caching - force fresh data from database every time
|
||||
self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
|
||||
self.send_header('Pragma', 'no-cache')
|
||||
self.send_header('Expires', '0')
|
||||
self.send_header(
|
||||
"Cache-Control", "no-store, no-cache, must-revalidate, max-age=0"
|
||||
)
|
||||
self.send_header("Pragma", "no-cache")
|
||||
self.send_header("Expires", "0")
|
||||
self.end_headers()
|
||||
try:
|
||||
from database import get_database
|
||||
import json
|
||||
|
||||
db = get_database()
|
||||
ip_stats = db.get_ip_stats_by_ip(ip_address)
|
||||
if ip_stats:
|
||||
self.wfile.write(json.dumps(ip_stats).encode())
|
||||
else:
|
||||
self.wfile.write(json.dumps({'error': 'IP not found'}).encode())
|
||||
self.wfile.write(json.dumps({"error": "IP not found"}).encode())
|
||||
except BrokenPipeError:
|
||||
pass
|
||||
except Exception as e:
|
||||
self.app_logger.error(f"Error fetching IP stats: {e}")
|
||||
self.wfile.write(json.dumps({'error': str(e)}).encode())
|
||||
self.wfile.write(json.dumps({"error": str(e)}).encode())
|
||||
return
|
||||
|
||||
# API endpoint for downloading malicious IPs file
|
||||
if self.config.dashboard_secret_path and self.path == f"{self.config.dashboard_secret_path}/api/download/malicious_ips.txt":
|
||||
if (
|
||||
self.config.dashboard_secret_path
|
||||
and self.path
|
||||
== f"{self.config.dashboard_secret_path}/api/download/malicious_ips.txt"
|
||||
):
|
||||
import os
|
||||
file_path = os.path.join(os.path.dirname(__file__), 'exports', 'malicious_ips.txt')
|
||||
|
||||
file_path = os.path.join(
|
||||
os.path.dirname(__file__), "exports", "malicious_ips.txt"
|
||||
)
|
||||
try:
|
||||
if os.path.exists(file_path):
|
||||
with open(file_path, 'rb') as f:
|
||||
with open(file_path, "rb") as f:
|
||||
content = f.read()
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/plain')
|
||||
self.send_header('Content-Disposition', 'attachment; filename="malicious_ips.txt"')
|
||||
self.send_header('Content-Length', str(len(content)))
|
||||
self.send_header("Content-type", "text/plain")
|
||||
self.send_header(
|
||||
"Content-Disposition",
|
||||
'attachment; filename="malicious_ips.txt"',
|
||||
)
|
||||
self.send_header("Content-Length", str(len(content)))
|
||||
self.end_headers()
|
||||
self.wfile.write(content)
|
||||
else:
|
||||
self.send_response(404)
|
||||
self.send_header('Content-type', 'text/plain')
|
||||
self.send_header("Content-type", "text/plain")
|
||||
self.end_headers()
|
||||
self.wfile.write(b'File not found')
|
||||
self.wfile.write(b"File not found")
|
||||
except BrokenPipeError:
|
||||
pass
|
||||
except Exception as e:
|
||||
self.app_logger.error(f"Error serving malicious IPs file: {e}")
|
||||
self.send_response(500)
|
||||
self.send_header('Content-type', 'text/plain')
|
||||
self.send_header("Content-type", "text/plain")
|
||||
self.end_headers()
|
||||
self.wfile.write(b'Internal server error')
|
||||
self.wfile.write(b"Internal server error")
|
||||
return
|
||||
|
||||
self.tracker.record_access(client_ip, self.path, user_agent, method='GET')
|
||||
self.tracker.record_access(client_ip, self.path, user_agent, method="GET")
|
||||
|
||||
# self.analyzer.infer_user_category(client_ip)
|
||||
# self.analyzer.update_ip_rep_infos(client_ip)
|
||||
|
||||
if self.tracker.is_suspicious_user_agent(user_agent):
|
||||
self.access_logger.warning(f"[SUSPICIOUS] {client_ip} - {user_agent[:50]} - {self.path}")
|
||||
self.access_logger.warning(
|
||||
f"[SUSPICIOUS] {client_ip} - {user_agent[:50]} - {self.path}"
|
||||
)
|
||||
|
||||
if self._should_return_error():
|
||||
error_code = self._get_random_error_code()
|
||||
self.access_logger.info(f"Returning error {error_code} to {client_ip} - {self.path}")
|
||||
self.access_logger.info(
|
||||
f"Returning error {error_code} to {client_ip} - {self.path}"
|
||||
)
|
||||
self.send_response(error_code)
|
||||
self.end_headers()
|
||||
return
|
||||
@@ -530,13 +607,15 @@ class Handler(BaseHTTPRequestHandler):
|
||||
|
||||
time.sleep(self.config.delay / 1000.0)
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/html')
|
||||
self.send_header("Content-type", "text/html")
|
||||
self.end_headers()
|
||||
|
||||
try:
|
||||
# Increment page visit counter for this IP and get the current count
|
||||
current_visit_count = self._increment_page_visit(client_ip)
|
||||
self.wfile.write(self.generate_page(self.path, current_visit_count).encode())
|
||||
self.wfile.write(
|
||||
self.generate_page(self.path, current_visit_count).encode()
|
||||
)
|
||||
|
||||
Handler.counter -= 1
|
||||
|
||||
|
||||
Reference in New Issue
Block a user