<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by peternguyen on Medium]]></title>
        <description><![CDATA[Stories by peternguyen on Medium]]></description>
        <link>https://medium.com/@peternguyen14?source=rss-a233da090f6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*BzzUc68ymmo70vpP.jpg</url>
            <title>Stories by peternguyen on Medium</title>
            <link>https://medium.com/@peternguyen14?source=rss-a233da090f6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 20:39:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@peternguyen14/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[HITB-XCTF2018 Qualification Writeup]]></title>
            <link>https://medium.com/@peternguyen14/hitb-xctf2018-qualification-writeup-b259ece64052?source=rss-a233da090f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/b259ece64052</guid>
            <category><![CDATA[ctf]]></category>
            <dc:creator><![CDATA[peternguyen]]></dc:creator>
            <pubDate>Sat, 14 Apr 2018 10:50:06 GMT</pubDate>
            <atom:updated>2018-04-14T10:50:06.816Z</atom:updated>
            <content:encoded><![CDATA[<ol><li><strong>Python Revenge<br></strong> The vulnerable application was written in Python which keep user secret.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0UkN8ZbLGnrDeHIZ6gVQXA.png" /><figcaption>At function getlocation they use unsafe serialize Pickle to deserialize data from user</figcaption></figure><p>They also limit system functions which prevent attacker can execute malicious on server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UgNen8Ma29dEEyM71WLtTw.png" /><figcaption>All blacklist</figcaption></figure><p>The app bases on Flask web framework, after researching on python source code, I found that they wasn’t blacklist __import__ function, I can import module as I want.</p><p>The next idea came to my mind is Jinja2 Template Injection (<a href="https://nvisium.com/resources/blog/2016/03/11/exploring-ssti-in-flask-jinja2-part-ii.html">https://nvisium.com/resources/blog/2016/03/11/exploring-ssti-in-flask-jinja2-part-ii.html</a>) which allows attacker execute shell in web server.</p><p>My exploit script: <a href="https://gist.github.com/peternguyen93/f6747f7a2fbfcfcd66546e77cad8491b">https://gist.github.com/peternguyen93/f6747f7a2fbfcfcd66546e77cad8491b</a></p><p><strong>2. Mute Pigs</strong></p><p>The vulnerability is quite clear, the bug is Use After Free/ Double Free.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/818/1*XvWSC64VlXOCeEDRbooCeg.png" /></figure><p>The program allows 4 types of chunk size is 0x10, 0x80, 0xA0000000, 0xFFFFFFFFFFFFFF70. By looking at these size I though the only way to solve this challenge is some how unlink an unsorted chunk which overwrite pointer into <strong>pigs</strong> array.</p><p>My exploitation steps is abuse<strong> unlink forward</strong> chunk in <strong>malloc_consolidate</strong> function by trigger malloc with big size.</p><p>To archive that, I have to create 2 small chunk in bss which have static address, bypass check condition of <strong>unlink</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/810/1*ShPRIDLrYGj895V7Jidpsg.png" /><figcaption>The edit function allow us to write 48 bytes at address 0x602120</figcaption></figure><p>I used fastbin attack to create 2 chunks locates at address 0x602120 then trigger malloc_consolidate by allocating large size. After successfully overwrite <strong>pigs</strong> array so I can easily write free@got table with system and gain shell.</p><p>My exploitation script : <a href="https://gist.github.com/peternguyen93/460c3487d58c8c6a1f67d1cff1c45054">https://gist.github.com/peternguyen93/460c3487d58c8c6a1f67d1cff1c45054</a></p><p><strong>3. 3Pigs</strong></p><p>This challenge is web/pwn which requires me focus most time on it, stayed all night in coffee shop to solve this challenge.</p><p>The challenge has 2 vulnerabilities, one on web and one on binary.</p><p>I spend a lot of time to analysis and exploit binary .</p><p>The bug of this binary is clear, they alloc a chunk at size 0x70 and write 16 bytes at offset 0x80 of this chunk.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/826/1*tL2dTFyDxuj9cOXGNczXnQ.png" /></figure><p>To archive this we have to call another function and provide the correct input to trigger that function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/994/1*FclMcG14k9bSaS_DG0VSuw.png" /></figure><p>This binary allows us create only 3 pigs chunk and stored them in an array. The pig object also have fixed size at 0xc8 bytes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6IRJIRFNMoSTs0CIIAzCfw.png" /></figure><p>First, I have to leak libc and heap address by create heap memory like:</p><blockquote>&lt;pig 1&gt;<br>&lt;junk 0x70&gt;<br>&lt;pig 2&gt;<br>&lt;pig 3&gt;</blockquote><p>Then free the <strong>pig 1, </strong>read function doesn’t terminate string with null byte so we can leak main_arena-&gt;bins[0] in heap.</p><p>Next I free <strong>pig 2 , </strong>pig 2 and pig 1 is now linked together so I can leak heap address like above.</p><p>The exploitation step is quite complicated, I create pig1 and pig2 chunk overlapping each other to write <strong>_IO_list_all</strong> which abuse FILE structure to execute shell command.</p><p>But we have to create a chunk in<strong> smallbin[4]</strong> which become (FILE *)(main_arena-&gt;bins[0])-&gt;chains later.</p><p>The easy way I do it all time (recently CTFs) is create a fake freed chunk with size 0x60 and put them in the end of linked list of unsorted bin. Then alloc a size we want (except 0x60), now this chunk is in <strong>smallbin[4].</strong></p><p>Finally, I trigger <strong>_IO_list_all</strong> to control FILE structure, the server used libc-2.26.so which had migitation of FILE structure vtable , so I use the technique like <strong>ghost_in_heap</strong> (HITCON2017).</p><p>Unluckily for me, calling check_secret function in binary in web API, they forbid the secret string.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SPN2XvpJmIXCzaR4EB-ayA.png" /></figure><p>How do we bypass this check ? Let’s take a look at this line</p><blockquote>secret = secret.format(“”)</blockquote><p>Googling a bit I had though about python format string injection <a href="http://lucumr.pocoo.org/2016/12/29/careful-with-str-format/">http://lucumr.pocoo.org/2016/12/29/careful-with-str-format/</a> which allow user can interact with object in string format.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/958/1*pFIO7RiaXAAsic7OK0Beyw.png" /></figure><p>I had written a small script to generate string format to create <strong>secret string</strong> to bypass the check <a href="https://gist.github.com/peternguyen93/30812a801bf9ca15b413b5b0bea6257e">SCRIPT</a>.</p><p>Here my full exploit code: <a href="https://gist.github.com/peternguyen93/50edcc35f0eb070c703bfcdc80de116f">https://gist.github.com/peternguyen93/50edcc35f0eb070c703bfcdc80de116f</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b259ece64052" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[HITCON 2017 : Ghost in The Heap Writeup]]></title>
            <link>https://medium.com/tradahacking/hitcon-2017-ghost-in-the-heap-writeup-ee6384cd0b7?source=rss-a233da090f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/ee6384cd0b7</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[hitcon2017]]></category>
            <category><![CDATA[ctf]]></category>
            <dc:creator><![CDATA[peternguyen]]></dc:creator>
            <pubDate>Thu, 09 Nov 2017 07:04:13 GMT</pubDate>
            <atom:updated>2017-11-09T07:58:10.230Z</atom:updated>
            <content:encoded><![CDATA[<p>This is quite interesting challenge in this CTF, I had spent nearly 20 hours to solve this challenges.</p><p>The vulnerability is in <strong>alloc_heap</strong> function. The scanf function read string into 168 bytes heap but scanf will automaticaly added <strong>NULL byte</strong> in the end as result lead to <strong>off byte one vulnerability</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PYBS0UP4nVqWfA37Wrcq6A.png" /><figcaption>IDA decompiler of alloc_heap</figcaption></figure><p>The program also have function allow alloc ghost in the heap with size 0x50 bytes (0x60 bytes includes malloc header).</p><p>We can’t not controll size of buffer so the technique off byte null <a href="https://googleprojectzero.blogspot.com/2014/08/the-poisoned-nul-byte-2014-edition.html">https://googleprojectzero.blogspot.com/2014/08/the-poisoned-nul-byte-2014-edition.html</a> can’t used.</p><p>In the beginning of challenge, after some hours read <strong>malloc.c </strong>in glibc to find some code path to achieve overlapping ghost chunk and heap chunk and then trigger<strong> unsorted bin </strong>unlink to overwrite <strong>_IO_list_all</strong>.</p><p>Leaking libc is quite simple, we have to create 3 heap chunk, free the middle and add ghost. Because of no terminated byte added, we can use it to leak <strong>main_arena-&gt;unsorted_bin </strong>stored in free heap chunk.</p><p>Leaking heap base is more complicated, create 3 heap chunk:A ,B, C, after that, adding <strong>ghost</strong> into heap to make sure we free 2 chunk A,C there don’t consolidate with top chunk. A,C is on unsorted bin linked list, free all chunk A,B,C, <strong>ghost</strong> to cleanup heap layout, and then alloc ghost chunk first, the heap base address is on ghost chunk, so that we can read it easily.</p><p>Create chunk overlapping by abuse NULL pointer and Consolidate Forward. We create 3 heap chunk called <strong>1, 2, 3,</strong> <strong>ghost</strong> chunk at the bottom of heap (near top chunk) like image on the left.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hsEPsPiatWNcoQTcRQmYSA.png" /></figure><p>After that free all heap chunk and add <strong>heap chunk 1</strong> with 168 characters which ovewrite off byte to free_chunk (0x160 to 0x100).</p><p>Alloc heap chunk 2 we got layout like image on the left. Free heap ghost chunk and free heap chunk 1. The free function think got chunk <strong>prev_size</strong> (0x160) is correct size, and <strong>consolidate forward</strong> with top chunk so that <strong>heap chunk 2</strong> is overlapped with top chunk.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uUujZPobNhlSPAnbBDwQQA.png" /></figure><p>Because of consolidate forward unlink heap 1 to unsorted bin and merge with top chunk so that we have to correct <strong>heap_1-&gt;bck</strong> and <strong>heap_1-&gt;fwd</strong> to make sure they point to address of heap_1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C6AmDjWwpGJo3Kwf3L_EbA.png" /></figure><p>In current glibc version, glibc also add a small check to unlink function to mitigate single byte overflow by checking next_chunk-&gt;prev_size, we also fix that by appending correct prev_size to heap chunk.</p><p>The top chunk is now overlapping with heap_1 we can align heap layout to gain free a chunk with size we control to setup <strong>main_arena-&gt;smallbin[4] </strong>which is <strong>FILE-&gt;chain</strong> structure, and then use unsorted_bin to overwrite <strong>_IO_list_all</strong> to control RIP.</p><p>Unfortunately for me, the libc challenge give me is different with my current libc (Ubuntu 16.04) after a bit searching I figured out that these libc binary is on Ubuntu 17.04 and include the newst migitation agains FILE vtable abusing.</p><p>I can’t overwrite FILE-&gt;vtable into heap any more.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FXke7j8SADZ3HLWWJGTEkQ.png" /></figure><p><strong>IO_validate_vtable</strong> allows address in address of FILE vtable range so my payload is failed.</p><p>After some hours finding way to bypass the migitation, I found some vtable has used many function pointer call like <strong>_IO_wstr_finish .</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Jw8kub9L_bZ8NwEZl5IxWQ.png" /></figure><p>_IO_wstr_finish take first argument is FILE structure which is controlled by me then trigger function call [rbx+0xe8] ,n this offset I also controlled.</p><p>Finally, Exploit chain is overwrite _FILE-&gt;vtable with address of _IO_wstr_finish and the spray other byte is one_gadget to trigger shell.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fAtGTmS4S6VG1hhAi_VRZQ.png" /></figure><p>My full payload is <a href="https://gist.github.com/peternguyen93/d541cf00289252eb518e40348d0f2235">https://gist.github.com/peternguyen93/d541cf00289252eb518e40348d0f2235</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ee6384cd0b7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tradahacking/hitcon-2017-ghost-in-the-heap-writeup-ee6384cd0b7">HITCON 2017 : Ghost in The Heap Writeup</a> was originally published in <a href="https://medium.com/tradahacking">tradahacking</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[HITB 2017 Writeup : SGX_Browser]]></title>
            <link>https://medium.com/tradahacking/hitb-2017-writeup-sgx-browser-5c1c48e0d6fe?source=rss-a233da090f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/5c1c48e0d6fe</guid>
            <category><![CDATA[ctf]]></category>
            <dc:creator><![CDATA[peternguyen]]></dc:creator>
            <pubDate>Fri, 15 Sep 2017 16:36:57 GMT</pubDate>
            <atom:updated>2017-09-16T03:21:04.700Z</atom:updated>
            <content:encoded><![CDATA[<p>After HITB 2017, It’s neary half month of this contest, I have free time to play some pwnable challenge I haven’t solved during CTF time. In this post, I wrote about SGX_Browser which is an interesting challenge. We have to exploit a browser in Ubuntu 16.04 environment.</p><p>So let’s analysis the binary, lucky for me, this binary is compiled with debug symbol so we don’t spend much time to reverse everything. This Browser provides <strong>javascript</strong> interface which called SGX_Code:</p><ul><li>SGX_Implement: create Enclave database.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FJ5vyKTWDTGiWEEWVs0urw.png" /></figure><ul><li>Enclave: can create SGX_Code which stored array number and have some methods to <strong>refer</strong> and <strong>derefer</strong> a SGX_Code.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QCVSeLOs5mfB5xMwAd_Rcg.png" /></figure><ul><li>SGXCode: an object that stored long long int number. This type of object have two type: normal SGXCode (present as 0 ) and refer SGXCode (present as 1).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VU73y_N_OSoi7C2UBfIXUQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AMTEaxWY4NDRdwV0TreIxQ.png" /><figcaption>All SGX Browser methods</figcaption></figure><p>After awhile I figured out the vulnerability of this browser, in <strong>refer</strong> and <strong>derefer</strong> an SGX_Code. I show to you in serveral pictures below.</p><p>Let see in method <strong>refer</strong> of Enclave object. The method <strong>Enclave::refer</strong> get 3 params : <em>name of normal SGXCode</em>, <em>name of refer SGXCode</em> and <em>index</em> to store normal SGXCode to refer SGXCode database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8EbOETG3idMeHiUKIThXlg.png" /><figcaption><strong>Enclave::refer</strong></figcaption></figure><p>They assign the third param to data_refer_index of normal SGXCode, then get id of normal SGXCode and use 2 values : <strong><em>index</em></strong> and <strong><em>id</em></strong> to store to refer SGXCode (refer_code_data).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WOGRqI_OgLDP-a6yOqgFIA.png" /><figcaption><strong>SGXCode::AddDataRefer</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ciWir_Zf9dCYz-10FE_5fw.png" /><figcaption><strong>SGXCode::AddCodeRefer</strong></figcaption></figure><p>In method <strong>SGXCode::AddCodeRefer </strong>we can see that <strong>vref_stack_idx</strong> is verify again. It’s seem to be fine with security coding.</p><p>Let’s analysis <strong>Enclave::derefer</strong> method. We can see that they fetch index of <strong>Code Refer</strong> from normal SGXCode and use this idx to remove this value from array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1w68k-TOaiBzdG_XcAkLxw.png" /><figcaption><strong>Enclave::derefer</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dnuER12CqEgmDqvuS9XVqw.png" /><figcaption><strong>SGXCode::DelCodeRefer</strong></figcaption></figure><p>We see <strong>SGXCode::DelCodeRefer</strong> method in image above the ref_stack_idx is signed integer and with no verify this value this function seem to be vunerable with out of <strong>bound read / write</strong> bug.</p><p>Get back to <strong>Enclave::refer</strong> method if we apply index with negative number like -1 the method Enclave::refer is fail but value data_refer_idx still set to this value. After that, we call <strong>Enclave::derefer</strong> this method is happy to reuse this negative number to remove value from array, so we got out of bound read/write bug.</p><p>Let’s analysis further for leaking address and building exploit to popup gnome-calculator.</p><p>Look at QArrayData struct, this struct is stored our number value in SGXCode object. The offset value present as offset from beginning of this struct to the first value of QArrayData. We can use this vulnerability above which set this offset to zero by set data_refer_idx is -1, that mean content of QArrayData now point to beginning of this struct, so we can simply set size of QArrayData more than they have to read our needed information heap segment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PjeseRkhnblPzmQEOe-Z-A.png" /></figure><p>In my exploit, I’m going to create a lot of SGXCode (2 types) with magic value that help me located exactly where they are in memory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZFVjH1cE0Uh5anOR4RqqIw.png" /></figure><p>In modern web browser , Javascript engine is used JIT technique to improve speed. By checking memory mapped page we can see Javascript engine map some page <strong>read/write/execute</strong> so we can use this to put our shellcode, then overwrite SGXCode vtable point to our fake vtable link to our shellcode.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lSvxEAXcI7vrwpDZAMfjbA.png" /></figure><p>We can see my payload and practice again to understand more:</p><ul><li>Payload : <a href="https://gist.github.com/peternguyen93/e79f275322581f05fcaeee0c3d17260f">https://gist.github.com/peternguyen93/e79f275322581f05fcaeee0c3d17260f</a></li><li>Binary : <a href="https://drive.google.com/drive/folders/0BzJ4CX4jElkjSEM1Q2tVSGRYMlk?usp=sharing">https://drive.google.com/drive/folders/0BzJ4CX4jElkjSEM1Q2tVSGRYMlk?usp=sharing</a></li></ul><p>Video clip demo:</p><p><a href="https://youtu.be/cGlsoDSnoNA">https://youtu.be/cGlsoDSnoNA</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5c1c48e0d6fe" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tradahacking/hitb-2017-writeup-sgx-browser-5c1c48e0d6fe">HITB 2017 Writeup : SGX_Browser</a> was originally published in <a href="https://medium.com/tradahacking">tradahacking</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[HITB 2017 : 1000 levels Writeup]]></title>
            <link>https://medium.com/tradahacking/hitb-2017-1000-levels-writeup-49b241e1ad3c?source=rss-a233da090f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/49b241e1ad3c</guid>
            <category><![CDATA[ctf]]></category>
            <dc:creator><![CDATA[peternguyen]]></dc:creator>
            <pubDate>Sun, 27 Aug 2017 06:12:33 GMT</pubDate>
            <atom:updated>2017-08-27T06:12:33.348Z</atom:updated>
            <content:encoded><![CDATA[<p>Vulnerability of this challenge is quite simple, as you can see buffer overflow in function <strong>level, </strong>you can see in the picture below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5m0jy_zhJ9RzZG1gsmWNmQ.png" /></figure><p>The buffer is only 40 bytes and they read more than it. But wait you can only send input with length divided by 8 so it too hard to partial write 1,2 bytes in ret address to leak system in function <strong>hint</strong>. Let find other place to help us exploit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/986/1*hPn_IfE3jP3V2mG0fA6EXQ.png" /></figure><p>In function hint we see it check if variable show_hint is set they will print out system address, after 30 mins I can’t find other way to do that. After see assembly of this function I found some interesting point.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lUVBm7khxf-OVYiWrvuKTQ.png" /></figure><p>The address of <strong>system </strong>function is on the stack at offset 0x100 in function hint. If we view assembly of <strong>stack</strong> of function go we can see some think quite strange.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6GKqRyA4NcqVYNBrFVBV5Q.png" /></figure><p>At the offset 0x110 they store value of first level we input, that lets me to a new way to solve this challenge, after that, I analysis more in function go and I found a uninitialize bug in go function let the system address is place on stack without overwritten by something else.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/714/1*-I3TFOE52rdZLGQKs6H9Sw.png" /></figure><p>If we input number less than zero v5 will store our system address. And one problem we have to solve is bypass PIE in this binary, the point is <strong>vdso</strong> page they have gadget to help us ret to system address and get shell.</p><p>Here my payload : <a href="https://gist.github.com/peternguyen93/fbcb4d8fc692f93e6d50ff6fb446c947">https://gist.github.com/peternguyen93/fbcb4d8fc692f93e6d50ff6fb446c947</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=49b241e1ad3c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tradahacking/hitb-2017-1000-levels-writeup-49b241e1ad3c">HITB 2017 : 1000 levels Writeup</a> was originally published in <a href="https://medium.com/tradahacking">tradahacking</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Debug Linux Kernel với Qemu và GDB]]></title>
            <link>https://medium.com/tradahacking/debug-linux-kernel-v%E1%BB%9Bi-qemu-v%C3%A0-gdb-38c2cd29f616?source=rss-a233da090f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/38c2cd29f616</guid>
            <category><![CDATA[other]]></category>
            <category><![CDATA[application-security]]></category>
            <category><![CDATA[linux-kernel]]></category>
            <category><![CDATA[an-toan-ung-dung]]></category>
            <category><![CDATA[debugging]]></category>
            <dc:creator><![CDATA[peternguyen]]></dc:creator>
            <pubDate>Thu, 27 Jul 2017 04:45:12 GMT</pubDate>
            <atom:updated>2017-07-27T07:57:35.721Z</atom:updated>
            <content:encoded><![CDATA[<p>Tuần trước mình có chơi vài challenge liên quan đến Exploit Linux Kernel trên ARM, mình viết bài này với hy vọng share chút kiến thức về debugging Linux Kernel để cho các bạn đang nghiên cứu và debug Linux Kernel.</p><p>Trong phần tut này mình đề cập đến việc setup một bản Linux hoàn chỉnh trên Linux Vexpress A9 bằng QEMU và GDB (peda/gef).</p><p>Cài đặt QEMU:</p><ul><li>Trên Ubuntu : sudo apt install qemu</li><li>Trên Mac OS X: brew install qemu</li></ul><p>Cài đặt GDB:</p><ul><li>Trên Ubuntu : sudo apt install gdb-multiarch</li><li>Trên Mac OS X: brew install gdb --with-all-target --with-brew-python --unsafe ssl</li></ul><p>Cài đặt GCC cross platform cho phép bạn compile binary ARM trên nền x86 giúp tăng tốc việc biên dịch mã nguồn Linux Kernel.</p><ul><li>Trên ubuntu : sudo apt install gcc-arm-linux-gnueabihf .</li><li>Trên Mac OS X :brew tap osx-cross/arm &amp;&amp; brew install arm-gcc-bin</li></ul><p>Do một số tool create root system chỉ có trên Linux cho nên việc setup các package trên ubuntu for ARM cần 1 máy ảo Linux để tạo file img.</p><pre>mkdir vexpress &amp;&amp;  cd vexpress<br>mkdir mount<br>dd if=/dev/zero of=./vexpress-8G.img bs=8M count=1024 #tạo img file 8 Gib<br>sudo losetup -f ./vexpress-8G.img #mount img file vào loop device<br>sudo mkfs.ext4 /dev/loop0 # format ext4<br>sudo mount /dev/loop0 ./mount</pre><p>Bước kế tiếp tiến hành tạo bootstrap system lên file img này (bước này đòi hỏi bạn phải có sẵn 1 VM chạy Ubuntu).</p><pre>sudo qemu-debootstrap --arch=armhf trusty mount/<br>sudo cp `which qemu-arm-static` mount/usr/bin/</pre><blockquote><strong>trusty</strong> là codename của ubuntu 14.04 và arch là armhf.</blockquote><p>Sau đó tiến hành chroot vào system vừa tạo tiến hành cấu hình vài thứ trước khi vào phần compile linux kernel.</p><pre>sudo chroot mount/<br>sed &#39;s/tty1/ttyAMA0/g&#39; /etc/init/tty1.conf &gt; /etc/init/ttyAMA0.conf<br>echo &quot;deb  http://ports.ubuntu.com trusty main restricted multiverse universe&quot; &gt; /etc/apt/sources.list<br>apt-get update<br>echo -e &quot;\nauto eth0\niface eth0 inet dhcp&quot; &gt;&gt; /etc/network/interfaces<br>passwd root # setup password cho root account</pre><blockquote>QEMU mặc định thương sửa dung GTK để render virtual monitor của VM, tuy nhiên để tiện hơn trong việc sử dung VM trong việc debug, QEMU cho phép người dùng connect terminal từ host machine vào guest machine. Trong trường hợp này mình sử dụng ttyAMA0 tương ứng với màn hình ảo của cổng serial trên Vexpress.</blockquote><p>Tiến hành biên dịch Linux Kernel ở đây minh chọn kernel 3.x.x download từ kernel.org và giải nén.</p><p>Thực hiện lệnh sau tiến để tạo file cấu hình chuẩn trên Vexpress</p><p>make ARCH=armhf CROSS_COMPILE=arm-linux-gnueabihf- vexpress_defconfig</p><p>make ARCH=armhf CROSS_COMPILE=arm-linux-gnueabihf- menuconfig</p><blockquote>Linux Kernel source code có khá nhiều tính năng và driver kèm theo do đó để tối ưu hóa Linux Kernel các bạn có thể loại bỏ hoặc thêm vào những tính năng cần thiết ở trong bài viết này mình sẽ tập trung vào các feature cần để compile Linux Kernel boot được trên QEMU.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/437/1*Wg4QAFOu2t1ZcMO-qUQpdQ.jpeg" /><figcaption>General &gt; Enable <strong>open by fhandle syscalls</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/686/1*Vz-u4eKQ4QLlS2gZ_vwc9A.jpeg" /><figcaption>Device Driver &gt; Generic Driver &gt; Enable 2 options như hình trên</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/1*gvZamY4snBFNq1zMfbqScw.jpeg" /><figcaption>Device Driver &gt; Network Device Support &gt; Ethernet Driver Support &gt; Enable Driver SMC như hình trên</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*Wl-CnHUPWaI30Ed_mRnoPw.jpeg" /><figcaption>Device Driver &gt; Network Device Support &gt; MMC SD SDIO Card Support &gt; Enable các option như hình trên đảm bảo SD card work với vexpress-a9</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/411/1*r_TqZXICqtvsLFXFMrW_Ww.jpeg" /><figcaption>File systems &gt; Enable Ext4 support</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/1*qpUuI9rSqMX7kQF7KRbizg.jpeg" /><figcaption>Kernel hacking &gt; Enable KGDB vaf Kernel Debugging</figcaption></figure><p>Do dòng kernel 3.x.x thiếu một vài option nên mình tiến hành add thêm vài config vào file vim .config</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/361/1*KWRpCKpGWpnA0ljeKjcMMA.jpeg" /><figcaption>set CONFIG_LBDAF=y</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/351/1*_LRMtjVkwDYmtG30sF_hYA.jpeg" /><figcaption>enable tiếp các config như hình trên</figcaption></figure><p>Save và compile kernel:</p><p>make ARCH=armhf CROSS_COMPILE=arm-linux-gnueabihf- -j2</p><p>Sau khi compile kernel xong copy các file sau :</p><p>cp linux-3.x.x/arch/arm/boot/zImage ./</p><p>cp linux-3.x.x/arch/arm/boot/dts/vexpress-v2p-ca9.dtb ./</p><p>Copy source linux vừa compile vào root của máy ảo bằng cách mount lại file img vào system, rồi copy.</p><p>Start QEMU bằng command sau:</p><p>qemu-system-arm -m 512M -M vexpress-a9 -kernel zImage -dtb vexpress-v2p-ca9.dtb -net user,hostfwd=tcp::10022-:22 -net nic -sd ./vexpress-8G.img -gdb tcp::1234 -nographic</p><blockquote>Các option:</blockquote><blockquote>net user,hostfwd=tcp::10022-:22 : forward ssh port từ guest vm sang host vm.</blockquote><blockquote>gdb tcp::1234 : enable gdbserver với Linux kernel.</blockquote><p>Tiến hành debug một Linux Module đơn giản mình sử dụng ebbchar sample từ trang này <a href="http://derekmolloy.ie/writing-a-linux-kernel-module-part-2-a-character-device/">http://derekmolloy.ie/writing-a-linux-kernel-module-part-2-a-character-device/</a> .</p><p>Trước khi compile module trên cd đến linux-3.x.x chạy lệnh make modules_install để cài các gói modules lib hỗ trợ việc compile Linux Modules.</p><p>Tiến hành compile và load module này bằng lệnh make &amp;&amp; insmod hello.ko.</p><p>Tiếp theo load debug symbol của modules và linux kernel vào GDB Client</p><p>gdb<br>set architecture arm<br>file /linux-3.x.x/vmlinux</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IWzbBhlo0S07PmRzB0DSwQ.png" /><figcaption>Extract địa chỉ của từng segment trong các module.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nIYdiU_0YcJkeg9k470jMA.png" /><figcaption>Sau đó add địa chỉ này cùng với các debug symbol vào gdb</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2FaVw72QFFljpRNiDYEpgQ.png" /><figcaption>Set breakpoint <strong>dev_open</strong> sau đó ở VM Guest chạy lệnh <strong>cat /dev/ebbchar </strong>và hit breakpoint</figcaption></figure><p>OK vậy giờ mình có thể debug Linux Kernel cũng như là Linux module như từng làm với ELF executable.</p><p>Happly Linux Kernel Hacking. :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=38c2cd29f616" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tradahacking/debug-linux-kernel-v%E1%BB%9Bi-qemu-v%C3%A0-gdb-38c2cd29f616">Debug Linux Kernel với Qemu và GDB</a> was originally published in <a href="https://medium.com/tradahacking">tradahacking</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>