-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.rs
419 lines (349 loc) · 12.6 KB
/
main.rs
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
use pnet::datalink::Channel::Ethernet;
use serde_derive::{Deserialize, Serialize};
use pnet::datalink::{self};
use pnet::packet::tcp::TcpPacket;
use pnet::packet::Packet;
use dialoguer::{theme::ColorfulTheme, Select, Input};
use std::sync::{Arc, Mutex};
use lazy_static::lazy_static;
use std::fs::{File, OpenOptions};
use std::io::{Read, Write};
use std::process::{Command, Stdio};
use pnet::packet::ipv4::Ipv4Packet;
use std::sync::atomic::{AtomicBool, Ordering};
use std::{fs, io, thread};
use std::path::Path;
use uuid::Uuid;
#[derive(Serialize, Deserialize, Debug, Clone)]
struct Rule {
id: String,
protocol: String,
source_ip: Option<String>,
destination_ip: Option<String>,
source_port: Option<u16>,
destination_port: Option<u16>,
action: String, // "allow" or "block"
}
lazy_static! {
static ref RULES: Arc<Mutex<Vec<Rule>>> = Arc::new(Mutex::new(Vec::new()));
}
lazy_static! {
static ref FIREWALL_RUNNING: AtomicBool = AtomicBool::new(false);
}
const RULES_FILE: &str = "firewall_rules.json";
fn save_rules(rules: &Vec<Rule>) -> io::Result<()> {
let json = serde_json::to_string(rules)?;
fs::write(RULES_FILE, json)?;
Ok(())
}
fn load_rules() -> io::Result<Vec<Rule>> {
let path = Path::new(RULES_FILE);
if path.exists() {
let mut file = File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let rules = serde_json::from_str(&contents)?;
Ok(rules)
} else {
Ok(Vec::new()) // Return an empty vector if the file does not exist
}
}
fn main() {
let loaded_rules = load_rules().unwrap_or_else(|e| {
eprintln!("Failed to load rules: {}", e);
Vec::new()
});
*RULES.lock().unwrap() = loaded_rules;
loop {
display_menu();
}
}
fn start_firewall() {
let interfaces = datalink::interfaces();
let interface_names: Vec<String> = interfaces.iter()
.map(|iface| iface.name.clone())
.collect();
if interface_names.is_empty() {
println!("No available network interfaces found.");
return;
}
// Clean logs when starting the firewall
clean_logs();
let selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Select a network interface to monitor")
.default(0)
.items(&interface_names)
.interact()
.unwrap();
let selected_interface = interface_names.get(selection).unwrap().clone();
println!("Starting firewall on interface: {}", selected_interface);
FIREWALL_RUNNING.store(true, Ordering::SeqCst);
thread::spawn(move || {
process_packets(selected_interface);
});
}
fn clean_logs() {
match File::create("firewall.log") {
Ok(_) => println!("Logs have been cleaned."),
Err(e) => eprintln!("Failed to clean logs: {}", e),
}
}
fn stop_firewall() {
FIREWALL_RUNNING.store(false, Ordering::SeqCst);
println!("Firewall stopped.");
}
fn check_firewall_status() {
if FIREWALL_RUNNING.load(Ordering::SeqCst) {
println!("Firewall status: Running");
} else {
println!("Firewall status: Stopped");
}
}
fn display_menu() {
let items = vec![
"View Rules", "Add Rule", "Remove Rule", "View Logs", "Clean Logs",
"Start Firewall", "Stop Firewall", "Check Firewall Status",
"Exit"
];
let selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Choose an action")
.default(0)
.items(&items)
.interact()
.unwrap();
match items[selection] {
"View Rules" => view_rules(),
"Add Rule" => add_rule(),
"Remove Rule" => remove_rule(),
"View Logs" => view_logs(),
"Clean Logs" => clean_logs(),
"Start Firewall" => start_firewall(),
"Stop Firewall" => stop_firewall(),
"Check Firewall Status" => check_firewall_status(),
"Exit" => std::process::exit(0),
_ => (),
}
}
fn view_rules() {
let rules = RULES.lock().unwrap();
for (index, rule) in rules.iter().enumerate() {
println!("{}: {:?}", index, rule);
}
}
fn add_rule() {
let protocol: String = Input::new()
.with_prompt("Enter protocol (e.g., 'tcp', 'udp')")
.interact_text()
.unwrap();
let source_ip: String = Input::new()
.with_prompt("Enter source IP (leave empty if not applicable)")
.default("".into())
.interact_text()
.unwrap();
let destination_ip: String = Input::new()
.with_prompt("Enter destination IP (leave empty if not applicable)")
.default("".into())
.interact_text()
.unwrap();
let source_port: u16 = Input::new()
.with_prompt("Enter source port (leave empty if not applicable)")
.default(0)
.interact_text()
.unwrap();
let destination_port: u16 = Input::new()
.with_prompt("Enter destination port (leave empty if not applicable)")
.default(0)
.interact_text()
.unwrap();
let actions = vec!["Allow", "Block"];
let action = Select::new()
.with_prompt("Choose action")
.default(0)
.items(&actions)
.interact()
.unwrap();
let new_rule = Rule {
id: Uuid::new_v4().to_string(),
protocol,
source_ip: if source_ip.is_empty() { None } else { Some(source_ip) },
destination_ip: if destination_ip.is_empty() { None } else { Some(destination_ip) },
source_port: if source_port == 0 { None } else { Some(source_port) },
destination_port: if destination_port == 0 { None } else { Some(destination_port) },
action: actions[action].to_lowercase(),
};
let mut rules = RULES.lock().unwrap();
rules.push(new_rule.clone());
save_rules(&rules).expect("Failed to save rules");
// IMPORTANT: Update Linux IP Tables
update_iptables(&new_rule.clone(), &new_rule.clone().action);
println!("Rule added.");
}
fn update_iptables(rule: &Rule, action: &str) {
let protocol = &rule.protocol;
let source_ip = rule.source_ip.as_ref().map_or("".to_string(), |ip| format!("--source {}", ip));
let destination_ip = rule.destination_ip.as_ref().map_or("".to_string(), |ip| format!("--destination {}", ip));
let source_port = rule.source_port.map_or("".to_string(), |port| format!("--sport {}", port));
let destination_port = rule.destination_port.map_or("".to_string(), |port| format!("--dport {}", port));
let target = if action == "block" { "DROP" } else { "ACCEPT" };
// Construct the iptables command as a string
let iptables_command = format!("sudo iptables -A INPUT -p {} {} {} {} {} -j {} -m comment --comment {}",
protocol, source_ip, destination_ip, source_port, destination_port, target, &rule.id);
// Print the executed command for debugging purposes
println!("Executing command: {}", iptables_command);
// Execute the iptables command
let output = Command::new("sh")
.arg("-c")
.arg(&iptables_command)
.stderr(Stdio::piped())
.output()
.expect("Failed to execute iptables command");
if output.status.success() {
println!("Rule updated in iptables.");
} else {
// Print the raw error message from stderr
let stderr_output = String::from_utf8_lossy(&output.stderr);
eprintln!("Failed to update rule in iptables. Error: {}", stderr_output);
}
}
fn remove_rule() {
// Get the rule descriptions and selection
let (selected_rule_id, selection) = {
let rules = RULES.lock().unwrap();
let rule_descriptions: Vec<String> = rules.iter().map(|rule| format!("{:?}", rule)).collect();
if rule_descriptions.is_empty() {
println!("No rules to remove.");
return;
}
let selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Select a rule to remove")
.default(0)
.items(&rule_descriptions)
.interact()
.unwrap();
// Clone the ID to use outside the lock scope
let selected_rule_id = rules[selection].id.clone();
(selected_rule_id, selection)
};
// Now we can remove the iptables rule outside the lock scope
remove_iptables_rule(&selected_rule_id);
// Now remove the rule from the application
let mut rules = RULES.lock().unwrap();
rules.remove(selection);
println!("Rule removed.");
}
fn remove_iptables_rule(rule_id: &str) {
// Construct the iptables command as a string
let iptables_command = format!(
"sudo iptables -L INPUT --line-numbers | grep -E '{}' | awk '{{print $1}}' | xargs -I {{}} sudo iptables -D INPUT {{}}",
rule_id
);
// Print the executed command for debugging purposes
println!("Executing command: {}", iptables_command);
// Execute the iptables command
let output = Command::new("sh")
.arg("-c")
.arg(&iptables_command)
.output()
.expect("Failed to execute iptables command");
// Print the output of the executed command for debugging
println!("Command output: {:?}", output);
if output.status.success() {
println!("Successfully removed iptables rule for rule ID: {}", rule_id);
} else {
eprintln!("Error removing iptables rule for rule ID: {}", rule_id);
}
}
fn view_logs() {
println!("Firewall Logs:");
match fs::read_to_string("firewall.log") {
Ok(contents) => println!("{}", contents),
Err(e) => println!("Error reading log file: {}", e),
}
}
fn process_packets(interface_name: String) {
let interfaces = datalink::interfaces();
let interface = interfaces.into_iter()
.find(|iface| iface.name == interface_name)
.expect("Error finding interface");
let (_, mut rx) = match datalink::channel(&interface, Default::default()) {
Ok(Ethernet(_, rx)) => ((), rx),
Ok(_) => panic!("Unsupported channel type"),
Err(e) => panic!("Error creating datalink channel: {}", e),
};
while FIREWALL_RUNNING.load(Ordering::SeqCst) {
match rx.next() {
Ok(packet) => {
if let Some(tcp_packet) = TcpPacket::new(packet) {
process_tcp_packet(&tcp_packet);
}
},
Err(e) => eprintln!("An error occurred while reading packet: {}", e),
}
}
}
fn process_tcp_packet(tcp_packet: &TcpPacket) {
let rules = RULES.lock().unwrap();
for rule in rules.iter() {
if packet_matches_rule(tcp_packet, rule) {
println!("Rule matched");
match rule.action.as_str() {
"block" => {
log_packet_action(tcp_packet, "Blocked");
return; // Dropping the packet
},
_ => (),
}
}
}
log_packet_action(tcp_packet, "Allowed");
// Further processing or forwarding the packet
}
fn packet_matches_rule(packet: &TcpPacket, rule: &Rule) -> bool {
// First, extract the IPv4 packet from the TCP packet
if let Some(ipv4_packet) = Ipv4Packet::new(packet.packet()) {
// Check protocol (assuming TCP, as we are working with TcpPacket)
if rule.protocol.to_lowercase() != "tcp" {
return false;
}
// Check source IP
if let Some(ref rule_src_ip) = rule.source_ip {
if ipv4_packet.get_source().to_string() != *rule_src_ip {
return false;
}
}
// Check destination IP
if let Some(ref rule_dst_ip) = rule.destination_ip {
if ipv4_packet.get_destination().to_string() != *rule_dst_ip {
return false;
}
}
// Check source port
if let Some(rule_src_port) = rule.source_port {
if packet.get_source() != rule_src_port {
return false;
}
}
// Check destination port
if let Some(rule_dst_port) = rule.destination_port {
if packet.get_destination() != rule_dst_port {
return false;
}
}
// If all checks pass, the packet matches the rule
return true;
}
false
}
// Log packet action (either to console or to a file)
fn log_packet_action(packet: &TcpPacket, action: &str) {
let log_message = format!("{} packet: {:?}, action: {}\n", action, packet, action);
let mut file = OpenOptions::new()
.create(true)
.write(true)
.append(true)
.open("firewall.log")
.unwrap();
if let Err(e) = writeln!(file, "{}", log_message) {
eprintln!("Couldn't write to log file: {}", e);
}
}