From ed3907e13ffd7e6b170e9e01fb86a8e4f0919986 Mon Sep 17 00:00:00 2001 From: rishitsaiya Date: Fri, 31 Jul 2020 18:25:06 +0530 Subject: [PATCH] Added Reversing Challenges --- Reversing/RicknMorty/README.md | 193 ++++++++++++++++++++++++++++++++ Reversing/RicknMorty/RickNMorty | Bin 0 -> 17144 bytes Reversing/RicknMorty/exploit.py | 34 ++++++ 3 files changed, 227 insertions(+) create mode 100644 Reversing/RicknMorty/README.md create mode 100644 Reversing/RicknMorty/RickNMorty create mode 100644 Reversing/RicknMorty/exploit.py diff --git a/Reversing/RicknMorty/README.md b/Reversing/RicknMorty/README.md new file mode 100644 index 0000000..6c57c3a --- /dev/null +++ b/Reversing/RicknMorty/README.md @@ -0,0 +1,193 @@ +## RickNMorty +The main idea finding the flag is reverse the functions using Ghidhra to understand the code. + +#### Step-1: +After I downloaded `RickNMorty`, and decompiled it in Ghidhra, I got the **main()** as follows: +```c +undefined8 main(void) +{ + int iVar1; + time_t tVar2; + long lVar3; + long local_48; + time_t local_40; + time_t local_38; + time_t local_30; + long local_28; + long local_20; + char *local_18; + int local_10; + int local_c; + + setbuf(stdin,(char *)0x0); + setbuf(stdout,(char *)0x0); + setbuf(stderr,(char *)0x0); + tVar2 = time(&local_30); + srand((uint)tVar2); + time(&local_38); + local_c = 1; + local_10 = 0; + while( true ) { + iVar1 = rand(); + if (iVar1 % 3 + 4 < local_10) break; + iVar1 = rand(); + local_20 = (long)(iVar1 % 10 + 6); + iVar1 = rand(); + local_28 = (long)(iVar1 % 10 + 6); + printf("%d %d\n",local_20,local_28); + __isoc99_scanf(&DAT_0040200f,&local_48); + lVar3 = function1(local_20); + lVar3 = function2(lVar3 + 3); + if (lVar3 != local_48) { + local_c = 0; + } + local_10 = local_10 + 1; + } + time(&local_40); + local_18 = (char *)(double)(local_40 - local_38); + printf(local_18,"fun() took %f seconds to execute \n"); + if ((local_c != 1) || (30.00000000 < (double)local_18)) { + printf("Nahh."); + } + else { + puts("Hey, you got me!"); + system("cat flag.txt"); + } + return 0; +} +``` + +#### Step-2: +A pair of random numbers is generated and passed through `function1()` & `function2()` and checked with pair of numbers with given number to get the flag. + +```c +long function1(long param_1,long param_2) +{ + int local_10; + int local_c; + + local_c = 0; + local_10 = 1; + while ((local_10 <= param_1 || (local_10 <= param_2))) { + if ((param_1 % (long)local_10 == 0) && (param_2 % (long)local_10 == 0)) { + local_c = local_10; + } + local_10 = local_10 + 1; + } + return (long)local_c; +} +``` + +```c +long function2(long param_1) +{ + long lVar1; + + if (param_1 == 0) { + lVar1 = 1; + } + else { + lVar1 = function2(param_1 + -1); + lVar1 = lVar1 * param_1; + } + return lVar1; +} +``` + +#### Step-3: +So, I wrote this `exploit.py` to get the flag: + +```python +from pwn import * + +context.log_level='DEBUG' +p = remote('chall.csivit.com', 30827) #Remote netcat + +def fun1(param_1, param_2): + local_c = 0 + local_10 = 1 + while (local_10 <= param_1) or (local_10 <= param_2): + if (param_1 % local_10 == 0) and (param_2 % local_10 == 0): + local_c = local_10 + local_10 += 1 + return local_c + +def fun2(param_1): + lvar1 = 0 + if param_1 == 0: + lvar1 = 1 + else: + lvar1 = fun2(param_1 - 1) + lvar1 = lvar1 * param_1 + return lvar1 + +while True: + line = p.recvline() + if not line or line.decode().startswith('fun() took'): + break + + nums = line.decode().rstrip().split(' ') + ans = fun1(int(nums[0]), int(nums[1])) + ans = fun2(ans + 3) + p.sendline(str(ans)) + +p.stream() +``` + +#### Step-4: +Running the script gave me: + +```bash +[DEBUG] PLT 0x40102c puts +[DEBUG] PLT 0x401040 setbuf +[DEBUG] PLT 0x401050 system +[DEBUG] PLT 0x401060 printf +[DEBUG] PLT 0x401070 srand +[DEBUG] PLT 0x401080 time +[DEBUG] PLT 0x401090 __isoc99_scanf +[DEBUG] PLT 0x4010a0 rand +[*] + Arch: amd64-64-little + RELRO: Partial RELRO + Stack: No canary found + NX: NX enabled + PIE: No PIE (0x400000) +[+] Opening connection to chall.csivit.com on port 30827: Done +[DEBUG] Received 0x6 bytes: + b'11 15\n' +[DEBUG] Sent 0x3 bytes: + b'24\n' +[DEBUG] Received 0x5 bytes: + b'9 12\n' +[DEBUG] Sent 0x4 bytes: + b'720\n' +[DEBUG] Received 0x5 bytes: + b'7 10\n' +[DEBUG] Sent 0x3 bytes: + b'24\n' +[DEBUG] Received 0x5 bytes: + b'9 11\n' +[DEBUG] Sent 0x3 bytes: + b'24\n' +[DEBUG] Received 0x5 bytes: + b'8 10\n' +[DEBUG] Sent 0x4 bytes: + b'120\n' +[DEBUG] Received 0x5 bytes: + b'6 13\n' +[DEBUG] Sent 0x3 bytes: + b'24\n' +[DEBUG] Received 0x28 bytes: + b'fun() took 0.000000 seconds to execute \n' +[DEBUG] Received 0x11 bytes: + b'Hey, you got me!\n' +Hey, you got me! +[DEBUG] Received 0x28 bytes: + b'csictf{h3_7u2n3d_h1m531f_1n70_4_p1ck13}\n' +csictf{h3_7u2n3d_h1m531f_1n70_4_p1ck13} +``` +Voila! We got the flag. + +#### Step-5: +Finally the flag becomes: +`csictf{h3_7u2n3d_h1m531f_1n70_4_p1ck13}` \ No newline at end of file diff --git a/Reversing/RicknMorty/RickNMorty b/Reversing/RicknMorty/RickNMorty new file mode 100644 index 0000000000000000000000000000000000000000..bdef3e142db427f940fe15fa9d09beda3610e6c3 GIT binary patch literal 17144 zcmeHOeQaCR6~DIgML%q(w6wIaycIGvK;5(}C8270PUH3krG=HafVTGG`Xjb*>|j4b z0;*0%Mblw5t2k0-~D(9fa}~Ak;hOzI&YK z7iVnyXA<*?-aGgF?m6e)d+xn2@A}>QsHc6q)nZ{pR<@O)bj&GpNWU|7Vlx*7*2ESv znO)1SVO4-sz$Nn>T}Hb~ObVHa_7T4zoDwI!LU|Gz14%Mx#A`(Ti5u-OHq51~v#qk85S-$1OxTkh zifHE)c21#BJSzf0oZ^Yq$LqB4ciJk0gtCl`wetHcM_r^lFK7Wwhb5Gd%SF6{ONv%mP!p;teC@+8(9>wx}H2MP75I7}~w3-fuq zEE4OpII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fbuyk8_9es273htDKb>m{J?Ft- zA_?7FB&d)Ft@ojUNOeaw=Hj7oX+ikt<-+*@d2v1fT*0uI0pS4HlB8ScQ$e?&G&uHk zMf^}|9DKpe#S74#L138+W@Hykv=R@H{}94;8a!IyT|%c}tIQL_0-$x8o~!Fp8u@i_ z;o6Ao>3y8epW=Ivk?46(*d|CA?Q>R{XXS)LiA%FLvwbUMy$@?CDxco(Ys7+&))(}D zpzwSYE_VTkr){DBv<_$z_I(%}pend9N%pkP+XEP?Qu(xAJs~QZfjlD#cLiu4q;PFW z_O!0(5fzq%Ce@$xh~E$SxYi_@)*nZN0kz*;f8mxr3<<0%+0!~@T-Xl_MJnIi{)584 zMd;CbrW*?c%J94XUUn6v&Gv@@!p}6&->gqE z@t43B-UbWyv~H~VH|@`ZEsh;3pVk?4Zl2D^)A*utsUKefTWliP(>ioQydX-DXC$&G z{x`@p+85TVjk?u6Mq@~m9r3qJ_Vj*zkFckDk{hxk`90y2>fa~JJS!~h1tWDP6;gWQ zBVbs6vmV)--^o!I$;fH!={fqnPdL02AMQXnk71X8q5bsy#NW-R?vN}{78lAuubG@v Vxq2?X`5|?4LuaVblNs literal 0 HcmV?d00001 diff --git a/Reversing/RicknMorty/exploit.py b/Reversing/RicknMorty/exploit.py new file mode 100644 index 0000000..e9c7bff --- /dev/null +++ b/Reversing/RicknMorty/exploit.py @@ -0,0 +1,34 @@ +from pwn import * + +context.log_level='DEBUG' +p = remote('chall.csivit.com', 30827) #Remote netcat + +def fun1(param_1, param_2): + local_c = 0 + local_10 = 1 + while (local_10 <= param_1) or (local_10 <= param_2): + if (param_1 % local_10 == 0) and (param_2 % local_10 == 0): + local_c = local_10 + local_10 += 1 + return local_c + +def fun2(param_1): + lvar1 = 0 + if param_1 == 0: + lvar1 = 1 + else: + lvar1 = fun2(param_1 - 1) + lvar1 = lvar1 * param_1 + return lvar1 + +while True: + line = p.recvline() + if not line or line.decode().startswith('fun() took'): + break + + nums = line.decode().rstrip().split(' ') + ans = fun1(int(nums[0]), int(nums[1])) + ans = fun2(ans + 3) + p.sendline(str(ans)) + +p.stream() \ No newline at end of file