Skip to content

Commit

Permalink
implement ja4 and support it in rama fp/echo
Browse files Browse the repository at this point in the history
Closes #378
  • Loading branch information
GlenDC committed Jan 3, 2025
1 parent 6fe0dbc commit 1218a0a
Show file tree
Hide file tree
Showing 10 changed files with 451 additions and 12 deletions.
17 changes: 16 additions & 1 deletion rama-cli/src/cmd/fp/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rama::{
HeaderMap, Request,
},
net::{
fingerprint::{Ja3, Ja4H},
fingerprint::{Ja3, Ja4, Ja4H},
http::RequestContext,
stream::SocketInfo,
},
Expand Down Expand Up @@ -235,13 +235,20 @@ pub(super) fn get_http_info(headers: HeaderMap, ext: &mut Extensions) -> HttpInf

#[derive(Debug, Clone, Serialize)]
pub(super) struct TlsDisplayInfo {
pub(super) ja4: Ja4DisplayInfo,
pub(super) ja3: Ja3DisplayInfo,
pub(super) protocol_version: String,
pub(super) cipher_suites: Vec<String>,
pub(super) compression_algorithms: Vec<String>,
pub(super) extensions: Vec<TlsDisplayInfoExtension>,
}

#[derive(Debug, Clone, Serialize)]
pub(super) struct Ja4DisplayInfo {
pub(super) full: String,
pub(super) hash: String,
}

#[derive(Debug, Clone, Serialize)]
pub(super) struct Ja3DisplayInfo {
pub(super) full: String,
Expand All @@ -265,11 +272,19 @@ pub(super) fn get_tls_display_info(ctx: &Context<Arc<State>>) -> Option<TlsDispl
.get::<SecureTransport>()
.and_then(|st| st.client_hello())?;

let ja4 = Ja4::compute(ctx.extensions())
.inspect_err(|err| tracing::error!(?err, "ja4 compute failure"))
.ok()?;

let ja3 = Ja3::compute(ctx.extensions())
.inspect_err(|err| tracing::error!(?err, "ja3 compute failure"))
.ok()?;

Some(TlsDisplayInfo {
ja4: Ja4DisplayInfo {
full: format!("{ja4:?}"),
hash: format!("{ja4}"),
},
ja3: Ja3DisplayInfo {
full: format!("{ja3}"),
hash: format!("{ja3:x}"),
Expand Down
11 changes: 9 additions & 2 deletions rama-cli/src/cmd/fp/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,12 +520,19 @@ fn render_page(title: &'static str, head: String, content: String) -> Html {

impl From<TlsDisplayInfo> for Vec<Table> {
fn from(info: TlsDisplayInfo) -> Self {
let mut vec = Vec::with_capacity(info.extensions.len() + 2);
let mut vec = Vec::with_capacity(info.extensions.len() + 3);
vec.push(Table {
title: "🆔 Ja4".to_owned(),
rows: vec![
("TLS Client Fingerprint".to_owned(), info.ja4.hash),
("Raw (Debug) String".to_owned(), info.ja4.full),
],
});
vec.push(Table {
title: "🆔 Ja3".to_owned(),
rows: vec![
("full".to_owned(), info.ja3.full),
("hash".to_owned(), info.ja3.hash),
("full".to_owned(), info.ja3.full),
],
});
vec.push(Table {
Expand Down
4 changes: 2 additions & 2 deletions rama-net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ workspace = true
[features]
default = []
http = ["dep:rama-http-types", "dep:sha2", "dep:itertools", "dep:hex"]
tls = ["dep:hex", "dep:md5", "dep:sha2"]
tls = ["dep:hex", "dep:md5", "dep:sha2", "dep:itertools"]
rustls = ["tls", "dep:rustls"]
boring = ["tls", "dep:boring", "dep:nom", "dep:itertools"]
boring = ["tls", "dep:boring", "dep:nom"]
rustls-ring = ["rustls", "rustls/ring"]
telemetry = ["rama-core/telemetry"]

Expand Down
3 changes: 1 addition & 2 deletions rama-net/src/fingerprint/ja3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use crate::tls::{

#[derive(Debug, Clone)]
/// Data which can be hashed using [`Self::hash`],
/// and which is also displayed as a "ja3" hash when serializing (serde)
/// or displaying it.
/// and which is also displayed as a "ja3" hash.
///
/// Computed using [`Ja3::compute`].
pub struct Ja3 {
Expand Down
4 changes: 2 additions & 2 deletions rama-net/src/fingerprint/ja4/http.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use itertools::Itertools as _;
use std::{
borrow::Cow,
fmt::{self, Write},
};

use itertools::Itertools;
use rama_http_types::{
header::{ACCEPT_LANGUAGE, COOKIE, REFERER},
proto::h1::Http1HeaderMap,
Expand All @@ -12,7 +12,7 @@ use rama_http_types::{

#[derive(Clone)]
/// Data which can be hashed using [`Self::hash`],
/// and which is also displayed as a "ja4h" hash when serializing (serde)
/// and which is also displayed as a "ja4h" hash.
/// or displaying it.
///
/// Computed using [`Ja4H::compute`].
Expand Down
3 changes: 3 additions & 0 deletions rama-net/src/fingerprint/ja4/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ pub use http::{Ja4H, Ja4HComputeError};

#[cfg(feature = "tls")]
mod tls;

#[cfg(feature = "tls")]
pub use tls::{Ja4, Ja4ComputeError};
Loading

0 comments on commit 1218a0a

Please sign in to comment.