-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
113 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
rules: | ||
- name: "enable_ssh" | ||
host: "example.com" | ||
sequence: | ||
- 12345 | ||
- 54321 | ||
- 32768 | ||
- 18933 | ||
- name: "disable_ssh" | ||
host: "example.com" | ||
sequence: | ||
- 18933 | ||
- 32768 | ||
- 54321 | ||
- 12345 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
extern crate serde; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub struct Rule { | ||
pub name: String, | ||
pub host: String, | ||
pub sequence: Vec<i32>, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct Config { | ||
pub rules: Vec<Rule>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use std::fs::File; | ||
use std::io::Read; | ||
|
||
pub use config::Config; | ||
pub use config::Rule; | ||
pub mod config; | ||
|
||
pub fn load_config(path: &str) -> Result<Config, Box<dyn std::error::Error>> { | ||
let mut file = File::open(path)?; | ||
let mut content = String::new(); | ||
|
||
file.read_to_string(&mut content)?; | ||
let config: Config = serde_yaml::from_str(&content)?; | ||
|
||
Ok(config) | ||
} | ||
|
||
// test case for load_config | ||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_load_config() { | ||
let config = load_config("config.yaml").unwrap(); | ||
assert_eq!(config.rules.len(), 2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use crate::config::Config; | ||
use crate::config::Rule; | ||
use std::collections::HashMap; | ||
use std::net::{SocketAddr, TcpStream, ToSocketAddrs}; | ||
use std::time::Duration; | ||
|
||
pub struct RuleExecutor { | ||
rules: HashMap<String, Rule>, | ||
} | ||
|
||
impl RuleExecutor { | ||
pub fn new(config: Config) -> RuleExecutor { | ||
let mut rules = HashMap::new(); | ||
for rule in config.rules { | ||
rules.insert(rule.name.clone(), rule); | ||
} | ||
|
||
RuleExecutor { rules } | ||
} | ||
|
||
pub fn run(&self, name: &str) -> Result<(), Box<dyn std::error::Error>> { | ||
if let Some(rule) = self.rules.get(name) { | ||
println!("Executing rule: {}", rule.name); | ||
// Iterate over the ports and attempt to connect to each | ||
for port in rule.sequence.iter() { | ||
let address = format!("{}:{}", rule.host, port); | ||
let addr: Vec<SocketAddr> = address.to_socket_addrs()?.collect(); | ||
println!("knocking at: {:?}", addr); | ||
|
||
// Attempt to connect to the target IP and port | ||
if let Ok(stream) = TcpStream::connect_timeout(&addr[0], Duration::from_millis(100)) | ||
{ | ||
drop(stream); | ||
} | ||
} | ||
} else { | ||
println!("Rule not found: {}", name); | ||
return Ok(()); | ||
} | ||
|
||
println!("Rule execution complete."); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
interface: "enp3s0" | ||
timeout: 5 | ||
rules: | ||
- name: "Enable SSH" | ||
- name: "enable_ssh" | ||
command: "/usr/sbin/iptables -A INPUT -s %IP% -p tcp --dport 22 -j ACCEPT" | ||
sequence: | ||
- 15523 | ||
- 17767 | ||
- 32768 | ||
- 28977 | ||
- 51234 | ||
- name: "Disable SSH" | ||
- name: "disable_ssh" | ||
command: "/usr/sbin/iptables -D INPUT -s %IP% -p tcp --dport 22 -j ACCEPT" | ||
sequence: | ||
- 51234 | ||
- 28977 | ||
- 32768 | ||
- 17767 | ||
- 15523 | ||
- 15523 |