Skip to content

Commit

Permalink
implement the sequence detector
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyYe committed Feb 29, 2024
1 parent 51eb3b4 commit 5e238f7
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
3 changes: 1 addition & 2 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
interface: "enp3s0"
timeout: 5
rules:
- name: "Enable SSH"
timeout: 5
command: "/usr/sbin/iptables -A INPUT -s %IP% -p tcp --dport 22 -j ACCEPT"
sequence:
- 15523
Expand All @@ -10,7 +10,6 @@ rules:
- 28977
- 51234
- name: "Disable SSH"
timeout: 5
command: "/usr/sbin/iptables -D INPUT -s %IP% -p tcp --dport 22 -j ACCEPT"
sequence:
- 51234
Expand Down
2 changes: 1 addition & 1 deletion src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use serde::{Deserialize, Serialize};
pub struct Rule {
pub name: String,
pub sequence: Vec<i32>,
pub timeout: i32,
pub command: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
pub interface: String,
pub timeout: i32,
pub rules: Vec<Rule>,
}
2 changes: 1 addition & 1 deletion src/sequence/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ mod port_sequence;

pub trait SequenceDetector {
fn add_sequence(&mut self, client_ip: String, sequence: i32);
fn match_sequence(&self, client_ip: &str) -> bool;
fn match_sequence(&mut self, client_ip: &str) -> bool;
}
12 changes: 8 additions & 4 deletions src/sequence/port_sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::sequence::SequenceDetector;

#[derive(Debug)]
pub struct PortSequenceDetector {
timeout: i32,
sequence_set: HashSet<i32>,
sequence_rules: Vec<Vec<i32>>,
client_sequences: HashMap<String, Vec<i32>>,
Expand All @@ -25,6 +26,7 @@ impl PortSequenceDetector {
}

PortSequenceDetector {
timeout: config.timeout,
sequence_set,
sequence_rules,
client_sequences: HashMap::new(),
Expand All @@ -43,13 +45,15 @@ impl SequenceDetector for PortSequenceDetector {
client_sequence.push(sequence);
}

fn match_sequence(&self, client_ip: &str) -> bool {
fn match_sequence(&mut self, client_ip: &str) -> bool {
// Check if the current sequence matches any of the rules
let client_sequence = self.client_sequences.get(client_ip);
let client_sequence = self.client_sequences.get_mut(client_ip);
if let Some(sequence) = client_sequence {
for rule in &self.sequence_rules {
if sequence.ends_with(rule) {
println!("Matched sequence: {:?}", rule);
// clear the sequence
sequence.clear();
return true;
}
}
Expand All @@ -66,17 +70,16 @@ mod tests {
fn create_config() -> Config {
Config {
interface: "enp3s0".to_string(),
timeout: 5,
rules: vec![
crate::config::Rule {
name: "enable ssh".to_string(),
sequence: vec![1, 2, 3],
timeout: 5,
command: "ls -lh".to_string(),
},
crate::config::Rule {
name: "disable ssh".to_string(),
sequence: vec![3, 5, 6],
timeout: 5,
command: "du -sh *".to_string(),
},
],
Expand Down Expand Up @@ -116,5 +119,6 @@ mod tests {
detector.add_sequence("127.0.0.1".to_owned(), 5);
detector.add_sequence("127.0.0.1".to_owned(), 6);
assert_eq!(detector.match_sequence("127.0.0.1"), true);
assert_eq!(detector.client_sequences.get("127.0.0.1").unwrap().len(), 0);
}
}

0 comments on commit 5e238f7

Please sign in to comment.