forked from metallbrot/TunFrame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·291 lines (238 loc) · 10.6 KB
/
Copy pathmain.py
File metadata and controls
executable file
·291 lines (238 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import subprocess
import time
import yaml
from pathlib import Path
import sys
import threading
import logging
import warnings
# Suppress sklearn version warnings
warnings.filterwarnings('ignore', category=UserWarning, module='sklearn')
PROJECT_ROOT = Path(__file__).resolve().parent
sys.path.insert(0, str(PROJECT_ROOT))
from detection.evaluate import evaluate, detector_ready, wartime_stop_event, peacetime_stop_event, peacetime_done, cfg_safe_get, format_duration
from config.dns_config_generator import generate_all_configs
CONFIG_PATH = PROJECT_ROOT / "config.yaml"
DOCKER_PATH = PROJECT_ROOT / "docker"
DOCKER_COMPOSE_FILE = DOCKER_PATH / "docker-compose.yml"
DOCKER_ENV_FILE = DOCKER_PATH / ".env"
CONFIG_TEMPLATE_DIR = PROJECT_ROOT / "config" / "templates"
RESULTS_DIR = PROJECT_ROOT / "results"
DETECTOR_PATH = PROJECT_ROOT / "detection" / "detectors"
LOGFILE = DOCKER_PATH / "dns-collector" / "logs" / "dnslogs.json"
# === LOGGER SETUP (ganz am Anfang) ===
class UnifiedOutputHandler(logging.Handler):
def __init__(self):
super().__init__()
self.last_progress_length = 0
self.setFormatter(logging.Formatter('%(message)s'))
def emit(self, record):
try:
msg = self.format(record)
is_progress = getattr(record, 'progress', False)
if is_progress:
# Progress message - pad to at least the previous length to overwrite old text
padded_msg = msg.ljust(self.last_progress_length)
sys.stdout.write(f"\r{padded_msg}")
sys.stdout.flush()
self.last_progress_length = len(msg)
else:
# Regular message - clear progress line first if needed
if self.last_progress_length > 0:
sys.stdout.write("\r" + " " * self.last_progress_length + "\r")
self.last_progress_length = 0
sys.stdout.write(msg + "\n")
sys.stdout.flush()
except Exception:
self.handleError(record)
logger = logging.getLogger('dns_detector')
logger.setLevel(logging.INFO)
logger.handlers.clear()
# Console handler with custom formatting
console_handler = UnifiedOutputHandler()
console_handler.setLevel(logging.INFO)
logger.addHandler(console_handler)
logger.propagate = False
def load_config(path=CONFIG_PATH):
with open(path) as f:
return yaml.safe_load(f)
def write_docker_env(pcap_dir):
DOCKER_ENV_FILE.write_text(f"PCAP_PATH={pcap_dir}\n")
def compose_up(profile):
cmd = [
"docker",
"compose",
"-f", str(DOCKER_COMPOSE_FILE),
"--profile", profile,
"up",
"-d",
]
subprocess.run(cmd, check=True)
def compose_down():
cmd = [
"docker",
"compose",
"-f", str(DOCKER_COMPOSE_FILE),
"--profile", "*",
"down"
]
try:
result = subprocess.run(cmd, check=False)
if result.returncode != 0:
logger.warning(f"[!] Docker compose down returned code {result.returncode}")
if result.stderr:
logger.warning(f"[!] Error: {result.stderr.strip()}")
else:
logger.info("[+] Docker containers stopped successfully")
except Exception as e:
logger.error(f"[!] Failed to run docker compose down: {e}")
def docker_exec(container: str, *docker_cmd: str, check: bool = True):
cmd = ["docker", "exec", container] + list(docker_cmd)
subprocess.run(cmd, check=check)
def replay_fire_and_forget(pcap: str, pps: int, label: str):
"""Fire and forget: startet tcpreplay ohne zu warten"""
if not pcap:
logger.warning(f"[-] Skipping {label} replay (disabled)")
return
cmd = ["tcpreplay"]
if pps is None or pps <= 0:
logger.info(f"[+] Firing {label} replay at original speed from {pcap}")
cmd.extend(["-i", "replay0", pcap])
else:
logger.info(f"[+] Firing {label} replay: {pps} PPS from {pcap}")
cmd.extend([f"--pps={pps}", "-i", "replay0", pcap])
docker_exec("pcap-replayer", *cmd, check=False)
def main():
cfg = load_config(CONFIG_PATH)
duration = int(cfg_safe_get(cfg, ["timing", "duration"], 60)) * 60
peacetime_duration = cfg_safe_get(cfg, ["timing", "peacetime_duration"]) * 60
tunneling_domains = cfg_safe_get(cfg, ["traffic", "tunnel", "tunneling_domains"], [])
server_ip = cfg_safe_get(cfg, ['traffic', 'tunnel', 'tunnel_server_ip'], '192.168.4.4')
public_resolver = cfg_safe_get(cfg, ['global', 'public_resolver'], '1.1.1.1')
allowlist = cfg_safe_get(cfg, ['allowlist', 'global_allowlist_path'])
if not peacetime_duration:
peacetime_duration = 0
if allowlist is None:
allowlist = []
logger.info("\n" + "="*60)
logger.info("EXPERIMENT SETUP")
logger.info("="*60)
logger.info(f"[+] Experiment: {cfg_safe_get(cfg, ['global', 'name'], '')}")
logger.info(f"[+] Peacetime Duration: {format_duration(peacetime_duration)}")
logger.info(f"[+] Wartime Duration: {format_duration(duration - peacetime_duration)}")
logger.info(f"[+] Total Duration: {format_duration(duration)}")
logger.info("="*60)
results_file = RESULTS_DIR / f"results_{cfg_safe_get(cfg, ['global', 'name'], 'experiment')}_{int(time.time())}.json"
RESULTS_DIR.mkdir(parents=True, exist_ok=True)
t_detect = None
try:
# Ensure docker is stopped before cleaning up
logger.info("[+] Ensuring clean state...")
try:
compose_down()
except:
pass # Ignore errors if nothing is running
# Clear old logfile to start fresh
LOGFILE.parent.mkdir(parents=True, exist_ok=True)
if LOGFILE.exists():
file_size = LOGFILE.stat().st_size / (1024*1024)
logger.info(f"[+] Clearing old logfile ({file_size:.1f} MB)")
LOGFILE.unlink()
LOGFILE.touch()
write_docker_env(PROJECT_ROOT / cfg_safe_get(cfg, ['traffic', 'pcap_path']))
generate_all_configs(tunneling_domains, server_ip, public_resolver, CONFIG_TEMPLATE_DIR, DOCKER_PATH)
logger.info("[+] Starting docker compose")
compose_up("essential")
time.sleep(5)
t_detect = threading.Thread(
target=evaluate,
args=(DETECTOR_PATH, str(LOGFILE), tunneling_domains, allowlist, str(results_file), cfg),
)
t_detect.start()
if not detector_ready.wait(timeout=30):
logger.error("[!] Detector initialization timed out after 30 seconds")
raise TimeoutError("Detector initialization timed out")
logger.info("[+] Detection ready")
end_time = time.time() + duration
logger.info(f"[+] Estimated end time: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(end_time))}")
logger.info("\n" + "="*60)
logger.info("TRAFFIC GENERATION")
logger.info("="*60)
#fire-and-forget
benign_enabled = bool(cfg_safe_get(cfg, ["traffic", "benign", "enabled"], False))
benign_pcap = cfg_safe_get(cfg, ["traffic", "benign", "pcap"], "")
benign_pps = cfg_safe_get(cfg, ["traffic", "benign", "pps"])
wildcard_enabled = bool(cfg_safe_get(cfg, ["traffic", "wildcard", "enabled"], False))
wildcard_pcap = cfg_safe_get(cfg, ["traffic", "wildcard", "pcap"], "")
wildcard_pps = cfg_safe_get(cfg, ["traffic", "wildcard", "pps"])
tunneling_enabled = bool(cfg_safe_get(cfg, ["traffic", "tunnel", "replay"], False))
tunneling_pcap = cfg_safe_get(cfg, ["traffic", "tunnel", "pcap"], "")
tunneling_pps = cfg_safe_get(cfg, ["traffic", "tunnel", "pps"])
# Start benign traffic
if benign_enabled:
threading.Thread(
target=replay_fire_and_forget,
args=(benign_pcap, benign_pps, "benign"),
daemon=True,
).start()
if wildcard_enabled:
threading.Thread(
target=replay_fire_and_forget,
args=(wildcard_pcap, wildcard_pps, "wildcard"),
daemon=True,
).start()
if wildcard_enabled or benign_enabled:
logger.info(f"[+] Benign traffic started - running for {format_duration(duration)}...")
else:
logger.info(f"[+] No benign traffic - waiting for external DNS traffic or wartime...")
if peacetime_duration and peacetime_duration != 0:
logger.info(f"[+] Peacetime started: Running for {format_duration(peacetime_duration)}...")
time.sleep(peacetime_duration)
peacetime_stop_event.set()
if not peacetime_done.wait(timeout=10):
logger.warning("[!] Peacetime cleanup timed out, continuing anyway")
else:
logger.info(f"[+] Peacetime done! Remainaing duration for wartime: {format_duration(duration - peacetime_duration)}")
# Start tunnel traffic if enabled
if tunneling_enabled:
threading.Thread(
target=replay_fire_and_forget,
args=(tunneling_pcap, tunneling_pps, "tunnel"),
daemon=True,
).start()
logger.info(f"[+] Tunnel replay started - running for {format_duration(duration - peacetime_duration)}...")
if cfg_safe_get(cfg, ["traffic", "tunnel", "docker"]):
toolname = cfg_safe_get(cfg, ["traffic", "tunnel", "toolname"])
logger.info(f"[+] Starting tunneling tool: {toolname}")
compose_up(toolname)
remaining_time = duration - peacetime_duration
if not wartime_stop_event.wait(timeout=remaining_time):
logger.info("[+] Experiment duration reached - shutting down")
else:
logger.info("[+] Detection completed early - shutting down")
wartime_stop_event.set()
t_detect.join()
compose_down()
except KeyboardInterrupt:
logger.info("\n[!] Interrupted by user - shutting down...")
peacetime_stop_event.set()
wartime_stop_event.set()
if t_detect is not None and t_detect.is_alive():
logger.info("[+] Waiting for detection thread to finish...")
t_detect.join(timeout=10)
logger.info("[+] Stopping docker containers...")
compose_down()
except Exception as e:
logger.error(f"[!] Error: {e}", exc_info=True)
peacetime_stop_event.set()
wartime_stop_event.set()
if t_detect is not None and t_detect.is_alive():
t_detect.join(timeout=5)
compose_down()
finally:
try:
compose_down()
except:
pass
if __name__ == "__main__":
main()