├── .github └── workflows │ └── rust.yml ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── Changelog.md ├── LICENSE ├── README.md └── src ├── applet.rs ├── b64app.rs ├── baseapp.rs ├── bgrepapp.rs ├── crcapp.rs ├── entropyapp.rs ├── escapeapp.rs ├── findsoapp.rs ├── flateapp.rs ├── hexapp.rs ├── main.rs ├── patternapp.rs ├── sliceapp.rs ├── timeapp.rs ├── urlapp.rs └── xorapp.rs /.github/workflows/rust.yml: -------------------------------------------------------------------------------- 1 | name: Rust 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ubuntu-latest 9 | steps: 10 | - uses: actions/checkout@v4 11 | - name: Build release 12 | run: cargo build --verbose --release 13 | - name: Run tests 14 | run: cargo test --bin rsbkb --verbose --release 15 | - uses: actions/upload-artifact@v4 16 | with: 17 | name: rsbkb-linux64-${{ github.sha }} 18 | path: target/release/rsbkb 19 | 20 | msrv: 21 | runs-on: ubuntu-latest 22 | steps: 23 | - uses: actions/checkout@v4 24 | - uses: taiki-e/install-action@cargo-hack 25 | - run: cargo hack check --rust-version --workspace --all-targets --ignore-private 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # These are backup files generated by rustfmt 7 | **/*.rs.bk 8 | 9 | *.swp 10 | /musl* 11 | launch.json 12 | tags 13 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "adler2" 7 | version = "2.0.0" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" 10 | 11 | [[package]] 12 | name = "aho-corasick" 13 | version = "1.1.3" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" 16 | dependencies = [ 17 | "memchr", 18 | ] 19 | 20 | [[package]] 21 | name = "anstream" 22 | version = "0.6.18" 23 | source = "registry+https://github.com/rust-lang/crates.io-index" 24 | checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" 25 | dependencies = [ 26 | "anstyle", 27 | "anstyle-parse", 28 | "anstyle-query", 29 | "anstyle-wincon", 30 | "colorchoice", 31 | "is_terminal_polyfill", 32 | "utf8parse", 33 | ] 34 | 35 | [[package]] 36 | name = "anstyle" 37 | version = "1.0.10" 38 | source = "registry+https://github.com/rust-lang/crates.io-index" 39 | checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" 40 | 41 | [[package]] 42 | name = "anstyle-parse" 43 | version = "0.2.6" 44 | source = "registry+https://github.com/rust-lang/crates.io-index" 45 | checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" 46 | dependencies = [ 47 | "utf8parse", 48 | ] 49 | 50 | [[package]] 51 | name = "anstyle-query" 52 | version = "1.1.2" 53 | source = "registry+https://github.com/rust-lang/crates.io-index" 54 | checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" 55 | dependencies = [ 56 | "windows-sys", 57 | ] 58 | 59 | [[package]] 60 | name = "anstyle-wincon" 61 | version = "3.0.7" 62 | source = "registry+https://github.com/rust-lang/crates.io-index" 63 | checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e" 64 | dependencies = [ 65 | "anstyle", 66 | "once_cell", 67 | "windows-sys", 68 | ] 69 | 70 | [[package]] 71 | name = "anyhow" 72 | version = "1.0.98" 73 | source = "registry+https://github.com/rust-lang/crates.io-index" 74 | checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487" 75 | 76 | [[package]] 77 | name = "assert_cmd" 78 | version = "2.0.13" 79 | source = "registry+https://github.com/rust-lang/crates.io-index" 80 | checksum = "00ad3f3a942eee60335ab4342358c161ee296829e0d16ff42fc1d6cb07815467" 81 | dependencies = [ 82 | "anstream", 83 | "anstyle", 84 | "bstr", 85 | "doc-comment", 86 | "predicates", 87 | "predicates-core", 88 | "predicates-tree", 89 | "wait-timeout", 90 | ] 91 | 92 | [[package]] 93 | name = "autocfg" 94 | version = "1.4.0" 95 | source = "registry+https://github.com/rust-lang/crates.io-index" 96 | checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" 97 | 98 | [[package]] 99 | name = "base64" 100 | version = "0.22.1" 101 | source = "registry+https://github.com/rust-lang/crates.io-index" 102 | checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" 103 | 104 | [[package]] 105 | name = "bitflags" 106 | version = "2.9.0" 107 | source = "registry+https://github.com/rust-lang/crates.io-index" 108 | checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" 109 | 110 | [[package]] 111 | name = "bstr" 112 | version = "1.10.0" 113 | source = "registry+https://github.com/rust-lang/crates.io-index" 114 | checksum = "40723b8fb387abc38f4f4a37c09073622e41dd12327033091ef8950659e6dc0c" 115 | dependencies = [ 116 | "memchr", 117 | "regex-automata", 118 | "serde", 119 | ] 120 | 121 | [[package]] 122 | name = "cfg-if" 123 | version = "1.0.0" 124 | source = "registry+https://github.com/rust-lang/crates.io-index" 125 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 126 | 127 | [[package]] 128 | name = "clap" 129 | version = "4.4.18" 130 | source = "registry+https://github.com/rust-lang/crates.io-index" 131 | checksum = "1e578d6ec4194633722ccf9544794b71b1385c3c027efe0c55db226fc880865c" 132 | dependencies = [ 133 | "clap_builder", 134 | "clap_derive", 135 | ] 136 | 137 | [[package]] 138 | name = "clap_builder" 139 | version = "4.4.18" 140 | source = "registry+https://github.com/rust-lang/crates.io-index" 141 | checksum = "4df4df40ec50c46000231c914968278b1eb05098cf8f1b3a518a95030e71d1c7" 142 | dependencies = [ 143 | "anstream", 144 | "anstyle", 145 | "clap_lex", 146 | "strsim", 147 | ] 148 | 149 | [[package]] 150 | name = "clap_derive" 151 | version = "4.4.7" 152 | source = "registry+https://github.com/rust-lang/crates.io-index" 153 | checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" 154 | dependencies = [ 155 | "heck", 156 | "proc-macro2", 157 | "quote", 158 | "syn", 159 | ] 160 | 161 | [[package]] 162 | name = "clap_lex" 163 | version = "0.6.0" 164 | source = "registry+https://github.com/rust-lang/crates.io-index" 165 | checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" 166 | 167 | [[package]] 168 | name = "colorchoice" 169 | version = "1.0.3" 170 | source = "registry+https://github.com/rust-lang/crates.io-index" 171 | checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" 172 | 173 | [[package]] 174 | name = "crc" 175 | version = "3.2.1" 176 | source = "registry+https://github.com/rust-lang/crates.io-index" 177 | checksum = "69e6e4d7b33a94f0991c26729976b10ebde1d34c3ee82408fb536164fa10d636" 178 | dependencies = [ 179 | "crc-catalog", 180 | ] 181 | 182 | [[package]] 183 | name = "crc-catalog" 184 | version = "2.4.0" 185 | source = "registry+https://github.com/rust-lang/crates.io-index" 186 | checksum = "19d374276b40fb8bbdee95aef7c7fa6b5316ec764510eb64b8dd0e2ed0d7e7f5" 187 | 188 | [[package]] 189 | name = "deranged" 190 | version = "0.4.0" 191 | source = "registry+https://github.com/rust-lang/crates.io-index" 192 | checksum = "9c9e6a11ca8224451684bc0d7d5a7adbf8f2fd6887261a1cfc3c0432f9d4068e" 193 | dependencies = [ 194 | "powerfmt", 195 | ] 196 | 197 | [[package]] 198 | name = "difflib" 199 | version = "0.4.0" 200 | source = "registry+https://github.com/rust-lang/crates.io-index" 201 | checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" 202 | 203 | [[package]] 204 | name = "doc-comment" 205 | version = "0.3.3" 206 | source = "registry+https://github.com/rust-lang/crates.io-index" 207 | checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" 208 | 209 | [[package]] 210 | name = "errno" 211 | version = "0.3.11" 212 | source = "registry+https://github.com/rust-lang/crates.io-index" 213 | checksum = "976dd42dc7e85965fe702eb8164f21f450704bdde31faefd6471dba214cb594e" 214 | dependencies = [ 215 | "libc", 216 | "windows-sys", 217 | ] 218 | 219 | [[package]] 220 | name = "fastrand" 221 | version = "2.3.0" 222 | source = "registry+https://github.com/rust-lang/crates.io-index" 223 | checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" 224 | 225 | [[package]] 226 | name = "float-cmp" 227 | version = "0.9.0" 228 | source = "registry+https://github.com/rust-lang/crates.io-index" 229 | checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4" 230 | dependencies = [ 231 | "num-traits", 232 | ] 233 | 234 | [[package]] 235 | name = "getrandom" 236 | version = "0.2.16" 237 | source = "registry+https://github.com/rust-lang/crates.io-index" 238 | checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592" 239 | dependencies = [ 240 | "cfg-if", 241 | "libc", 242 | "wasi 0.11.0+wasi-snapshot-preview1", 243 | ] 244 | 245 | [[package]] 246 | name = "getrandom" 247 | version = "0.3.2" 248 | source = "registry+https://github.com/rust-lang/crates.io-index" 249 | checksum = "73fea8450eea4bac3940448fb7ae50d91f034f941199fcd9d909a5a07aa455f0" 250 | dependencies = [ 251 | "cfg-if", 252 | "libc", 253 | "r-efi", 254 | "wasi 0.14.2+wasi-0.2.4", 255 | ] 256 | 257 | [[package]] 258 | name = "glob" 259 | version = "0.3.2" 260 | source = "registry+https://github.com/rust-lang/crates.io-index" 261 | checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2" 262 | 263 | [[package]] 264 | name = "goblin" 265 | version = "0.8.2" 266 | source = "registry+https://github.com/rust-lang/crates.io-index" 267 | checksum = "1b363a30c165f666402fe6a3024d3bec7ebc898f96a4a23bd1c99f8dbf3f4f47" 268 | dependencies = [ 269 | "log", 270 | "plain", 271 | "scroll", 272 | ] 273 | 274 | [[package]] 275 | name = "heck" 276 | version = "0.4.1" 277 | source = "registry+https://github.com/rust-lang/crates.io-index" 278 | checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" 279 | 280 | [[package]] 281 | name = "hex" 282 | version = "0.4.3" 283 | source = "registry+https://github.com/rust-lang/crates.io-index" 284 | checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" 285 | 286 | [[package]] 287 | name = "htmlentity" 288 | version = "1.3.2" 289 | source = "registry+https://github.com/rust-lang/crates.io-index" 290 | checksum = "dd54ae4f69adcc1a43637dcff230852832c3ad50df31a90e0cb5f001dd441359" 291 | dependencies = [ 292 | "anyhow", 293 | "lazy_static", 294 | "thiserror", 295 | ] 296 | 297 | [[package]] 298 | name = "is_terminal_polyfill" 299 | version = "1.70.1" 300 | source = "registry+https://github.com/rust-lang/crates.io-index" 301 | checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" 302 | 303 | [[package]] 304 | name = "itoa" 305 | version = "1.0.15" 306 | source = "registry+https://github.com/rust-lang/crates.io-index" 307 | checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" 308 | 309 | [[package]] 310 | name = "lazy_static" 311 | version = "1.5.0" 312 | source = "registry+https://github.com/rust-lang/crates.io-index" 313 | checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" 314 | 315 | [[package]] 316 | name = "libc" 317 | version = "0.2.172" 318 | source = "registry+https://github.com/rust-lang/crates.io-index" 319 | checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa" 320 | 321 | [[package]] 322 | name = "linux-raw-sys" 323 | version = "0.9.4" 324 | source = "registry+https://github.com/rust-lang/crates.io-index" 325 | checksum = "cd945864f07fe9f5371a27ad7b52a172b4b499999f1d97574c9fa68373937e12" 326 | 327 | [[package]] 328 | name = "log" 329 | version = "0.4.27" 330 | source = "registry+https://github.com/rust-lang/crates.io-index" 331 | checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" 332 | 333 | [[package]] 334 | name = "memchr" 335 | version = "2.7.4" 336 | source = "registry+https://github.com/rust-lang/crates.io-index" 337 | checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" 338 | 339 | [[package]] 340 | name = "memmap2" 341 | version = "0.9.5" 342 | source = "registry+https://github.com/rust-lang/crates.io-index" 343 | checksum = "fd3f7eed9d3848f8b98834af67102b720745c4ec028fcd0aa0239277e7de374f" 344 | dependencies = [ 345 | "libc", 346 | ] 347 | 348 | [[package]] 349 | name = "miniz_oxide" 350 | version = "0.8.8" 351 | source = "registry+https://github.com/rust-lang/crates.io-index" 352 | checksum = "3be647b768db090acb35d5ec5db2b0e1f1de11133ca123b9eacf5137868f892a" 353 | dependencies = [ 354 | "adler2", 355 | ] 356 | 357 | [[package]] 358 | name = "normalize-line-endings" 359 | version = "0.3.0" 360 | source = "registry+https://github.com/rust-lang/crates.io-index" 361 | checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" 362 | 363 | [[package]] 364 | name = "num-bigint" 365 | version = "0.4.6" 366 | source = "registry+https://github.com/rust-lang/crates.io-index" 367 | checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" 368 | dependencies = [ 369 | "num-integer", 370 | "num-traits", 371 | ] 372 | 373 | [[package]] 374 | name = "num-conv" 375 | version = "0.1.0" 376 | source = "registry+https://github.com/rust-lang/crates.io-index" 377 | checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" 378 | 379 | [[package]] 380 | name = "num-integer" 381 | version = "0.1.46" 382 | source = "registry+https://github.com/rust-lang/crates.io-index" 383 | checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" 384 | dependencies = [ 385 | "num-traits", 386 | ] 387 | 388 | [[package]] 389 | name = "num-traits" 390 | version = "0.2.19" 391 | source = "registry+https://github.com/rust-lang/crates.io-index" 392 | checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" 393 | dependencies = [ 394 | "autocfg", 395 | ] 396 | 397 | [[package]] 398 | name = "num_threads" 399 | version = "0.1.7" 400 | source = "registry+https://github.com/rust-lang/crates.io-index" 401 | checksum = "5c7398b9c8b70908f6371f47ed36737907c87c52af34c268fed0bf0ceb92ead9" 402 | dependencies = [ 403 | "libc", 404 | ] 405 | 406 | [[package]] 407 | name = "once_cell" 408 | version = "1.21.3" 409 | source = "registry+https://github.com/rust-lang/crates.io-index" 410 | checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" 411 | 412 | [[package]] 413 | name = "percent-encoding" 414 | version = "2.3.1" 415 | source = "registry+https://github.com/rust-lang/crates.io-index" 416 | checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" 417 | 418 | [[package]] 419 | name = "plain" 420 | version = "0.2.3" 421 | source = "registry+https://github.com/rust-lang/crates.io-index" 422 | checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" 423 | 424 | [[package]] 425 | name = "powerfmt" 426 | version = "0.2.0" 427 | source = "registry+https://github.com/rust-lang/crates.io-index" 428 | checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" 429 | 430 | [[package]] 431 | name = "ppv-lite86" 432 | version = "0.2.21" 433 | source = "registry+https://github.com/rust-lang/crates.io-index" 434 | checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" 435 | dependencies = [ 436 | "zerocopy", 437 | ] 438 | 439 | [[package]] 440 | name = "predicates" 441 | version = "3.1.0" 442 | source = "registry+https://github.com/rust-lang/crates.io-index" 443 | checksum = "68b87bfd4605926cdfefc1c3b5f8fe560e3feca9d5552cf68c466d3d8236c7e8" 444 | dependencies = [ 445 | "anstyle", 446 | "difflib", 447 | "float-cmp", 448 | "normalize-line-endings", 449 | "predicates-core", 450 | "regex", 451 | ] 452 | 453 | [[package]] 454 | name = "predicates-core" 455 | version = "1.0.6" 456 | source = "registry+https://github.com/rust-lang/crates.io-index" 457 | checksum = "b794032607612e7abeb4db69adb4e33590fa6cf1149e95fd7cb00e634b92f174" 458 | 459 | [[package]] 460 | name = "predicates-tree" 461 | version = "1.0.9" 462 | source = "registry+https://github.com/rust-lang/crates.io-index" 463 | checksum = "368ba315fb8c5052ab692e68a0eefec6ec57b23a36959c14496f0b0df2c0cecf" 464 | dependencies = [ 465 | "predicates-core", 466 | "termtree", 467 | ] 468 | 469 | [[package]] 470 | name = "proc-macro2" 471 | version = "1.0.95" 472 | source = "registry+https://github.com/rust-lang/crates.io-index" 473 | checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" 474 | dependencies = [ 475 | "unicode-ident", 476 | ] 477 | 478 | [[package]] 479 | name = "quote" 480 | version = "1.0.40" 481 | source = "registry+https://github.com/rust-lang/crates.io-index" 482 | checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" 483 | dependencies = [ 484 | "proc-macro2", 485 | ] 486 | 487 | [[package]] 488 | name = "r-efi" 489 | version = "5.2.0" 490 | source = "registry+https://github.com/rust-lang/crates.io-index" 491 | checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5" 492 | 493 | [[package]] 494 | name = "rand" 495 | version = "0.8.5" 496 | source = "registry+https://github.com/rust-lang/crates.io-index" 497 | checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" 498 | dependencies = [ 499 | "libc", 500 | "rand_chacha", 501 | "rand_core", 502 | ] 503 | 504 | [[package]] 505 | name = "rand_chacha" 506 | version = "0.3.1" 507 | source = "registry+https://github.com/rust-lang/crates.io-index" 508 | checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" 509 | dependencies = [ 510 | "ppv-lite86", 511 | "rand_core", 512 | ] 513 | 514 | [[package]] 515 | name = "rand_core" 516 | version = "0.6.4" 517 | source = "registry+https://github.com/rust-lang/crates.io-index" 518 | checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" 519 | dependencies = [ 520 | "getrandom 0.2.16", 521 | ] 522 | 523 | [[package]] 524 | name = "regex" 525 | version = "1.11.1" 526 | source = "registry+https://github.com/rust-lang/crates.io-index" 527 | checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" 528 | dependencies = [ 529 | "aho-corasick", 530 | "memchr", 531 | "regex-automata", 532 | "regex-syntax", 533 | ] 534 | 535 | [[package]] 536 | name = "regex-automata" 537 | version = "0.4.9" 538 | source = "registry+https://github.com/rust-lang/crates.io-index" 539 | checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" 540 | dependencies = [ 541 | "aho-corasick", 542 | "memchr", 543 | "regex-syntax", 544 | ] 545 | 546 | [[package]] 547 | name = "regex-syntax" 548 | version = "0.8.5" 549 | source = "registry+https://github.com/rust-lang/crates.io-index" 550 | checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" 551 | 552 | [[package]] 553 | name = "rsbkb" 554 | version = "1.8.1" 555 | dependencies = [ 556 | "anyhow", 557 | "assert_cmd", 558 | "base64", 559 | "clap", 560 | "crc", 561 | "glob", 562 | "goblin", 563 | "hex", 564 | "htmlentity", 565 | "memmap2", 566 | "miniz_oxide", 567 | "num-bigint", 568 | "num-traits", 569 | "percent-encoding", 570 | "predicates", 571 | "rand", 572 | "regex", 573 | "tempfile", 574 | "time", 575 | ] 576 | 577 | [[package]] 578 | name = "rustix" 579 | version = "1.0.5" 580 | source = "registry+https://github.com/rust-lang/crates.io-index" 581 | checksum = "d97817398dd4bb2e6da002002db259209759911da105da92bec29ccb12cf58bf" 582 | dependencies = [ 583 | "bitflags", 584 | "errno", 585 | "libc", 586 | "linux-raw-sys", 587 | "windows-sys", 588 | ] 589 | 590 | [[package]] 591 | name = "scroll" 592 | version = "0.12.0" 593 | source = "registry+https://github.com/rust-lang/crates.io-index" 594 | checksum = "6ab8598aa408498679922eff7fa985c25d58a90771bd6be794434c5277eab1a6" 595 | dependencies = [ 596 | "scroll_derive", 597 | ] 598 | 599 | [[package]] 600 | name = "scroll_derive" 601 | version = "0.12.1" 602 | source = "registry+https://github.com/rust-lang/crates.io-index" 603 | checksum = "1783eabc414609e28a5ba76aee5ddd52199f7107a0b24c2e9746a1ecc34a683d" 604 | dependencies = [ 605 | "proc-macro2", 606 | "quote", 607 | "syn", 608 | ] 609 | 610 | [[package]] 611 | name = "serde" 612 | version = "1.0.219" 613 | source = "registry+https://github.com/rust-lang/crates.io-index" 614 | checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" 615 | dependencies = [ 616 | "serde_derive", 617 | ] 618 | 619 | [[package]] 620 | name = "serde_derive" 621 | version = "1.0.219" 622 | source = "registry+https://github.com/rust-lang/crates.io-index" 623 | checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" 624 | dependencies = [ 625 | "proc-macro2", 626 | "quote", 627 | "syn", 628 | ] 629 | 630 | [[package]] 631 | name = "strsim" 632 | version = "0.10.0" 633 | source = "registry+https://github.com/rust-lang/crates.io-index" 634 | checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" 635 | 636 | [[package]] 637 | name = "syn" 638 | version = "2.0.101" 639 | source = "registry+https://github.com/rust-lang/crates.io-index" 640 | checksum = "8ce2b7fc941b3a24138a0a7cf8e858bfc6a992e7978a068a5c760deb0ed43caf" 641 | dependencies = [ 642 | "proc-macro2", 643 | "quote", 644 | "unicode-ident", 645 | ] 646 | 647 | [[package]] 648 | name = "tempfile" 649 | version = "3.19.1" 650 | source = "registry+https://github.com/rust-lang/crates.io-index" 651 | checksum = "7437ac7763b9b123ccf33c338a5cc1bac6f69b45a136c19bdd8a65e3916435bf" 652 | dependencies = [ 653 | "fastrand", 654 | "getrandom 0.3.2", 655 | "once_cell", 656 | "rustix", 657 | "windows-sys", 658 | ] 659 | 660 | [[package]] 661 | name = "termtree" 662 | version = "0.4.1" 663 | source = "registry+https://github.com/rust-lang/crates.io-index" 664 | checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" 665 | 666 | [[package]] 667 | name = "thiserror" 668 | version = "1.0.69" 669 | source = "registry+https://github.com/rust-lang/crates.io-index" 670 | checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" 671 | dependencies = [ 672 | "thiserror-impl", 673 | ] 674 | 675 | [[package]] 676 | name = "thiserror-impl" 677 | version = "1.0.69" 678 | source = "registry+https://github.com/rust-lang/crates.io-index" 679 | checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" 680 | dependencies = [ 681 | "proc-macro2", 682 | "quote", 683 | "syn", 684 | ] 685 | 686 | [[package]] 687 | name = "time" 688 | version = "0.3.41" 689 | source = "registry+https://github.com/rust-lang/crates.io-index" 690 | checksum = "8a7619e19bc266e0f9c5e6686659d394bc57973859340060a69221e57dbc0c40" 691 | dependencies = [ 692 | "deranged", 693 | "itoa", 694 | "libc", 695 | "num-conv", 696 | "num_threads", 697 | "powerfmt", 698 | "serde", 699 | "time-core", 700 | "time-macros", 701 | ] 702 | 703 | [[package]] 704 | name = "time-core" 705 | version = "0.1.4" 706 | source = "registry+https://github.com/rust-lang/crates.io-index" 707 | checksum = "c9e9a38711f559d9e3ce1cdb06dd7c5b8ea546bc90052da6d06bb76da74bb07c" 708 | 709 | [[package]] 710 | name = "time-macros" 711 | version = "0.2.22" 712 | source = "registry+https://github.com/rust-lang/crates.io-index" 713 | checksum = "3526739392ec93fd8b359c8e98514cb3e8e021beb4e5f597b00a0221f8ed8a49" 714 | dependencies = [ 715 | "num-conv", 716 | "time-core", 717 | ] 718 | 719 | [[package]] 720 | name = "unicode-ident" 721 | version = "1.0.18" 722 | source = "registry+https://github.com/rust-lang/crates.io-index" 723 | checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" 724 | 725 | [[package]] 726 | name = "utf8parse" 727 | version = "0.2.2" 728 | source = "registry+https://github.com/rust-lang/crates.io-index" 729 | checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" 730 | 731 | [[package]] 732 | name = "wait-timeout" 733 | version = "0.2.1" 734 | source = "registry+https://github.com/rust-lang/crates.io-index" 735 | checksum = "09ac3b126d3914f9849036f826e054cbabdc8519970b8998ddaf3b5bd3c65f11" 736 | dependencies = [ 737 | "libc", 738 | ] 739 | 740 | [[package]] 741 | name = "wasi" 742 | version = "0.11.0+wasi-snapshot-preview1" 743 | source = "registry+https://github.com/rust-lang/crates.io-index" 744 | checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" 745 | 746 | [[package]] 747 | name = "wasi" 748 | version = "0.14.2+wasi-0.2.4" 749 | source = "registry+https://github.com/rust-lang/crates.io-index" 750 | checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3" 751 | dependencies = [ 752 | "wit-bindgen-rt", 753 | ] 754 | 755 | [[package]] 756 | name = "windows-sys" 757 | version = "0.59.0" 758 | source = "registry+https://github.com/rust-lang/crates.io-index" 759 | checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" 760 | dependencies = [ 761 | "windows-targets", 762 | ] 763 | 764 | [[package]] 765 | name = "windows-targets" 766 | version = "0.52.6" 767 | source = "registry+https://github.com/rust-lang/crates.io-index" 768 | checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" 769 | dependencies = [ 770 | "windows_aarch64_gnullvm", 771 | "windows_aarch64_msvc", 772 | "windows_i686_gnu", 773 | "windows_i686_gnullvm", 774 | "windows_i686_msvc", 775 | "windows_x86_64_gnu", 776 | "windows_x86_64_gnullvm", 777 | "windows_x86_64_msvc", 778 | ] 779 | 780 | [[package]] 781 | name = "windows_aarch64_gnullvm" 782 | version = "0.52.6" 783 | source = "registry+https://github.com/rust-lang/crates.io-index" 784 | checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" 785 | 786 | [[package]] 787 | name = "windows_aarch64_msvc" 788 | version = "0.52.6" 789 | source = "registry+https://github.com/rust-lang/crates.io-index" 790 | checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" 791 | 792 | [[package]] 793 | name = "windows_i686_gnu" 794 | version = "0.52.6" 795 | source = "registry+https://github.com/rust-lang/crates.io-index" 796 | checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" 797 | 798 | [[package]] 799 | name = "windows_i686_gnullvm" 800 | version = "0.52.6" 801 | source = "registry+https://github.com/rust-lang/crates.io-index" 802 | checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" 803 | 804 | [[package]] 805 | name = "windows_i686_msvc" 806 | version = "0.52.6" 807 | source = "registry+https://github.com/rust-lang/crates.io-index" 808 | checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" 809 | 810 | [[package]] 811 | name = "windows_x86_64_gnu" 812 | version = "0.52.6" 813 | source = "registry+https://github.com/rust-lang/crates.io-index" 814 | checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" 815 | 816 | [[package]] 817 | name = "windows_x86_64_gnullvm" 818 | version = "0.52.6" 819 | source = "registry+https://github.com/rust-lang/crates.io-index" 820 | checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" 821 | 822 | [[package]] 823 | name = "windows_x86_64_msvc" 824 | version = "0.52.6" 825 | source = "registry+https://github.com/rust-lang/crates.io-index" 826 | checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" 827 | 828 | [[package]] 829 | name = "wit-bindgen-rt" 830 | version = "0.39.0" 831 | source = "registry+https://github.com/rust-lang/crates.io-index" 832 | checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" 833 | dependencies = [ 834 | "bitflags", 835 | ] 836 | 837 | [[package]] 838 | name = "zerocopy" 839 | version = "0.8.25" 840 | source = "registry+https://github.com/rust-lang/crates.io-index" 841 | checksum = "a1702d9583232ddb9174e01bb7c15a2ab8fb1bc6f227aa1233858c351a3ba0cb" 842 | dependencies = [ 843 | "zerocopy-derive", 844 | ] 845 | 846 | [[package]] 847 | name = "zerocopy-derive" 848 | version = "0.8.25" 849 | source = "registry+https://github.com/rust-lang/crates.io-index" 850 | checksum = "28a6e20d751156648aa063f3800b706ee209a32c0b4d9f24be3d980b01be55ef" 851 | dependencies = [ 852 | "proc-macro2", 853 | "quote", 854 | "syn", 855 | ] 856 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rsbkb" 3 | version = "1.8.1" 4 | authors = ["Raphaël Rigo "] 5 | edition = "2018" 6 | rust-version = "1.70" 7 | description = "rsbkb has multiple tools which are designed to be called directly (through symlinks). This allows various operations on data to be chained easily like CyberChef but through pipes." 8 | license = "GPL-3.0-only" 9 | readme = "README.md" 10 | repository = "https://github.com/trou/rsbkb" 11 | keywords = ["encode", "decode", "hack"] 12 | categories = ["command-line-utilities"] 13 | 14 | [profile.release] 15 | overflow-checks = true 16 | lto = true # Enable link-time optimization 17 | codegen-units = 1 # Reduce number of codegen units to increase optimizations 18 | 19 | [profile.release-small] 20 | inherits = "release" 21 | overflow-checks = true 22 | opt-level = 'z' # Optimize for size 23 | lto = true # Enable link-time optimization 24 | codegen-units = 1 # Reduce number of codegen units to increase optimizations 25 | panic = 'abort' # Abort on panic 26 | strip = true # Strip symbols from binary 27 | 28 | [dependencies] 29 | hex = "0.4" 30 | base64 = "0.22" 31 | percent-encoding = "2.2.0" 32 | clap = {version = "~4.4", features = ["derive"]} 33 | crc = "3" 34 | time = {version = "0.3", features=["local-offset", "formatting"]} 35 | regex = {version = "1.10", default-features=false, features=["std", "perf"]} 36 | memmap2 = "0.9" 37 | goblin = {version = "0.8", features=["elf32", "elf64", "endian_fd"]} 38 | anyhow = "1.0.86" 39 | miniz_oxide = "0.8.0" 40 | glob = "0.3.1" 41 | num-bigint = "0.4.6" 42 | num-traits = "0.2.19" 43 | htmlentity = "1.3.2" 44 | 45 | [dev-dependencies] 46 | tempfile = "3.3.0" 47 | rand = "0.8.5" 48 | assert_cmd = {version = "=2.0.13", features = ["color-auto"]} 49 | predicates = "3.0.4" 50 | 51 | [[bin]] 52 | name = "rsbkb" 53 | path = "src/main.rs" 54 | -------------------------------------------------------------------------------- /Changelog.md: -------------------------------------------------------------------------------- 1 | * 2025-03-19: v1.8.1: 2 | * `(un)escape`: add HTML entities support 3 | * `urlenc`: fix encoding of "0xFF", thanks sha5010! 4 | * 2025-01-03: v1.8, the "Happy New Year" release: 5 | * `findso`: better help, fixes. `-s` flag to skip symlinks. 6 | * `tsdec`: add Chrome/WebKit timestamp support. 7 | * `symlink` command to create symlinks for all applets. 8 | * 2024-11-11: v1.7: 9 | * new `escape` and `unescape` applets for easy string (un)escaping 10 | * `base`: default to hex output for decimal input 11 | * improve code documentation 12 | * 2024-09-14: v1.6: 13 | * new `base` applet for easy radix conversion 14 | * crate published for easy install 15 | * 2024-08-15: v1.5.1: small fix for `findso` 16 | * 2024-08-14: v1.5: 17 | * `slice` now supports specifying `end` relative to end of file 18 | * `findso`: 19 | * add `-a` to look for the given function in all `.so` files in specified paths 20 | * improve `ld.so.conf` parser to handle `include` directives 21 | * 2024-05-20: v1.4: 22 | * `crc` can now compute all known types, alg list updated. 23 | * Add `--recursive` option to `bgrep`, thanks @marius851000!. 24 | * `urlenc`: 25 | * Add `--exclude-chars` 26 | * Add `-u` to use RFC3986 27 | * Add `--custom` to specify custom list of chars to encode 28 | * 2024-01-24: v1.3: `slice` now supports non-seekable files. `tsdec` verbose mode. `bgrep` multiple args. Tests now cover real CLI invocations. 29 | * 2023-09-26: v1.2.1: fix CLI flags parsing, add skipping of invalid files in findso 30 | * 2023-08-13: v1.2: inflate/deflate applet ; base64 update: support custom alphabet ; global: check if given value is potentially a file and warn user 31 | * 2023-01-08: v1.1: better usage info and error reporting 32 | * 2022-08-20: v1.0: `findso` applet, clap update 33 | * 2022-06-25: v0.9: `time` crate update, basic implementation of binary grep in `bgrep` 34 | * 2022-05-01: v0.8: handle most errors gracefully, add more CRC algorithms (`crc` command), negative offset for `slice` 35 | * 2020-09-09: v0.6: add buffer overflow pattern tools, timestamp decoding 36 | * 2020-08-13: v0.5: code rewrite, merge `xorf` into `xor` and `unhex2` into `unhex`. `slice` now supports relative `end`. 37 | * 2020-08-06: v0.3: URL-safe support for base64, cleaner argument parsing 38 | * 2020-04-12: v0.2: `xorf` command to xor files 39 | * 2019-09-30: v0.1: Initial release 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rsbkb (Rust blackbag) 2 | 3 | ## What is it? 4 | 5 | `rsbkb` has multiple tools which are designed to be called directly (through 6 | symlinks, like `busybox`). This allows various operations on data to be chained 7 | easily like CyberChef but through pipes. 8 | 9 | It also includes various practical tools like `entropy`, a binary `grep`, a 10 | timestamp decoder, etc. 11 | 12 | 13 | ## Quick start 14 | 15 | 1. install with `cargo install rsbkb` or download the binary from the [releases](https://github.com/trou/rsbkb/releases) page 16 | 2. run `rsbkb` to list applets 17 | 3. run `rsbkb help ` to learn more 18 | 4. optionally create symlinks to call applets directly: `rsbkb symlink` 19 | 20 | ## Examples 21 | 22 | Read 10 bytes from `/etc/passwd` starting at offset `0x2f`, then `xor` with 23 | `0xF2`, encode it in URL-safe base64 and finally URL encode it: 24 | 25 | ``` 26 | $ slice /etc/passwd 0x2f +10 | xor -x f2 | b64 -u | urlenc 27 | l5%2DdnMjdh4GA3Q%3D%3D 28 | ``` 29 | 30 | Various examples: 31 | 32 | ``` 33 | $ unhex 4141:4141 34 | AA:AA 35 | $ echo -n'4141:4141' | unhex 36 | AA:AA 37 | $ crc32 '41 41 41 32' 38 | e60ce752 39 | $ echo -n '41 41 41 32' | crc32 40 | e60ce752 41 | $ echo test | b64 | urlenc 42 | dGVzdAo%3D 43 | $ tsdec 146424672000234122 44 | 2065-01-01T00:00:00.0234122Z 45 | $ tsdec 0 46 | 1970-01-01T00:00:00Z 47 | $ rsbkb bofpatt 60 48 | Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9 49 | $ rsbkb bofpattoff -b 0x41623841 50 | Decoded pattern: Ab8A (big endian: true) 51 | Offset: 54 (mod 20280) / 0x36 52 | $ echo -n tototutu | rsbkb entropy 53 | 0.188 54 | $ bgrep -x 454c460201 /bin/ls 55 | 0x1 56 | $ bgrep "\x45\x4c..\x01" /bin/ls 57 | 0x1 58 | $ findso -p /lib/x86_64-linux-gnu/ -r memcpy /bin/ls 59 | /lib/x86_64-linux-gnu/libc.so.6 60 | $ findso -l /etc/ld.so.conf -a memcpy 61 | /lib/i386-linux-gnu/libc.so.6 62 | [...] 63 | $ base 0x14 64 | 20 65 | $ echo "echo 'test'" | escape -t bash-single 66 | 'echo '"'"'test'"'"'' 67 | ``` 68 | 69 | ## How to use 70 | 71 | ### Build it / Get it 72 | 73 | * `cargo build --release` 74 | * `cargo install rsbkb` 75 | * get the binary from the [releases page](https://github.com/trou/rsbkb/releases). 76 | * get the latest artifact from the [Actions](https://github.com/trou/rsbkb/actions) page. 77 | 78 | ### Usage 79 | 80 | 81 | * Almost all tools take values as an argument on the command line or if not present, read from `stdin` 82 | * Tool name can be specified on the command line `rsbkb TOOL` 83 | * Or can be called busybox-style: `ln -s rsbkb unhex ; unhex 4142`. Create symlinks with: 84 | 85 | ``` 86 | rsbkb symlink 87 | ``` 88 | 89 | ## Included tools 90 | 91 | * `hex`: hex encode 92 | * `unhex`: decode hex data (either in the middle of arbitrary data, or strictly) 93 | * `b64`: base64 encode (use `-u` or `--URL` for URL-safe b64) 94 | * `d64`: base64 decode (use `-u` or `--URL` for URL-safe b64) 95 | * `urlenc`: url encode (see `--help` for advanced options) 96 | * `urldec`: url decode 97 | * `xor`: xor (use `-x` to specify the key, in hex, `-f` to specify a file) 98 | * `crc`: all CRC algorithms implemented in the [Crc](https://docs.rs/crc/3.2.1/crc/) crate 99 | * `crc16`: CRC-16 100 | * `crc32`: CRC-32 101 | * `bofpatt` / `boffpattoff`: buffer overflow pattern generator / offset calculator 102 | * `tsdec`: decode various timestamps (Epoch with different resolutions, Windows FILETIME) 103 | * `slice`: take a "slice" of a file (like `dd`): 104 | * `slice input_file 10` will output `input_file` from offset 10 on `stdout` 105 | * `slice input_file 0x10 0x20` will do the same from 0x10 to 0x20 (excluded) 106 | * `slice input_file 0x10 +0xFF` will copy `0xFF` bytes starting at `0x10` 107 | * `slice input_file -0x10` will the last 0x10 bytes from `input_file` 108 | * `entropy`: entropy of a file 109 | * `bgrep`: simple binary grep 110 | * `findso`: find which ELF shared library (.so) exports a given name/function 111 | * `inflate` and `deflate`: raw inflate/deflate compression, fault tolerant and with optional Zlib header support 112 | * `base`: easy radix conversion of big integers 113 | * `escape`: backslash-escape special characters in strings (generic, single quote, shell, bash, bash single) 114 | * `unescape`: unescape `\` escaped chars in strings 115 | 116 | ### Getting help 117 | 118 | ```console 119 | $ rsbkb help 120 | rsbkb 1.8.0 (Rust BlackBag) - by Raphaël Rigo 121 | 122 | Usage: rsbkb [APPLET] 123 | 124 | APPLETS: 125 | list list applets 126 | hex hex encode 127 | unhex hex decode 128 | urlenc URL encode 129 | urldec URL decode 130 | crc16 compute CRC-16 131 | crc32 compute CRC-32 132 | crc flexible CRC computation 133 | b64 base64 encode 134 | d64 base64 decode 135 | bofpattoff buffer overflow pattern offset finder 136 | bofpatt buffer overflow pattern generator 137 | xor xor value 138 | entropy compute file entropy 139 | slice cut slices from file or stdin 140 | bgrep binary grep 141 | findso find which .so implements a given function 142 | tsdec timestamp decoder 143 | deflate (raw) deflate compression 144 | inflate (raw) inflate decompression 145 | base convert integer between different bases 146 | escape backslash-escape input strings 147 | unescape (backslash) unescape input strings 148 | help Print this message or the help of the given subcommand(s) 149 | 150 | $ rsbkb help slice 151 | cut slices from file or stdin 152 | 153 | Usage: rsbkb slice [end] 154 | 155 | Arguments: 156 | file to slice, - for stdin 157 | start of slice, relative to end of file if negative 158 | [end] end of slice: absolute, relative to if prefixed with +, relative to end of file if negative 159 | ``` 160 | 161 | ## Credits and heritage 162 | 163 | This is a Rust reimplementation of some tools found in emonti's 164 | [rbkb](https://github.com/emonti/rbkb), itself a Ruby reimplementation of 165 | Matasano's BlackBag. 166 | -------------------------------------------------------------------------------- /src/applet.rs: -------------------------------------------------------------------------------- 1 | #![allow(clippy::new_ret_no_self)] 2 | use anyhow::{Context, Result}; 3 | use clap::{arg, Command}; 4 | 5 | pub trait Applet { 6 | /// The string which will define the subcommand. 7 | fn command(&self) -> &'static str; 8 | 9 | /// Simple description of the applet. 10 | fn description(&self) -> &'static str; 11 | 12 | /// Overload to return "false" if the applet directly 13 | /// outputs data to stdout. 14 | fn returns_data(&self) -> bool { 15 | true 16 | } 17 | 18 | /// Receives the arguments as understood by `clap` and builds the resulting `Applet`. 19 | fn parse_args(&self, args: &clap::ArgMatches) -> Result>; 20 | 21 | /// Define the applet's arguments. 22 | fn clap_command(&self) -> Command { 23 | Command::new(self.command()) 24 | .about(self.description()) 25 | .arg(arg!([value] "input value, reads from stdin in not present")) 26 | } 27 | 28 | /// By default, applets accept the input as: 29 | /// - an argument, named "value" 30 | /// - stdin, if value is not supplied 31 | /// 32 | /// Applets can overload this method to have a different behaviour 33 | /// (for example if they have more args). 34 | fn arg_or_stdin(&self) -> Option<&'static str> { 35 | Some("value") 36 | } 37 | 38 | /// Called by `main` to process the data in `val` 39 | fn process(&self, val: Vec) -> Result>; 40 | 41 | /* No error wrapping to make it easier to test */ 42 | #[cfg(test)] 43 | fn process_test(&self, val: Vec) -> Vec { 44 | self.process(val).unwrap() 45 | } 46 | 47 | fn new() -> Box 48 | where 49 | Self: Sized; 50 | } 51 | 52 | /* Helper to trim whitespace 53 | * Note: trim_ascii does this in Rust 1.80 54 | * */ 55 | pub trait SliceExt { 56 | fn trim(&self) -> &Self; 57 | } 58 | 59 | impl SliceExt for [u8] { 60 | fn trim(&self) -> &[u8] { 61 | const fn is_not_whitespace(c: &u8) -> bool { 62 | !c.is_ascii_whitespace() 63 | } 64 | 65 | if let Some(first) = self.iter().position(is_not_whitespace) { 66 | if let Some(last) = self.iter().rposition(is_not_whitespace) { 67 | &self[first..=last] 68 | } else { 69 | unreachable!(); 70 | } 71 | } else { 72 | &[] 73 | } 74 | } 75 | } 76 | 77 | // We cannot use a default implementation for trait as from_str_radix is not defined 78 | // in a trait but as a function of for most base types 79 | pub trait FromStrWithRadix { 80 | fn from_str_with_radix(s: &str) -> Result 81 | where 82 | Self: Sized; 83 | } 84 | 85 | // Helper to add "from_str_with_radix" to given types 86 | macro_rules! from_str_with_radix_for_types { 87 | ($($x:ident),* ) => 88 | { 89 | $( 90 | impl FromStrWithRadix for $x { 91 | fn from_str_with_radix(s: &str) -> Result { 92 | if s.len() > 2 && &s[0..2] == "0x" { 93 | Self::from_str_radix(&s[2..], 16) 94 | } else if s.len() > 2 && &s[0..2] == "0o" { 95 | Self::from_str_radix(&s[2..], 8) 96 | } else { 97 | s.parse() 98 | } 99 | .context("Could not convert str") 100 | } 101 | })* 102 | 103 | }; 104 | } 105 | 106 | from_str_with_radix_for_types!(u64, i64, usize); 107 | -------------------------------------------------------------------------------- /src/b64app.rs: -------------------------------------------------------------------------------- 1 | use crate::applet::Applet; 2 | use crate::applet::SliceExt; 3 | use anyhow::{Context, Result}; 4 | use base64::engine::general_purpose; 5 | use base64::engine::Engine; 6 | use clap::{arg, Command}; 7 | 8 | pub struct B64EncApplet { 9 | engine: general_purpose::GeneralPurpose, 10 | } 11 | 12 | impl Applet for B64EncApplet { 13 | fn command(&self) -> &'static str { 14 | "b64" 15 | } 16 | fn description(&self) -> &'static str { 17 | "base64 encode" 18 | } 19 | 20 | fn clap_command(&self) -> Command { 21 | Command::new(self.command()) 22 | .about(self.description()) 23 | .arg(arg!(-u --URL "Use URL-safe base64")) 24 | .arg( 25 | arg!(-a --alphabet "specify custom alphabet") 26 | .conflicts_with("URL") 27 | .required(false), 28 | ) 29 | .arg(arg!([value] "input value, reads from stdin in not present")) 30 | } 31 | 32 | fn new() -> Box { 33 | Box::new(Self { 34 | engine: general_purpose::STANDARD, 35 | }) 36 | } 37 | 38 | fn parse_args(&self, args: &clap::ArgMatches) -> Result> { 39 | Ok(Box::new(Self { 40 | engine: if args.get_flag("URL") { 41 | general_purpose::URL_SAFE 42 | } else if args.contains_id("alphabet") { 43 | let custom = base64::alphabet::Alphabet::new( 44 | args.get_one::("alphabet") 45 | .with_context(|| "alphabet is not specified")?, 46 | ) 47 | .with_context(|| "Invalid alphabet")?; 48 | base64::engine::GeneralPurpose::new(&custom, base64::engine::general_purpose::PAD) 49 | } else { 50 | general_purpose::STANDARD 51 | }, 52 | })) 53 | } 54 | 55 | fn process(&self, val: Vec) -> Result> { 56 | Ok(self.engine.encode(val).as_bytes().to_vec()) 57 | } 58 | } 59 | 60 | pub struct B64DecApplet { 61 | engine: general_purpose::GeneralPurpose, 62 | strict: bool, 63 | } 64 | 65 | impl Applet for B64DecApplet { 66 | fn command(&self) -> &'static str { 67 | "d64" 68 | } 69 | 70 | fn description(&self) -> &'static str { 71 | "base64 decode" 72 | } 73 | 74 | fn clap_command(&self) -> Command { 75 | Command::new(self.command()) 76 | .about(self.description()) 77 | .arg(arg!(-u --URL "use URL-safe base64")) 78 | .arg( 79 | arg!(-a --alphabet "specify custom alphabet") 80 | .conflicts_with("URL") 81 | .required(false), 82 | ) 83 | .arg(arg!(-s --strict "strict decoding, error on invalid data")) 84 | .arg(arg!([value] "input value, reads from stdin in not present")) 85 | } 86 | 87 | fn new() -> Box { 88 | let engine_cfg = 89 | base64::engine::GeneralPurposeConfig::new().with_decode_allow_trailing_bits(true); 90 | Box::new(Self { 91 | engine: general_purpose::GeneralPurpose::new(&base64::alphabet::STANDARD, engine_cfg), 92 | strict: false, 93 | }) 94 | } 95 | 96 | fn parse_args(&self, args: &clap::ArgMatches) -> Result> { 97 | let engine_cfg = 98 | base64::engine::GeneralPurposeConfig::new().with_decode_allow_trailing_bits(true); 99 | let alphabet = if args.get_flag("URL") { 100 | base64::alphabet::URL_SAFE 101 | } else if args.contains_id("alphabet") { 102 | base64::alphabet::Alphabet::new( 103 | args.get_one::("alphabet") 104 | .with_context(|| "alphabet is not specified")?, 105 | ) 106 | .with_context(|| "Invalid alphabet")? 107 | } else { 108 | base64::alphabet::STANDARD 109 | }; 110 | Ok(Box::new(Self { 111 | engine: general_purpose::GeneralPurpose::new(&alphabet, engine_cfg), 112 | strict: false, 113 | })) 114 | } 115 | 116 | /* b64_decode. With two modes: 117 | * - strict: decode until the end of the valid base64 118 | * - lenient: decode the b64 input until the first invalid byte 119 | * and return the decoded data concatenated with the rest 120 | */ 121 | fn process(&self, b64val: Vec) -> Result> { 122 | let mut trimmed: Vec = b64val.trim().into(); 123 | 124 | // If the length is invalid, decode up to the supplementary bytes 125 | if trimmed.len() % 4 != 0 && !self.strict { 126 | let end = trimmed.len() - (trimmed.len() % 4); 127 | let mut decoded = self.process(trimmed[0..end].to_vec())?; 128 | decoded.extend_from_slice(&trimmed[end..]); 129 | return Ok(decoded); 130 | } 131 | 132 | let decoded = self.engine.decode(b64val); 133 | match decoded { 134 | Ok(res) => Ok(res), 135 | Err(ref e) => { 136 | if self.strict { 137 | decoded.with_context(|| "Decoding base64 failed") 138 | } else { 139 | match e { 140 | base64::DecodeError::InvalidLastSymbol(offset, _) 141 | | base64::DecodeError::InvalidByte(offset, _) => { 142 | let mut end = trimmed.split_off(*offset); 143 | let mut decoded = self.process(trimmed)?; 144 | if !self.strict { 145 | decoded.append(&mut end); 146 | } 147 | Ok(decoded) 148 | } 149 | // Should not happen since we handle trailing data 150 | // before in non-strict mode 151 | base64::DecodeError::InvalidLength(_) => { 152 | decoded.with_context(|| "Decoding base64 failed") 153 | } 154 | base64::DecodeError::InvalidPadding => { 155 | decoded.with_context(|| "Decoding base64 failed: invalid padding") 156 | } 157 | } 158 | } 159 | } 160 | } 161 | } 162 | } 163 | 164 | #[cfg(test)] 165 | mod tests { 166 | use super::*; 167 | 168 | #[test] 169 | fn test_b64_inv_lenient() { 170 | let engine_cfg = 171 | base64::engine::GeneralPurposeConfig::new().with_decode_allow_trailing_bits(true); 172 | let engine = general_purpose::GeneralPurpose::new(&base64::alphabet::STANDARD, engine_cfg); 173 | 174 | let d64 = B64DecApplet { 175 | strict: false, 176 | engine: engine, 177 | }; 178 | assert_eq!( 179 | "::::".as_bytes().to_vec(), 180 | d64.process_test("::::".as_bytes().to_vec()) 181 | ); 182 | } 183 | 184 | #[test] 185 | fn test_b64_enc() { 186 | let b64 = B64EncApplet { 187 | engine: general_purpose::STANDARD, 188 | }; 189 | // https://tools.ietf.org/html/rfc4648#page-12 190 | assert_eq!( 191 | "".as_bytes().to_vec(), 192 | b64.process_test("".as_bytes().to_vec()) 193 | ); 194 | assert_eq!( 195 | "Zg==".as_bytes().to_vec(), 196 | b64.process_test("f".as_bytes().to_vec()) 197 | ); 198 | assert_eq!( 199 | "Zm8=".as_bytes().to_vec(), 200 | b64.process_test("fo".as_bytes().to_vec()) 201 | ); 202 | assert_eq!( 203 | "Zm9v".as_bytes().to_vec(), 204 | b64.process_test("foo".as_bytes().to_vec()) 205 | ); 206 | assert_eq!( 207 | "Zm9vYg==".as_bytes().to_vec(), 208 | b64.process_test("foob".as_bytes().to_vec()) 209 | ); 210 | assert_eq!( 211 | "Zm9vYmE=".as_bytes().to_vec(), 212 | b64.process_test("fooba".as_bytes().to_vec()) 213 | ); 214 | assert_eq!( 215 | "Zm9vYmFy".as_bytes().to_vec(), 216 | b64.process_test("foobar".as_bytes().to_vec()) 217 | ); 218 | 219 | let test = [0x14, 0xfb, 0x9c, 0x03, 0xd9, 0x7e].to_vec(); 220 | assert_eq!("FPucA9l+".as_bytes().to_vec(), b64.process_test(test)); 221 | } 222 | 223 | #[test] 224 | fn test_b64_url_enc() { 225 | let b64 = B64EncApplet { 226 | engine: general_purpose::URL_SAFE, 227 | }; 228 | // https://tools.ietf.org/html/rfc4648#page-12 229 | assert_eq!( 230 | "".as_bytes().to_vec(), 231 | b64.process_test("".as_bytes().to_vec()) 232 | ); 233 | assert_eq!( 234 | "Zg==".as_bytes().to_vec(), 235 | b64.process_test("f".as_bytes().to_vec()) 236 | ); 237 | assert_eq!( 238 | "Zm8=".as_bytes().to_vec(), 239 | b64.process_test("fo".as_bytes().to_vec()) 240 | ); 241 | assert_eq!( 242 | "Zm9v".as_bytes().to_vec(), 243 | b64.process_test("foo".as_bytes().to_vec()) 244 | ); 245 | assert_eq!( 246 | "Zm9vYg==".as_bytes().to_vec(), 247 | b64.process_test("foob".as_bytes().to_vec()) 248 | ); 249 | assert_eq!( 250 | "Zm9vYmE=".as_bytes().to_vec(), 251 | b64.process_test("fooba".as_bytes().to_vec()) 252 | ); 253 | assert_eq!( 254 | "Zm9vYmFy".as_bytes().to_vec(), 255 | b64.process_test("foobar".as_bytes().to_vec()) 256 | ); 257 | assert_eq!( 258 | "ZZ-A".as_bytes().to_vec(), 259 | b64.process_test([0x65, 0x9F, 0x80].to_vec()) 260 | ); 261 | 262 | let test = [0x14, 0xfb, 0x9c, 0x03, 0xd9, 0x7e].to_vec(); 263 | assert_eq!("FPucA9l-".as_bytes().to_vec(), b64.process_test(test)); 264 | } 265 | 266 | #[test] 267 | fn test_encode_and_back() { 268 | let b64 = B64EncApplet { 269 | engine: general_purpose::STANDARD, 270 | }; 271 | let d64 = B64DecApplet { 272 | strict: true, 273 | engine: general_purpose::STANDARD, 274 | }; 275 | 276 | let to_enc = [0x74, 0x65, 0x73, 0x74, 0x52, 0xaf, 0x20].to_vec(); 277 | assert_eq!(to_enc, d64.process_test(b64.process_test(to_enc.clone()))); 278 | } 279 | 280 | #[test] 281 | fn test_encode_and_back_url() { 282 | let b64 = B64EncApplet { 283 | engine: general_purpose::URL_SAFE, 284 | }; 285 | let d64 = B64DecApplet { 286 | strict: true, 287 | engine: general_purpose::URL_SAFE, 288 | }; 289 | 290 | let to_enc = [0x74, 0x65, 0x73, 0x74, 0x52, 0xaf, 0x20].to_vec(); 291 | assert_eq!(to_enc, d64.process_test(b64.process_test(to_enc.clone()))); 292 | } 293 | } 294 | -------------------------------------------------------------------------------- /src/baseapp.rs: -------------------------------------------------------------------------------- 1 | use crate::applet::Applet; 2 | use anyhow::{Context, Result}; 3 | use clap::{arg, Command}; 4 | use num_bigint::BigUint; 5 | use num_traits::Num; 6 | 7 | use crate::applet::SliceExt; 8 | 9 | pub struct BaseIntApplet { 10 | source_radix: Option, 11 | target_radix: u32, 12 | } 13 | 14 | impl Applet for BaseIntApplet { 15 | fn command(&self) -> &'static str { 16 | "base" 17 | } 18 | fn description(&self) -> &'static str { 19 | "convert integer between different bases" 20 | } 21 | 22 | fn new() -> Box { 23 | Box::new(Self { 24 | source_radix: None, 25 | target_radix: 10, 26 | }) 27 | } 28 | 29 | fn clap_command(&self) -> Command { 30 | Command::new(self.command()) 31 | .about(self.description()) 32 | .arg(arg!(-f --from "source radix, by default, parse standard prefixes (0x, 0b, 0o)") 33 | .value_parser(clap::value_parser!(u32).range(2..37))) 34 | .arg(arg!(-t --to "target radix, defaults to decimal, except if input was decimal, then default to hex") 35 | .value_parser(clap::value_parser!(u32).range(2..37))) 36 | .arg(arg!([value] "input value, reads from stdin in not present")) 37 | } 38 | 39 | fn parse_args(&self, args: &clap::ArgMatches) -> Result> { 40 | let target_radix: u32 = if let Some(target) = args.get_one::("to") { 41 | *target 42 | } else { 43 | 10 44 | }; 45 | Ok(Box::new(Self { 46 | source_radix: args.get_one::("from").copied(), 47 | target_radix, 48 | })) 49 | } 50 | 51 | fn process(&self, val: Vec) -> Result> { 52 | // Remove whitespace to make conversions work with stdin input 53 | let val = val.trim(); 54 | 55 | let (srcrad, int) = if let Some(src) = self.source_radix { 56 | ( 57 | src, 58 | BigUint::parse_bytes(val, src).context("Could not convert input")?, 59 | ) 60 | } else { 61 | let int_str = String::from_utf8_lossy(val); 62 | 63 | // Base was not specified, check standard prefixes 64 | if int_str.len() > 2 && &int_str[0..2] == "0x" { 65 | ( 66 | 16, 67 | BigUint::from_str_radix(&int_str[2..], 16) 68 | .context("Could not parse argument as hex integer")?, 69 | ) 70 | } else if int_str.len() > 2 && &int_str[0..2] == "0o" { 71 | ( 72 | 8, 73 | BigUint::from_str_radix(&int_str[2..], 8) 74 | .context("Could not parse argument as octal integer")?, 75 | ) 76 | } else { 77 | ( 78 | 10, 79 | int_str 80 | .parse() 81 | .context("Could not parse argument as integer")?, 82 | ) 83 | } 84 | }; 85 | 86 | // If both source and target radices are equal to 10, actually output hex 87 | if srcrad == 10 && self.target_radix == 10 { 88 | Ok((format!("0x{}", int.to_str_radix(16))).as_bytes().to_vec()) 89 | } else { 90 | Ok(int.to_str_radix(self.target_radix).as_bytes().to_vec()) 91 | } 92 | } 93 | } 94 | 95 | #[cfg(test)] 96 | mod tests { 97 | #[test] 98 | fn test_base_cli_no_radix() { 99 | assert_cmd::Command::cargo_bin("rsbkb") 100 | .expect("Could not run binary") 101 | .args(&["base", "10"]) 102 | .assert() 103 | .stdout("0xa") 104 | .success(); 105 | } 106 | 107 | #[test] 108 | fn test_base_cli_arg() { 109 | assert_cmd::Command::cargo_bin("rsbkb") 110 | .expect("Could not run binary") 111 | .args(&["base", "0x10"]) 112 | .assert() 113 | .stdout("16") 114 | .success(); 115 | } 116 | 117 | #[test] 118 | fn test_base_cli_arg_from_to() { 119 | assert_cmd::Command::cargo_bin("rsbkb") 120 | .expect("Could not run binary") 121 | .args(&["base", "-f", "2", "-t", "16", "10000"]) 122 | .assert() 123 | .stdout("10") 124 | .success(); 125 | } 126 | 127 | #[test] 128 | fn test_base_cli_stdin() { 129 | assert_cmd::Command::cargo_bin("rsbkb") 130 | .expect("Could not run binary") 131 | .args(&["base"]) 132 | .write_stdin("0xA\n") 133 | .assert() 134 | .stdout("10") 135 | .success(); 136 | } 137 | 138 | #[test] 139 | fn test_base_cli_arg_to() { 140 | assert_cmd::Command::cargo_bin("rsbkb") 141 | .expect("Could not run binary") 142 | .args(&["base", "-t", "32", "0o7675"]) 143 | .assert() 144 | .stdout("3tt") 145 | .success(); 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /src/bgrepapp.rs: -------------------------------------------------------------------------------- 1 | use crate::applet::Applet; 2 | use anyhow::{bail, Context, Result}; 3 | use clap::{arg, Command}; 4 | use memmap2::Mmap; 5 | use std::{ 6 | collections::BTreeSet, 7 | fs::{self, read_dir, File}, 8 | path::PathBuf, 9 | }; 10 | 11 | use regex::bytes::{Regex, RegexBuilder}; 12 | 13 | /// Build the regex pattern with the given options. 14 | /// By default, the `unicode` flag is set to false, and `dot_matches_new_line` set to true. 15 | /// Code borrowed from gahag's bgrep 16 | fn build_pattern>(pattern: &P) -> Result { 17 | let mut builder = RegexBuilder::new(pattern.as_ref()); 18 | 19 | builder.unicode(false); 20 | builder.dot_matches_new_line(true); 21 | builder 22 | .build() 23 | .with_context(|| "Could not build regular expression") 24 | } 25 | 26 | pub struct BgrepApplet { 27 | paths: Option>, 28 | pattern: Option, 29 | verbose: bool, 30 | recursive: bool, 31 | } 32 | 33 | impl Applet for BgrepApplet { 34 | fn command(&self) -> &'static str { 35 | "bgrep" 36 | } 37 | 38 | fn description(&self) -> &'static str { 39 | "binary grep" 40 | } 41 | 42 | fn returns_data(&self) -> bool { 43 | false 44 | } 45 | 46 | fn clap_command(&self) -> Command { 47 | Command::new(self.command()) 48 | .about(self.description()) 49 | .arg(arg!(-v --verbose "verbose")) 50 | .arg(arg!(-x --hex "pattern is hex")) 51 | .arg(arg!(-r --recursive "search in subfolders")) 52 | .arg(arg!( "pattern to search")) 53 | .arg(arg!( "file(s) or directory(ies) to search in").num_args(1..)) 54 | } 55 | 56 | fn arg_or_stdin(&self) -> Option<&'static str> { 57 | None 58 | } 59 | 60 | fn new() -> Box { 61 | Box::new(Self { 62 | paths: None, 63 | pattern: None, 64 | verbose: false, 65 | recursive: false, 66 | }) 67 | } 68 | 69 | fn parse_args(&self, args: &clap::ArgMatches) -> Result> { 70 | let filenames = args 71 | .get_many::("path") 72 | .unwrap() 73 | .map(|s| s.to_string()) 74 | .collect(); 75 | let pattern_val = args.get_one::("pattern").unwrap(); 76 | 77 | /* Convert hex pattern to "\x00" format if needed */ 78 | let mut s = String::new(); 79 | let final_pat = if args.get_flag("hex") { 80 | if pattern_val.len() % 2 != 0 { 81 | bail!("hex pattern length is not even"); 82 | } 83 | for i in 0..(pattern_val.len() / 2) { 84 | s += "\\x"; 85 | s += &pattern_val[i * 2..i * 2 + 2]; 86 | } 87 | s.as_str() 88 | } else { 89 | pattern_val 90 | }; 91 | 92 | let pattern = build_pattern(&final_pat)?; 93 | 94 | Ok(Box::new(Self { 95 | paths: Some(filenames), 96 | pattern: Some(pattern), 97 | verbose: args.get_flag("verbose"), 98 | recursive: args.get_flag("recursive"), 99 | })) 100 | } 101 | 102 | fn process(&self, _val: Vec) -> Result> { 103 | let input_paths = self.paths.as_ref().unwrap(); 104 | let many = input_paths.len() > 1 || self.recursive; 105 | // Make sure we keep the search order based on what is given first as the input 106 | for input_path in input_paths.iter() { 107 | // A BTreeSet ensure we get a consistant order 108 | let mut paths_to_explore = BTreeSet::new(); 109 | paths_to_explore.insert(PathBuf::from(input_path)); 110 | 111 | while let Some(path) = paths_to_explore.pop_first() { 112 | let path_metadata = match fs::metadata(&path) { 113 | Ok(x) => x, 114 | Err(err) => { 115 | eprintln!( 116 | "Skiping {} with non-obtainable metadata ({})", 117 | path.to_string_lossy(), 118 | err 119 | ); 120 | continue; 121 | } 122 | }; 123 | 124 | if path_metadata.is_file() { 125 | let f = File::open(&path); 126 | match f { 127 | Ok(f) => { 128 | /* Mmap is necessarily unsafe as data can change unexpectedly */ 129 | let data = unsafe { 130 | Mmap::map(&f).with_context(|| "Could not mmap input file")? 131 | }; 132 | 133 | let regex = self.pattern.as_ref().unwrap(); 134 | let matches = regex.find_iter(&data); 135 | 136 | /* Print offsets on stdout directly, to avoid buffering */ 137 | for m in matches { 138 | if many { 139 | println!("{}: 0x{:x}", path.to_string_lossy(), m.start()); 140 | } else { 141 | println!("0x{:x}", m.start()); 142 | } 143 | } 144 | } 145 | Err(e) => eprintln!("Could not open {}: {}", path.to_string_lossy(), e), 146 | } 147 | } else if path_metadata.is_dir() { 148 | if !self.recursive { 149 | if self.verbose { 150 | eprintln!("Skipping directory {}", path.to_string_lossy()) 151 | } 152 | continue; 153 | } 154 | 155 | let dir_read = match read_dir(&path) { 156 | Ok(x) => x, 157 | Err(err) => { 158 | eprintln!( 159 | "Skipping directory {}, failed to list childs ({})", 160 | path.to_string_lossy(), 161 | err 162 | ); 163 | continue; 164 | } 165 | }; 166 | for sub_path_unchecked in dir_read { 167 | let sub_path = match sub_path_unchecked { 168 | Ok(x) => x, 169 | Err(err) => { 170 | eprintln!("Skipping a sub-path of directory {}, failed to list a child ({})", path.to_string_lossy(), err); 171 | continue; 172 | } 173 | }; 174 | paths_to_explore.insert(sub_path.path()); 175 | } 176 | } else { 177 | if self.verbose { 178 | eprintln!("Skipping non-file {}", path.to_string_lossy()); 179 | } 180 | continue; 181 | } 182 | } 183 | } 184 | 185 | /* Return empty Vec as we output directly on stdout */ 186 | Ok(Vec::::new()) 187 | } 188 | } 189 | 190 | #[cfg(test)] 191 | mod tests { 192 | use std::{fs::File, io::Write}; 193 | 194 | #[test] 195 | fn test_cli() { 196 | let mut data: [u8; 10] = [0; 10]; 197 | for i in (0..10).into_iter() { 198 | data[i] = i as u8; 199 | } 200 | 201 | let mut tmpfile = tempfile::NamedTempFile::new().unwrap(); 202 | tmpfile.write(&data).unwrap(); 203 | 204 | assert_cmd::Command::cargo_bin("rsbkb") 205 | .expect("Could not run binary") 206 | .args(&["bgrep", "-x", "020304", &tmpfile.path().to_str().unwrap()]) 207 | .assert() 208 | .stdout("0x2\n") 209 | .success(); 210 | } 211 | 212 | #[test] 213 | fn test_cli_multiple() { 214 | let mut tmpfile1 = tempfile::NamedTempFile::new().unwrap(); 215 | tmpfile1.write(b"tmpfile1").unwrap(); 216 | 217 | let mut tmpfile2 = tempfile::NamedTempFile::new().unwrap(); 218 | tmpfile2.write(b"2tmpfile").unwrap(); 219 | 220 | assert_cmd::Command::cargo_bin("rsbkb") 221 | .expect("Could not run binary") 222 | .args(&[ 223 | "bgrep", 224 | "tmpfile", 225 | &tmpfile1.path().to_str().unwrap(), 226 | &tmpfile2.path().to_str().unwrap(), 227 | ]) 228 | .assert() 229 | .stdout(predicates::str::contains(": 0x0\n")) 230 | .stdout(predicates::str::contains(": 0x1\n")) 231 | .success(); 232 | } 233 | 234 | #[test] 235 | fn test_recursive() { 236 | let tmp_dir = tempfile::TempDir::new().unwrap(); 237 | 238 | { 239 | let mut tmp_file = File::create(&tmp_dir.path().join("test_file.bin")).unwrap(); 240 | tmp_file.write(b"2tmpfile").unwrap(); 241 | } 242 | 243 | assert_cmd::Command::cargo_bin("rsbkb") 244 | .expect("Could not run binary") 245 | .args(&[ 246 | "bgrep", 247 | "--recursive", 248 | "tmpfile", 249 | tmp_dir 250 | .path() 251 | .to_str() 252 | .expect("Could not convert temp path to unicode"), 253 | ]) 254 | .assert() 255 | .stdout(predicates::str::contains(": 0x1\n")) 256 | .success(); 257 | } 258 | } 259 | -------------------------------------------------------------------------------- /src/crcapp.rs: -------------------------------------------------------------------------------- 1 | extern crate crc; 2 | use crate::applet::Applet; 3 | use anyhow::{bail, Result}; 4 | use clap::{arg, Command}; 5 | use crc::*; 6 | use std::process; 7 | 8 | const ALL_CRCS: [&str; 111] = [ 9 | "CRC_3_GSM", 10 | "CRC_3_ROHC", 11 | "CRC_4_G_704", 12 | "CRC_4_INTERLAKEN", 13 | "CRC_5_EPC_C1G2", 14 | "CRC_5_G_704", 15 | "CRC_5_USB", 16 | "CRC_6_CDMA2000_A", 17 | "CRC_6_CDMA2000_B", 18 | "CRC_6_DARC", 19 | "CRC_6_GSM", 20 | "CRC_6_G_704", 21 | "CRC_7_MMC", 22 | "CRC_7_ROHC", 23 | "CRC_7_UMTS", 24 | "CRC_8_AUTOSAR", 25 | "CRC_8_BLUETOOTH", 26 | "CRC_8_CDMA2000", 27 | "CRC_8_DARC", 28 | "CRC_8_DVB_S2", 29 | "CRC_8_GSM_A", 30 | "CRC_8_GSM_B", 31 | "CRC_8_HITAG", 32 | "CRC_8_I_432_1", 33 | "CRC_8_I_CODE", 34 | "CRC_8_LTE", 35 | "CRC_8_MAXIM_DOW", 36 | "CRC_8_MIFARE_MAD", 37 | "CRC_8_NRSC_5", 38 | "CRC_8_OPENSAFETY", 39 | "CRC_8_ROHC", 40 | "CRC_8_SAE_J1850", 41 | "CRC_8_SMBUS", 42 | "CRC_8_TECH_3250", 43 | "CRC_8_WCDMA", 44 | "CRC_10_ATM", 45 | "CRC_10_CDMA2000", 46 | "CRC_10_GSM", 47 | "CRC_11_FLEXRAY", 48 | "CRC_11_UMTS", 49 | "CRC_12_CDMA2000", 50 | "CRC_12_DECT", 51 | "CRC_12_GSM", 52 | "CRC_12_UMTS", 53 | "CRC_13_BBC", 54 | "CRC_14_DARC", 55 | "CRC_14_GSM", 56 | "CRC_15_CAN", 57 | "CRC_15_MPT1327", 58 | "CRC_16_ARC", 59 | "CRC_16_CDMA2000", 60 | "CRC_16_CMS", 61 | "CRC_16_DDS_110", 62 | "CRC_16_DECT_R", 63 | "CRC_16_DECT_X", 64 | "CRC_16_DNP", 65 | "CRC_16_EN_13757", 66 | "CRC_16_GENIBUS", 67 | "CRC_16_GSM", 68 | "CRC_16_IBM_3740", 69 | "CRC_16_IBM_SDLC", 70 | "CRC_16_ISO_IEC_14443_3_A", 71 | "CRC_16_KERMIT", 72 | "CRC_16_LJ1200", 73 | "CRC_16_M17", 74 | "CRC_16_MAXIM_DOW", 75 | "CRC_16_MCRF4XX", 76 | "CRC_16_MODBUS", 77 | "CRC_16_NRSC_5", 78 | "CRC_16_OPENSAFETY_A", 79 | "CRC_16_OPENSAFETY_B", 80 | "CRC_16_PROFIBUS", 81 | "CRC_16_RIELLO", 82 | "CRC_16_SPI_FUJITSU", 83 | "CRC_16_T10_DIF", 84 | "CRC_16_TELEDISK", 85 | "CRC_16_TMS37157", 86 | "CRC_16_UMTS", 87 | "CRC_16_USB", 88 | "CRC_16_XMODEM", 89 | "CRC_17_CAN_FD", 90 | "CRC_21_CAN_FD", 91 | "CRC_24_BLE", 92 | "CRC_24_FLEXRAY_A", 93 | "CRC_24_FLEXRAY_B", 94 | "CRC_24_INTERLAKEN", 95 | "CRC_24_LTE_A", 96 | "CRC_24_LTE_B", 97 | "CRC_24_OPENPGP", 98 | "CRC_24_OS_9", 99 | "CRC_30_CDMA", 100 | "CRC_31_PHILIPS", 101 | "CRC_32_AIXM", 102 | "CRC_32_AUTOSAR", 103 | "CRC_32_BASE91_D", 104 | "CRC_32_BZIP2", 105 | "CRC_32_CD_ROM_EDC", 106 | "CRC_32_CKSUM", 107 | "CRC_32_ISCSI", 108 | "CRC_32_ISO_HDLC", 109 | "CRC_32_MEF", 110 | "CRC_32_MPEG_2", 111 | "CRC_32_XFER", 112 | "CRC_40_GSM", 113 | "CRC_64_ECMA_182", 114 | "CRC_64_GO_ISO", 115 | "CRC_64_MS", 116 | "CRC_64_REDIS", 117 | "CRC_64_WE", 118 | "CRC_64_XZ", 119 | "CRC_82_DARC", 120 | ]; 121 | 122 | pub struct CRC16Applet {} 123 | 124 | impl Applet for CRC16Applet { 125 | fn command(&self) -> &'static str { 126 | "crc16" 127 | } 128 | fn description(&self) -> &'static str { 129 | "compute CRC-16" 130 | } 131 | 132 | fn new() -> Box { 133 | Box::new(Self {}) 134 | } 135 | 136 | fn parse_args(&self, _args: &clap::ArgMatches) -> Result> { 137 | Ok(Box::new(Self {})) 138 | } 139 | 140 | fn process(&self, val: Vec) -> Result> { 141 | const CRC16: Crc = Crc::::new(&CRC_16_IBM_SDLC); 142 | Ok(format!("{:04x}", CRC16.checksum(&val)).as_bytes().to_vec()) 143 | } 144 | } 145 | 146 | pub struct CRC32Applet {} 147 | 148 | impl Applet for CRC32Applet { 149 | fn command(&self) -> &'static str { 150 | "crc32" 151 | } 152 | fn description(&self) -> &'static str { 153 | "compute CRC-32" 154 | } 155 | 156 | fn new() -> Box { 157 | Box::new(Self {}) 158 | } 159 | 160 | fn parse_args(&self, _args: &clap::ArgMatches) -> Result> { 161 | Ok(Box::new(Self {})) 162 | } 163 | 164 | fn process(&self, val: Vec) -> Result> { 165 | const CRC32: Crc = Crc::::new(&CRC_32_ISO_HDLC); 166 | Ok(format!("{:08x}", CRC32.checksum(&val)).as_bytes().to_vec()) 167 | } 168 | } 169 | 170 | pub struct CRCApplet { 171 | crctype: String, 172 | } 173 | 174 | macro_rules! algs { 175 | ( $ident:expr; $size:tt; $( $x:expr ),* ) => { 176 | match $ident { 177 | $( stringify!($x) => Crc::<$size>::new(&$x), )* 178 | _ => { bail!("Unknown CRC algorithm.") } , 179 | } 180 | } 181 | } 182 | 183 | impl Applet for CRCApplet { 184 | fn command(&self) -> &'static str { 185 | "crc" 186 | } 187 | fn description(&self) -> &'static str { 188 | "flexible CRC computation" 189 | } 190 | 191 | fn new() -> Box { 192 | Box::new(Self { 193 | crctype: "lol".to_string(), 194 | }) 195 | } 196 | 197 | fn clap_command(&self) -> Command { 198 | Command::new(self.command()) 199 | .about(self.description()) 200 | .arg(arg!(-l --list "List supported CRC algorithms")) 201 | .arg( 202 | arg!([type] "CRC type to compute. Use 'all' to compute all known algorithms.") 203 | .required_unless_present("list"), 204 | ) 205 | .arg(arg!([value] "input value, reads from stdin in not present")) 206 | } 207 | 208 | fn parse_args(&self, args: &clap::ArgMatches) -> Result> { 209 | if args.get_flag("list") { 210 | println!("Supported algorithms:"); 211 | println!("{}", ALL_CRCS.join("\n")); 212 | println!("\nSee https://docs.rs/crc/ for more info"); 213 | process::exit(0); 214 | } 215 | Ok(Box::new(Self { 216 | crctype: args.get_one::("type").unwrap().to_string(), 217 | })) 218 | } 219 | 220 | fn process(&self, val: Vec) -> Result> { 221 | let alg_name: &str = self.crctype.as_str(); 222 | if alg_name == "all" { 223 | let mut res = String::new(); 224 | for alg in ALL_CRCS.iter() { 225 | res.push_str(format!("{}: 0x{}\n", alg, self.do_crc(alg, &val)?.as_str()).as_str()); 226 | } 227 | Ok(res.as_bytes().to_vec()) 228 | } else { 229 | Ok(self.do_crc(alg_name, &val)?.as_bytes().to_vec()) 230 | } 231 | } 232 | } 233 | 234 | impl CRCApplet { 235 | fn do_crc(&self, alg_name: &str, val: &[u8]) -> Result { 236 | let alg_size: u8 = (*alg_name.split('_').collect::>().get(1).unwrap()) 237 | .parse() 238 | .unwrap(); 239 | match alg_size { 240 | 0..=8 => { 241 | let crc8 = algs!(alg_name; u8; 242 | CRC_3_GSM, CRC_3_ROHC, CRC_4_G_704, CRC_4_INTERLAKEN, CRC_5_EPC_C1G2, CRC_5_G_704, CRC_5_USB, 243 | CRC_6_CDMA2000_A, CRC_6_CDMA2000_B, CRC_6_DARC, CRC_6_GSM, CRC_6_G_704, CRC_7_MMC, CRC_7_ROHC, 244 | CRC_7_UMTS, CRC_8_AUTOSAR, CRC_8_BLUETOOTH, CRC_8_CDMA2000, CRC_8_DARC, CRC_8_DVB_S2, CRC_8_GSM_A, 245 | CRC_8_GSM_B, CRC_8_HITAG, CRC_8_I_432_1, CRC_8_I_CODE, CRC_8_LTE, CRC_8_MAXIM_DOW, 246 | CRC_8_MIFARE_MAD, CRC_8_NRSC_5, CRC_8_OPENSAFETY, CRC_8_ROHC, CRC_8_SAE_J1850, CRC_8_SMBUS, 247 | CRC_8_TECH_3250, CRC_8_WCDMA); 248 | Ok(format!("{:02x}", crc8.checksum(val))) 249 | } 250 | 9..=16 => { 251 | let crc16 = algs!(alg_name; u16; 252 | CRC_10_ATM, CRC_10_CDMA2000, CRC_10_GSM, CRC_11_FLEXRAY, CRC_11_UMTS, 253 | CRC_12_CDMA2000, CRC_12_DECT, CRC_12_GSM, CRC_12_UMTS, CRC_13_BBC, 254 | CRC_14_DARC, CRC_14_GSM, CRC_15_CAN, CRC_15_MPT1327, CRC_16_ARC, 255 | CRC_16_CDMA2000, CRC_16_CMS, CRC_16_DDS_110, CRC_16_DECT_R, 256 | CRC_16_DECT_X, CRC_16_DNP, CRC_16_EN_13757, CRC_16_GENIBUS, CRC_16_GSM, 257 | CRC_16_IBM_3740, CRC_16_IBM_SDLC, CRC_16_ISO_IEC_14443_3_A, 258 | CRC_16_KERMIT, CRC_16_LJ1200, CRC_16_M17, CRC_16_MAXIM_DOW, 259 | CRC_16_MCRF4XX, CRC_16_MODBUS, CRC_16_NRSC_5, CRC_16_OPENSAFETY_A, 260 | CRC_16_OPENSAFETY_B, CRC_16_PROFIBUS, CRC_16_RIELLO, 261 | CRC_16_SPI_FUJITSU, CRC_16_T10_DIF, CRC_16_TELEDISK, CRC_16_TMS37157, 262 | CRC_16_UMTS, CRC_16_USB, CRC_16_XMODEM); 263 | Ok(format!("{:04x}", crc16.checksum(val))) 264 | } 265 | 17..=32 => { 266 | let crc32 = algs!(alg_name; u32; 267 | CRC_17_CAN_FD, CRC_21_CAN_FD, CRC_24_BLE, CRC_24_FLEXRAY_A, CRC_24_FLEXRAY_B, CRC_24_INTERLAKEN, 268 | CRC_24_LTE_A, CRC_24_LTE_B, CRC_24_OPENPGP, CRC_24_OS_9, CRC_30_CDMA, CRC_31_PHILIPS, CRC_32_AIXM, 269 | CRC_32_AUTOSAR, CRC_32_BASE91_D, CRC_32_BZIP2, CRC_32_CD_ROM_EDC, CRC_32_CKSUM, CRC_32_ISCSI, 270 | CRC_32_ISO_HDLC, CRC_32_JAMCRC, CRC_32_MEF, CRC_32_MPEG_2, CRC_32_XFER); 271 | Ok(format!("{:08x}", crc32.checksum(val))) 272 | } 273 | 33..=64 => { 274 | let crc64 = algs!(alg_name; u64; CRC_40_GSM, CRC_64_ECMA_182, CRC_64_GO_ISO, CRC_64_MS, CRC_64_REDIS, CRC_64_WE, CRC_64_XZ); 275 | Ok(format!("{:016x}", crc64.checksum(val))) 276 | } 277 | 65..=128 => { 278 | let crc128 = algs!(alg_name; u128; CRC_82_DARC); 279 | Ok(format!("{:032x}", crc128.checksum(val))) 280 | } 281 | _ => { 282 | bail!("Unknown CRC algorithm"); 283 | } 284 | } 285 | } 286 | } 287 | 288 | #[cfg(test)] 289 | mod tests { 290 | use super::*; 291 | 292 | #[test] 293 | fn test_crc32() { 294 | let crc32 = CRC32Applet {}; 295 | assert_eq!( 296 | "10cca4f1".as_bytes().to_vec(), 297 | crc32.process_test("toto".as_bytes().to_vec()) 298 | ); 299 | } 300 | 301 | #[test] 302 | fn test_crc() { 303 | let crc = CRCApplet { 304 | crctype: "CRC_32_AIXM".to_string(), 305 | }; 306 | assert_eq!( 307 | "fa83f52a".as_bytes().to_vec(), 308 | crc.process_test("toto".as_bytes().to_vec()) 309 | ); 310 | } 311 | 312 | #[test] 313 | fn test_crc16() { 314 | let crc16 = CRC16Applet {}; 315 | assert_eq!( 316 | "97a8".as_bytes().to_vec(), 317 | crc16.process_test("toto".as_bytes().to_vec()) 318 | ); 319 | } 320 | } 321 | -------------------------------------------------------------------------------- /src/entropyapp.rs: -------------------------------------------------------------------------------- 1 | use crate::applet::Applet; 2 | use anyhow::Result; 3 | 4 | pub struct EntropyApplet {} 5 | 6 | fn entropy(val: &[u8]) -> f64 { 7 | if val.is_empty() { 8 | return 0.0; 9 | } 10 | /* Compute how many times each value appears */ 11 | let mut counts: [i64; 256] = [0; 256]; 12 | for v in val.iter() { 13 | counts[*v as usize] += 1 14 | } 15 | 16 | let mut res: f64 = 0.0; 17 | let len: f64 = val.len() as f64; 18 | 19 | /* Compute entropy */ 20 | for count in counts.iter() { 21 | if *count > 0 { 22 | let p = (*count as f64) / len; 23 | res -= p * p.log(256.0) 24 | } 25 | } 26 | res 27 | } 28 | 29 | impl Applet for EntropyApplet { 30 | fn command(&self) -> &'static str { 31 | "entropy" 32 | } 33 | fn description(&self) -> &'static str { 34 | "compute file entropy" 35 | } 36 | 37 | fn new() -> Box { 38 | Box::new(Self {}) 39 | } 40 | 41 | fn parse_args(&self, _args: &clap::ArgMatches) -> Result> { 42 | Ok(Box::new(Self {})) 43 | } 44 | 45 | fn process(&self, val: Vec) -> Result> { 46 | Ok(format!("{:.3}", entropy(val.as_slice())) 47 | .as_bytes() 48 | .to_vec()) 49 | } 50 | } 51 | 52 | #[cfg(test)] 53 | mod tests { 54 | use super::*; 55 | 56 | fn run_ent(app: &EntropyApplet, val: Vec) -> String { 57 | String::from_utf8(app.process_test(val)).unwrap() 58 | } 59 | 60 | #[test] 61 | fn test() { 62 | let ent = EntropyApplet {}; 63 | assert_eq!(run_ent(&ent, Vec::new()), "0.000"); 64 | assert_eq!(run_ent(&ent, vec![1, 2, 3, 4]), "0.250"); 65 | let mut all_bytes: Vec = Vec::with_capacity(256); 66 | for i in 0..255 { 67 | all_bytes.push(i as u8) 68 | } 69 | assert_eq!(run_ent(&ent, all_bytes), "0.999"); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/escapeapp.rs: -------------------------------------------------------------------------------- 1 | use crate::applet::Applet; 2 | use crate::applet::SliceExt; 3 | use anyhow::{Context, Result}; 4 | use clap::{arg, Command}; 5 | use htmlentity::entity::{decode, encode, CharacterSet, EncodeType}; 6 | 7 | #[derive(clap::ValueEnum, Clone, Default, Debug)] 8 | enum EscType { 9 | #[default] 10 | Generic, 11 | Single, 12 | Shell, 13 | Bash, 14 | BashSingle, 15 | HTMLEntities, 16 | } 17 | 18 | const SHELL_CHARS: &[u8; 4] = b"`$\"\\"; 19 | 20 | // Note that bash is crazy regarding '!' 21 | // echo "\!" will output \! 22 | // ref: https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html 23 | // > If enabled, history expansion will be performed unless an ‘!’ appearing in double quotes is 24 | // > escaped using a backslash. The backslash preceding the ‘!’ is not removed. 25 | const BASH_CHARS: &[u8; 5] = b"`$\"\\!"; 26 | 27 | trait SliceEsc { 28 | fn escape(&self, esc_type: &EscType) -> Vec; 29 | fn escape_chars(&self, chars: &[u8]) -> Vec; 30 | fn escape_bash_single(&self) -> Vec; 31 | } 32 | 33 | impl SliceEsc for [u8] { 34 | fn escape(&self, esc_type: &EscType) -> Vec { 35 | match esc_type { 36 | EscType::Generic | EscType::Single => self.escape_ascii().collect(), 37 | EscType::Shell => self.escape_chars(SHELL_CHARS), 38 | EscType::Bash => self.escape_chars(BASH_CHARS), 39 | EscType::BashSingle => self.escape_bash_single(), 40 | EscType::HTMLEntities => encode( 41 | self, 42 | &EncodeType::NamedOrHex, 43 | &CharacterSet::SpecialCharsAndNonASCII, 44 | ) 45 | .into_bytes(), 46 | } 47 | } 48 | 49 | fn escape_chars(&self, chars: &[u8]) -> Vec { 50 | let mut res = Vec::::with_capacity(self.len()); 51 | for c in self { 52 | if chars.contains(c) { 53 | res.push(b'\\'); 54 | } 55 | res.push(*c); 56 | } 57 | res 58 | } 59 | 60 | fn escape_bash_single(&self) -> Vec { 61 | let mut res = Vec::::with_capacity(self.len()); 62 | let mut parts = self.split(|b| *b == b'\'').peekable(); 63 | while let Some(part) = parts.next() { 64 | res.extend_from_slice(part); 65 | if parts.peek().is_some() { 66 | // https://stackoverflow.com/a/1250279 67 | res.extend_from_slice(b"'\"'\"'") 68 | } 69 | } 70 | res 71 | } 72 | } 73 | 74 | pub struct EscapeApplet { 75 | esc_type: EscType, 76 | no_quote: bool, 77 | no_detect: bool, 78 | multiline: bool, 79 | } 80 | 81 | impl Applet for EscapeApplet { 82 | fn command(&self) -> &'static str { 83 | "escape" 84 | } 85 | fn description(&self) -> &'static str { 86 | "backslash-escape input strings" 87 | } 88 | 89 | fn clap_command(&self) -> Command { 90 | Command::new(self.command()) 91 | .about(self.description()) 92 | .arg(arg!(-m --multiline "expect multiline string, do not trim input")) 93 | .arg(arg!(-d --"no-detect" "do not detect surrounding quotes")) 94 | .arg(arg!(-n --"no-quote" "do not wrap output in quotes")) 95 | .arg( 96 | arg!(-t --type [type] "type of escape") 97 | .value_parser(clap::builder::EnumValueParser::::new()) 98 | .default_value("generic"), 99 | ) 100 | .arg(arg!([value] "input value, reads from stdin in not present")) 101 | } 102 | 103 | fn parse_args(&self, args: &clap::ArgMatches) -> Result> { 104 | Ok(Box::new(Self { 105 | esc_type: args.get_one::("type").unwrap().clone(), 106 | no_quote: args.get_flag("no-quote"), 107 | no_detect: args.get_flag("no-detect"), 108 | multiline: args.get_flag("multiline"), 109 | })) 110 | } 111 | 112 | fn process(&self, val: Vec) -> Result> { 113 | let to_escape = if self.multiline { 114 | val 115 | } else { 116 | val.trim().into() 117 | }; 118 | 119 | // Detect (unless no_detect) surrounding quotes to: 120 | // - remove them 121 | // - escape 122 | // - restore them 123 | let (quote, to_escape_nq) = if !self.no_detect { 124 | // set unwrap_or result to other chars, just for 125 | // simpler code 126 | let first = *to_escape.first().unwrap_or(&b' '); 127 | let last = *to_escape.last().unwrap_or(&b'*'); 128 | 129 | // If quotes don't match or start/end chars are not quote, 130 | // return quote char according to escape type 131 | if first != last || (first != b'\'' && first != b'"') { 132 | match self.esc_type { 133 | EscType::BashSingle | EscType::Single => (Some(b'\''), to_escape), 134 | _ => (Some(b'"'), to_escape), 135 | } 136 | } else { 137 | // if we have matching quotes, return quote char and remove them 138 | let end_pos = to_escape.len() - 1; 139 | match first { 140 | b'\'' => (Some(b'\''), to_escape[1..end_pos].to_vec()), 141 | b'"' => (Some(b'"'), to_escape[1..end_pos].to_vec()), 142 | _ => (None, to_escape), 143 | } 144 | } 145 | } else { 146 | // no_detect 147 | match self.esc_type { 148 | EscType::BashSingle | EscType::Single => (Some(b'\''), to_escape), 149 | _ => (Some(b'"'), to_escape), 150 | } 151 | }; 152 | 153 | let escaped = to_escape_nq.escape(&self.esc_type); 154 | if self.no_quote || quote.is_none() { 155 | Ok(escaped) 156 | } else { 157 | let mut res = Vec::::with_capacity(escaped.len() + 2); 158 | res.push(quote.unwrap()); 159 | res.extend(escaped); 160 | res.push(quote.unwrap()); 161 | Ok(res) 162 | } 163 | } 164 | 165 | fn new() -> Box { 166 | Box::new(Self { 167 | esc_type: EscType::Generic, 168 | no_quote: false, 169 | no_detect: false, 170 | multiline: false, 171 | }) 172 | } 173 | } 174 | 175 | pub struct UnEscapeApplet { 176 | multiline: bool, 177 | html_entities: bool, 178 | } 179 | 180 | impl Applet for UnEscapeApplet { 181 | fn command(&self) -> &'static str { 182 | "unescape" 183 | } 184 | fn description(&self) -> &'static str { 185 | "(backslash) unescape input strings" 186 | } 187 | 188 | fn new() -> Box { 189 | Box::new(Self { 190 | multiline: false, 191 | html_entities: false, 192 | }) 193 | } 194 | 195 | fn clap_command(&self) -> Command { 196 | Command::new(self.command()) 197 | .about(self.description()) 198 | .arg(arg!(-t --html "unescape HTML entities")) 199 | .arg(arg!(-m --multiline "expect multiline string, do not trim input")) 200 | .arg(arg!([value] "input value, reads from stdin in not present")) 201 | } 202 | 203 | fn parse_args(&self, args: &clap::ArgMatches) -> Result> { 204 | Ok(Box::new(Self { 205 | multiline: args.get_flag("multiline"), 206 | html_entities: args.get_flag("html"), 207 | })) 208 | } 209 | 210 | fn process(&self, val: Vec) -> Result> { 211 | enum EscapeState { 212 | Backslash, 213 | Hex1, 214 | Hex2, 215 | Normal, 216 | } 217 | 218 | let to_unescape = if self.multiline { 219 | val 220 | } else { 221 | val.trim().into() 222 | }; 223 | 224 | // Special case for HTMLEntities 225 | if self.html_entities { 226 | return Ok(decode(&to_unescape).into_bytes()); 227 | }; 228 | 229 | let mut res = Vec::with_capacity(to_unescape.len()); 230 | let mut state = EscapeState::Normal; 231 | let mut hexchars: [u8; 2] = [0, 0]; 232 | for c in to_unescape.iter() { 233 | state = match (state, c) { 234 | (EscapeState::Normal, b'\\') => EscapeState::Backslash, 235 | (EscapeState::Normal, c) => { 236 | res.push(*c); 237 | EscapeState::Normal 238 | } 239 | (EscapeState::Backslash, b'x') => EscapeState::Hex1, 240 | (EscapeState::Backslash, b't') => { 241 | res.push(0x9); 242 | EscapeState::Normal 243 | } 244 | (EscapeState::Backslash, b'n') => { 245 | res.push(0xA); 246 | EscapeState::Normal 247 | } 248 | (EscapeState::Backslash, b'r') => { 249 | res.push(0xD); 250 | EscapeState::Normal 251 | } 252 | (EscapeState::Backslash, c) => { 253 | res.push(*c); 254 | EscapeState::Normal 255 | } 256 | (EscapeState::Hex1, c) => { 257 | hexchars[0] = *c; 258 | EscapeState::Hex2 259 | } 260 | (EscapeState::Hex2, c) => { 261 | hexchars[1] = *c; 262 | res.push( 263 | u8::from_str_radix( 264 | std::str::from_utf8(&hexchars) 265 | .context("invalid hex chars in escaped string")?, 266 | 16, 267 | ) 268 | .context("invalid hex char in escaped string")?, 269 | ); 270 | EscapeState::Normal 271 | } 272 | }; 273 | } 274 | Ok(res) 275 | } 276 | } 277 | 278 | #[cfg(test)] 279 | mod tests { 280 | #[test] 281 | fn test_base_escape_arg_auto() { 282 | assert_cmd::Command::cargo_bin("rsbkb") 283 | .expect("Could not run binary") 284 | .args(&["escape", r"te'st"]) 285 | .assert() 286 | .stdout(r#""te\'st""#) 287 | .success(); 288 | } 289 | 290 | #[test] 291 | fn test_base_escape_stdin_auto() { 292 | assert_cmd::Command::cargo_bin("rsbkb") 293 | .expect("Could not run binary") 294 | .args(&["escape"]) 295 | .write_stdin("'te'st'\n") // by default, trim input so '\n' will be removed 296 | .assert() 297 | .stdout(r"'te\'st'") 298 | .success(); 299 | } 300 | 301 | #[test] 302 | fn test_base_escape_stdin_no_detect() { 303 | assert_cmd::Command::cargo_bin("rsbkb") 304 | .expect("Could not run binary") 305 | .args(&["escape", "-d"]) 306 | // no detect mode will not try to determine enclosing quote type, 307 | // just escape them 308 | .write_stdin(r"'test'") 309 | .assert() 310 | .stdout(r#""\'test\'""#) 311 | .success(); 312 | } 313 | 314 | #[test] 315 | fn test_base_escape_stdin_auto_multiline() { 316 | assert_cmd::Command::cargo_bin("rsbkb") 317 | .expect("Could not run binary") 318 | .args(&["escape", "-m"]) 319 | // multiline mode will not trim '\n', escaping them instead 320 | .write_stdin("te'st\nte\"st\n") 321 | .assert() 322 | .stdout(r#""te\'st\nte\"st\n""#) 323 | .success(); 324 | } 325 | 326 | #[test] 327 | fn test_base_escape_stdin_bash_single() { 328 | assert_cmd::Command::cargo_bin("rsbkb") 329 | .expect("Could not run binary") 330 | .args(&["escape", "-t", "bash-single"]) 331 | .write_stdin("te'st") 332 | .assert() 333 | .stdout(r#"'te'"'"'st'"#) 334 | .success(); 335 | } 336 | 337 | #[test] 338 | fn test_base_escape_stdin_bash() { 339 | assert_cmd::Command::cargo_bin("rsbkb") 340 | .expect("Could not run binary") 341 | .args(&["escape", "-t", "bash"]) 342 | .write_stdin(r#""!t"e`s$t""#) 343 | .assert() 344 | .stdout(r#""\!t\"e\`s\$t""#) 345 | .success(); 346 | } 347 | 348 | #[test] 349 | fn test_base_escape_stdin_posix_shell() { 350 | assert_cmd::Command::cargo_bin("rsbkb") 351 | .expect("Could not run binary") 352 | .args(&["escape", "-t", "shell"]) 353 | .write_stdin(r#""!t"e`s$t""#) 354 | .assert() 355 | .stdout(r#""!t\"e\`s\$t""#) 356 | .success(); 357 | } 358 | 359 | #[test] 360 | fn test_base_escape_stdin_single() { 361 | assert_cmd::Command::cargo_bin("rsbkb") 362 | .expect("Could not run binary") 363 | .args(&["escape", "-t", "single"]) 364 | .write_stdin(r#"sin'gle"#) 365 | .assert() 366 | .stdout(r#"'sin\'gle'"#) 367 | .success(); 368 | } 369 | 370 | #[test] 371 | fn test_base_escape_stdin_single_noquote() { 372 | assert_cmd::Command::cargo_bin("rsbkb") 373 | .expect("Could not run binary") 374 | .args(&["escape", "-t", "single", "-n"]) 375 | .write_stdin(r#"sin'gle"#) 376 | .assert() 377 | .stdout(r#"sin\'gle"#) 378 | .success(); 379 | } 380 | } 381 | -------------------------------------------------------------------------------- /src/findsoapp.rs: -------------------------------------------------------------------------------- 1 | use crate::applet::Applet; 2 | use anyhow::{Context, Result}; 3 | use clap::{arg, Command}; 4 | use goblin::elf; 5 | use std::fs; 6 | use std::path::PathBuf; 7 | use std::str::FromStr; 8 | 9 | pub struct FindSoApplet { 10 | // Function we are looking for 11 | function: Option, 12 | // .so files 13 | files: Option>, 14 | // First .so is a binary to look for dependencies in 15 | is_ref: bool, 16 | // LD_LIBRARY_PATH equivalent 17 | paths: Option>, 18 | // don't show warnings 19 | quiet: bool, 20 | // skip symbolic links in results 21 | skip_symlinks: bool, 22 | } 23 | 24 | fn parse_ld_so_conf(ldconf_path: &str) -> Result> { 25 | let conf_file = fs::read_to_string(ldconf_path) 26 | .with_context(|| format!("Could not read config file \"{}\"", ldconf_path))?; 27 | let conf_lines = conf_file.split('\n'); 28 | 29 | // Handle "normal" lines: skip comments and includes 30 | let mut ldpaths: Vec = conf_lines 31 | .clone() 32 | .filter(|p| !p.starts_with("include ") && p.get(0..1).unwrap_or("#") != "#") // Skip empty lines and comments 33 | .map(|p| PathBuf::from_str(p).unwrap()) 34 | .collect::>(); 35 | 36 | // Handle includes: get a list of included paths 37 | let includes = conf_lines 38 | .filter(|l| l.starts_with("include ")) 39 | .map(|p| p.replace("include ", "")); 40 | 41 | for inc_path in includes.into_iter() { 42 | for inc_match in glob::glob(inc_path.as_str()).unwrap().flatten() { 43 | ldpaths.extend(parse_ld_so_conf(inc_match.to_str().unwrap())?); 44 | } 45 | } 46 | Ok(ldpaths) 47 | } 48 | 49 | impl Applet for FindSoApplet { 50 | fn command(&self) -> &'static str { 51 | "findso" 52 | } 53 | 54 | fn description(&self) -> &'static str { 55 | "find which .so implements a given function" 56 | } 57 | 58 | fn returns_data(&self) -> bool { 59 | false 60 | } 61 | 62 | fn clap_command(&self) -> Command { 63 | Command::new(self.command()) 64 | .about(self.description()) 65 | .arg(arg!(-a --all "search in all '*.so*' files found in LDPATH").conflicts_with("ref")) 66 | .arg(arg!(-s --"skip-symlinks" "ignore symbolic links")) 67 | .arg(arg!(-r --ref "use first file as reference ELF to get .so list from")) 68 | .arg(arg!(-q --quiet "don't show warnings on invalid files")) 69 | .arg(arg!(-p --ldpath "'\':\' separated list of paths to look for .so in'")) 70 | .arg(arg!(-l --ldconf [CONF] "use config file to get LD paths, /etc/ld.so.conf is used if not specified")) 71 | .arg(arg!( "function to search")) 72 | .arg( 73 | arg!([files]... "files to search in, optional if --all is set") 74 | .required_unless_present("all"), 75 | ) 76 | .after_long_help("Examples: 77 | 'findso -a memcpy -l': search for 'memcpy' in all .so files in paths defined in /etc/ld.so.conf 78 | 'findso -r memcpy /bin/ls -l': search for memcpy in all .so files referenced in /bin/ls in system paths 79 | 'findso -q memcpy /usr/lib32/*.so*': search for memcpy in all given files 80 | 'findso -p /usr/lib32/:/usr/lib64/ -a -q memcpy': search for memcpy in given paths") 81 | } 82 | 83 | fn arg_or_stdin(&self) -> Option<&'static str> { 84 | None 85 | } 86 | 87 | fn new() -> Box { 88 | Box::new(Self { 89 | files: None, 90 | function: None, 91 | is_ref: false, 92 | paths: None, 93 | quiet: false, 94 | skip_symlinks: false, 95 | }) 96 | } 97 | 98 | fn parse_args(&self, args: &clap::ArgMatches) -> Result> { 99 | let mut filenames: Vec = Vec::new(); 100 | let function_val = args.get_one::("function").unwrap(); 101 | let paths = if args.contains_id("ldpath") || args.contains_id("ldconf") { 102 | let mut paths: Vec = if let Some(ldpaths) = args.get_one::("ldpath") { 103 | ldpaths 104 | .split(':') 105 | .map(|p| PathBuf::from_str(p).unwrap()) 106 | .collect() 107 | } else { 108 | Vec::::new() 109 | }; 110 | 111 | // parse ld.so.conf "like" file 112 | if args.contains_id("ldconf") { 113 | let ld_so_conf = "/etc/ld.so.conf".to_string(); 114 | let ldconf = args.get_one::("ldconf").or(Some(&ld_so_conf)); 115 | let ldpaths = parse_ld_so_conf(ldconf.unwrap())?; 116 | paths.extend(ldpaths); 117 | } 118 | 119 | // check that paths are actually valid directories 120 | paths.retain(|p| p.is_dir()); 121 | 122 | Some(paths) 123 | } else { 124 | None 125 | }; 126 | 127 | if args.get_flag("all") { 128 | if let Some(paths_v) = &paths { 129 | for p in paths_v { 130 | let so_files: Vec = glob::glob(p.join("*.so*").to_str().unwrap()) 131 | .with_context(|| format!("Could not find .so files in {}", p.display()))? 132 | .map(|p| p.expect("could not find so")) 133 | .collect(); 134 | filenames.extend(so_files); 135 | } 136 | } else { 137 | anyhow::bail!("--all without any paths"); 138 | } 139 | } else { 140 | filenames.extend( 141 | args.get_many::("files") 142 | .unwrap() 143 | .map(|x| PathBuf::from_str(x).unwrap()), 144 | ); 145 | } 146 | 147 | Ok(Box::new(Self { 148 | files: Some(filenames), 149 | function: Some(function_val.to_string()), 150 | is_ref: args.get_flag("ref"), 151 | paths, 152 | quiet: args.get_flag("quiet"), 153 | skip_symlinks: args.get_flag("skip-symlinks"), 154 | })) 155 | } 156 | 157 | fn process(&self, _val: Vec) -> Result> { 158 | let fun = self.function.as_ref().unwrap(); 159 | let mut sofiles: Vec = Vec::from(self.files.as_ref().unwrap().as_slice()); 160 | 161 | // Load dependencies from first file 162 | if self.is_ref { 163 | let f_data: Vec = fs::read(&sofiles[0]) 164 | .with_context(|| format!("Could not read file \"{}\"", sofiles[0].display()))?; 165 | let elf_ref = elf::Elf::parse(f_data.as_slice()) 166 | .with_context(|| "Could not parse reference as ELF")?; 167 | sofiles.extend( 168 | elf_ref 169 | .libraries 170 | .iter() 171 | .map(|l| PathBuf::from_str(l).unwrap()), 172 | ); 173 | }; 174 | 175 | let mut resolved_sofiles: Vec = Vec::new(); 176 | 177 | /* if ld paths were specified, try to resolve file names */ 178 | if let Some(paths) = &self.paths { 179 | for so_path in sofiles.iter_mut() { 180 | if so_path.is_relative() { 181 | for p in paths.iter() { 182 | let full_path = p.join(&so_path); 183 | if full_path.is_file() && self.skip_symlinks && !full_path.is_symlink() { 184 | resolved_sofiles.push(full_path); 185 | } 186 | } 187 | } 188 | } 189 | } else { 190 | resolved_sofiles.extend(sofiles); 191 | } 192 | 193 | for f in resolved_sofiles.iter() { 194 | // Skip directories 195 | if fs::metadata(f) 196 | .with_context(|| format!("Could not open {}", f.display()))? 197 | .is_dir() 198 | { 199 | continue; 200 | } 201 | let f_data = 202 | fs::read(f).with_context(|| format!("Could not read file {}", f.display()))?; 203 | let elf_file = elf::Elf::parse(f_data.as_slice()); 204 | if let Ok(elf_file) = elf_file { 205 | let strtab = elf_file.dynstrtab; 206 | 207 | let found = elf_file 208 | .dynsyms 209 | .iter() 210 | .any(|s| !s.is_import() && strtab.get_at(s.st_name) == Some(fun)); 211 | if found && !(self.skip_symlinks && f.is_symlink()) { 212 | println!("{}", f.display()); 213 | } 214 | } else if !self.quiet { 215 | eprintln!("Could not parse {} as ELF", f.display()); 216 | } 217 | } 218 | 219 | /* Return empty Vec as we output directly on stdout */ 220 | Ok(Vec::::new()) 221 | } 222 | } 223 | -------------------------------------------------------------------------------- /src/flateapp.rs: -------------------------------------------------------------------------------- 1 | use crate::applet::Applet; 2 | use anyhow::Result; 3 | use clap::{arg, value_parser, Command}; 4 | use miniz_oxide::{deflate, inflate, DataFormat}; 5 | 6 | pub struct DeflateApplet { 7 | format: DataFormat, 8 | level: u8, 9 | } 10 | 11 | impl Applet for DeflateApplet { 12 | fn command(&self) -> &'static str { 13 | "deflate" 14 | } 15 | 16 | fn clap_command(&self) -> Command { 17 | Command::new(self.command()) 18 | .about(self.description()) 19 | .arg( 20 | arg!(-l --level "compression level") 21 | .value_parser(value_parser!(u8).range(1..11)) 22 | .default_value("6"), 23 | ) 24 | .arg(arg!(-z --zlib "add Zlib header")) 25 | .arg(arg!([value] "input value, reads from stdin in not present")) 26 | } 27 | 28 | fn description(&self) -> &'static str { 29 | "(raw) deflate compression" 30 | } 31 | 32 | fn parse_args(&self, args: &clap::ArgMatches) -> Result> { 33 | let f = if args.get_flag("zlib") { 34 | DataFormat::Zlib 35 | } else { 36 | DataFormat::Raw 37 | }; 38 | let l: &u8 = args.get_one("level").unwrap(); 39 | Ok(Box::new(Self { 40 | format: f, 41 | level: *l, 42 | })) 43 | } 44 | 45 | fn process(&self, val: Vec) -> Result> { 46 | match self.format { 47 | DataFormat::Raw => Ok(deflate::compress_to_vec(val.as_slice(), self.level)), 48 | _ => Ok(deflate::compress_to_vec_zlib(val.as_slice(), self.level)), 49 | } 50 | } 51 | 52 | fn new() -> Box { 53 | Box::new(Self { 54 | format: DataFormat::Raw, 55 | level: 6, 56 | }) 57 | } 58 | } 59 | 60 | pub struct InflateApplet { 61 | format: DataFormat, 62 | quiet: bool, 63 | } 64 | 65 | impl Applet for InflateApplet { 66 | fn command(&self) -> &'static str { 67 | "inflate" 68 | } 69 | 70 | fn clap_command(&self) -> Command { 71 | Command::new(self.command()) 72 | .about(self.description()) 73 | .arg(arg!(-z --zlib "expect Zlib header")) 74 | .arg(arg!(-q --quiet "don't output error message on stderr if decompression failed")) 75 | .arg(arg!([value] "input value, reads from stdin in not present")) 76 | } 77 | 78 | fn description(&self) -> &'static str { 79 | "(raw) inflate decompression" 80 | } 81 | 82 | fn parse_args(&self, args: &clap::ArgMatches) -> Result> { 83 | let f = if args.get_flag("zlib") { 84 | DataFormat::Zlib 85 | } else { 86 | DataFormat::Raw 87 | }; 88 | Ok(Box::new(Self { 89 | format: f, 90 | quiet: args.get_flag("quiet"), 91 | })) 92 | } 93 | 94 | fn process(&self, val: Vec) -> Result> { 95 | let dec_res = match self.format { 96 | DataFormat::Raw => inflate::decompress_to_vec(val.as_slice()), 97 | _ => inflate::decompress_to_vec_zlib(val.as_slice()), 98 | }; 99 | match dec_res { 100 | Ok(r) => Ok(r), 101 | Err(e) => { 102 | if !self.quiet { 103 | eprintln!( 104 | "Decompression error: {:?} (still outputing data to stdout)", 105 | e.status 106 | ); 107 | } 108 | Ok(e.output) 109 | } 110 | } 111 | } 112 | 113 | fn new() -> Box { 114 | Box::new(Self { 115 | format: DataFormat::Raw, 116 | quiet: false, 117 | }) 118 | } 119 | } 120 | 121 | #[cfg(test)] 122 | mod tests { 123 | use super::*; 124 | 125 | #[test] 126 | fn test_inflate() { 127 | let inf = InflateApplet { 128 | quiet: true, 129 | format: DataFormat::Raw, 130 | }; 131 | assert_eq!( 132 | inf.process([0x2b, 0x49, 0x2d, 0x2e, 0x29, 0x01, 0x62, 0x2e, 0x00].to_vec()) 133 | .unwrap(), 134 | "testtest\n".as_bytes().to_vec() 135 | ); 136 | } 137 | 138 | #[test] 139 | fn test_inflate_trunc() { 140 | let inf = InflateApplet { 141 | quiet: true, 142 | format: DataFormat::Raw, 143 | }; 144 | assert_eq!( 145 | inf.process([0x2b, 0x49, 0x2d, 0x2e, 0x29].to_vec()) 146 | .unwrap(), 147 | [116, 101, 115, 116, 0, 0, 0, 0, 0, 0].to_vec() 148 | ); 149 | } 150 | 151 | #[test] 152 | fn test_inflate_no_header() { 153 | let inf = InflateApplet { 154 | quiet: true, 155 | format: DataFormat::Zlib, 156 | }; 157 | assert_eq!( 158 | inf.process([0x2b, 0x49, 0x2d, 0x2e, 0x29].to_vec()) 159 | .unwrap(), 160 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0].to_vec() 161 | ); 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /src/hexapp.rs: -------------------------------------------------------------------------------- 1 | use crate::applet::Applet; 2 | use crate::applet::SliceExt; 3 | use anyhow::{Context, Result}; 4 | use clap::{arg, Command}; 5 | 6 | pub struct HexApplet {} 7 | 8 | impl Applet for HexApplet { 9 | fn command(&self) -> &'static str { 10 | "hex" 11 | } 12 | fn description(&self) -> &'static str { 13 | "hex encode" 14 | } 15 | 16 | fn parse_args(&self, _args: &clap::ArgMatches) -> Result> { 17 | Ok(Box::new(Self {})) 18 | } 19 | 20 | fn process(&self, val: Vec) -> Result> { 21 | Ok(hex::encode(val).as_bytes().to_vec()) 22 | } 23 | 24 | fn new() -> Box { 25 | Box::new(Self {}) 26 | } 27 | } 28 | 29 | pub struct UnHexApplet { 30 | hexonly: bool, 31 | strict: bool, 32 | } 33 | 34 | impl UnHexApplet { 35 | fn hex_decode_hexonly(&self, val: Vec) -> Result> { 36 | let mut trimmed: Vec = val.trim().into(); 37 | let res = hex::decode(&trimmed); 38 | if self.strict { 39 | return res.with_context(|| "Invalid hex input"); 40 | } 41 | /* remove spaces */ 42 | trimmed.retain(|&x| x != 0x20); 43 | let res = hex::decode(&trimmed); 44 | match res { 45 | Ok(decoded) => Ok(decoded), 46 | Err(e) => match e { 47 | hex::FromHexError::InvalidHexCharacter { c: _, index } => { 48 | let mut end = trimmed.split_off(index); 49 | let mut decoded = self.hex_decode_hexonly(trimmed)?; 50 | decoded.append(&mut end); 51 | Ok(decoded) 52 | } 53 | hex::FromHexError::OddLength => { 54 | // TODO: refactor 55 | let mut end = trimmed.split_off(trimmed.len() - 1); 56 | let mut decoded = self.hex_decode_hexonly(trimmed)?; 57 | decoded.append(&mut end); 58 | Ok(decoded) 59 | } 60 | _ => panic!("{}", e), 61 | }, 62 | } 63 | } 64 | 65 | fn hex_decode_all(&self, hexval: Vec) -> Result> { 66 | let mut res: Vec = vec![]; 67 | let iter = &mut hexval.windows(2); 68 | let mut last: &[u8] = &[]; 69 | loop { 70 | let chro = iter.next(); 71 | let chr = match chro { 72 | None => { 73 | res.extend_from_slice(last); 74 | return Ok(res); 75 | } 76 | Some(a) => a, 77 | }; 78 | 79 | if (chr[0] as char).is_ascii_hexdigit() && (chr[1] as char).is_ascii_hexdigit() { 80 | res.append(&mut hex::decode(chr).with_context(|| "hex decoding failed")?); 81 | /* make sure we dont miss the last char if we have something like 82 | * "41 " as input */ 83 | let next_win = iter.next().unwrap_or(&[]); 84 | if next_win.len() > 1 { 85 | last = &next_win[1..2] 86 | } else { 87 | last = &[] 88 | }; 89 | } else { 90 | res.extend_from_slice(&chr[0..1]); 91 | last = &chr[1..2]; 92 | } 93 | } 94 | } 95 | } 96 | 97 | impl Applet for UnHexApplet { 98 | fn command(&self) -> &'static str { 99 | "unhex" 100 | } 101 | fn description(&self) -> &'static str { 102 | "hex decode" 103 | } 104 | 105 | fn new() -> Box { 106 | Box::new(Self { 107 | hexonly: false, 108 | strict: false, 109 | }) 110 | } 111 | 112 | fn clap_command(&self) -> Command { 113 | Command::new(self.command()).about(self.description()) 114 | .arg(arg!(-o --"hex-only" "expect only hex data, stop at first non-hex byte (but copy the rest, except spaces)")) 115 | .arg(arg!(-s --strict "strict decoding, error on invalid data")) 116 | .arg(arg!([value] "input value, reads from stdin in not present")) 117 | .after_help("By default, decode all hex data in the input, regardless of garbage in-between.") 118 | } 119 | 120 | fn parse_args(&self, args: &clap::ArgMatches) -> Result> { 121 | Ok(Box::new(Self { 122 | hexonly: args.get_flag("hex-only") || args.get_flag("strict"), 123 | strict: args.get_flag("strict"), 124 | })) 125 | } 126 | 127 | fn process(&self, val: Vec) -> Result> { 128 | if self.hexonly { 129 | self.hex_decode_hexonly(val) 130 | } else { 131 | self.hex_decode_all(val) 132 | } 133 | } 134 | } 135 | 136 | #[cfg(test)] 137 | mod tests { 138 | use super::*; 139 | 140 | #[test] 141 | fn test_hex_cli_arg() { 142 | assert_cmd::Command::cargo_bin("rsbkb") 143 | .expect("Could not run binary") 144 | .args(&["hex", "aAé!"]) 145 | .assert() 146 | .stdout("6141c3a921") 147 | .success(); 148 | } 149 | 150 | #[test] 151 | fn test_hex_cli_stdin() { 152 | assert_cmd::Command::cargo_bin("rsbkb") 153 | .expect("Could not run binary") 154 | .args(&["hex"]) 155 | .write_stdin("aAé!\n") 156 | .assert() 157 | .stdout("6141c3a9210a") 158 | .success(); 159 | } 160 | 161 | #[test] 162 | fn test_unhex_cli_arg() { 163 | assert_cmd::Command::cargo_bin("rsbkb") 164 | .expect("Could not run binary") 165 | .args(&["unhex", "6141210a00ff"]) 166 | .assert() 167 | .stdout(&b"aA!\n\x00\xff"[..]) 168 | .success(); 169 | } 170 | 171 | #[test] 172 | fn test_unhex_cli_stdin() { 173 | assert_cmd::Command::cargo_bin("rsbkb") 174 | .expect("Could not run binary") 175 | .args(&["unhex"]) 176 | .write_stdin("41ff\n00FF") 177 | .assert() 178 | .stdout(&[0x41, 0xFF, 0x0A, 0x00, 0xFF][..]) 179 | .success(); 180 | } 181 | 182 | #[test] 183 | fn test_unhex_cli_stdin_hexonly() { 184 | assert_cmd::Command::cargo_bin("rsbkb") 185 | .expect("Could not run binary") 186 | .args(&["unhex", "-o"]) 187 | .write_stdin("41ff\n00FF") 188 | .assert() 189 | .stdout(&b"A\xFF\n00FF"[..]) 190 | .success(); 191 | } 192 | 193 | #[test] 194 | fn test_unhex_cli_stdin_strict() { 195 | assert_cmd::Command::cargo_bin("rsbkb") 196 | .expect("Could not run binary") 197 | .args(&["unhex", "-s"]) 198 | .write_stdin("41l") 199 | .assert() 200 | .stdout(&b""[..]) 201 | .stderr(predicates::str::contains("Odd number of digits")) 202 | .failure(); 203 | assert_cmd::Command::cargo_bin("rsbkb") 204 | .expect("Could not run binary") 205 | .args(&["unhex", "-s"]) 206 | .write_stdin("41ll") 207 | .assert() 208 | .stdout(&b""[..]) 209 | .stderr(predicates::str::contains("Invalid character")) 210 | .failure(); 211 | } 212 | 213 | #[test] 214 | fn test_hex() { 215 | let hex = HexApplet {}; 216 | assert_eq!( 217 | String::from_utf8(hex.process_test([0, 0xFF].to_vec())).unwrap(), 218 | "00ff" 219 | ); 220 | } 221 | 222 | #[test] 223 | fn test_unhex_hexonly() { 224 | let unhex = UnHexApplet { 225 | strict: false, 226 | hexonly: true, 227 | }; 228 | assert_eq!( 229 | unhex 230 | .process("01 23 45 67 89 ab cd ef".as_bytes().to_vec()) 231 | .unwrap(), 232 | [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef] 233 | ); 234 | assert_eq!( 235 | unhex 236 | .process("0123456789abcdef".as_bytes().to_vec()) 237 | .unwrap(), 238 | [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef] 239 | ); 240 | } 241 | 242 | #[test] 243 | fn test_unhex() { 244 | let unhex = UnHexApplet { 245 | strict: false, 246 | hexonly: false, 247 | }; 248 | assert_eq!( 249 | unhex.process("test52af ".as_bytes().to_vec()).unwrap(), 250 | [0x74, 0x65, 0x73, 0x74, 0x52, 0xaf, 0x20] 251 | ); 252 | assert_eq!( 253 | unhex.process("test52af".as_bytes().to_vec()).unwrap(), 254 | [0x74, 0x65, 0x73, 0x74, 0x52, 0xaf] 255 | ); 256 | assert_eq!( 257 | unhex.process("!52af".as_bytes().to_vec()).unwrap(), 258 | [0x21, 0x52, 0xaf] 259 | ); 260 | assert_eq!( 261 | unhex.process("!5 2af".as_bytes().to_vec()).unwrap(), 262 | [0x21, 0x35, 0x20, 0x2a, 0x66] 263 | ); 264 | } 265 | } 266 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | use anyhow::{anyhow, Context, Result}; 2 | 3 | use std::io::{self, IsTerminal}; 4 | use std::io::{Read, Write}; 5 | use std::path::Path; 6 | extern crate base64; 7 | extern crate clap; 8 | use clap::Command; 9 | extern crate crc; 10 | extern crate hex; 11 | extern crate percent_encoding; 12 | 13 | mod applet; 14 | use applet::Applet; 15 | 16 | mod hexapp; 17 | use hexapp::HexApplet; 18 | use hexapp::UnHexApplet; 19 | 20 | mod urlapp; 21 | use urlapp::UrlDecApplet; 22 | use urlapp::UrlEncApplet; 23 | 24 | mod b64app; 25 | use b64app::B64DecApplet; 26 | use b64app::B64EncApplet; 27 | 28 | mod crcapp; 29 | use crcapp::CRC16Applet; 30 | use crcapp::CRC32Applet; 31 | use crcapp::CRCApplet; 32 | 33 | mod xorapp; 34 | use xorapp::XorApplet; 35 | 36 | mod sliceapp; 37 | use sliceapp::SliceApplet; 38 | 39 | mod timeapp; 40 | use timeapp::TimeApplet; 41 | 42 | mod patternapp; 43 | use patternapp::BofPattGenApplet; 44 | use patternapp::BofPattOffApplet; 45 | 46 | mod entropyapp; 47 | use entropyapp::EntropyApplet; 48 | 49 | mod bgrepapp; 50 | use bgrepapp::BgrepApplet; 51 | 52 | mod findsoapp; 53 | use findsoapp::FindSoApplet; 54 | 55 | mod flateapp; 56 | use flateapp::DeflateApplet; 57 | use flateapp::InflateApplet; 58 | 59 | mod baseapp; 60 | use baseapp::BaseIntApplet; 61 | 62 | mod escapeapp; 63 | use escapeapp::EscapeApplet; 64 | use escapeapp::UnEscapeApplet; 65 | 66 | // Helper to "register" applets 67 | macro_rules! applets { 68 | ($a:ident = $($x:ident),* ) => 69 | { 70 | let $a : Vec>= vec![$($x::new(),)*]; 71 | 72 | }; 73 | } 74 | 75 | fn main() -> Result<()> { 76 | applets!( 77 | apps = HexApplet, 78 | UnHexApplet, 79 | UrlEncApplet, 80 | UrlDecApplet, 81 | CRC16Applet, 82 | CRC32Applet, 83 | CRCApplet, 84 | B64EncApplet, 85 | B64DecApplet, 86 | BofPattOffApplet, 87 | BofPattGenApplet, 88 | XorApplet, 89 | EntropyApplet, 90 | SliceApplet, 91 | BgrepApplet, 92 | FindSoApplet, 93 | TimeApplet, 94 | DeflateApplet, 95 | InflateApplet, 96 | BaseIntApplet, 97 | EscapeApplet, 98 | UnEscapeApplet 99 | ); 100 | 101 | // Define a busybox-like multicall binary 102 | // Subcommands must be defined both as subcommands for "rsbkb" and 103 | // as main subcommands 104 | let mut app = clap::Command::new("rsbkb") 105 | .multicall(true) 106 | .version(env!("CARGO_PKG_VERSION")) 107 | .propagate_version(true) 108 | .subcommand( 109 | Command::new("rsbkb") 110 | .help_template( 111 | "\ 112 | {before-help}{name} {version} ({about}) -- GPL v3 -- by {author-with-newline} 113 | {usage-heading} {usage} 114 | 115 | {all-args}{after-help} 116 | ", 117 | ) 118 | .author("Raphaël Rigo ") 119 | .about("Rust BlackBag") 120 | .arg_required_else_help(true) 121 | .subcommands([Command::new("list").about("list applets")]) 122 | .subcommands([ 123 | Command::new("symlink").about("create symbolic links for applets (Unix only)") 124 | ]) 125 | .subcommand_value_name("APPLET") 126 | .subcommand_help_heading("APPLETS") 127 | .subcommands(apps.iter().map(|app| app.clap_command())), 128 | ) 129 | .subcommands(apps.iter().map(|app| app.clap_command())); 130 | 131 | // Parse args 132 | let matches = app.get_matches_mut(); 133 | 134 | /* Check if we're called as "rsbkb" */ 135 | let subc = matches.subcommand_name(); 136 | let real_matches = if subc == Some("rsbkb") { 137 | // get applet 138 | matches.subcommand().unwrap().1 139 | } else { 140 | &matches 141 | }; 142 | 143 | // Get subcommand and args 144 | let (subcommand, sub_matches) = real_matches 145 | .subcommand() 146 | .ok_or_else(|| anyhow!("Subcommand required"))?; 147 | 148 | // list applets 149 | if subcommand == "list" { 150 | for app in apps.iter() { 151 | println!("{}", app.command()); 152 | } 153 | return Ok(()); 154 | } else if subcommand == "symlink" { 155 | if cfg!(unix) { 156 | let exe = std::env::current_exe().context("Could not determine rsbkb binary path")?; 157 | let exe_base = exe 158 | .file_name() 159 | .ok_or_else(|| anyhow!("Could not determine rsbkb exe name"))?; 160 | let dir = exe 161 | .parent() 162 | .ok_or_else(|| anyhow!("Could not determine rsbkb binary folder"))?; 163 | println!("Symlinking applets to {:?} in folder {:?}", exe_base, dir); 164 | for app in apps.iter() { 165 | let res = std::os::unix::fs::symlink(&exe, dir.join(app.command())); 166 | if res.is_err() { 167 | println!("Could not symlink {}: {:?}", app.command(), res); 168 | } 169 | } 170 | } else { 171 | println!("symlink command is only supported on Unix-like operating systems."); 172 | } 173 | return Ok(()); 174 | } 175 | 176 | // Find corresponding app 177 | let selected_app = apps.iter().find(|a| a.command() == subcommand).unwrap(); 178 | 179 | // Parse applet args and get actual applet with options 180 | let selected_app = selected_app.parse_args(sub_matches)?; 181 | 182 | let mut inputval = vec![]; 183 | 184 | if let Some(argname) = selected_app.arg_or_stdin() { 185 | /* Check if the given arg is present, else read from stdin */ 186 | if !sub_matches.contains_id(argname) { 187 | io::stdin() 188 | .read_to_end(&mut inputval) 189 | .expect("Reading stdin failed"); 190 | } else { 191 | /* Check if the given argument could be a filename, which is probably not 192 | * what the user wants */ 193 | let argname_val: &String = sub_matches.get_one::(argname).unwrap(); 194 | if Path::new(argname_val).exists() { 195 | eprintln!( 196 | "'{}' is a file, maybe you want to pass it to stdin instead?", 197 | argname_val 198 | ); 199 | } 200 | inputval = argname_val.as_bytes().to_vec(); 201 | } 202 | }; 203 | 204 | let res = selected_app.process(inputval)?; 205 | 206 | if selected_app.returns_data() { 207 | let mut stdout = io::stdout(); 208 | let write_res = stdout.write_all(&res); 209 | 210 | // Ignore broken pipe 211 | match write_res { 212 | Err(err) if err.kind() != std::io::ErrorKind::BrokenPipe => { 213 | return Err(err.into()); 214 | } 215 | Err(_) => { 216 | return Ok(()); 217 | } 218 | Ok(_) => (), 219 | }; 220 | 221 | /* Only add a newline when outputing to a terminal */ 222 | if std::io::stdout().is_terminal() { 223 | println!(); 224 | } 225 | } 226 | Ok(()) 227 | } 228 | -------------------------------------------------------------------------------- /src/patternapp.rs: -------------------------------------------------------------------------------- 1 | use crate::applet::Applet; 2 | use crate::applet::FromStrWithRadix; 3 | use anyhow::Result; 4 | use clap::{arg, Command}; 5 | use std::char; 6 | 7 | pub struct BofPattGenApplet { 8 | len: usize, 9 | } 10 | 11 | const UPPER: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 12 | const LOWER: &str = "abcdefghijklmnopqrstuvwxyz"; 13 | const DIGITS: &str = "0123456789"; 14 | 15 | fn gen_pattern(len: usize, res: &mut Vec) { 16 | for u in UPPER.bytes() { 17 | if res.len() >= len { 18 | return; 19 | } 20 | for l in LOWER.bytes() { 21 | for d in DIGITS.bytes() { 22 | res.push(u); 23 | res.push(l); 24 | res.push(d); 25 | } 26 | } 27 | } 28 | gen_pattern(len - res.len(), res); 29 | } 30 | 31 | impl Applet for BofPattGenApplet { 32 | fn command(&self) -> &'static str { 33 | "bofpatt" 34 | } 35 | fn description(&self) -> &'static str { 36 | "buffer overflow pattern generator" 37 | } 38 | 39 | fn new() -> Box { 40 | Box::new(Self { len: 0 }) 41 | } 42 | 43 | fn arg_or_stdin(&self) -> Option<&'static str> { 44 | None 45 | } 46 | 47 | fn clap_command(&self) -> Command { 48 | Command::new(self.command()) 49 | .about(self.description()) 50 | .arg(arg!( "Pattern length")) 51 | } 52 | 53 | fn parse_args(&self, args: &clap::ArgMatches) -> Result> { 54 | let max_len: usize = UPPER.len() * LOWER.len() * DIGITS.len() * 3; 55 | let len_s = args.get_one::("length").unwrap(); 56 | let len = usize::from_str_with_radix(len_s)?; 57 | if len > max_len { 58 | eprintln!("Warning: pattern length's longer than max_len {}.", max_len); 59 | } 60 | Ok(Box::new(Self { len })) 61 | } 62 | 63 | fn process(&self, _data: Vec) -> Result> { 64 | let mut res: Vec = Vec::with_capacity(self.len); 65 | gen_pattern(self.len, &mut res); 66 | res.truncate(self.len); 67 | Ok(res) 68 | } 69 | } 70 | 71 | pub struct BofPattOffApplet { 72 | extract: String, 73 | } 74 | 75 | impl Applet for BofPattOffApplet { 76 | fn command(&self) -> &'static str { 77 | "bofpattoff" 78 | } 79 | fn description(&self) -> &'static str { 80 | "buffer overflow pattern offset finder" 81 | } 82 | 83 | fn new() -> Box { 84 | Box::new(Self { 85 | extract: String::new(), 86 | }) 87 | } 88 | 89 | fn arg_or_stdin(&self) -> Option<&'static str> { 90 | None 91 | } 92 | 93 | fn clap_command(&self) -> Command { 94 | Command::new(self.command()) 95 | .about(self.description()) 96 | .arg(arg!(-b --"big-endian" "Parse hex value as big endian")) 97 | .arg(arg!( "Pattern extract (Use 0xAABBCCDD for reg value)")) 98 | } 99 | 100 | fn parse_args(&self, args: &clap::ArgMatches) -> Result> { 101 | let mut extract = String::new(); 102 | let arg_val = args.get_one::("extract").unwrap(); 103 | let big_endian = args.get_flag("big-endian"); 104 | if &arg_val[0..2] == "0x" { 105 | let mut arg_int = u64::from_str_with_radix(arg_val)?; 106 | while arg_int != 0 { 107 | let c = char::from_u32((arg_int & 0xFF) as u32).unwrap(); 108 | if big_endian { 109 | extract.insert(0, c); 110 | } else { 111 | extract.push(c); 112 | } 113 | arg_int >>= 8; 114 | } 115 | println!("Decoded pattern: {} (big endian: {})", extract, big_endian); 116 | } else { 117 | extract.push_str(arg_val); 118 | } 119 | Ok(Box::new(Self { extract })) 120 | } 121 | 122 | fn process(&self, _val: Vec) -> Result> { 123 | let max_len: usize = UPPER.len() * LOWER.len() * DIGITS.len() * 3; 124 | let mut full_pattern: Vec = Vec::with_capacity(max_len); 125 | gen_pattern(max_len, &mut full_pattern); 126 | let pattern_str = String::from_utf8(full_pattern).unwrap(); 127 | let offset = pattern_str.find(self.extract.as_str()); 128 | let res = match offset { 129 | Some(o) => format!("Offset: {} (mod {}) / {:#x}", o, max_len, o), 130 | _ => String::from("Pattern not found"), 131 | }; 132 | Ok(res.as_bytes().to_vec()) 133 | } 134 | } 135 | 136 | #[cfg(test)] 137 | mod tests { 138 | use super::*; 139 | 140 | #[test] 141 | fn test_gen() { 142 | let pat = BofPattGenApplet { len: 40 }; 143 | assert_eq!( 144 | String::from_utf8(pat.process_test(vec![])).unwrap(), 145 | "Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2A" 146 | ); 147 | } 148 | 149 | #[test] 150 | fn test_off() { 151 | let pat = BofPattOffApplet { 152 | extract: String::from("Yq6Y"), 153 | }; 154 | assert_eq!( 155 | String::from_utf8(pat.process_test(vec![])).unwrap(), 156 | "Offset: 19218 (mod 20280) / 0x4b12" 157 | ); 158 | } 159 | 160 | #[test] 161 | fn test_not_found() { 162 | let pat = BofPattOffApplet { 163 | extract: String::from("***"), 164 | }; 165 | assert_eq!( 166 | String::from_utf8(pat.process_test(vec![])).unwrap(), 167 | "Pattern not found" 168 | ); 169 | } 170 | } 171 | -------------------------------------------------------------------------------- /src/sliceapp.rs: -------------------------------------------------------------------------------- 1 | use crate::applet::{Applet, FromStrWithRadix}; 2 | use anyhow::{bail, Context, Result}; 3 | use clap::{arg, Command}; 4 | use std::fs::{File, OpenOptions}; 5 | use std::io::{BufRead, BufReader, Read, Seek, SeekFrom}; 6 | 7 | #[derive(Debug)] 8 | struct Position { 9 | offset: u64, 10 | relative: bool, 11 | from_end: bool, 12 | } 13 | 14 | pub struct SliceApplet { 15 | file: Option, 16 | start: Position, 17 | end: Option, 18 | } 19 | 20 | /* Helper to parse "start" and "end". 21 | * return value: value, plus_prefix, minus_prefix 22 | */ 23 | fn parse_value_with_prefix(s: &String) -> Result { 24 | if s.is_empty() { 25 | bail!("Invalid length for value"); 26 | } 27 | 28 | let first = s.chars().next().unwrap(); 29 | 30 | let str_stripped = &s[1..].to_string(); 31 | 32 | let (from_end, relative, str_strip): (bool, bool, &String) = if first == '-' { 33 | (true, false, str_stripped) 34 | } else if first == '+' { 35 | (false, true, str_stripped) 36 | } else { 37 | (false, false, s) 38 | }; 39 | let offset = u64::from_str_with_radix(str_strip).with_context(|| "Invalid offset value")?; 40 | Ok(Position { 41 | offset, 42 | relative, 43 | from_end, 44 | }) 45 | } 46 | 47 | impl Applet for SliceApplet { 48 | fn command(&self) -> &'static str { 49 | "slice" 50 | } 51 | fn description(&self) -> &'static str { 52 | "cut slices from file or stdin" 53 | } 54 | 55 | fn clap_command(&self) -> Command { 56 | Command::new(self.command()) 57 | .about(self.description()) 58 | .arg(arg!( "file to slice, - for stdin")) 59 | .arg(arg!( "start of slice, relative to end of file if negative")) 60 | .arg(arg!([end] "end of slice: absolute, relative to if prefixed with +, relative to end of file if negative")) 61 | } 62 | 63 | fn arg_or_stdin(&self) -> Option<&'static str> { 64 | None 65 | } 66 | 67 | fn new() -> Box { 68 | Box::new(Self { 69 | file: None, 70 | start: Position { 71 | offset: 0, 72 | relative: false, 73 | from_end: false, 74 | }, 75 | end: None, 76 | }) 77 | } 78 | 79 | fn parse_args(&self, args: &clap::ArgMatches) -> Result> { 80 | let filename = args.get_one::("file").unwrap(); 81 | let start_val = args.get_one::("start").unwrap(); 82 | let end_opt = args.get_one::("end"); 83 | 84 | let start = parse_value_with_prefix(start_val)?; 85 | 86 | let end = match end_opt { 87 | None => None, 88 | Some(end_val) => Some(parse_value_with_prefix(end_val)?), 89 | }; 90 | 91 | Ok(Box::new(Self { 92 | file: Some(filename.to_string()), 93 | start, 94 | end, 95 | })) 96 | } 97 | 98 | fn process(&self, _val: Vec) -> Result> { 99 | let filename = self.file.as_ref().unwrap(); 100 | 101 | if filename == "-" 102 | || File::open(filename) 103 | .with_context(|| format!("can't open file \"{}\"", filename))? 104 | .rewind() 105 | .is_err() 106 | { 107 | if self.start.from_end || self.end.as_ref().is_some_and(|e| e.from_end) { 108 | bail!("Cannot seek from end in an unseekable file"); 109 | } 110 | self.process_unseekable(filename) 111 | } else { 112 | self.process_seekable(filename) 113 | } 114 | } 115 | } 116 | 117 | impl SliceApplet { 118 | fn process_unseekable(&self, filename: &str) -> Result> { 119 | let mut f: Box = if filename == "-" { 120 | Box::new(BufReader::new(std::io::stdin())) 121 | } else { 122 | Box::new(BufReader::new( 123 | OpenOptions::new() 124 | .read(true) 125 | .write(false) 126 | .open(filename) 127 | .with_context(|| format!("can't open file \"{}\"", filename))?, 128 | )) 129 | }; 130 | 131 | // Read initial data 132 | let mut res = vec![0; self.start.offset as usize]; 133 | 134 | f.read_exact(&mut res) 135 | .with_context(|| "Could not read until start")?; 136 | 137 | // Drop it 138 | res.clear(); 139 | 140 | let start = self.start.offset; 141 | 142 | if self.end.is_some() { 143 | let end_pos = self.end.as_ref().unwrap(); 144 | 145 | let end = if end_pos.relative { 146 | start + end_pos.offset 147 | } else { 148 | end_pos.offset 149 | }; 150 | 151 | if end < start { 152 | bail!("specified end < start"); 153 | } 154 | let len: usize = (end - start) as usize; 155 | res.resize(len, 0); 156 | f.read_exact(&mut res).with_context(|| "Read failed")?; 157 | } else { 158 | f.read_to_end(&mut res).with_context(|| "Read failed")?; 159 | } 160 | Ok(res.to_vec()) 161 | } 162 | 163 | fn process_seekable(&self, filename: &str) -> Result> { 164 | let f = OpenOptions::new() 165 | .read(true) 166 | .write(false) 167 | .open(filename) 168 | .with_context(|| format!("can't open file \"{}\"", filename))?; 169 | let flen = f 170 | .metadata() 171 | .with_context(|| format!("Could not get file len for {}", filename))? 172 | .len(); 173 | let mut fbuf = BufReader::new(&f); 174 | 175 | let start = if self.start.from_end { 176 | if self.start.offset > flen { 177 | bail!("start is before beginning of file"); 178 | } 179 | flen - self.start.offset 180 | } else { 181 | self.start.offset 182 | }; 183 | if start > flen { 184 | bail!("start (0x{:X}) is after end of file (0x{:X})", start, flen); 185 | } 186 | fbuf.seek(SeekFrom::Start(start)) 187 | .with_context(|| "seek failed")?; 188 | 189 | let mut res = vec![]; 190 | if self.end.is_some() { 191 | let end_pos = self.end.as_ref().unwrap(); 192 | let end = if end_pos.from_end { 193 | flen - end_pos.offset 194 | } else if end_pos.relative { 195 | start + end_pos.offset 196 | } else { 197 | end_pos.offset 198 | }; 199 | 200 | if end < start { 201 | bail!("specified end < start"); 202 | } else if end > flen { 203 | bail!("end (0x{:X}) is after end of file (0x{:X})", end, flen); 204 | } 205 | let len: usize = (end - start) as usize; 206 | res.resize(len, 0); 207 | fbuf.read_exact(&mut res).with_context(|| "Read failed")?; 208 | } else { 209 | fbuf.read_to_end(&mut res).with_context(|| "Read failed")?; 210 | } 211 | Ok(res.to_vec()) 212 | } 213 | } 214 | 215 | #[cfg(test)] 216 | mod tests { 217 | use super::*; 218 | use rand::{thread_rng, Rng}; 219 | use std::io::Write; 220 | use tempfile::NamedTempFile; 221 | 222 | fn setup() -> (NamedTempFile, [u8; 100]) { 223 | let mut rand_data = [0u8; 100]; 224 | thread_rng().fill(&mut rand_data[..]); 225 | 226 | let mut tmpfile = tempfile::NamedTempFile::new().unwrap(); 227 | tmpfile.write(&rand_data).unwrap(); 228 | (tmpfile, rand_data) 229 | } 230 | 231 | #[test] 232 | fn test_empty_slice() { 233 | let (tmpfile, d) = setup(); 234 | let filepath = tmpfile.path().to_str().unwrap().to_string(); 235 | let pat = SliceApplet { 236 | file: Some(filepath), 237 | start: Position { 238 | offset: 0, 239 | relative: false, 240 | from_end: false, 241 | }, 242 | end: Some(Position { 243 | offset: 0, 244 | relative: false, 245 | from_end: false, 246 | }), 247 | }; 248 | 249 | assert_eq!(d[0..0], pat.process_test(Vec::new())); 250 | } 251 | 252 | #[test] 253 | fn test_slice() { 254 | let (tmpfile, d) = setup(); 255 | let filepath = tmpfile.path().to_str().unwrap().to_string(); 256 | let pat = SliceApplet { 257 | file: Some(filepath), 258 | start: Position { 259 | offset: 0, 260 | relative: false, 261 | from_end: false, 262 | }, 263 | end: Some(Position { 264 | offset: 10, 265 | relative: false, 266 | from_end: false, 267 | }), 268 | }; 269 | 270 | assert_eq!(d[0..10], pat.process_test(Vec::new())); 271 | } 272 | 273 | #[test] 274 | fn test_slice_to_end() { 275 | let (tmpfile, d) = setup(); 276 | let filepath = tmpfile.path().to_str().unwrap().to_string(); 277 | let pat = SliceApplet { 278 | file: Some(filepath), 279 | start: Position { 280 | offset: 10, 281 | relative: false, 282 | from_end: false, 283 | }, 284 | end: None, 285 | }; 286 | 287 | assert_eq!(d[10..], pat.process_test(Vec::new())); 288 | } 289 | 290 | #[test] 291 | fn test_slice_end_from_end() { 292 | let (tmpfile, d) = setup(); 293 | let filepath = tmpfile.path().to_str().unwrap().to_string(); 294 | let pat = SliceApplet { 295 | file: Some(filepath), 296 | start: Position { 297 | offset: 10, 298 | relative: false, 299 | from_end: false, 300 | }, 301 | end: Some(Position { 302 | offset: 10, 303 | relative: false, 304 | from_end: true, 305 | }), 306 | }; 307 | 308 | assert_eq!(d[10..(d.len() - 10)], pat.process_test(Vec::new())); 309 | } 310 | 311 | #[test] 312 | fn test_cli_file() { 313 | let mut data: [u8; 10] = [0; 10]; 314 | for i in (0..10).into_iter() { 315 | data[i] = i as u8; 316 | } 317 | 318 | let mut tmpfile = tempfile::NamedTempFile::new().unwrap(); 319 | tmpfile.write(&data).unwrap(); 320 | 321 | assert_cmd::Command::cargo_bin("rsbkb") 322 | .expect("Could not run binary") 323 | .args(&["slice", &tmpfile.path().to_str().unwrap(), "2", "+0x3"]) 324 | .assert() 325 | .stdout(&b"\x02\x03\x04"[..]) 326 | .success(); 327 | 328 | assert_cmd::Command::cargo_bin("rsbkb") 329 | .expect("Could not run binary") 330 | .args(&["slice", &tmpfile.path().to_str().unwrap(), "2"]) 331 | .assert() 332 | .stdout(&b"\x02\x03\x04\x05\x06\x07\x08\x09"[..]) 333 | .success(); 334 | 335 | /* Should fail because "start" is before beginning of file */ 336 | assert_cmd::Command::cargo_bin("rsbkb") 337 | .expect("Could not run binary") 338 | .args(&["slice", &tmpfile.path().to_str().unwrap(), "-200"]) 339 | .assert() 340 | .failure(); 341 | 342 | /* Should fail because "end" is before "start */ 343 | assert_cmd::Command::cargo_bin("rsbkb") 344 | .expect("Could not run binary") 345 | .args(&["slice", &tmpfile.path().to_str().unwrap(), "0", "-300"]) 346 | .assert() 347 | .failure(); 348 | 349 | assert_cmd::Command::cargo_bin("rsbkb") 350 | .expect("Could not run binary") 351 | .args(&["slice", "--", &tmpfile.path().to_str().unwrap(), "-2"]) 352 | .assert() 353 | .stdout(&b"\x08\x09"[..]) 354 | .success(); 355 | 356 | assert_cmd::Command::cargo_bin("rsbkb") 357 | .expect("Could not run binary") 358 | .args(&[ 359 | "slice", 360 | "--", 361 | &tmpfile.path().to_str().unwrap(), 362 | "-0x2", 363 | "+1", 364 | ]) 365 | .assert() 366 | .stdout(&b"\x08"[..]) 367 | .success(); 368 | } 369 | 370 | #[test] 371 | fn test_cli_stdin() { 372 | let mut data: [u8; 10] = [0; 10]; 373 | for i in (0..10).into_iter() { 374 | data[i] = i as u8; 375 | } 376 | 377 | assert_cmd::Command::cargo_bin("rsbkb") 378 | .expect("Could not run binary") 379 | .args(&["slice", "-", "2", "+3"]) 380 | .write_stdin(*&data) 381 | .assert() 382 | .stdout(&b"\x02\x03\x04"[..]) 383 | .success(); 384 | 385 | assert_cmd::Command::cargo_bin("rsbkb") 386 | .expect("Could not run binary") 387 | .args(&["slice", "-", "2"]) 388 | .write_stdin(*&data) 389 | .assert() 390 | .stdout(&b"\x02\x03\x04\x05\x06\x07\x08\x09"[..]) 391 | .success(); 392 | 393 | /* Should fail because stdin is not seekable */ 394 | assert_cmd::Command::cargo_bin("rsbkb") 395 | .expect("Could not run binary") 396 | .args(&["slice", "-", "-2"]) 397 | .write_stdin(*&data) 398 | .assert() 399 | .stdout("") 400 | .failure(); 401 | 402 | /* Should fail because stdin is not seekable */ 403 | assert_cmd::Command::cargo_bin("rsbkb") 404 | .expect("Could not run binary") 405 | .args(&["slice", "-", "0", "-10"]) 406 | .write_stdin(*&data) 407 | .assert() 408 | .stdout("") 409 | .failure(); 410 | 411 | assert_cmd::Command::cargo_bin("rsbkb") 412 | .expect("Could not run binary") 413 | .args(&["slice", "-", "0", "0"]) 414 | .write_stdin(*&data) 415 | .assert() 416 | .stdout(&b""[..]) 417 | .success(); 418 | } 419 | } 420 | -------------------------------------------------------------------------------- /src/timeapp.rs: -------------------------------------------------------------------------------- 1 | use crate::applet::{Applet, FromStrWithRadix}; 2 | use anyhow::{Context, Result}; 3 | use clap::{arg, Command}; 4 | use std::convert::TryFrom; 5 | use time::{format_description, Duration, OffsetDateTime, UtcOffset}; 6 | 7 | /* 8 | Decode a numeric timestamp in Epoch seconds format to a human-readable timestamp. 9 | 10 | An Epoch timestamp (1-10 digits) is an integer that counts the number of seconds since Jan 1 11 | 1970. 12 | 13 | Useful values for ranges (all Jan-1 00:00:00): 14 | 1970: 0 15 | 2015: 1420070400 16 | 2025: 1735689600 17 | 2030: 1900000000 18 | */ 19 | fn decode_epoch_seconds(ts: i64) -> Result { 20 | OffsetDateTime::from_unix_timestamp(ts).with_context(|| "Could not decode as epoch") 21 | } 22 | 23 | /* Decode epoch date with more precision */ 24 | fn decode_epoch_subseconds(ts: i64, resolution: i64) -> Result { 25 | let micros: i32 = i32::try_from((ts % resolution) * (1_000_000_000 / resolution)).unwrap(); 26 | let unix = OffsetDateTime::from_unix_timestamp(ts / resolution); 27 | if let Ok(date) = unix { 28 | Ok(date + Duration::new(0, micros)) 29 | } else { 30 | unix.with_context(|| "Could not decode as epoch") 31 | } 32 | } 33 | 34 | /* 35 | Decode a numeric timestamp in Windows FileTime format to a human-readable timestamp. 36 | 37 | A Windows FileTime timestamp (18 digits) is a 64-bit value that represents the number of 38 | 100-nanosecond intervals since 12:00AM Jan 1 1601 UTC. 39 | 40 | Useful values for ranges (all Jan-1 00:00:00): 41 | 1970: 116444736000000000 42 | 2015: 130645440000000000 43 | 2025: 133801632000000000 44 | 2065: 146424672000000000 45 | */ 46 | fn decode_windows_filetime(ts: i64) -> Result { 47 | /* Shift to Unix Epoch */ 48 | let shifted = ts - 116_444_736_000_000_000; 49 | decode_epoch_subseconds(shifted, 10_000_000) 50 | } 51 | 52 | pub struct TimeApplet { 53 | local: bool, 54 | verbose: bool, 55 | } 56 | impl Applet for TimeApplet { 57 | fn command(&self) -> &'static str { 58 | "tsdec" 59 | } 60 | fn description(&self) -> &'static str { 61 | "timestamp decoder" 62 | } 63 | 64 | fn clap_command(&self) -> Command { 65 | Command::new(self.command()) 66 | .about(self.description()) 67 | .arg(arg!(-l --local "show time in local time zone")) 68 | .arg(arg!(-v --verbose "show which type of timestamp was used for decoding")) 69 | .arg(arg!([value] "input value, reads from stdin in not present")) 70 | } 71 | 72 | fn new() -> Box { 73 | Box::new(Self { 74 | local: false, 75 | verbose: false, 76 | }) 77 | } 78 | 79 | fn parse_args(&self, args: &clap::ArgMatches) -> Result> { 80 | Ok(Box::new(Self { 81 | local: args.get_flag("local"), 82 | verbose: args.get_flag("verbose"), 83 | })) 84 | } 85 | 86 | fn process(&self, val: Vec) -> Result> { 87 | let ts_str = String::from_utf8(val).unwrap(); 88 | let ts_int = i64::from_str_with_radix(ts_str.as_str()).unwrap(); 89 | let ts_len = if !ts_str.starts_with("0x") { 90 | // if the string is in decimal, return the number of digits 91 | ts_str.len() 92 | } else { 93 | // if in hex, compute length using log 94 | let ts_f: f64 = ts_int as f64; 95 | (ts_f.log10() as usize) + 1 96 | }; 97 | let (ts, type_str) = match (ts_len, ts_int) { 98 | (10, _) => (decode_epoch_seconds(ts_int), "Seconds since Epoch"), 99 | (12, _) => 100 | /* Epoch centiseconds */ 101 | { 102 | ( 103 | decode_epoch_subseconds(ts_int, 100), 104 | "Centiseconds since Epoch", 105 | ) 106 | } 107 | (13, _) => 108 | /* Epoch milliseconds */ 109 | { 110 | ( 111 | decode_epoch_subseconds(ts_int, 1000), 112 | "Milliseconds since Epoch", 113 | ) 114 | } 115 | (16, _) => 116 | /* Epoch microseconds */ 117 | { 118 | ( 119 | decode_epoch_subseconds(ts_int, 1_000_000), 120 | "Microseconds since Epoch", 121 | ) 122 | } 123 | (17, _) => 124 | /* Chrome/WebKit timestamp: microseconds since 1601-01-01 */ 125 | { 126 | ( 127 | decode_windows_filetime(ts_int * 10), 128 | "Chrome/WebKit timestamp", 129 | ) 130 | } 131 | (18, _) => 132 | /* Windows FILETIME */ 133 | { 134 | (decode_windows_filetime(ts_int), "Windows FILETIME") 135 | } 136 | _ => (decode_epoch_seconds(ts_int), "Seconds since Epoch"), 137 | }; 138 | let tse = ts.with_context(|| "Could not convert timestamp")?; 139 | if self.verbose { 140 | eprintln!("Used format: {}", type_str); 141 | } 142 | let ts = if self.local { 143 | let offset = UtcOffset::current_local_offset().unwrap_or(UtcOffset::UTC); 144 | tse.to_offset(offset) 145 | } else { 146 | tse 147 | }; 148 | let date_str = ts 149 | .format(&format_description::well_known::Rfc3339) 150 | .with_context(|| "Date formatting failed")?; 151 | Ok(date_str.as_bytes().to_vec()) 152 | } 153 | } 154 | 155 | #[cfg(test)] 156 | mod tests { 157 | use super::*; 158 | 159 | fn run_decode(app: &TimeApplet, ts: &str) -> String { 160 | String::from_utf8(app.process_test(ts.as_bytes().to_vec())).unwrap() 161 | } 162 | 163 | #[test] 164 | fn test_verbose_cli_stdin() { 165 | assert_cmd::Command::cargo_bin("rsbkb") 166 | .expect("Could not run binary") 167 | .args(&["tsdec", "-v"]) 168 | .write_stdin("1") 169 | .assert() 170 | .stdout("1970-01-01T00:00:01Z") 171 | .stderr("Used format: Seconds since Epoch\n") 172 | .success(); 173 | } 174 | 175 | #[test] 176 | fn test_decimal() { 177 | let ts = TimeApplet { 178 | local: false, 179 | verbose: false, 180 | }; 181 | assert_eq!(run_decode(&ts, "0"), "1970-01-01T00:00:00Z"); 182 | assert_eq!(run_decode(&ts, "1420070400"), "2015-01-01T00:00:00Z"); 183 | assert_eq!(run_decode(&ts, "142007040000"), "2015-01-01T00:00:00Z"); 184 | assert_eq!(run_decode(&ts, "1420070400000"), "2015-01-01T00:00:00Z"); 185 | assert_eq!(run_decode(&ts, "1420070400000000"), "2015-01-01T00:00:00Z"); 186 | assert_eq!(run_decode(&ts, "142007040001"), "2015-01-01T00:00:00.01Z"); 187 | assert_eq!(run_decode(&ts, "1420070400001"), "2015-01-01T00:00:00.001Z"); 188 | assert_eq!( 189 | run_decode(&ts, "1420070400000001"), 190 | "2015-01-01T00:00:00.000001Z" 191 | ); 192 | assert_eq!( 193 | run_decode(&ts, "146424672000234122"), 194 | "2065-01-01T00:00:00.0234122Z" 195 | ); 196 | assert_eq!( 197 | run_decode(&ts, "000000000000000000"), 198 | "1601-01-01T00:00:00Z" 199 | ); 200 | } 201 | 202 | #[test] 203 | fn test_hex() { 204 | let ts = TimeApplet { 205 | local: false, 206 | verbose: false, 207 | }; 208 | assert_eq!(run_decode(&ts, "0x0"), "1970-01-01T00:00:00Z"); 209 | assert_eq!(run_decode(&ts, "0x1"), "1970-01-01T00:00:01Z"); 210 | } 211 | } 212 | -------------------------------------------------------------------------------- /src/urlapp.rs: -------------------------------------------------------------------------------- 1 | use crate::applet::Applet; 2 | use crate::applet::SliceExt; 3 | use anyhow::Result; 4 | use clap::{arg, Command}; 5 | 6 | pub struct UrlEncApplet { 7 | // true: should be encoded 8 | table: [bool; 256], 9 | } 10 | 11 | // Encoding table according to RFC 3986 12 | fn build_url_table(excluded: &str, table: &mut [bool; 256]) { 13 | for i in 0..256 { 14 | let c = char::from_u32(i).unwrap(); 15 | // encode non graphic chars and signs from the RFC3986 list, excepted 16 | // explicitly excluded 17 | if !c.is_ascii_graphic() 18 | || (!excluded.contains(c) 19 | && matches!( 20 | c, 21 | '!' | '#' 22 | | '$' 23 | | '%' 24 | | '&' 25 | | '\'' 26 | | '(' 27 | | ')' 28 | | '*' 29 | | '+' 30 | | ',' 31 | | '/' 32 | | ':' 33 | | ';' 34 | | '=' 35 | | '?' 36 | | '@' 37 | | '[' 38 | | ']' 39 | )) 40 | { 41 | table[i as usize] = true; 42 | } 43 | } 44 | } 45 | 46 | fn build_custom_table(excluded: &str, custom: &str, table: &mut [bool; 256]) { 47 | for i in 0..256 { 48 | let c = char::from_u32(i).unwrap(); 49 | if custom.contains(c) { 50 | table[i as usize] = !excluded.contains(c); 51 | } else { 52 | table[i as usize] = false; 53 | } 54 | } 55 | } 56 | 57 | // Default is to encode non alpha-numeric (ASCII) chars 58 | fn build_default_table(excluded: &str, table: &mut [bool; 256]) { 59 | for i in 0..256 { 60 | let c = char::from_u32(i).unwrap(); 61 | if c.is_ascii_alphanumeric() { 62 | table[i as usize] = false; 63 | } else { 64 | table[i as usize] = !excluded.contains(c); 65 | } 66 | } 67 | } 68 | 69 | impl Applet for UrlEncApplet { 70 | fn command(&self) -> &'static str { 71 | "urlenc" 72 | } 73 | fn description(&self) -> &'static str { 74 | "URL encode" 75 | } 76 | 77 | fn new() -> Box { 78 | Box::new(Self { 79 | table: [false; 256], 80 | }) 81 | } 82 | 83 | fn clap_command(&self) -> Command { 84 | Command::new(self.command()) 85 | .about(self.description()) 86 | .arg(arg!(-u --"rfc3986" "use RFC3986 (URL) list of chars to encode")) 87 | .arg( 88 | arg!(-c --"custom" "string specifying chars to encode") 89 | .conflicts_with("rfc3986"), 90 | ) 91 | .arg(arg!(-e --"exclude-chars" "a string of chars to exclude from encoding")) 92 | .arg(arg!([value] "input value, reads from stdin in not present")) 93 | .after_help("By default, encode all non alphanumeric characters in the input.") 94 | } 95 | 96 | fn parse_args(&self, args: &clap::ArgMatches) -> Result> { 97 | let empty_exclude = "".to_string(); 98 | let excluded = if args.contains_id("exclude-chars") { 99 | args.get_one::("exclude-chars").unwrap() 100 | } else { 101 | &empty_exclude 102 | }; 103 | let mut table = [false; 256]; 104 | if args.get_flag("rfc3986") { 105 | build_url_table(excluded, &mut table); 106 | } else if args.contains_id("custom") { 107 | let custom = args.get_one::("custom").unwrap(); 108 | build_custom_table(excluded, custom, &mut table); 109 | } else { 110 | build_default_table(excluded, &mut table); 111 | }; 112 | Ok(Box::new(Self { table })) 113 | } 114 | 115 | fn process(&self, val: Vec) -> Result> { 116 | let mut encoded = Vec::with_capacity(val.len()); 117 | for b in val.iter() { 118 | if self.table[*b as usize] { 119 | // format! is not the fastest, but we are encoding URLs, not gigabytes of data 120 | encoded.extend_from_slice(format!("%{:02x}", *b).as_bytes()); 121 | } else { 122 | encoded.push(*b); 123 | }; 124 | } 125 | Ok(encoded) 126 | } 127 | } 128 | 129 | pub struct UrlDecApplet {} 130 | 131 | impl Applet for UrlDecApplet { 132 | fn command(&self) -> &'static str { 133 | "urldec" 134 | } 135 | fn description(&self) -> &'static str { 136 | "URL decode" 137 | } 138 | 139 | fn new() -> Box { 140 | Box::new(Self {}) 141 | } 142 | 143 | fn parse_args(&self, _args: &clap::ArgMatches) -> Result> { 144 | Ok(Box::new(Self {})) 145 | } 146 | 147 | fn process(&self, urlval: Vec) -> Result> { 148 | let trimmed: Vec = urlval.trim().into(); 149 | let decoded: Vec = percent_encoding::percent_decode(&trimmed).collect(); 150 | Ok(decoded) 151 | } 152 | } 153 | 154 | #[cfg(test)] 155 | mod tests { 156 | use super::*; 157 | 158 | #[test] 159 | fn test_urlenc_cli_arg() { 160 | assert_cmd::Command::cargo_bin("rsbkb") 161 | .expect("Could not run binary") 162 | .args(&["urlenc", "aAé!,"]) 163 | .assert() 164 | .stdout("aA%c3%a9%21%2c") 165 | .success(); 166 | } 167 | 168 | #[test] 169 | fn test_urlenc_cli_arg_exclude() { 170 | assert_cmd::Command::cargo_bin("rsbkb") 171 | .expect("Could not run binary") 172 | .args(&["urlenc", "-e", "!,", "aAé!,"]) 173 | .assert() 174 | .stdout("aA%c3%a9!,") 175 | .success(); 176 | } 177 | 178 | #[test] 179 | fn test_urlenc_cli_arg_custom() { 180 | assert_cmd::Command::cargo_bin("rsbkb") 181 | .expect("Could not run binary") 182 | .args(&["urlenc", "-e", "!,", "-c", "aA,", "aAé!,"]) 183 | .assert() 184 | .stdout("%61%41é!,") 185 | .success(); 186 | } 187 | 188 | #[test] 189 | fn test_urlenc_stdin() { 190 | assert_cmd::Command::cargo_bin("rsbkb") 191 | .expect("Could not run binary") 192 | .args(&["urlenc"]) 193 | .write_stdin("aAé!,") 194 | .assert() 195 | .stdout("aA%c3%a9%21%2c") 196 | .success(); 197 | } 198 | 199 | #[test] 200 | fn test_urlenc() { 201 | let mut table = [false; 256]; 202 | build_default_table(&"".to_string(), &mut table); 203 | let urlenc = UrlEncApplet { table: table }; 204 | let encoded = urlenc 205 | .process("aA!,é".as_bytes().to_vec()) 206 | .expect("encoding failed"); 207 | assert_eq!(String::from_utf8(encoded).unwrap(), "aA%21%2c%c3%a9"); 208 | } 209 | 210 | #[test] 211 | fn test_urlenc_00_ff() { 212 | let mut table = [false; 256]; 213 | build_default_table(&"".to_string(), &mut table); 214 | let urlenc = UrlEncApplet { table: table }; 215 | let encoded = urlenc.process([0, 0xFF].to_vec()).expect("encoding failed"); 216 | assert_eq!(String::from_utf8(encoded).unwrap(), "%00%ff"); 217 | } 218 | 219 | #[test] 220 | fn test_urlencdec() { 221 | let mut table = [false; 256]; 222 | build_default_table(&"".to_string(), &mut table); 223 | let urlenc = UrlEncApplet { table: table }; 224 | let urldec = UrlDecApplet {}; 225 | let test_string = "aA!,é"; 226 | let encoded = urlenc 227 | .process(test_string.as_bytes().to_vec()) 228 | .expect("encoding failed"); 229 | let decoded = urldec.process(encoded).expect("decoding failed"); 230 | assert_eq!(String::from_utf8(decoded).unwrap(), test_string); 231 | } 232 | } 233 | -------------------------------------------------------------------------------- /src/xorapp.rs: -------------------------------------------------------------------------------- 1 | use crate::applet::Applet; 2 | use anyhow::{Context, Result}; 3 | use clap::{arg, Command}; 4 | use std::fs; 5 | 6 | pub struct XorApplet { 7 | key_bytes: Vec, 8 | } 9 | 10 | impl Applet for XorApplet { 11 | fn command(&self) -> &'static str { 12 | "xor" 13 | } 14 | fn description(&self) -> &'static str { 15 | "xor value" 16 | } 17 | 18 | fn clap_command(&self) -> Command { 19 | Command::new(self.command()) 20 | .about(self.description()) 21 | .arg( 22 | arg!(-x --xorkey "Xor key in hex format") 23 | .required_unless_present("keyfile") 24 | .conflicts_with("keyfile"), 25 | ) 26 | .arg(arg!(-f --keyfile "File to use as key")) 27 | .arg(arg!([value] "input value, reads from stdin in not present")) 28 | } 29 | 30 | fn new() -> Box { 31 | Box::new(Self { key_bytes: vec![] }) 32 | } 33 | 34 | fn parse_args(&self, args: &clap::ArgMatches) -> Result> { 35 | let key_bytes = if args.contains_id("xorkey") { 36 | hex::decode(args.get_one::("xorkey").unwrap().replace(' ', "")) 37 | .with_context(|| "Xor key decoding failed")? 38 | } else { 39 | fs::read(args.get_one::("keyfile").unwrap()) 40 | .with_context(|| "Could not read keyfile")? 41 | }; 42 | Ok(Box::new(Self { key_bytes })) 43 | } 44 | 45 | fn process(&self, val: Vec) -> Result> { 46 | let inf_key = self.key_bytes.iter().cycle(); // Iterate endlessly over key bytes 47 | Ok(val.iter().zip(inf_key).map(|(x, k)| x ^ k).collect()) 48 | } 49 | } 50 | 51 | #[cfg(test)] 52 | mod tests { 53 | use super::*; 54 | use std::io::Write; 55 | 56 | #[test] 57 | fn test_hex_key_cli() { 58 | assert_cmd::Command::cargo_bin("rsbkb") 59 | .expect("Could not run binary") 60 | .args(&["xor", "-x", "41", "AAAA"]) 61 | .assert() 62 | .stdout(&b"\0\0\0\0"[..]) 63 | .success(); 64 | } 65 | 66 | #[test] 67 | fn test_key_file_cli_stdin() { 68 | let rand_key: [u8; 17] = [ 69 | 0x7f, 0x5a, 0x88, 0x7b, 0xe8, 0x81, 0xd6, 0x5e, 0x39, 0xf4, 0x7e, 0x25, 0xf2, 0x05, 70 | 0xdc, 0x22, 0x86, 71 | ]; 72 | let zero_data = [0u8; 32]; 73 | 74 | let mut tmpkey = tempfile::NamedTempFile::new().unwrap(); 75 | tmpkey.write(&rand_key.clone()).unwrap(); 76 | 77 | assert_cmd::Command::cargo_bin("rsbkb") 78 | .expect("Could not run binary") 79 | .args(&["xor", "-f", tmpkey.path().to_str().expect("Could not get path as str")]) 80 | .write_stdin(zero_data) 81 | .assert() 82 | .stdout(&b"\x7fZ\x88{\xE8\x81\xD6^9\xF4~%\xF2\x05\xDC\"\x86\x7fZ\x88{\xE8\x81\xD6^9\xF4~%\xF2\x05\xDC"[..]) 83 | .success(); 84 | 85 | assert_cmd::Command::cargo_bin("rsbkb") 86 | .expect("Could not run binary") 87 | .args(&[ 88 | "xor", 89 | "-f", 90 | tmpkey.path().to_str().expect("Could not get path as str"), 91 | ]) 92 | .write_stdin(rand_key) 93 | .assert() 94 | .stdout(&b"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"[..]) 95 | .success(); 96 | } 97 | 98 | #[test] 99 | fn test_simple() { 100 | let data = vec![1, 0x55, 0xAA, 0xFF, 0]; 101 | let x = XorApplet { 102 | key_bytes: data.clone(), 103 | }; 104 | assert_eq!(x.process_test(vec![0, 0, 0, 0, 0]), data); 105 | assert_eq!( 106 | x.process_test(vec![0xFF, 0xFF, 0xFF, 0xFF, 0xFF]), 107 | vec![0xFE, 0xAA, 0x55, 0, 0xFF] 108 | ); 109 | assert_eq!(x.process_test(vec![0]), vec![1]); 110 | assert_eq!( 111 | x.process_test(vec![0, 0, 0, 0, 0, 0]), 112 | vec![1, 0x55, 0xAA, 0xFF, 0, 1] 113 | ); 114 | } 115 | } 116 | --------------------------------------------------------------------------------