Skip to content
Merged
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
601 changes: 598 additions & 3 deletions Cargo.lock

Large diffs are not rendered by default.

20 changes: 19 additions & 1 deletion crates/stackforge-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,26 @@ default-net = "0.22.0"
rand = { version = "0.9.2", optional = true }
pcap-file = "2"

# TLS crypto dependencies
hmac = { version = "0.12", optional = true }
sha2 = { version = "0.10", optional = true }
sha1 = { version = "0.10", optional = true }
md-5 = { version = "0.10", optional = true }
aes = { version = "0.8", optional = true }
cbc = { version = "0.1", optional = true }
aes-gcm = { version = "0.10", optional = true }
chacha20poly1305 = { version = "0.10", optional = true }
hkdf = { version = "0.12", optional = true }
p256 = { version = "0.13", features = ["ecdh"], optional = true }
x25519-dalek = { version = "2", features = ["getrandom", "static_secrets"], optional = true }
digest = { version = "0.10", optional = true }
rc4 = { version = "0.1", optional = true }
des = { version = "0.8", optional = true }

[features]
default = ["rand"]
default = ["rand", "tls"]
tls = ["hmac", "sha2", "sha1", "md-5", "aes", "cbc", "aes-gcm", "chacha20poly1305", "hkdf", "p256", "x25519-dalek", "digest"]
tls-weak-crypto = ["tls", "rc4", "des"]

[lints]
workspace = true
2 changes: 2 additions & 0 deletions crates/stackforge-core/src/layer/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ pub static LAYER_BINDINGS: &[LayerBinding] = &[
"proto",
ip_protocol::ICMP as u16,
),
// TCP -> * (port-based, dport field)
LayerBinding::new(LayerKind::Tcp, LayerKind::Tls, "dport", 443),
// IPv6 -> *
LayerBinding::new(
LayerKind::Ipv6,
Expand Down
51 changes: 51 additions & 0 deletions crates/stackforge-core/src/layer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod raw;
pub mod ssh;
pub mod stack;
pub mod tcp;
pub mod tls;
pub mod udp;

use std::ops::Range;
Expand All @@ -34,6 +35,10 @@ pub use tcp::{
TcpFlags, TcpLayer, TcpOption, TcpOptionKind, TcpOptions, TcpOptionsBuilder, TcpSackBlock,
TcpTimestamp, service_name, service_port, tcp_checksum, tcp_checksum_ipv4, verify_tcp_checksum,
};
pub use tls::{
TLS_FIELDS, TLS_PORT, TLS_RECORD_HEADER_LEN, TlsAlertBuilder, TlsCcsBuilder, TlsContentType,
TlsLayer, TlsRecordBuilder, TlsVersion,
};
pub use udp::{
UDP_HEADER_LEN, UdpBuilder, UdpLayer, udp_checksum_ipv4, udp_checksum_ipv6,
verify_udp_checksum_ipv4, verify_udp_checksum_ipv6,
Expand All @@ -59,6 +64,7 @@ pub enum LayerKind {
LLC = 13,
SNAP = 14,
Ssh = 15,
Tls = 16,
Raw = 255,
}

Expand All @@ -82,6 +88,7 @@ impl LayerKind {
Self::LLC => "LLC",
Self::SNAP => "SNAP",
Self::Ssh => "SSH",
Self::Tls => "TLS",
Self::Raw => "Raw",
}
}
Expand All @@ -103,6 +110,7 @@ impl LayerKind {
Self::LLC => 3,
Self::SNAP => 5,
Self::Ssh => ssh::SSH_BINARY_HEADER_LEN,
Self::Tls => tls::TLS_RECORD_HEADER_LEN,
Self::Raw => 0,
}
}
Expand Down Expand Up @@ -235,6 +243,7 @@ pub enum LayerEnum {
Udp(UdpLayer),
Dns(DnsLayer),
Ssh(SshLayer),
Tls(TlsLayer),
Raw(RawLayer),
}

Expand All @@ -253,6 +262,7 @@ impl LayerEnum {
Self::Udp(_) => LayerKind::Udp,
Self::Dns(_) => LayerKind::Dns,
Self::Ssh(_) => LayerKind::Ssh,
Self::Tls(_) => LayerKind::Tls,
Self::Raw(_) => LayerKind::Raw,
}
}
Expand All @@ -271,6 +281,7 @@ impl LayerEnum {
Self::Udp(l) => &l.index,
Self::Dns(l) => &l.index,
Self::Ssh(l) => &l.index,
Self::Tls(l) => &l.index,
Self::Raw(l) => &l.index,
}
}
Expand All @@ -288,6 +299,7 @@ impl LayerEnum {
Self::Udp(l) => l.summary(buf),
Self::Dns(l) => l.summary(buf),
Self::Ssh(l) => l.summary(buf),
Self::Tls(l) => l.summary(buf),
Self::Raw(l) => l.summary(buf),
}
}
Expand Down Expand Up @@ -317,6 +329,7 @@ impl LayerEnum {
Self::Udp(l) => l.header_len(buf),
Self::Dns(l) => l.header_len(buf),
Self::Ssh(l) => l.header_len(buf),
Self::Tls(l) => l.header_len(buf),
Self::Raw(l) => l.header_len(buf),
}
}
Expand All @@ -336,6 +349,7 @@ impl LayerEnum {
Self::Udp(l) => udp_show_fields(l, buf),
Self::Dns(l) => dns_show_fields(l, buf),
Self::Ssh(l) => ssh_show_fields(l, buf),
Self::Tls(l) => tls_show_fields(l, buf),
Self::Raw(l) => raw::raw_show_fields(l, buf),
}
}
Expand All @@ -353,6 +367,7 @@ impl LayerEnum {
Self::Udp(l) => l.get_field(buf, name),
Self::Raw(l) => l.get_field(buf, name),
Self::Ssh(l) => l.get_field(buf, name),
Self::Tls(l) => l.get_field(buf, name),
// Placeholder layers don't have dynamic field access yet
Self::Ipv6(_) | Self::Icmpv6(_) | Self::Dns(_) => None,
}
Expand All @@ -376,6 +391,7 @@ impl LayerEnum {
Self::Udp(l) => l.set_field(buf, name, value),
Self::Raw(l) => l.set_field(buf, name, value),
Self::Ssh(l) => l.set_field(buf, name, value),
Self::Tls(l) => l.set_field(buf, name, value),
// Placeholder layers don't have dynamic field access yet
Self::Ipv6(_) | Self::Icmpv6(_) | Self::Dns(_) => None,
}
Expand All @@ -393,6 +409,7 @@ impl LayerEnum {
Self::Udp(l) => l.field_names(),
Self::Raw(_) => RawLayer::field_names(),
Self::Ssh(l) => l.field_names(),
Self::Tls(l) => l.field_names(),
// Placeholder layers
Self::Ipv6(_) | Self::Icmpv6(_) | Self::Dns(_) => &[],
}
Expand Down Expand Up @@ -870,6 +887,40 @@ fn ssh_show_fields(l: &SshLayer, buf: &[u8]) -> Vec<(&'static str, String)> {
fields
}

fn tls_show_fields(l: &TlsLayer, buf: &[u8]) -> Vec<(&'static str, String)> {
let mut fields = Vec::new();
fields.push((
"type",
l.content_type(buf)
.map(|ct| format!("{} ({})", ct.as_u8(), ct.name()))
.unwrap_or_else(|_| "?".into()),
));
fields.push((
"version",
l.version(buf)
.map(|v| {
let ver = TlsVersion(v);
format!("{:#06x} ({})", v, ver.name())
})
.unwrap_or_else(|_| "?".into()),
));
fields.push((
"len",
l.length(buf)
.map(|v| v.to_string())
.unwrap_or_else(|_| "?".into()),
));
let frag = l.fragment(buf);
if !frag.is_empty() {
if frag.len() <= 16 {
fields.push(("fragment", format!("[{} bytes] {:02x?}", frag.len(), frag)));
} else {
fields.push(("fragment", format!("[{} bytes]", frag.len())));
}
}
fields
}

// Placeholder layer structs (to be fully implemented in later weeks)
#[derive(Debug, Clone)]
pub struct Ipv6Layer {
Expand Down
7 changes: 7 additions & 0 deletions crates/stackforge-core/src/layer/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use super::icmp::builder::IcmpBuilder;
use super::ipv4::builder::Ipv4Builder;
use super::ssh::builder::SshBuilder;
use super::tcp::builder::TcpBuilder;
use super::tls::builder::TlsRecordBuilder;
use super::udp::builder::UdpBuilder;
use super::{ArpBuilder, LayerKind};
use crate::Packet;
Expand All @@ -68,6 +69,8 @@ pub enum LayerStackEntry {
Icmp(IcmpBuilder),
/// SSH layer
Ssh(SshBuilder),
/// TLS record layer
Tls(TlsRecordBuilder),
/// Raw bytes payload
Raw(Vec<u8>),
}
Expand All @@ -83,6 +86,7 @@ impl LayerStackEntry {
Self::Udp(_) => LayerKind::Udp,
Self::Icmp(_) => LayerKind::Icmp,
Self::Ssh(_) => LayerKind::Ssh,
Self::Tls(_) => LayerKind::Tls,
Self::Raw(_) => LayerKind::Raw,
}
}
Expand All @@ -97,6 +101,7 @@ impl LayerStackEntry {
Self::Udp(b) => b.build(),
Self::Icmp(b) => b.build(),
Self::Ssh(b) => b.build(),
Self::Tls(b) => b.build(),
Self::Raw(data) => data.clone(),
}
}
Expand All @@ -111,6 +116,7 @@ impl LayerStackEntry {
Self::Udp(b) => b.header_size(),
Self::Icmp(b) => b.header_size(),
Self::Ssh(b) => b.header_size(),
Self::Tls(b) => b.record_size(),
Self::Raw(data) => data.len(),
}
}
Expand All @@ -125,6 +131,7 @@ impl LayerStackEntry {
Self::Udp(_) => UDP_HEADER_LEN,
Self::Icmp(_) => ICMP_MIN_HEADER_LEN,
Self::Ssh(b) => b.header_size(),
Self::Tls(_) => 5, // TLS record header is 5 bytes
Self::Raw(data) => data.len(),
}
}
Expand Down
Loading