-
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
1 parent
e2b720d
commit 3baadb2
Showing
4 changed files
with
331 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -4,3 +4,4 @@ version = "0.1.0" | |
edition = "2021" | ||
|
||
[dependencies] | ||
pnet = "0.35.0" |
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,10 @@ | ||
### Get network | ||
``` | ||
ip link show | ||
``` | ||
|
||
### Process packages | ||
``` | ||
cargo build --release | ||
sudo RUST_BACKTRACE=1 ./target/release/p0f | ||
``` |
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,3 +1,80 @@ | ||
extern crate pnet; | ||
|
||
use pnet::datalink::{self, Channel::Ethernet, Config, NetworkInterface}; | ||
use pnet::packet::{ipv6::Ipv6Packet, tcp::TcpPacket, Packet}; | ||
use std::net::Ipv6Addr; | ||
|
||
fn main() { | ||
println!("Hello, world!"); | ||
println!("Program started"); | ||
let interface_name = "wlp0s20f3"; // Your interface name here | ||
let interfaces: Vec<NetworkInterface> = datalink::interfaces(); | ||
let interface: NetworkInterface = interfaces.into_iter() | ||
.filter(|iface| iface.name == interface_name) | ||
.next() | ||
.expect("Could not find the interface"); | ||
|
||
let mut config = Config::default(); | ||
config.promiscuous = true; // Enable promiscuous mode | ||
|
||
// Open the channel | ||
let (mut _tx, mut rx) = match datalink::channel(&interface, config) { | ||
Ok(Ethernet(tx, rx)) => (tx, rx), | ||
Ok(_) => panic!("Unhandled channel type"), | ||
Err(e) => panic!("Unable to create channel: {}", e), | ||
}; | ||
|
||
// Loop to capture packets | ||
loop { | ||
match rx.next() { | ||
Ok(packet) => { | ||
process_packet(packet); | ||
} | ||
Err(_) => { | ||
eprintln!("Failed to capture packet"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn process_packet(packet: &[u8]) { | ||
if let Some(ipv6_packet) = Ipv6Packet::new(packet) { | ||
let client_ip = ipv6_packet.get_source(); | ||
let server_ip = ipv6_packet.get_destination(); | ||
|
||
// Extract TCP segment | ||
if let Some(tcp_packet) = TcpPacket::new(ipv6_packet.payload()) { | ||
let client_port = tcp_packet.get_source(); | ||
let server_port = tcp_packet.get_destination(); | ||
let payload = tcp_packet.payload(); | ||
|
||
process_http_payload(payload, client_ip, client_port, server_ip, server_port); | ||
} | ||
} | ||
} | ||
|
||
// 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) { | ||
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) { | ||
println!( | ||
".-[ {}/{} -> {}/{} ]-", | ||
client_ip, client_port, server_ip, server_port | ||
); | ||
println!("|"); | ||
println!("| client = {}/{}", client_ip, client_port); | ||
println!("| headers = {}", headers); | ||
println!("| raw_sig = {}", extract_raw_signature(headers)); | ||
println!("|"); | ||
println!("`----"); | ||
} | ||
|
||
fn extract_raw_signature(headers: &str) -> String { | ||
headers.to_string() | ||
} | ||
|