cfsolve 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +40 -0
  3. data/README.md +30 -5
  4. data/UPSTREAM.md +24 -3
  5. data/lib/cfsolve/profiles/safari-26-ios/answers.json +805 -797
  6. data/lib/cfsolve/profiles/safari-26-ios/challenge-page-answers.json +909 -899
  7. data/lib/cfsolve/version.rb +2 -2
  8. data/lib/cfsolve.rb +21 -10
  9. data/vendor/cfsolve/examples/listing_dump.rs +24 -0
  10. data/vendor/cfsolve/examples/probe_similarity.rs +37 -0
  11. data/vendor/cfsolve/examples/rebase_reference.rs +96 -0
  12. data/vendor/cfsolve/examples/reread_capture.rs +31 -0
  13. data/vendor/cfsolve/examples/turnstile_smoke.rs +79 -0
  14. data/vendor/cfsolve/profiles/safari-26-ios/answers.json +805 -797
  15. data/vendor/cfsolve/profiles/safari-26-ios/challenge-page-answers.json +909 -899
  16. data/vendor/cfsolve/src/capture/sessions.rs +88 -4
  17. data/vendor/cfsolve/src/capture.rs +1 -0
  18. data/vendor/cfsolve/src/deobfuscate/ast.rs +16 -0
  19. data/vendor/cfsolve/src/deobfuscate/control_flow.rs +1 -1
  20. data/vendor/cfsolve/src/deobfuscate/dead_branches.rs +33 -0
  21. data/vendor/cfsolve/src/deobfuscate/merged_functions.rs +24 -4
  22. data/vendor/cfsolve/src/embedding_page/extra_params.rs +3 -3
  23. data/vendor/cfsolve/src/embedding_page/page.rs +76 -65
  24. data/vendor/cfsolve/src/stage2/analysis/discovery.rs +7 -4
  25. data/vendor/cfsolve/src/stage2/benchmark.rs +29 -8
  26. data/vendor/cfsolve/src/stage2/call_stack.rs +134 -95
  27. data/vendor/cfsolve/src/stage2/challenge_page.rs +10 -3
  28. data/vendor/cfsolve/src/stage2/probes/parent_info.rs +345 -61
  29. data/vendor/cfsolve/src/stage2/probes/performance.rs +42 -5
  30. data/vendor/cfsolve/src/stage2/probes/stack.rs +41 -0
  31. data/vendor/cfsolve/src/stage2/probes/websocket_echo.rs +11 -2
  32. data/vendor/cfsolve/src/stage2/reference.json +1 -1
  33. data/vendor/cfsolve/src/stage2/reference.rs +70 -110
  34. data/vendor/cfsolve/src/stage2/registry.rs +4 -3
  35. data/vendor/cfsolve/src/stage2/trace.rs +1 -1
  36. data/vendor/cfsolve/src/vm/dataflow.rs +15 -6
  37. data/vendor/cfsolve/src/vm/disassemble.rs +2 -1
  38. data/vendor/cfsolve/src/vm/encoded_strings.rs +1553 -0
  39. data/vendor/cfsolve/src/vm/extract.rs +205 -152
  40. data/vendor/cfsolve/src/vm/interpret/access.rs +0 -2
  41. data/vendor/cfsolve/src/vm/interpret/builtin.rs +0 -13
  42. data/vendor/cfsolve/src/vm/interpret/intrinsic.rs +30 -7
  43. data/vendor/cfsolve/src/vm/interpret/mix32.rs +216 -86
  44. data/vendor/cfsolve/src/vm/interpret/native_dispatch.rs +42 -21
  45. data/vendor/cfsolve/src/vm/interpret/pattern.rs +268 -67
  46. data/vendor/cfsolve/src/vm/interpret/register_helpers.rs +261 -30
  47. data/vendor/cfsolve/src/vm/machine.rs +14 -12
  48. data/vendor/cfsolve/src/vm/output.rs +4 -1
  49. data/vendor/cfsolve/src/vm/registers.rs +17 -23
  50. data/vendor/cfsolve/src/vm/session.rs +9 -4
  51. data/vendor/cfsolve/src/vm.rs +1 -0
  52. metadata +7 -1
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Cfsolve
4
- VERSION = "0.1.3"
5
- UPSTREAM_REVISION = "d94de0472bad95dee84fb312f9e39f573f8148eb"
4
+ VERSION = "0.1.5"
5
+ UPSTREAM_REVISION = "82b19a39f8366b02c9eaf54b1c09ce97870ba981"
6
6
  end
data/lib/cfsolve.rb CHANGED
@@ -73,23 +73,34 @@ module Cfsolve
73
73
 
74
74
  request[:configuration] = JSON.parse(@configuration)
75
75
  request[:timeout] = timeout
76
+ Timeout.timeout(timeout, TimeoutError, "The solver exceeded its deadline") do
77
+ attempts = 0
78
+ begin
79
+ attempts += 1
80
+ execute(request)
81
+ rescue UnsupportedError
82
+ retry if attempts < 3
83
+ raise
84
+ end
85
+ end
86
+ end
87
+
88
+ def execute(request)
76
89
  child_input, input = IO.pipe
77
90
  output, child_output = IO.pipe
78
91
  pid = Process.spawn(EXECUTABLE, in: child_input, out: child_output, err: File::NULL)
79
92
  status = nil
80
93
  child_input.close
81
94
  child_output.close
82
- Timeout.timeout(timeout, TimeoutError, "The solver exceeded its deadline") do
83
- input.write(JSON.generate(request))
84
- input.close
85
- result = JSON.parse(output.read)
86
- _, status = Process.wait2(pid)
87
- raise Error, "The solver process failed" unless status.success?
88
- if (error = result["error"])
89
- raise ERRORS.fetch(error.fetch("kind"), Error), error.fetch("message")
90
- end
91
- result
95
+ input.write(JSON.generate(request))
96
+ input.close
97
+ result = JSON.parse(output.read)
98
+ _, status = Process.wait2(pid)
99
+ raise Error, "The solver process failed" unless status.success?
100
+ if (error = result["error"])
101
+ raise ERRORS.fetch(error.fetch("kind"), Error), error.fetch("message")
92
102
  end
103
+ result
93
104
  rescue Errno::ENOENT
94
105
  raise ConfigurationError, "The cfsolve executable is missing; reinstall the gem", cause: nil
95
106
  rescue JSON::ParserError, IOError, Errno::EPIPE
@@ -0,0 +1,24 @@
1
+ //! Temporary: `listing_dump <deobfuscated.js> <program base64 file>` prints the full listing.
2
+
3
+ use std::time::Instant;
4
+ use std::{env, fs};
5
+
6
+ use anyhow::Result;
7
+ use base64::Engine;
8
+ use base64::engine::general_purpose::STANDARD;
9
+ use cfsolve::script::Source;
10
+
11
+ fn main() -> Result<()> {
12
+ let arguments: Vec<String> = env::args().skip(1).collect();
13
+ let text = fs::read_to_string(&arguments[0])?;
14
+ let program = STANDARD.decode(fs::read_to_string(&arguments[1])?.trim())?;
15
+ let source = Source::try_new(&text)?;
16
+ let machine = source.machine()?;
17
+ let began = Instant::now();
18
+ let listing = machine.disassemble_from(&program, &[machine.entry()], &["cf-chl-peek"])?;
19
+ eprintln!("disassemble: {:?}", began.elapsed());
20
+ if arguments.get(2).is_none_or(|flag| flag != "--quiet") {
21
+ println!("{listing:#?}");
22
+ }
23
+ Ok(())
24
+ }
@@ -0,0 +1,37 @@
1
+ //! Show each captured probe's closest reference probe and its non-name strings, to see why
2
+ //! probes of a new build fail to pair.
3
+ //! Usage: probe_similarity <capture.json> [probe index...]
4
+
5
+ use std::env;
6
+ use std::path::Path;
7
+
8
+ use anyhow::{Context, Result};
9
+ use cfsolve::capture::CapturedSession;
10
+ use cfsolve::stage2::analysis;
11
+
12
+ fn main() -> Result<()> {
13
+ let mut arguments = env::args().skip(1);
14
+ let path = arguments.next().context("supply a capture JSON file")?;
15
+ let detail: Vec<usize> = arguments.filter_map(|text| text.parse().ok()).collect();
16
+ let capture = CapturedSession::read(Path::new(&path))?;
17
+ let analysis = analysis::analyze(&capture.source()?, &capture.program()?)?;
18
+ let plan = analysis.plan();
19
+ let vocabulary = plan.vocabulary();
20
+ for (position, probe) in plan.probes().iter().enumerate() {
21
+ let candidate = vocabulary.candidate(position);
22
+ println!(
23
+ "{}\tpartner {:?}\tbest {:?}\t{} strings",
24
+ probe.index,
25
+ vocabulary.partner(position),
26
+ candidate.map(|candidate| (candidate.reference, candidate.similarity)),
27
+ probe.features.len(),
28
+ );
29
+ if detail.contains(&probe.index) {
30
+ println!(" {:?}", probe.features);
31
+ let payload = capture.probe_payload().ok();
32
+ let record = payload.and_then(|payload| payload.get(&probe.index.to_string()));
33
+ println!(" sent {}", serde_json::to_string(&record)?);
34
+ }
35
+ }
36
+ Ok(())
37
+ }
@@ -0,0 +1,96 @@
1
+ //! Rebuild the reference from a capture of a new build, keeping the reference probe numbers.
2
+ //! Usage: rebase_reference <capture.json> <old reference.json> <reference.rs> <new reference.json>
3
+ //!
4
+ //! Each reference probe becomes the capture's probe paired with it, or the capture's probe of
5
+ //! the same family when that family is recognized without pairing; a family the build no longer
6
+ //! has keeps its old strings. Prints each hex name `reference.rs` declares with the capture's name
7
+ //! for it, through the vocabulary of every probe that mentions it, and `?` where a probe does not
8
+ //! translate it, so the declarations can be moved to the new reference.
9
+
10
+ use std::collections::{BTreeMap, BTreeSet};
11
+ use std::path::Path;
12
+ use std::{env, fs};
13
+
14
+ use anyhow::{Context, Result};
15
+ use cfsolve::capture::CapturedSession;
16
+ use cfsolve::stage2::analysis;
17
+ use cfsolve::stage2::registry::Kind;
18
+ use serde_json::{Value, json};
19
+
20
+ fn main() -> Result<()> {
21
+ let arguments: Vec<String> = env::args().skip(1).collect();
22
+ let [capture, old, declarations, new] = arguments.as_slice() else {
23
+ anyhow::bail!("supply capture.json, the old reference.json, reference.rs and an output");
24
+ };
25
+ let capture = CapturedSession::read(Path::new(capture))?;
26
+ let analysis = analysis::analyze(&capture.source()?, &capture.program()?)?;
27
+ let plan = analysis.plan();
28
+ let vocabulary = plan.vocabulary();
29
+ let old: Value = serde_json::from_str(&fs::read_to_string(old)?)?;
30
+ let old_probes = old["probes"].as_array().context("no reference probes")?;
31
+ // Each reference probe's session probe position.
32
+ let mut positions: BTreeMap<usize, usize> = BTreeMap::new();
33
+ for (position, probe) in plan.probes().iter().enumerate() {
34
+ let reference = vocabulary.partner(position).or_else(|| {
35
+ let kind = probe.operation.as_ref()?.kind();
36
+ match kind.references() {
37
+ [reference] => Some(*reference),
38
+ _ => None,
39
+ }
40
+ });
41
+ if let Some(reference) = reference {
42
+ anyhow::ensure!(
43
+ positions.insert(reference, position).is_none(),
44
+ "two probes stand for reference probe {reference}"
45
+ );
46
+ }
47
+ }
48
+ let mut probes = Vec::new();
49
+ for (number, old_probe) in (1..).zip(old_probes) {
50
+ if let Some(&position) = positions.get(&number) {
51
+ probes.push(json!(plan.probes()[position].features));
52
+ } else {
53
+ eprintln!(
54
+ "reference probe {number} ({}) has no probe in this build; keeping its strings",
55
+ Kind::from_reference(number).map_or("?".into(), |kind| kind.to_string())
56
+ );
57
+ probes.push(old_probe.clone());
58
+ }
59
+ }
60
+ let build = capture
61
+ .iframe_html()
62
+ .split("/turnstile/v0/g/")
63
+ .nth(1)
64
+ .and_then(|rest| rest.get(..12))
65
+ .unwrap_or("unknown");
66
+ let rebuilt = json!({"build": build, "probes": probes});
67
+ fs::write(new, serde_json::to_string(&rebuilt)?)?;
68
+
69
+ // Translate each declared name through the probes that mention it.
70
+ let declarations = fs::read_to_string(declarations)?;
71
+ let declared: BTreeSet<&str> = declarations
72
+ .split('"')
73
+ .filter(|text| text.len() == 25 && text.starts_with('_'))
74
+ .filter(|text| text[1..].bytes().all(|byte| byte.is_ascii_hexdigit()))
75
+ .collect();
76
+ for name in declared {
77
+ let mut translations = BTreeSet::new();
78
+ for (number, old_probe) in (1..).zip(old_probes) {
79
+ let Some(&position) = positions.get(&number) else {
80
+ continue;
81
+ };
82
+ let strings = old_probe.as_array();
83
+ if strings.is_some_and(|strings| strings.iter().any(|text| text == name)) {
84
+ let session = vocabulary.to_session(position, name);
85
+ translations.insert(if session == name {
86
+ format!("?@{number}")
87
+ } else {
88
+ session.to_owned()
89
+ });
90
+ }
91
+ }
92
+ let translations: Vec<String> = translations.into_iter().collect();
93
+ println!("{name} {}", translations.join(" "));
94
+ }
95
+ Ok(())
96
+ }
@@ -0,0 +1,31 @@
1
+ //! Read the payloads of capture-proxy sessions logged without them, as when the proxy could not
2
+ //! extract a session's parameters, and write completed copies of the sessions.
3
+ //! Usage: reread_capture <capture-dir> <output-dir>
4
+ //!
5
+ //! Reads each `sessions/session-*` under the capture directory and writes the completed session
6
+ //! under `<output-dir>/sessions/` with the same name. The capture directory is left unchanged.
7
+
8
+ use std::path::PathBuf;
9
+ use std::{env, fs};
10
+
11
+ use anyhow::{Context, Result};
12
+ use cfsolve::capture;
13
+
14
+ fn main() -> Result<()> {
15
+ let mut arguments = env::args().skip(1);
16
+ let directory = PathBuf::from(arguments.next().context("supply a capture directory")?);
17
+ let output = PathBuf::from(arguments.next().context("supply an output directory")?);
18
+ let mut sessions: Vec<PathBuf> = fs::read_dir(directory.join("sessions"))?
19
+ .map(|entry| Ok(entry?.path()))
20
+ .collect::<Result<_>>()?;
21
+ sessions.sort();
22
+ for session in sessions {
23
+ let name = session.file_name().context("a session has no name")?;
24
+ let out = output.join("sessions").join(name);
25
+ match capture::reread_session(&session, &out) {
26
+ Ok(count) => println!("{}: {count} payloads", session.display()),
27
+ Err(error) => println!("{}: {error:#}", session.display()),
28
+ }
29
+ }
30
+ Ok(())
31
+ }
@@ -0,0 +1,79 @@
1
+ //! Repeat fresh widget solves without printing tokens.
2
+ //! Usage: turnstile_smoke <profile.json> <page-url> <sitekey> [attempts]
3
+ //! CFSOLVE_SMOKE_CAPTURE optionally saves anonymous challenge diagnostics.
4
+
5
+ use std::{
6
+ env,
7
+ fs,
8
+ path::PathBuf,
9
+ sync::Arc,
10
+ time::{Duration, Instant},
11
+ };
12
+
13
+ use anyhow::{Context, Result, ensure};
14
+ use cfsolve::{Browser, Options, PageSource, Sink, Turnstile, Widget};
15
+
16
+ struct Capture(PathBuf);
17
+
18
+ impl Sink for Capture {
19
+ fn artifact(&self, name: &str, contents: &[u8]) -> Result<()> {
20
+ fs::write(self.0.join(name), contents)?;
21
+ Ok(())
22
+ }
23
+
24
+ fn progress(&self, _: &str) {}
25
+ }
26
+
27
+ #[tokio::main]
28
+ async fn main() -> Result<()> {
29
+ let mut arguments = env::args().skip(1);
30
+ let profile = arguments.next().context("supply a profile")?;
31
+ let url: cfsolve::Url = arguments.next().context("supply the page URL")?.parse()?;
32
+ let sitekey = arguments.next().context("supply the sitekey")?;
33
+ let attempts: usize = arguments.next().map_or(Ok(10), |value| value.parse())?;
34
+ ensure!(
35
+ (1..=100).contains(&attempts),
36
+ "attempts must be between 1 and 100"
37
+ );
38
+ let browser = Browser::read(&profile)?;
39
+ let started = Instant::now();
40
+ for attempt in 1..=attempts {
41
+ let mut options = Options::default();
42
+ if let Some(root) = env::var_os("CFSOLVE_SMOKE_CAPTURE") {
43
+ let directory = PathBuf::from(root).join(format!("attempt-{attempt}"));
44
+ fs::create_dir_all(&directory)?;
45
+ options.sink = Some(Arc::new(Capture(directory)));
46
+ }
47
+ let tick = Instant::now();
48
+ let widget = Turnstile {
49
+ url: url.clone(),
50
+ page: PageSource::Navigate,
51
+ widget: Some(Widget {
52
+ sitekey: sitekey.clone(),
53
+ action: None,
54
+ cdata: None,
55
+ }),
56
+ };
57
+ let token = tokio::time::timeout(
58
+ Duration::from_secs(30),
59
+ browser.solve_turnstile(widget, &options),
60
+ )
61
+ .await
62
+ .with_context(|| format!("attempt {attempt} exceeded 30 seconds"))?
63
+ .with_context(|| format!("attempt {attempt} failed"))?;
64
+ ensure!(
65
+ !token.is_empty(),
66
+ "attempt {attempt} returned an empty token"
67
+ );
68
+ println!(
69
+ "PASS {attempt}/{attempts}: {}-byte token, {:.2}s",
70
+ token.len(),
71
+ tick.elapsed().as_secs_f64()
72
+ );
73
+ }
74
+ println!(
75
+ "{attempts}/{attempts} fresh solves passed in {:.2}s",
76
+ started.elapsed().as_secs_f64()
77
+ );
78
+ Ok(())
79
+ }