|
| 1 | +--- |
| 2 | +title: Rust fixme 1 |
| 3 | +date: 2025-08-15 |
| 4 | +categories: [Capture The Flags, PicoCTF] |
| 5 | +tags: [ctf, picoctf, general skills, writeups] |
| 6 | +description: PicoCTF Rust fixme 1 Challenge |
| 7 | +--- |
| 8 | + |
| 9 | + |
| 10 | +> Challenge description: |
| 11 | +> |
| 12 | +>Have you heard of Rust? Fix the syntax errors in this Rust file to print the flag! |
| 13 | +{: .prompt-info } |
| 14 | + |
| 15 | +Alrighty, this is a pretty simple challenge, find the syntax errors, and fix them. First you need to copy the project file over and then extract it. All you need to do to run the project is type `cargo run`. This will compile the Rust, and then attempt to run it. It will also tell you any issues we have with the code upon compilation. |
| 16 | + |
| 17 | +```terminal |
| 18 | +picoCTF/rust-fixme-1/fixme1 [📦 v0.1.0][🦀 v1.87.0] |
| 19 | +❯ cargo run |
| 20 | + Updating crates.io index |
| 21 | + Downloaded crossbeam-epoch v0.9.18 |
| 22 | + Downloaded xor_cryptor v1.2.3 |
| 23 | + Downloaded either v1.13.0 |
| 24 | + Downloaded crossbeam-deque v0.8.5 |
| 25 | + Downloaded rayon-core v1.12.1 |
| 26 | + Downloaded rayon v1.10.0 |
| 27 | + Downloaded 6 crates (337.7KiB) in 0.38s |
| 28 | + Compiling crossbeam-utils v0.8.20 |
| 29 | + Compiling rayon-core v1.12.1 |
| 30 | + Compiling either v1.13.0 |
| 31 | + Compiling crossbeam-epoch v0.9.18 |
| 32 | + Compiling crossbeam-deque v0.8.5 |
| 33 | + Compiling rayon v1.10.0 |
| 34 | + Compiling xor_cryptor v1.2.3 |
| 35 | + Compiling rust_proj v0.1.0 (/home/slavetomints/training/picoCTF/rust-fixme-1/fixme1) |
| 36 | +error: expected `;`, found keyword `let` |
| 37 | + --> src/main.rs:5:37 |
| 38 | + | |
| 39 | +5 | let key = String::from("CSUCKS") // How do we end statements in Rust? |
| 40 | + | ^ help: add `;` here |
| 41 | +... |
| 42 | +8 | let hex_values = ["41", "30", "20", "63", "4a", "45", "54", "76", "01", "1c", "7e", "59", "6... |
| 43 | + | --- unexpected token |
| 44 | +
|
| 45 | +error: argument never used |
| 46 | + --> src/main.rs:26:9 |
| 47 | + | |
| 48 | +25 | ":?", // How do we print out a variable in the println function? |
| 49 | + | ---- formatting specifier missing |
| 50 | +26 | String::from_utf8_lossy(&decrypted_buffer) |
| 51 | + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ argument never used |
| 52 | +
|
| 53 | +error[E0425]: cannot find value `ret` in this scope |
| 54 | + --> src/main.rs:18:9 |
| 55 | + | |
| 56 | +18 | ret; // How do we return in rust? |
| 57 | + | ^^^ help: a local variable with a similar name exists: `res` |
| 58 | +
|
| 59 | +For more information about this error, try `rustc --explain E0425`. |
| 60 | +error: could not compile `rust_proj` (bin "rust_proj") due to 3 previous errors |
| 61 | +``` |
| 62 | + |
| 63 | +Alright, looks like there was a missing `;`. It's time to look at the code. |
| 64 | + |
| 65 | +```rust |
| 66 | +use xor_cryptor::XORCryptor; |
| 67 | + |
| 68 | +fn main() { |
| 69 | + // Key for decryption |
| 70 | + let key = String::from("CSUCKS") // How do we end statements in Rust? |
| 71 | + |
| 72 | + // Encrypted flag values |
| 73 | + let hex_values = ["41", "30", "20", "63", "4a", "45", "54", "76", "01", "1c", "7e", "59", "63", "e1", "61", "25", "7f", "5a", "60", "50", "11", "38", "1f", "3a", "60", "e9", "62", "20", "0c", "e6", "50", "d3", "35"]; |
| 74 | + |
| 75 | + // Convert the hexadecimal strings to bytes and collect them into a vector |
| 76 | + let encrypted_buffer: Vec<u8> = hex_values.iter() |
| 77 | + .map(|&hex| u8::from_str_radix(hex, 16).unwrap()) |
| 78 | + .collect(); |
| 79 | + |
| 80 | + // Create decrpytion object |
| 81 | + let res = XORCryptor::new(&key); |
| 82 | + if res.is_err() { |
| 83 | + ret; // How do we return in rust? |
| 84 | + } |
| 85 | + let xrc = res.unwrap(); |
| 86 | + |
| 87 | + // Decrypt flag and print it out |
| 88 | + let decrypted_buffer = xrc.decrypt_vec(encrypted_buffer); |
| 89 | + println!( |
| 90 | + ":?", // How do we print out a variable in the println function? |
| 91 | + String::from_utf8_lossy(&decrypted_buffer) |
| 92 | + ); |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +Alright let's add the missing `;`. That should fix that issue. |
| 97 | + |
| 98 | +There's another simple issue. This seems to be an issue with the `ret;` line. This is because in order to return in Rust you need to use `return` keyword. Now that issue is fixed. |
| 99 | + |
| 100 | +Now for the last issue, The error message says `error: argument never used` if we look at the code snippet it's this: |
| 101 | + |
| 102 | +```rust |
| 103 | +let decrypted_buffer = xrc.decrypt_vec(encrypted_buffer); |
| 104 | +println!( |
| 105 | + ":?", // How do we print out a variable in the println function? |
| 106 | + String::from_utf8_lossy(&decrypted_buffer) |
| 107 | +); |
| 108 | +``` |
| 109 | + |
| 110 | +In order to use the variable, we need to add brackets to the string. We'll put them around the `:?` because those are special formatters to also print debug information, but you can just replace them as well. |
| 111 | + |
| 112 | +With all of these fixes, time to look at the new code: |
| 113 | + |
| 114 | +```rust |
| 115 | +use xor_cryptor::XORCryptor; |
| 116 | + |
| 117 | +fn main() { |
| 118 | + // Key for decryption |
| 119 | + let key = String::from("CSUCKS"); // How do we end statements in Rust? |
| 120 | + |
| 121 | + // Encrypted flag values |
| 122 | + let hex_values = ["41", "30", "20", "63", "4a", "45", "54", "76", "01", "1c", "7e", "59", "63", "e1", "61", "25", "7f", "5a", "60", "50", "11", "38", "1f", "3a", "60", "e9", "62", "20", "0c", "e6", "50", "d3", "35"]; |
| 123 | + |
| 124 | + // Convert the hexadecimal strings to bytes and collect them into a vector |
| 125 | + let encrypted_buffer: Vec<u8> = hex_values.iter() |
| 126 | + .map(|&hex| u8::from_str_radix(hex, 16).unwrap()) |
| 127 | + .collect(); |
| 128 | + |
| 129 | + // Create decrpytion object |
| 130 | + let res = XORCryptor::new(&key); |
| 131 | + if res.is_err() { |
| 132 | + return; // How do we return in rust? |
| 133 | + } |
| 134 | + let xrc = res.unwrap(); |
| 135 | + |
| 136 | + // Decrypt flag and print it out |
| 137 | + let decrypted_buffer = xrc.decrypt_vec(encrypted_buffer); |
| 138 | + println!( |
| 139 | + "{:?}", // How do we print out a variable in the println function? |
| 140 | + String::from_utf8_lossy(&decrypted_buffer) |
| 141 | + ); |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +If we run it again, we get the following: |
| 146 | + |
| 147 | +```terminal |
| 148 | +picoCTF/rust-fixme-1/fixme1 [📦 v0.1.0][🦀 v1.87.0] |
| 149 | +❯ cargo run |
| 150 | + Compiling rust_proj v0.1.0 (/home/slavetomints/training/picoCTF/rust-fixme-1/fixme1) |
| 151 | + Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.13s |
| 152 | + Running `target/debug/rust_proj` |
| 153 | +"picoCTF{4r3_y0u_4_ru$t4c30n_n0w?}" |
| 154 | +``` |
| 155 | + |
| 156 | +FLAG: `picoCTF{4r3_y0u_4_ru$t4c30n_n0w?}` |
0 commit comments