rsacracker 0.8.0

Powerful RSA cracker for CTFs. Supports RSA, X509, OPENSSH in PEM and DER formats.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use clap::{command, Parser};
use discrete_logarithm::discrete_log_with_factors;
use display_bytes::display_bytes;
use itertools::Itertools;
use main_error::MainError;
use rug::{
    integer::{IsPrime, Order},
    Integer,
};
use std::{
    io::{self, IsTerminal, Read},
    sync::Arc,
    time::Duration,
};

use rsacracker::{integer_to_bytes, integer_to_string, Attack, IntegerArg, Parameters, ATTACKS};
use update_informer::{registry, Check};

#[derive(Debug, Clone)]
struct AttackArg(Arc<dyn Attack + Sync + Send>);

impl std::str::FromStr for AttackArg {
    type Err = String;

    fn from_str(attack: &str) -> Result<Self, Self::Err> {
        ATTACKS
            .iter()
            .find(|a| a.name() == attack)
            .map(|a| Self(a.clone()))
            .ok_or_else(|| format!("Unknown attack: {}", attack))
    }
}

#[derive(Parser, Debug, Clone)]
#[command(author, version, about)]
struct Args {
    /// Retrieve values from raw file
    #[clap(short, long)]
    raw: Option<String>,
    /// Cipher: the message to uncipher.
    #[clap(short, long)]
    cipher: Option<IntegerArg>,
    /// Cipher file: the file to uncipher.
    #[clap(short = 'f', long)]
    cipherfile: Option<std::path::PathBuf>,
    /// Write unciphered data to a file. If many unciphered data are found, they will be written to files suffixed with _1, _2, ...
    #[clap(short = 'o', long)]
    outfile: Option<std::path::PathBuf>,
    /// Modulus.
    #[clap(short)]
    n: Option<IntegerArg>,
    /// Public exponent. Default: 65537
    #[clap(short, default_value = "65537")]
    e: IntegerArg,
    /// Prime number p.
    #[clap(short)]
    p: Option<IntegerArg>,
    /// Prime number q.
    #[clap(short)]
    q: Option<IntegerArg>,
    /// Private exponent.
    #[clap(short)]
    d: Option<IntegerArg>,
    /// Phi or Euler's totient function of n. (p-1)(q-1)
    #[clap(long)]
    phi: Option<IntegerArg>,
    /// dP or dmp1 CRT exponent. (d mod p-1)
    #[clap(long, alias = "dmp1")]
    dp: Option<IntegerArg>,
    /// dQ or dmq1 CRT exponent. (d mod q-1)
    #[clap(long, alias = "dmq1")]
    dq: Option<IntegerArg>,
    /// qInv or iqmp CRT coefficient. (q^-1 mod p)
    #[clap(long, alias = "iqmp")]
    qinv: Option<IntegerArg>,
    /// pInv or ipmq CRT coefficient. (p^-1 mod q)
    #[clap(long, alias = "ipmq")]
    pinv: Option<IntegerArg>,
    /// The sum of the two primes p and q.
    #[clap(long)]
    sum_pq: Option<IntegerArg>,
    /// Discrete logarithm attack. When c and e are swapped in the RSA encryption formula. (e^c mod n)
    #[clap(long, alias = "dislog")]
    dlog: bool,
    /// Public or private key file. (RSA, X509, OPENSSH in PEM and DER formats.)
    #[clap(short, long)]
    key: Option<String>,
    /// Private key password/passphrase if encrypted.
    #[clap(long)]
    password: Option<String>,
    /// Print the public key in PEM format.
    #[clap(long)]
    public: bool,
    /// Print the private key in PEM format.
    #[clap(long)]
    private: bool,
    /// Add a password/passphrase to the private key.
    #[clap(long, requires("private"))]
    addpassword: Option<String>,
    /// Print all the input parameters.
    #[clap(long)]
    showinputs: bool,
    /// Print the private RSA key variables n, e, p, q and d.
    #[clap(long)]
    dump: bool,
    /// Print the extended RSA key variables n, e, p, q, d, dP, dQ, pInv and qInv.
    #[clap(long)]
    dumpext: bool,
    /// Print all factors of n.
    #[clap(long)]
    factors: bool,
    /// Number of threads to use. Default: number of CPUs
    #[clap(short, long, default_value_t = num_cpus::get())]
    threads: usize,
    /// Specify attacks to run. Default: all. (e.g. --attacks ecm,wiener,sparse)
    #[clap(
        short,
        long,
        alias = "attacks",
        value_delimiter = ',',
        conflicts_with = "exclude"
    )]
    attack: Option<Vec<AttackArg>>,
    /// Specify attacks to exclude. Default: none. (e.g. --exclude ecm,wiener,sparse)
    #[clap(long, value_delimiter = ',', conflicts_with = "attack")]
    exclude: Option<Vec<AttackArg>>,
    /// List all available attacks.
    #[clap(long)]
    list: bool,
}

fn display_unciphered_data(uncipher: &Integer) {
    println!("Int = {uncipher}");
    println!("Hex = 0x{uncipher:02x}");
    if let Some(str) = integer_to_string(uncipher) {
        println!("String = \"{str}\"");
    } else {
        println!(
            "Bytes = b\"{}\"",
            display_bytes(&integer_to_bytes(uncipher))
        );
    }
}

fn display_or_output(
    uncipher: &Integer,
    outfile: &Option<std::path::PathBuf>,
) -> Result<(), Box<dyn std::error::Error>> {
    if let Some(outfile) = outfile {
        println!(
            "Write unciphered data to file: {}",
            outfile.to_string_lossy()
        );
        std::fs::write(outfile, integer_to_bytes(uncipher))?;
    } else {
        println!("Unciphered data:");
        display_unciphered_data(uncipher);
    }

    Ok(())
}

/// Add a suffix to the file path, before the extension.
fn suffix_path(path: &std::path::Path, suffix: &str) -> std::path::PathBuf {
    let mut path = path.to_path_buf();
    if let Some(ext) = path.extension() {
        let ext = ext.to_str().unwrap();
        let stem = path.file_stem().unwrap().to_str().unwrap();
        path.set_file_name(format!("{}{}.{}", stem, suffix, ext));
    } else {
        path.set_file_name(format!("{}{}", path.to_string_lossy(), suffix));
    }
    path
}

fn main() -> Result<(), MainError> {
    let pkg_name = env!("CARGO_PKG_NAME");
    let current_version = env!("CARGO_PKG_VERSION");
    let informer = update_informer::new(registry::Crates, pkg_name, current_version)
        .interval(Duration::from_secs(60 * 60));
    if let Ok(Some(new_version)) = informer.check_version() {
        eprintln!("A new release of {pkg_name} is available: v{current_version} -> {new_version}");
        eprintln!("You can update by running: cargo install {pkg_name}\n");
    }

    // Parse command line arguments
    let args = Args::parse();

    // List attacks
    if args.list {
        println!("Available attacks:");
        for attack in ATTACKS.iter() {
            println!("  {}", attack.name());
        }
        return Ok(());
    }

    // Read cipher
    let c = if args.cipher.is_some() {
        args.cipher.map(|n| n.0)
    } else if let Some(cipher_path) = args.cipherfile.as_ref() {
        match std::fs::read(cipher_path) {
            Ok(bytes) => Some(Integer::from_digits(&bytes, Order::Msf)),
            Err(err) => return Err(format!("{}: {err}", cipher_path.to_string_lossy()).into()),
        }
    } else {
        None
    };

    // Parse raw
    let mut stdin = io::stdin();
    let mut params = if !stdin.is_terminal() {
        // Piped input
        let mut raw = String::new();
        stdin.read_to_string(&mut raw)?;
        Parameters::from_raw(&raw)
    } else if let Some(raw) = args.raw.as_ref() {
        // rsacracker --raw
        let raw = std::fs::read_to_string(raw)?;
        Parameters::from_raw(&raw)
    } else {
        Parameters::default()
    };

    // Build parameters
    params += Parameters {
        c,
        n: args.n.map(|n| n.0),
        e: args.e.0,
        p: args.p.map(|n| n.0),
        q: args.q.map(|n| n.0),
        d: args.d.map(|n| n.0),
        phi: args.phi.map(|n| n.0),
        dp: args.dp.map(|n| n.0),
        dq: args.dq.map(|n| n.0),
        qinv: args.qinv.map(|n| n.0),
        pinv: args.pinv.map(|n| n.0),
        sum_pq: args.sum_pq.map(|n| n.0),
    };

    // Read public and private keys
    if let Some(key) = args.key {
        let bytes = std::fs::read(key)?;

        params += Parameters::from_private_key(&bytes, args.password.as_deref())
            .or_else(|| Parameters::from_public_key(&bytes))
            .ok_or("Invalid key")?;
    };

    if args.showinputs {
        println!("{params}");
        return Ok(());
    }

    // Check if discrete logarithm can be computed
    if args.dlog && params.c.is_none() {
        return Err("Discrete logarithm requires a cipher".into());
    }

    // Print public key
    if args.public {
        if let Some(n) = &params.n {
            let rsa = openssl::rsa::Rsa::from_public_components(
                openssl::bn::BigNum::from_slice(&n.to_digits(Order::Msf)).unwrap(),
                openssl::bn::BigNum::from_slice(&params.e.to_digits(Order::Msf)).unwrap(),
            )
            .or(Err("Invalid public key parameters"))?;
            let pem = rsa
                .public_key_to_pem()
                .map(|pem| String::from_utf8(pem).unwrap())
                .unwrap();
            print!("{pem}",);
            return Ok(());
        } else {
            return Err("No public key found".into());
        }
    }

    // Build attack list
    let attacks = args
        .attack
        // Collect attacks
        .map(|attacks| {
            attacks
                .into_iter()
                .map(|attack| attack.0)
                .collect::<Vec<_>>()
        })
        .unwrap_or(ATTACKS.to_vec())
        // Exclude attacks
        .into_iter()
        .filter(|attack| {
            args.exclude
                .as_ref()
                .map(|exclude| !exclude.iter().any(|a| a.0.name() == attack.name()))
                .unwrap_or(true)
        })
        // Sort attacks by kind and speed
        .sorted_by_key(|a| (a.kind(), a.speed()))
        .collect::<Vec<_>>();

    // Run attacks
    let res = rsacracker::run_specific_attacks_with_threads(&params, &attacks, args.threads);
    let solution = match res {
        Ok(solution) => solution,
        Err(partial_factors) => {
            // Print partial factors if any
            if let Some(partial_factors) = partial_factors {
                println!("Partial factors of n:");
                for (i, p) in partial_factors.as_vec().into_iter().enumerate() {
                    print!("p{} = {}", i + 1, p);
                    if p.is_probably_prime(100) != IsPrime::Yes {
                        print!(" (composite)");
                    }
                    println!();
                }
            }
            return Err("No attack succeeded".into());
        }
    };
    println!("Succeeded with attack: {}", solution.attack);

    // Print factors
    if args.factors {
        if let Some(private_key) = &solution.pk {
            println!("Factors of n:");
            if private_key.factors.len() == 2 {
                println!("p = {}", private_key.p());
                println!("q = {}", private_key.q());
            } else {
                for (i, p) in private_key.factors.as_vec().into_iter().enumerate() {
                    println!("p{} = {}", i + 1, p);
                }
            }
        } else {
            return Err("No private key found".into());
        }
        return Ok(());
    }

    // Print private key
    if args.private || args.dump || args.dumpext {
        if let Some(private_key) = &solution.pk {
            if args.private {
                print!("{}", private_key.to_pem(&args.addpassword).unwrap());
            }
            if args.dump || args.dumpext {
                println!("Private key:");
                println!("n = {}", private_key.n);
                println!("e = {}", private_key.e);

                // Print factors
                if private_key.factors.len() == 2 {
                    println!("p = {}", private_key.p());
                    println!("q = {}", private_key.q());
                } else {
                    for (i, p) in private_key.factors.as_vec().into_iter().enumerate() {
                        println!("p{} = {}", i + 1, p);
                    }
                }
                println!("d = {}", private_key.d);
            }
            if args.dumpext {
                println!("Extended private key:");
                println!("phi = {}", private_key.phi());
                println!("dP = {}", private_key.dp());
                println!("dQ = {}", private_key.dq());
                println!("pInv = {}", private_key.pinv());
                println!("qInv = {}", private_key.qinv());
            }
        } else {
            return Err("No private key found".into());
        }
        return Ok(());
    }

    // Print unciphered data
    if let Some(uncipher) = solution.m {
        display_or_output(&uncipher, &args.outfile)?;

        // Print discrete logarithm
        if args.dlog {
            if let Some(pk) = &solution.pk {
                println!("Compute discrete logarithm...");
                if let Ok(dlog) = discrete_log_with_factors(
                    &pk.n,
                    &params.c.unwrap(),
                    &pk.e,
                    &pk.factors.to_hash_map(),
                ) {
                    display_or_output(&dlog, &args.outfile.map(|f| suffix_path(&f, "_dlog")))?;
                } else {
                    return Err("Discrete logarithm failed".into());
                }
            } else {
                return Err("Discrete requires a private key".into());
            }
        }
    }
    // Print multiple unciphered data
    else if !solution.ms.is_empty() {
        println!("Multiple unciphered data found:");
        for (uncipher, i) in solution.ms.iter().zip(1..) {
            println!();
            display_or_output(
                uncipher,
                &args
                    .outfile
                    .clone()
                    .map(|f| suffix_path(&f, &format!("_{i}"))),
            )?;
        }
    }

    Ok(())
}