Skip to content

Commit

Permalink
getting network interface by parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
biandratti committed Sep 8, 2024
1 parent 3baadb2 commit 7e36a82
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
.idea/
193 changes: 193 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ version = "0.1.0"
edition = "2021"

[dependencies]
clap = { version = "4.5.17", features = ["derive"] }
pnet = "0.35.0"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
### Get network
### Get network Interface
```
ip link show
```

### Process packages
```
cargo build --release
sudo RUST_BACKTRACE=1 ./target/release/p0f
sudo RUST_BACKTRACE=1 ./target/release/p0f --interface <INTERFACE>
```
45 changes: 32 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
extern crate pnet;

use clap::Parser;
use pnet::datalink::{self, Channel::Ethernet, Config, NetworkInterface};
use pnet::packet::{ipv6::Ipv6Packet, tcp::TcpPacket, Packet};
use std::net::Ipv6Addr;

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
#[arg(short, long)]
interface: String,
}

fn main() {
println!("Program started");
let interface_name = "wlp0s20f3"; // Your interface name here
let args = Args::parse();
let interface_name = args.interface;
let interfaces: Vec<NetworkInterface> = datalink::interfaces();
let interface: NetworkInterface = interfaces.into_iter()
.filter(|iface| iface.name == interface_name)
.next()

let interface: NetworkInterface = interfaces
.into_iter()
.find(|iface| iface.name == interface_name)
.expect("Could not find the interface");

let mut config = Config::default();
config.promiscuous = true; // Enable promiscuous mode
let config = Config {
promiscuous: true,
..Config::default()
};

// Open the channel
let (mut _tx, mut rx) = match datalink::channel(&interface, config) {
Expand All @@ -23,7 +32,6 @@ fn main() {
Err(e) => panic!("Unable to create channel: {}", e),
};

// Loop to capture packets
loop {
match rx.next() {
Ok(packet) => {
Expand Down Expand Up @@ -53,15 +61,27 @@ fn process_packet(packet: &[u8]) {
}

// Function to process the HTTP payload and log relevant details
fn process_http_payload(payload: &[u8], client_ip: Ipv6Addr, client_port: u16, server_ip: Ipv6Addr, server_port: u16) {
fn process_http_payload(
payload: &[u8],
client_ip: Ipv6Addr,
client_port: u16,
server_ip: Ipv6Addr,
server_port: u16,
) {
let payload_str = match std::str::from_utf8(payload) {
Ok(v) => v,
Err(_) => return, // Not valid UTF-8, skip processing
};
log_http_signature(client_ip, client_port, server_ip, server_port, payload_str);
}

fn log_http_signature(client_ip: Ipv6Addr, client_port: u16, server_ip: Ipv6Addr, server_port: u16, headers: &str) {
fn log_http_signature(
client_ip: Ipv6Addr,
client_port: u16,
server_ip: Ipv6Addr,
server_port: u16,
headers: &str,
) {
println!(
".-[ {}/{} -> {}/{} ]-",
client_ip, client_port, server_ip, server_port
Expand All @@ -77,4 +97,3 @@ fn log_http_signature(client_ip: Ipv6Addr, client_port: u16, server_ip: Ipv6Addr
fn extract_raw_signature(headers: &str) -> String {
headers.to_string()
}

0 comments on commit 7e36a82

Please sign in to comment.