Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 79 additions & 74 deletions benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#![feature(test)]
extern crate test;

extern crate sniffglue;
extern crate pktparse;
extern crate sniffglue;

pub use sniffglue::*;

Expand All @@ -12,49 +12,49 @@ mod tests {
use test::Bencher;

const HTML: [u8; 390] = [
32, 32, 32, 32, 32, 32, 32, 112, 97, 100, 100, 105, 110, 103, 58, 32, 49, 101, 109, 59,
10, 32, 32, 32, 32, 32, 32, 32, 32, 125, 10, 32, 32, 32, 32, 125, 10, 32, 32, 32, 32,
60, 47, 115, 116, 121, 108, 101, 62, 32, 32, 32, 32, 10, 60, 47, 104, 101, 97, 100, 62,
10, 10, 60, 98, 111, 100, 121, 62, 10, 60, 100, 105, 118, 62, 10, 32, 32, 32, 32, 60,
104, 49, 62, 69, 120, 97, 109, 112, 108, 101, 32, 68, 111, 109, 97, 105, 110, 60, 47,
104, 49, 62, 10, 32, 32, 32, 32, 60, 112, 62, 84, 104, 105, 115, 32, 100, 111, 109, 97,
105, 110, 32, 105, 115, 32, 101, 115, 116, 97, 98, 108, 105, 115, 104, 101, 100, 32,
116, 111, 32, 98, 101, 32, 117, 115, 101, 100, 32, 102, 111, 114, 32, 105, 108, 108,
117, 115, 116, 114, 97, 116, 105, 118, 101, 32, 101, 120, 97, 109, 112, 108, 101, 115,
32, 105, 110, 32, 100, 111, 99, 117, 109, 101, 110, 116, 115, 46, 32, 89, 111, 117, 32,
109, 97, 121, 32, 117, 115, 101, 32, 116, 104, 105, 115, 10, 32, 32, 32, 32, 100, 111,
109, 97, 105, 110, 32, 105, 110, 32, 101, 120, 97, 109, 112, 108, 101, 115, 32, 119,
105, 116, 104, 111, 117, 116, 32, 112, 114, 105, 111, 114, 32, 99, 111, 111, 114, 100,
105, 110, 97, 116, 105, 111, 110, 32, 111, 114, 32, 97, 115, 107, 105, 110, 103, 32,
102, 111, 114, 32, 112, 101, 114, 109, 105, 115, 115, 105, 111, 110, 46, 60, 47, 112,
62, 10, 32, 32, 32, 32, 60, 112, 62, 60, 97, 32, 104, 114, 101, 102, 61, 34, 104, 116,
116, 112, 58, 47, 47, 119, 119, 119, 46, 105, 97, 110, 97, 46, 111, 114, 103, 47, 100,
111, 109, 97, 105, 110, 115, 47, 101, 120, 97, 109, 112, 108, 101, 34, 62, 77, 111,
114, 101, 32, 105, 110, 102, 111, 114, 109, 97, 116, 105, 111, 110, 46, 46, 46, 60,
47, 97, 62, 60, 47, 112, 62, 10, 60, 47, 100, 105, 118, 62, 10, 60, 47, 98, 111, 100,
121, 62, 10, 60, 47, 104, 116, 109, 108, 62, 10
32, 32, 32, 32, 32, 32, 32, 112, 97, 100, 100, 105, 110, 103, 58, 32, 49, 101, 109, 59, 10,
32, 32, 32, 32, 32, 32, 32, 32, 125, 10, 32, 32, 32, 32, 125, 10, 32, 32, 32, 32, 60, 47,
115, 116, 121, 108, 101, 62, 32, 32, 32, 32, 10, 60, 47, 104, 101, 97, 100, 62, 10, 10, 60,
98, 111, 100, 121, 62, 10, 60, 100, 105, 118, 62, 10, 32, 32, 32, 32, 60, 104, 49, 62, 69,
120, 97, 109, 112, 108, 101, 32, 68, 111, 109, 97, 105, 110, 60, 47, 104, 49, 62, 10, 32,
32, 32, 32, 60, 112, 62, 84, 104, 105, 115, 32, 100, 111, 109, 97, 105, 110, 32, 105, 115,
32, 101, 115, 116, 97, 98, 108, 105, 115, 104, 101, 100, 32, 116, 111, 32, 98, 101, 32,
117, 115, 101, 100, 32, 102, 111, 114, 32, 105, 108, 108, 117, 115, 116, 114, 97, 116, 105,
118, 101, 32, 101, 120, 97, 109, 112, 108, 101, 115, 32, 105, 110, 32, 100, 111, 99, 117,
109, 101, 110, 116, 115, 46, 32, 89, 111, 117, 32, 109, 97, 121, 32, 117, 115, 101, 32,
116, 104, 105, 115, 10, 32, 32, 32, 32, 100, 111, 109, 97, 105, 110, 32, 105, 110, 32, 101,
120, 97, 109, 112, 108, 101, 115, 32, 119, 105, 116, 104, 111, 117, 116, 32, 112, 114, 105,
111, 114, 32, 99, 111, 111, 114, 100, 105, 110, 97, 116, 105, 111, 110, 32, 111, 114, 32,
97, 115, 107, 105, 110, 103, 32, 102, 111, 114, 32, 112, 101, 114, 109, 105, 115, 115, 105,
111, 110, 46, 60, 47, 112, 62, 10, 32, 32, 32, 32, 60, 112, 62, 60, 97, 32, 104, 114, 101,
102, 61, 34, 104, 116, 116, 112, 58, 47, 47, 119, 119, 119, 46, 105, 97, 110, 97, 46, 111,
114, 103, 47, 100, 111, 109, 97, 105, 110, 115, 47, 101, 120, 97, 109, 112, 108, 101, 34,
62, 77, 111, 114, 101, 32, 105, 110, 102, 111, 114, 109, 97, 116, 105, 111, 110, 46, 46,
46, 60, 47, 97, 62, 60, 47, 112, 62, 10, 60, 47, 100, 105, 118, 62, 10, 60, 47, 98, 111,
100, 121, 62, 10, 60, 47, 104, 116, 109, 108, 62, 10,
];

#[test]
fn tcp() {
use structs::raw::Raw::Ether;
use structs::ether::Ether::IPv4;
use structs::ipv4::IPv4::TCP;
use structs::raw::Raw::Ether;
use structs::tcp::TCP::Text;

use pktparse::ethernet::{MacAddress, EtherType, EthernetFrame};
use pktparse::ethernet::{EtherType, EthernetFrame, MacAddress};
use pktparse::ipv4::{IPv4Header, IPv4Protocol};
use pktparse::tcp::TcpHeader;

let mut pkt = Vec::new();
pkt.extend([
0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc,
0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
8, 0, 69, 0, 1, 186, 78, 105, 64, 0, 55, 6, 251, 115,
93, 184, 216, 34,
192, 168, 44, 55,
0, 80, 142, 158, 133, 72, 141, 7, 64, 115, 177, 1, 128, 24, 1, 27, 200, 121, 0, 0, 1, 1, 8, 10, 59, 135, 198, 7, 93, 127, 194, 19,
].iter());
pkt.extend(
[
0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 8, 0, 69,
0, 1, 186, 78, 105, 64, 0, 55, 6, 251, 115, 93, 184, 216, 34, 192, 168, 44, 55, 0,
80, 142, 158, 133, 72, 141, 7, 64, 115, 177, 1, 128, 24, 1, 27, 200, 121, 0, 0, 1,
1, 8, 10, 59, 135, 198, 7, 93, 127, 194, 19,
]
.iter(),
);
pkt.extend(HTML.iter());

let expected = Ok(Ether(
Expand All @@ -63,40 +63,44 @@ mod tests {
dest_mac: MacAddress([0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc]),
ethertype: EtherType::IPv4,
},
IPv4(IPv4Header {
version: 4,
ihl: 20,
tos: 0,
length: 442,
id: 20073,
flags: 2,
fragment_offset: 0,
ttl: 55,
protocol: IPv4Protocol::TCP,
chksum: 64371,
source_addr: "93.184.216.34".parse().unwrap(),
dest_addr: "192.168.44.55".parse().unwrap(),
},
TCP(TcpHeader {
source_port: 80,
dest_port: 36510,
sequence_no: 2236124423,
ack_no: 1081323777,
data_offset: 8,
reserved: 0,
flag_urg: false,
flag_ack: true,
flag_psh: true,
flag_rst: false,
flag_syn: false,
flag_fin: false,
window: 283,
checksum: 51321,
urgent_pointer: 0,
options: None
},
Text(String::from_utf8(HTML.to_vec()).unwrap())
))));
IPv4(
IPv4Header {
version: 4,
ihl: 20,
tos: 0,
length: 442,
id: 20073,
flags: 2,
fragment_offset: 0,
ttl: 55,
protocol: IPv4Protocol::TCP,
chksum: 64371,
source_addr: "93.184.216.34".parse().unwrap(),
dest_addr: "192.168.44.55".parse().unwrap(),
},
TCP(
TcpHeader {
source_port: 80,
dest_port: 36510,
sequence_no: 2236124423,
ack_no: 1081323777,
data_offset: 8,
reserved: 0,
flag_urg: false,
flag_ack: true,
flag_psh: true,
flag_rst: false,
flag_syn: false,
flag_fin: false,
window: 283,
checksum: 51321,
urgent_pointer: 0,
options: None,
},
Text(String::from_utf8(HTML.to_vec()).unwrap()),
),
),
));

let x = centrifuge::parse(&pkt);
assert_eq!(expected, x);
Expand All @@ -112,14 +116,15 @@ mod tests {
#[bench]
fn bench(b: &mut Bencher) {
let mut pkt = Vec::new();
pkt.extend([
0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc,
0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
8, 0, 69, 0, 1, 186, 78, 105, 64, 0, 55, 6, 251, 115,
93, 184, 216, 34,
192, 168, 44, 55,
0, 80, 142, 158, 133, 72, 141, 7, 64, 115, 177, 1, 128, 24, 1, 27, 200, 121, 0, 0, 1, 1, 8, 10, 59, 135, 198, 7, 93, 127, 194, 19,
].iter());
pkt.extend(
[
0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 8, 0, 69,
0, 1, 186, 78, 105, 64, 0, 55, 6, 251, 115, 93, 184, 216, 34, 192, 168, 44, 55, 0,
80, 142, 158, 133, 72, 141, 7, 64, 115, 177, 1, 128, 24, 1, 27, 200, 121, 0, 0, 1,
1, 8, 10, 59, 135, 198, 7, 93, 127, 194, 19,
]
.iter(),
);
pkt.extend(HTML.iter());

b.iter(|| {
Expand Down
10 changes: 4 additions & 6 deletions examples/boxxy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[macro_use] extern crate boxxy;
extern crate sniffglue;
#[macro_use]
extern crate boxxy;
extern crate env_logger;
extern crate sniffglue;

fn stage1(sh: &mut boxxy::Shell, _args: Vec<String>) -> Result<(), boxxy::Error> {
shprintln!(sh, "[*] starting stage1");
Expand All @@ -22,9 +23,6 @@ fn main() {
println!("stage1 activate sandbox stage1/2");
println!("stage2 activate sandbox stage2/2");

let toolbox = boxxy::Toolbox::new().with(vec![
("stage1", stage1),
("stage2", stage2),
]);
let toolbox = boxxy::Toolbox::new().with(vec![("stage1", stage1), ("stage2", stage2)]);
boxxy::Shell::new(toolbox).run()
}
2 changes: 1 addition & 1 deletion examples/read_packet.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate base64;
extern crate sniffglue;
extern crate env_logger;
extern crate sniffglue;

use std::env;

Expand Down
6 changes: 3 additions & 3 deletions src/centrifuge/arp.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use pktparse;

use crate::structs::CentrifugeError;
use crate::structs::arp::ARP;
use crate::structs::CentrifugeError;

pub fn extract(remaining: &[u8]) -> Result<ARP, CentrifugeError> {
if let Ok((_remaining, arp_pkt)) = pktparse::arp::parse_arp_pkt(remaining) {
use pktparse::arp::Operation;
match arp_pkt.operation {
Operation::Request => Ok(ARP::Request(arp_pkt)),
Operation::Reply => Ok(ARP::Reply(arp_pkt)),
Operation::Request => Ok(ARP::Request(arp_pkt)),
Operation::Reply => Ok(ARP::Reply(arp_pkt)),
Operation::Other(_) => Err(CentrifugeError::UnknownProtocol), // TODO
}
} else {
Expand Down
47 changes: 20 additions & 27 deletions src/centrifuge/cjdns.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,31 @@
use crate::structs::{cjdns, CentrifugeError};
use nom::number::complete::be_u16;
use nom::bytes::complete::{tag, take};
use nom::number::complete::be_u16;

const BEACON_PASSWORD_LEN: usize = 20;
const BEACON_PUBKEY_LEN: usize = 32;


fn cjdns_eth_header(input: &[u8]) -> nom::IResult<&[u8], cjdns::CjdnsEthPkt> {
let (input, (
_version,
_zero,
_length,
_fc00,
_padding,
version,
password,
pubkey,
)) = nom::sequence::tuple((
tag(b"\x00"),
tag(b"\x00"),
be_u16,
tag(b"\xfc\x00"),
take(2usize),

be_u16,
take(BEACON_PASSWORD_LEN),
take(BEACON_PUBKEY_LEN),
))(input)?;
let (input, (_version, _zero, _length, _fc00, _padding, version, password, pubkey)) =
nom::sequence::tuple((
tag(b"\x00"),
tag(b"\x00"),
be_u16,
tag(b"\xfc\x00"),
take(2usize),
be_u16,
take(BEACON_PASSWORD_LEN),
take(BEACON_PUBKEY_LEN),
))(input)?;

Ok((input, cjdns::CjdnsEthPkt {
version,
password: password.to_vec(),
pubkey: pubkey.to_vec(),
}))
Ok((
input,
cjdns::CjdnsEthPkt {
version,
password: password.to_vec(),
pubkey: pubkey.to_vec(),
},
))
}

pub fn parse(remaining: &[u8]) -> Result<cjdns::CjdnsEthPkt, CentrifugeError> {
Expand Down
33 changes: 17 additions & 16 deletions src/centrifuge/dhcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,28 @@ use dhcp4r;
use crate::structs::dhcp::*;
use crate::structs::{self, CentrifugeError};

fn wrap_packet(dhcp: &dhcp4r::packet::Packet, packet: structs::dhcp::Packet) -> structs::dhcp::DHCP {
fn wrap_packet(
dhcp: &dhcp4r::packet::Packet,
packet: structs::dhcp::Packet,
) -> structs::dhcp::DHCP {
use crate::structs::dhcp::DHCP::*;

match dhcp.option(dhcp4r::options::DHCP_MESSAGE_TYPE) {
Some(dhcp4r::options::DhcpOption::DhcpMessageType(msg_type)) => {
match msg_type {
dhcp4r::options::MessageType::Ack => ACK(packet),
dhcp4r::options::MessageType::Decline => DECLINE(packet),
dhcp4r::options::MessageType::Discover => DISCOVER(packet),
dhcp4r::options::MessageType::Inform => INFORM(packet),
dhcp4r::options::MessageType::Nak => NAK(packet),
dhcp4r::options::MessageType::Offer => OFFER(packet),
dhcp4r::options::MessageType::Release => RELEASE(packet),
dhcp4r::options::MessageType::Request => REQUEST(packet),
}
}
Some(dhcp4r::options::DhcpOption::DhcpMessageType(msg_type)) => match msg_type {
dhcp4r::options::MessageType::Ack => ACK(packet),
dhcp4r::options::MessageType::Decline => DECLINE(packet),
dhcp4r::options::MessageType::Discover => DISCOVER(packet),
dhcp4r::options::MessageType::Inform => INFORM(packet),
dhcp4r::options::MessageType::Nak => NAK(packet),
dhcp4r::options::MessageType::Offer => OFFER(packet),
dhcp4r::options::MessageType::Release => RELEASE(packet),
dhcp4r::options::MessageType::Request => REQUEST(packet),
},
_ => UNKNOWN(packet),
}
}


pub fn extract(remaining: &[u8]) -> Result<structs::dhcp::DHCP, CentrifugeError> {

let dhcp = match dhcp4r::packet::Packet::from(remaining) {
Ok(dhcp) => dhcp,
Err(_err) => return Err(CentrifugeError::InvalidPacket),
Expand All @@ -39,7 +38,9 @@ pub fn extract(remaining: &[u8]) -> Result<structs::dhcp::DHCP, CentrifugeError>
DhcpOption::RequestedIpAddress(addr) => packet.requested_ip_address = Some(*addr),
DhcpOption::HostName(hostname) => packet.hostname = Some(hostname.to_string()),
DhcpOption::Router(router) => packet.router = Some(router.to_vec()),
DhcpOption::DomainNameServer(server) => packet.domain_name_server = Some(server.to_vec()),
DhcpOption::DomainNameServer(server) => {
packet.domain_name_server = Some(server.to_vec())
}
_ => (),
}
}
Expand Down
20 changes: 11 additions & 9 deletions src/centrifuge/dns.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
use dns_parser;
use crate::structs::{self, CentrifugeError};
use dns_parser;

pub fn extract(remaining: &[u8]) -> Result<structs::dns::DNS, CentrifugeError> {
if let Ok(dns) = dns_parser::Packet::parse(remaining) {
if dns.header.query {
// dns request
let questions = dns.questions
.into_iter()
.map(|q| (q.qtype.into(), q.qname.to_string()))
.collect();
let questions = dns
.questions
.into_iter()
.map(|q| (q.qtype.into(), q.qname.to_string()))
.collect();

Ok(structs::dns::Request::new(questions).wrap())
} else {
// dns response
let answers = dns.answers
.into_iter()
.map(|a| (a.name.to_string(), structs::dns::Record::from(a.data)))
.collect();
let answers = dns
.answers
.into_iter()
.map(|a| (a.name.to_string(), structs::dns::Record::from(a.data)))
.collect();

Ok(structs::dns::Response::new(answers).wrap())
}
Expand Down
Loading