-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsetup_network_monitoring.py
More file actions
1794 lines (1551 loc) · 49.4 KB
/
Copy pathsetup_network_monitoring.py
File metadata and controls
1794 lines (1551 loc) · 49.4 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
CTF Layer 1 Monitoring Setup Script
Sets up network monitoring with Suricata, Zeek, and ELK stack for CTF infrastructure
"""
import os
import re
import sys
import argparse
from pathlib import Path
from dotenv import load_dotenv
sys.stdout.reconfigure(line_buffering=True)
# Load environment variables
load_dotenv()
IPTABLES_FILE = os.getenv("IPTABLES_FILE","/etc/iptables-backend/iptables.sh")
from monitoring.utils.script_helper import (
log_info, log_debug, log_error, log_warning, log_success, log_section,
run_cmd, run_cmd_with_realtime_output, Timer, time_function, DEBUG_MODE
)
# ==== CONFIGURATION CONSTANTS ====
CHALLENGE_NETWORKS_MASK = "/9"
NETWORK_SIZE = "/24"
PF_RING_VERSION = "9.0.0"
CHALLENGES_ROOT_SUBNET = os.getenv("CHALLENGES_ROOT_SUBNET", "10.128.0.0")
SSH_USER = os.getenv("MONITORING_VM_USER", "ubuntu")
NEW_SSH_PASSWORD = os.getenv("MONITORING_VM_PASSWORD", "meow1234")
MONITORING_IP = os.getenv("MONITORING_HOST", "10.0.0.103")
MONITORING_DNS = os.getenv("MONITORING_DNS", "clickhouse.local")
PROXMOX_IP = os.getenv("PROXMOX_HOST", "10.0.0.1")
WEBSERVER_IP = os.getenv("WEBSERVER_HOST", "10.0.0.101")
DATABASE_IP = os.getenv("DATABASE_HOST", "10.0.0.102")
BACKEND_NETWORK = os.getenv("BACKEND_NETWORK_SUBNET", "10.0.0.0/24")
SURICATA_DIR = Path(os.getenv("SURICATA_FILES_DIR", "/etc/suricata"))
SURICATA_RULES_DIR = Path(os.getenv("SURICATA_RULES_DIR", "/var/lib/suricata/rules"))
CHALLENGE_NETWORKS = CHALLENGES_ROOT_SUBNET + CHALLENGE_NETWORKS_MASK
MONITORING_VPN_INTERFACE = os.getenv("MONITORING_VPN_INTERFACE", "ctf_monitoring")
MONITORING_BACKEND_INTERFACE = os.getenv("BACKEND_NETWORK_DEVICE", "backend")
MONITORING_DMZ_INTERFACE = os.getenv("MONITORING_DMZ_INTERFACE", "dmz_monitoring")
CERT_DIR = os.getenv("SSL_TLS_CERTS_DIR", "/root/heiST/setup/certs")
FULLCHAIN = os.path.join(CERT_DIR, "fullchain.pem")
PRIVKEY = os.path.join(CERT_DIR, "privkey.pem")
VECTOR_SETUP_DIR = os.getenv("VECTOR_FILES_DIR", "/root/heiST/monitoring/vector")
VECTOR_DIR = os.getenv("VECTOR_DIR", "/etc/vector/")
ZEEK_SITE_DIR = Path(os.getenv("ZEEK_SITE_DIR", "/usr/local/zeek/share/zeek/site"))
SURICATA_LOG_DIR = Path(os.getenv("SURICATA_LOG_DIR", "/var/log/suricata"))
PROXMOX_INTERNAL_IP = os.getenv("PROXMOX_INTERNAL_IP", "10.0.3.4")
def parse_arguments():
"""Handle command-line arguments"""
parser = argparse.ArgumentParser(description="CTF Monitoring Setup Script")
parser.add_argument('--pf_ring', action='store_true',
help='Install PF_RING for high-performance monitoring')
parser.add_argument('-d', '--debug', action='store_true',
help='Enable debug output')
return parser.parse_args()
@time_function
def install_local_packages():
"""Install required system packages for monitoring"""
log_section("Installing Local Monitoring Packages")
debian_version = None
with open("/etc/os-release") as f:
for line in f:
if line.startswith("DEBIAN_VERSION_FULL="):
debian_version = line.strip().split("=")[1].strip('"').split(".")[0]
if not debian_version:
debian_version = "12"
# Add Zeek repository
zeek_install_commands = [
["apt-get", "install", "-y", "apt-transport-https", "ca-certificates", "curl", "gnupg"],
["bash", "-c",
f"curl -fsSL https://download.opensuse.org/repositories/security:zeek/Debian_{debian_version}/Release.key | "
"gpg --dearmor -o /etc/apt/trusted.gpg.d/zeek.gpg"],
["bash", "-c",
f'echo "deb [arch=amd64] https://download.opensuse.org/repositories/security:/zeek/Debian_{debian_version}/ /" | tee /etc/apt/sources.list.d/security.zeek.list'],
["apt-get", "update"]
]
for cmd in zeek_install_commands:
try:
if cmd[0] == "apt-get" and cmd[1] == "install":
exit_code = run_cmd_with_realtime_output(cmd, check=True)
elif cmd[0] == "apt-get" and cmd[1] == "update":
exit_code = run_cmd_with_realtime_output(cmd, check=True)
else:
result = run_cmd(cmd, check=True)
except Exception as e:
log_warning(f"Zeek repo setup warning: {e}")
if "zeek" in ' '.join(cmd):
log_warning("Consider manual installation if this persists: https://zeek.org/get-zeek/")
# Install base packages
packages = [
"zeek",
"zeekctl",
"curl",
"jq",
"tcpdump",
"python3-requests",
"python3-yaml"
]
for package in packages:
try:
log_info(f"Installing {package}...")
exit_code = run_cmd_with_realtime_output(["apt", "install", "-y", package], check=True)
except Exception as e:
log_warning(f"Package installation warning for {package}: {e}")
if package == "zeek":
log_warning("Trying alternative Zeek installation method...")
try:
exit_code = run_cmd_with_realtime_output(["apt", "install", "-y", "zeek-lts"], check=True)
except Exception:
log_error("Zeek installation failed. You may need to build from source.")
# Install Suricata
try:
install_suricata()
except Exception as e:
log_error(f"Error installing Suricata: {e}")
log_success("Local packages installed")
@time_function
def install_suricata():
"""Install Suricata from source with dependencies"""
log_section("Installing Suricata from Source")
packages = [
"autoconf",
"automake",
"build-essential",
"cargo",
"cbindgen",
"libjansson-dev",
"libpcap-dev",
"libpcre2-dev",
"libtool",
"libyaml-dev",
"make",
"pkg-config",
"zlib1g-dev",
"libnuma-dev",
"libdpdk-dev",
"libcap-ng-dev",
"libmagic-dev",
"liblz4-dev",
"libunwind-dev",
"libmaxminddb-dev",
"libnetfilter-queue-dev",
"libnfnetlink-dev"
]
for package in packages:
log_info(f"Installing dependency: {package}")
exit_code = run_cmd_with_realtime_output(["apt", "install", "-y", package], check=True)
log_info("Installing Rust toolchain...")
exit_code = run_cmd("curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs -o /tmp/rustup-init", check=True, shell=True)
run_cmd("chmod +x /tmp/rustup-init", check=True, shell=True)
env = os.environ.copy()
env["CARGO_HOME"] = "/root/.cargo"
env["RUSTUP_HOME"] = "/root/.rustup"
exit_code = run_cmd_with_realtime_output(["/tmp/rustup-init", "-y", "--no-modify-path"], check=True, env=env)
cargo_bin = "/root/.cargo/bin"
os.environ["PATH"] = f"{cargo_bin}:{os.environ['PATH']}"
result = run_cmd([f"{cargo_bin}/rustup", "--version"], check=True)
log_debug(f"rustup version: {result.stdout}")
exit_code = run_cmd_with_realtime_output([f"{cargo_bin}/rustup", "update"], check=True)
# Build and install Suricata
log_info("Downloading Suricata source...")
run_cmd("wget https://www.openinfosecfoundation.org/download/suricata-8.0.0.tar.gz", check=True, shell=True)
run_cmd("tar -xzvf suricata-8.0.0.tar.gz", check=True, shell=True)
log_info("Configuring and compiling Suricata (this may take a while)...")
exit_code = run_cmd_with_realtime_output([
"sh", "-c",
"cd suricata-8.0.0 && ./configure --prefix=/usr/ --sysconfdir=/etc --localstatedir=/var --enable-geoip --enable-dpdk --enable-nfqueue && make"
], check=True)
log_info("Installing Suricata...")
exit_code = run_cmd_with_realtime_output(["sh", "-c", "cd suricata-8.0.0 && make install-full"], check=True)
log_success("Suricata installed from source")
@time_function
def install_pf_ring(args):
"""Install and configure PF_RING for high-performance packet capture"""
try:
log_section("Installing PF_RING")
log_info("Installing PF_RING dependencies")
run_cmd(
'echo "deb http://download.proxmox.com/debian/pve bookworm pve-no-subscription" > /etc/apt/sources.list.d/pve-no-subscription.list',
check=True, shell=True
)
exit_code = run_cmd_with_realtime_output([
"sh", "-c",
"apt-get update && apt-get install -y build-essential pve-headers-$(uname -r) bison flex libnuma-dev wget"
], check=True)
log_info("Downloading PF_RING")
run_cmd(
f"wget https://github.com/ntop/PF_RING/archive/refs/tags/{PF_RING_VERSION}.tar.gz -O /tmp/pfring.tar.gz",
check=True, shell=True
)
run_cmd(f"tar xvzf /tmp/pfring.tar.gz -C /tmp", check=True, shell=True)
log_info("Compiling and installing PF_RING kernel module")
os.chdir(f"/tmp/PF_RING-{PF_RING_VERSION}/kernel")
exit_code = run_cmd_with_realtime_output(["make"], check=True)
exit_code = run_cmd_with_realtime_output(["make", "install"], check=True)
log_info("Compiling and installing PF_RING userland tools")
os.chdir(f"/tmp/PF_RING-{PF_RING_VERSION}/userland")
exit_code = run_cmd_with_realtime_output(["make"], check=True)
exit_code = run_cmd_with_realtime_output(["make", "install"], check=True)
log_info("Loading kernel module")
run_cmd("modprobe pf_ring transparent_mode=2 min_num_slots=32768 enable_tx_capture=1", check=True, shell=True)
# Persistent module loading
with open("/etc/modules-load.d/pfring.conf", "w") as f:
f.write("pf_ring\n")
with open("/etc/modprobe.d/pfring.conf", "w") as f:
f.write("options pf_ring transparent_mode=2 min_num_slots=32768 enable_tx_capture=1\n")
log_info("Verifying installation")
run_cmd(["ldconfig"], check=True)
result = run_cmd("lsmod | grep pf_ring", check=True, shell=True)
if "pf_ring" not in result.stdout:
raise Exception("PF_RING kernel module not loaded")
configure_pf_ring_interface()
create_network_monitoring_device(MONITORING_VPN_INTERFACE)
create_network_monitoring_device(MONITORING_DMZ_INTERFACE)
optimize_system()
log_success("PF_RING successfully installed and configured")
except Exception as e:
log_error(f"PF_RING installation failed: {e}")
raise
@time_function
def configure_pf_ring_interface():
"""Configure network interfaces for PF_RING"""
try:
log_section("Configuring Network Interfaces for PF_RING")
# Use monitoring interface directly
real_iface = MONITORING_VPN_INTERFACE
log_debug(f"Using interface: {real_iface}")
commands = [
# Disable NIC offloading
f"ethtool -K {real_iface} rx off tx off sg off tso off gso off gro off lro off",
# Increase ring buffers
f"ethtool -G {real_iface} rx 4096 tx 4096",
# Set MTU
f"ip link set {real_iface} mtu 9000",
# Enable promiscuous mode
f"ip link set {real_iface} promisc on",
# Kernel tuning
"sysctl -w net.core.rmem_max=16777216",
"sysctl -w net.core.wmem_max=16777216",
"sysctl -w net.core.netdev_max_backlog=10000",
"sysctl -w net.core.dev_weight=600",
]
for cmd in commands:
run_cmd(cmd, check=True, shell=True)
log_success("Network interfaces configured for PF_RING")
except Exception as e:
log_error(f"Interface configuration failed: {e}")
raise
@time_function
def create_network_monitoring_device(iface):
"""Create network monitoring device and make it persistent"""
log_info(f"Creating network monitoring device: {iface}")
# 1. Create and activate it immediately
commands = [
f"modprobe ifb",
f"ip link add {iface} type ifb",
f"ip link set dev {iface} up"
]
for cmd in commands:
run_cmd(cmd, check=False, shell=True)
config_block = f"""
# Virtual IFB device for monitoring
auto {iface}
iface {iface} inet manual
pre-up modprobe ifb
pre-up ip link add {iface} type ifb || true
up ip link set {iface} up
down ip link set {iface} down
post-down ip link del {iface} || true
"""
try:
with open("/etc/network/interfaces", "a") as f:
f.write(config_block)
log_success(f"Added persistent configuration for {iface} to /etc/network/interfaces")
except Exception:
log_error(f"Failed to add persistent configuration for {iface}")
@time_function
def set_backend_nfqueue_rules():
"""Set NFQueue rules for backend traffic inspection"""
log_section("Setting up NFQueue Rules")
commands = [
"iptables -I FORWARD -i vmbr0 -o backend -j NFQUEUE --queue-num 0 --queue-bypass",
"iptables -I FORWARD -i backend -o vmbr0 -j NFQUEUE --queue-num 0 --queue-bypass"
]
for cmd in commands:
try:
run_cmd(cmd, check=True, shell=True)
try:
with open(IPTABLES_FILE, "a") as f:
f.write(cmd + "\n")
except Exception as file_err:
log_warning(f"Failed to write rule to {IPTABLES_FILE}: {file_err}")
except Exception as e:
log_warning(f"Failed to execute NFQueue rule: {cmd}")
log_warning(f"Error: {e}")
continue
log_success("NFQueue rules configured")
@time_function
def optimize_system():
"""Apply system-wide optimizations for monitoring performance"""
try:
log_section("Applying System Optimizations")
# Increase file descriptors
with open("/etc/security/limits.d/ctf-monitoring.conf", "w") as f:
f.write("* soft nofile 1000000\n* hard nofile 1000000\n")
# Kernel tuning
with open("/etc/sysctl.d/99-ctf-monitoring.conf", "w") as f:
f.write("""net.core.rmem_max=16777216
net.core.wmem_max=16777216
net.core.netdev_max_backlog=10000
net.core.somaxconn=65535
net.ipv4.tcp_max_syn_backlog=20480
vm.swappiness=10
vm.overcommit_memory=1
""")
run_cmd("sysctl -p /etc/sysctl.d/99-ctf-monitoring.conf", check=True, shell=True)
# Disable CPU frequency scaling
log_info("Installing and configuring cpufrequtils...")
exit_code = run_cmd_with_realtime_output(["apt", "install", "-y", "cpufrequtils"], check=True)
with open("/etc/default/cpufrequtils", "w") as f:
f.write('GOVERNOR="performance"\n')
run_cmd("systemctl restart cpufrequtils", check=True, shell=True)
log_success("System optimizations applied")
except Exception as e:
log_error(f"System optimization failed: {e}")
raise
@time_function
def setup_suricata_disables():
"""Configure disabled rules for main Suricata instance"""
log_section("Configuring Suricata Disabled Rules")
disable_config = """2200121
"""
with open(f"{SURICATA_DIR}/disable.conf", "w") as f:
f.write(disable_config)
@time_function
def setup_suricata_dmz_disables():
"""Configure disabled rules for DMZ Suricata instance"""
disable_config = """2200121
"""
with open(f"{SURICATA_DIR}/dmz-disable.conf", "w") as f:
f.write(disable_config)
@time_function
def setup_suricata_backend_disables():
"""Configure disabled rules for backend Suricata instance"""
disable_config = """2010939
2013504
2200122
2200003
"""
with open(f"{SURICATA_DIR}/backend-disable.conf", "w") as f:
f.write(disable_config)
@time_function
def setup_suricata_rules():
"""Download and configure Suricata rules for main monitoring"""
log_section("Setting up Suricata Rules")
# Update Suricata rules
log_info("Downloading and updating Suricata rules...")
exit_code = run_cmd_with_realtime_output(
f"suricata-update --disable-conf {SURICATA_DIR}/disable.conf".split(),
check=True
)
run_cmd(f"sed -i 's/^#//' {SURICATA_RULES_DIR}/suricata.rules", check=True, shell=True)
# Create Lua script for subnet checking
lua_script = SURICATA_RULES_DIR / "check_diff_subnet.lua"
lua_script.write_text("""-- Suricata Lua script: alert if source and destination are in different /24 networks within 10.128.0.0/9
function init (args)
local needs = {}
needs["packet"] = tostring(true)
return needs
end
function match(args)
local src_ip = args["ip_src"]
local dst_ip = args["ip_dst"]
if not src_ip or not dst_ip then
return 0
end
-- Convert IP to number and mask to /24
local src_num = ip_to_number(src_ip)
local dst_num = ip_to_number(dst_ip)
if (src_num & 0xFF800000) ~= 0x0A800000 or -- Check 10.128.0.0/9
(dst_num & 0xFF800000) ~= 0x0A800000 then
return 0
end
-- Compare /24 prefixes
return (src_num & 0xFFFFFF00) ~= (dst_num & 0xFFFFFF00) and 1 or 0
end
function ip_to_number(ip_str)
local o1, o2, o3, o4 = ip_str:match("(%d+)%.(%d+)%.(%d+)%.(%d+)")
return tonumber(o1) * 0x1000000 + tonumber(o2) * 0x10000 + tonumber(o3) * 0x100 + tonumber(o4)
end
""")
# Create custom CTF rules
rules_file = SURICATA_RULES_DIR / "ctf-custom.rules"
rules_file.parent.mkdir(parents=True, exist_ok=True)
# Rule templates
tcp_template = "alert tcp {src} any -> {dst} any (msg:\"{msg}\"; flags:S; threshold:type both,track by_src,count {count},seconds {seconds}; sid:{sid}; priority:{priority}; metadata:policy {metadata};)"
generic_tcp_template = "alert tcp {src} any -> {dst} any (msg:\"{msg}\"; flags:S; sid:{sid}; priority:{priority}; metadata:policy {metadata};)"
infrastructure_ips = [PROXMOX_IP, MONITORING_IP, WEBSERVER_IP, DATABASE_IP]
with rules_file.open("w") as f:
# Infrastructure access rules
f.write(tcp_template.format(
src="any",
dst=",".join(infrastructure_ips),
msg="CTF: Infrastructure Access",
count=5,
seconds=300,
sid=9001000,
priority=1,
metadata="ctf-infrastructure"
) + "\n")
# HTTP to backend rules
f.write(generic_tcp_template.format(
src="any",
dst=BACKEND_NETWORK,
msg="CTF: HTTP to Infrastructure",
sid=9001007,
priority=2,
metadata="ctf-infrastructure"
) + "\n")
# Cross-Challenge Network Access
f.write(
f"alert tcp {CHALLENGE_NETWORKS} any -> {CHALLENGE_NETWORKS} any (msg:\"CTF: Cross-Challenge /24 Access\"; sid:9002001; lua:check_diff_subnet.lua;)\n")
# Port scanning detection
f.write(
"alert tcp any any -> any any (msg:\"CTF: Rapid Port Hopping\"; flags:S; threshold:type both,track by_src,count 20,seconds 30; sid:9001020; priority:2;)\n")
f.write(
"alert tcp any any -> any any (msg:\"CTF: Sequential Port Scan\"; flags:S; threshold:type both, track by_src, count 10, seconds 10; sid:9001021; priority:3;)\n")
# DNS tunneling detection
f.write(
"alert dns any any -> any 53 (msg:\"CTF: DNS Query Volume Spike\"; threshold:type both,track by_src,count 200,seconds 60; sid:9001004; rev:2; priority:2;)\n")
f.write(
"alert dns any any -> any 53 (msg:\"CTF: Possible DNS tunneling (long label / b32/b64)\"; dns.query; pcre:\"/(?:[A-Za-z2-7]{40,}|[A-Za-z0-9+/]{40,}=*)\\./R\"; threshold:type both,track by_src,count 10,seconds 60; sid:9001010; rev:2; priority:2;)\n")
f.write(
"alert dns any any -> any 53 (msg:\"CTF: DNS TXT record abuse (heuristic)\"; dns.opcode:0; dns.query; content:\"=\"; nocase; content:\".\"; endswith; pcre:\"/^([A-Za-z0-9_\\-]{1,63}\\.){1,10}$/\"; threshold:type both,track by_src,count 15,seconds 60; sid:9001011; rev:3; priority:2;)\n")
log_success("Suricata rules configured")
@time_function
def setup_suricata_backend_rules():
"""Download and configure Suricata rules for backend monitoring"""
log_section("Setting up Backend Suricata Rules")
log_info("Downloading backend Suricata rules...")
exit_code = run_cmd_with_realtime_output(
f"suricata-update --disable-conf {SURICATA_DIR}/backend-disable.conf --output {SURICATA_RULES_DIR}/backend".split(),
check=True
)
run_cmd(f"sed -i 's/^#//' {SURICATA_RULES_DIR}/backend/suricata.rules", check=True, shell=True)
# Add custom backend rule
backend_custom_path = SURICATA_RULES_DIR / "backend" / "backend-custom.rules"
custom_rule = 'alert ip $SQL_SERVERS any -> $EXTERNAL_NET any (msg:"SQL server talking to EXTERNAL_NET"; sid:1000001; rev:1; classtype:bad-unknown;)'
backend_custom_path.write_text(custom_rule + "\n")
log_debug(f"Custom backend rule written to {backend_custom_path}")
log_success("Backend Suricata rules configured")
@time_function
def setup_suricata_dmz_rules():
"""Download and configure Suricata rules for DMZ monitoring"""
log_section("Setting up DMZ Suricata Rules")
log_info("Downloading DMZ Suricata rules...")
exit_code = run_cmd_with_realtime_output(
f"suricata-update --disable-conf {SURICATA_DIR}/dmz-disable.conf --output {SURICATA_RULES_DIR}/dmz".split(),
check=True
)
run_cmd(f"sed -i 's/^#//' {SURICATA_RULES_DIR}/dmz/suricata.rules", check=True, shell=True)
log_success("DMZ Suricata rules configured")
@time_function
def setup_suricata_config(args):
"""Configure main Suricata instance for VPN monitoring"""
log_section("Configuring Main Suricata Instance")
run_cmd(f"sudo mkdir -p {SURICATA_LOG_DIR}/pcap", check=True, shell=True)
if args.pf_ring:
interface_config = f"""
# PF_RING configuration
pfring:
- interface: {MONITORING_VPN_INTERFACE}
threads: auto
cluster-id: 99
cluster-type: cluster_flow
bypass: yes
use-mmap: yes
ring-size: 262144
block-size: 1048576
copy-mode: 1
copy-iface: eth0
"""
else:
interface_config = f"""
# Standard af-packet configuration
af-packet:
- interface: {MONITORING_VPN_INTERFACE}
cluster-id: 99
cluster-type: cluster_flow
threads: auto
tpacket-v3: yes
ring-size: 262144
block-size: 1048576
defrag: yes
use-mmap: yes
mmap-locked: yes
"""
suricata_config = f"""%YAML 1.1
---
# CTF Suricata Configuration - VPN Monitoring
vars:
address-groups:
HOME_NET: "[{CHALLENGE_NETWORKS}]"
INFRA_NET: "[{BACKEND_NETWORK}]"
EXTERNAL_NET: any
HTTP_SERVERS: "$HOME_NET"
SQL_SERVERS: "$HOME_NET"
TELNET_SERVERS: "$HOME_NET"
SMTP_SERVERS: "$HOME_NET"
DNS_SERVERS: "$HOME_NET"
port-groups:
HTTP_PORTS: "[80,8080]"
SSH_PORTS: "[22]"
ORACLE_PORTS: any
SHELLCODE_PORTS: any
default-rule-path: {SURICATA_RULES_DIR}
rule-files:
- suricata.rules
- ctf-custom.rules
security:
lua:
scripts-dir: {SURICATA_RULES_DIR}
allow-rules: true
max-bytes: 500000
max-instr: 1000000
max-stack: 1000
{interface_config}
# Logging configuration
outputs:
- fast:
enabled: yes
filename: {SURICATA_LOG_DIR}/fast_vpn.log
- pcap-log:
enabled: yes
filename: pcap/suricata_vpn.pcap
limit: 5000000000
- eve-log:
enabled: yes
filetype: regular
filename: {SURICATA_LOG_DIR}/eve_vpn.json
community-id: true
community-id-seed: 0
types:
- alert:
payload: yes
payload-buffer-size: 4kb
payload-printable: yes
packet: yes
metadata: yes
http-body: yes
http-body-printable: yes
- anomaly:
enabled: yes
- dns:
query: yes
answer: yes
- http:
extended: yes
- flow
- stats:
enabled: yes
totals: yes
threads: yes
append: yes
interval: 60
- netflow:
enabled: yes
- ssh:
enabled: yes
extended: yes
- tls:
extended: yes
fingerprints: yes
- files:
enabled: yes
- smtp:
enabled: yes
extended: yes
- ftp
- rdp
- nfs
- smb
- tftp
- ike
- dcerpc
- krb5
- snmp
- rfb
- sip
- dhcp:
enabled: yes
- mqtt:
- http2
- http-log:
enabled: yes
filename: {SURICATA_LOG_DIR}/http_vpn.log
append: yes
extended: yes
- tcp-data:
enabled: no
type: file
filename: {SURICATA_LOG_DIR}/tcp_vpn.log
- http-body-data:
enabled: yes
type: file
filename: {SURICATA_LOG_DIR}/http-data_vpn.log
- stats:
enabled: yes
filename: {SURICATA_LOG_DIR}/stats_vpn.log
append: yes
totals: yes
threads: no
# Performance tuning
threading:
set-cpu-affinity: yes
cpu-affinity:
- management-cpu-set:
cpu: [ "0" ]
- receive-cpu-set:
cpu: [ "1" ]
- worker-cpu-set:
cpu: [ "all" ]
detect-thread-ratio: 1.0
# Host and flow settings
host:
hash-size: 4096
prealloc: 1000
memcap: 128mb
flow:
memcap: 128mb
hash-size: 65536
prealloc: 10000
emergency-recovery: 30
# Stream processing
stream:
memcap: 64mb
checksum-validation: yes
reassembly:
memcap: 256mb
depth: 1mb
toserver-chunk-size: 2560
toclient-chunk-size: 2560
# Application layer
app-layer:
protocols:
https:
enabled: yes
libhtp:
default-body-limit: 10mb # body capture limit
request-body-limit: 10mb
response-body-limit: 10mb
ftp:
enabled: yes
ssh:
enabled: yes
smb:
enabled: yes
dcerpc:
enabled: yes
smtp:
enabled: yes
dhcp:
enabled: yes
ntp:
enabled: yes
tftp:
enabled: yes
enip:
enabled: yes
nfs:
enabled: yes
ikev2:
enabled: yes
krb5:
enabled: yes
snmp:
enabled: yes
sip:
enabled: yes
rfb:
enabled: yes
mqtt:
enabled: yes
dns:
tcp:
enabled: yes
detection-ports:
dp: 53
udp:
enabled: yes
detection-ports:
dp: 53
http:
enabled: yes
libhtp:
default-config:
personality: IDS
request-body-limit: 100kb
response-body-limit: 100kb
tls:
enabled: yes
detection-ports:
dp: 443
dnp3:
enabled: no
modbus:
enabled: yes
rdp:
enabled: yes
http2:
enabled: yes
imap:
enabled: yes
# Logging level
logging:
default-log-level: notice
outputs:
- console:
enabled: yes
- file:
enabled: yes
level: info
filename: {SURICATA_LOG_DIR}/suricata_vpn.log
"""
config_file = SURICATA_DIR / "suricata-ctf.yaml"
config_file.write_text(suricata_config)
# Test configuration
log_info("Testing Suricata configuration")
try:
result = run_cmd(["suricata", "-T", "-c", str(config_file)], check=True)
log_success("Suricata configuration test successful")
if result.stdout and DEBUG_MODE:
log_debug(result.stdout)
except Exception as e:
log_error("Suricata configuration test failed")
raise Exception("Suricata configuration test failed")
@time_function
def setup_suricata_backend_config():
"""Configure Suricata for backend monitoring using NFQUEUE"""
log_section("Configuring Backend Suricata Instance")
suricata_config = f"""%YAML 1.1
---
# CTF Suricata Configuration - Backend Monitoring (NFQUEUE Inline)
vars:
address-groups:
HOME_NET: "[{BACKEND_NETWORK}]"
EXTERNAL_NET: "!$HOME_NET"
HTTP_SERVERS: "[{WEBSERVER_IP},{MONITORING_IP}]"
SQL_SERVERS: "{DATABASE_IP}"
TELNET_SERVERS: "$HOME_NET"
SMTP_SERVERS: "$HOME_NET"
DNS_SERVERS: "$HOME_NET"
port-groups:
HTTP_PORTS: "[80,8080,3000,9090,8123,9100]"
SSH_PORTS: "[22]"
ORACLE_PORTS: any
SHELLCODE_PORTS: any
default-rule-path: {SURICATA_RULES_DIR}/backend
rule-files:
- suricata.rules
- backend-custom.rules
security:
lua:
scripts-dir: {SURICATA_RULES_DIR}
allow-rules: true
max-bytes: 500000
max-instr: 1000000
max-stack: 1000
# NFQUEUE configuration for inline monitoring
nfqueue:
- id: 0
fail-open: no
bypass: no
copy-mode: ips
# Logging configuration
outputs:
- fast:
enabled: yes
filename: {SURICATA_LOG_DIR}/fast_backend.log
- pcap-log:
enabled: yes
filename: pcap/suricata_backend.pcap
limit: 5000000000
- eve-log:
enabled: yes
filetype: regular
filename: {SURICATA_LOG_DIR}/eve_backend.json
community-id: true
community-id-seed: 0
types:
- alert:
payload: yes
payload-buffer-size: 4kb
payload-printable: yes
packet: yes
metadata: yes
http-body: yes
http-body-printable: yes
- anomaly:
enabled: yes
- dns:
query: yes
answer: yes
- http:
extended: yes
- flow
- stats:
enabled: yes
totals: yes
threads: yes
append: yes
interval: 60
- netflow:
enabled: yes
- ssh:
enabled: yes
extended: yes
- tls:
extended: yes
fingerprints: yes
- files:
enabled: yes
- smtp:
enabled: yes
extended: yes
- ftp
- rdp
- nfs
- smb
- tftp
- ike
- dcerpc
- krb5
- snmp
- rfb
- sip
- dhcp:
enabled: yes
- mqtt:
- http2
- http-log:
enabled: yes
filename: {SURICATA_LOG_DIR}/http_backend.log
append: yes
extended: yes
- tcp-data:
enabled: no
type: file
filename: {SURICATA_LOG_DIR}/tcp_backend.log
- http-body-data:
enabled: yes
type: file
filename: {SURICATA_LOG_DIR}/http-data_backend.log
- stats:
enabled: yes
filename: {SURICATA_LOG_DIR}/stats_backend.log
append: yes
totals: yes