Skip to content

Commit

Permalink
integrate with the server
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyYe committed Feb 29, 2024
1 parent a09957d commit 9c3e6fb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use sequence::PortSequenceDetector;
use server::Server;

mod config;
mod sequence;
mod server;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let server = Server::new("enp3s0".to_string());
// Load the configuration
let config = config::load_config("config.yaml")?;
// Create the sequence detector
let detector = PortSequenceDetector::new(config);

let mut server = Server::new("enp3s0".to_string(), Box::new(detector));
server.start();

Ok(())
Expand Down
2 changes: 2 additions & 0 deletions src/sequence/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub use port_sequence::PortSequenceDetector;

mod port_sequence;

pub trait SequenceDetector {
Expand Down
13 changes: 11 additions & 2 deletions src/sequence/port_sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,17 @@ impl SequenceDetector for PortSequenceDetector {
return;
}

let client_sequence = self.client_sequences.entry(client_ip).or_insert(Vec::new());
println!(
"SYN packet detected from: {} to target port: {}",
client_ip, sequence
);

let client_sequence = self
.client_sequences
.entry(client_ip.clone())
.or_insert(Vec::new());
client_sequence.push(sequence);
self.match_sequence(&client_ip);
}

fn match_sequence(&mut self, client_ip: &str) -> bool {
Expand All @@ -51,7 +60,7 @@ impl SequenceDetector for PortSequenceDetector {
if let Some(sequence) = client_sequence {
for rule in &self.sequence_rules {
if sequence.ends_with(rule) {
println!("Matched sequence: {:?}", rule);
println!("Matched knock sequence: {:?} from: {}", rule, client_ip);
// clear the sequence
sequence.clear();
return true;
Expand Down
19 changes: 11 additions & 8 deletions src/server/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@ use pnet::packet::ip::IpNextHeaderProtocols;
use pnet::packet::tcp::TcpPacket;
use pnet::packet::Packet;

use crate::sequence::SequenceDetector;

pub struct Server {
interface_name: String,
detector: Box<dyn SequenceDetector>,
}

impl Server {
pub fn new(interface: String) -> Server {
Server {
pub fn new(interface: String, detector: Box<dyn SequenceDetector>) -> Box<Server> {
Box::new(Server {
interface_name: interface,
}
detector,
})
}

pub fn start(&self) {
pub fn start(&mut self) {
let interface = datalink::interfaces()
.into_iter()
.find(|iface: &NetworkInterface| iface.name == self.interface_name)
Expand All @@ -29,7 +33,7 @@ impl Server {
Ok(Ethernet(tx, rx)) => (tx, rx),
Ok(_) => panic!("Unhandled channel type"),
Err(e) => panic!(
"An error occurred when creating the datalink channel: {}",
"An error occurred when creating the data link channel: {}",
e
),
};
Expand All @@ -50,10 +54,9 @@ impl Server {
&& tcp.get_flags() & pnet::packet::tcp::TcpFlags::ACK
== 0
{
println!(
"SYN packet detected from: {} to target port: {:?}",
self.detector.add_sequence(
header.get_source().to_string(),
tcp.get_destination()
tcp.get_destination() as i32,
);
}
}
Expand Down

0 comments on commit 9c3e6fb

Please sign in to comment.