<?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 Taishi Nojima on Medium]]></title>
        <description><![CDATA[Stories by Taishi Nojima on Medium]]></description>
        <link>https://medium.com/@taishinojima?source=rss-68b87bc59967------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*BY1pkDMhn_mDi8DB.</url>
            <title>Stories by Taishi Nojima on Medium</title>
            <link>https://medium.com/@taishinojima?source=rss-68b87bc59967------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 15 Nov 2026 00:52:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@taishinojima/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[Awake Security BlackHat 2017 Soirée PCAP Challenge Write-up — Analyzing a PCAP file in a hard way]]></title>
            <link>https://medium.com/@taishinojima/awake-security-blackhat-2017-soir%C3%A9e-pcap-challenge-write-up-analyzing-a-pcap-file-in-a-hard-way-48006c0628fe?source=rss-68b87bc59967------2</link>
            <guid isPermaLink="false">https://medium.com/p/48006c0628fe</guid>
            <category><![CDATA[network-security]]></category>
            <category><![CDATA[network-analysis]]></category>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <dc:creator><![CDATA[Taishi Nojima]]></dc:creator>
            <pubDate>Wed, 09 Aug 2017 00:00:00 GMT</pubDate>
            <atom:updated>2017-08-17T06:05:23.350Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi! It’s been a real while since the last post (I said the same in the last post)… But this summer, I’m working in one of the security teams at Facebook as an intern, and one of my colleagues told me about an interesting PCAP analysis challenge.</p><p>Solving this was a ticket for an after party for this year’s BlackHat organized by <a href="http://fortune.com/2017/07/17/cyber-security-startup-awake/">Awake Security</a>. Although I couldn’t go to the BlackHat this year nor the after party, I found this challenge very interesting and worth a post.</p><p>If anyone wants to try the challenge, <a href="https://taishi8117.github.io/download/awake/awake-puzzle-bh2017.pcap">this</a> is the actual PCAP file downloaded from <a href="https://docs.google.com/forms/d/e/1FAIpQLSd0yUXDMdPwqPSU9nnToG01qDPrOIL1R_snp3yT_Bj1wpk2cA/viewform">this registration form</a>.</p><p>Let’s dive in!</p><h3>Reading the instruction</h3><p>When you open up the PCAP file, you’ll notice that there’s a HTTP GET request to /instructions.hello. Let’s see what’s in there first by “Follow HTTP Stream”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*R5XL10DR8gx9P5sp.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/978/1*8L1Y9oLmnL0huTRVoDVp4g.png" /></figure><p>Well, it looks like a normal instruction except one thing: Accept-Encoding: gzip, xor. Never heard of xor encoding for HTTP request, maybe a clue for the flag.</p><h3>Weird stuff in mDNS</h3><p>When you look through other frames, you’ll notice a few <a href="https://en.wikipedia.org/wiki/Multicast_DNS">mDNS</a> packets buried in a bunch of TCP packets. But the search queries definitely don’t look legit, like alert.msg, cyberchef.helps and key.version? They might be another clue for the flag!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tSwHsdeMJKHUzz3q.png" /></figure><h3>Are they really just TCP?</h3><p>You might be stuck at this point (well, I was), as what’s left were five similar TCP connections between 75.101.121.222:44344 and 192.168.1.116 in which you can find little human readable strings, except for “Agbogbloshie” and “Golden Showers Far East Import” (WTF?).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/969/0*trksUSzDr7Vr8ofz.png" /></figure><p>But if you look closely enough, you’ll realize that the data payload of many of these packets starts with the bytestring 16 03 03 00.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gjarVuoMlRt-tdel.png" /></figure><p>Unfortunately, I don’t know the header bytestring of every common protocol by heart, so I just <a href="https://www.google.com/search?site=&amp;source=hp&amp;q=16+03+03+00">googled it</a> and found that it’s a header for TLS packet! (<a href="http://blog.fourthbit.com/2014/12/23/traffic-analysis-of-an-ssl-slash-tls-session">This web page</a> has a great explanation for the protocol format.)</p><p>Now, all you need to do is to decode these packets as SSL stream.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/0*iumajROLEGQ2I-RP.png" /></figure><h3>Finding the flag</h3><p>Once you decode them as SSL and go through them, you’ll realize one weird thing: Encrypted Alert in the frame 524.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gjOyiILtpp1uYBMd.png" /></figure><p>If you remember the earlier clue alert.message, you’ll realize this packet probably has some clue. The packet header says the message length is 308 (or 0x0134 if you prefer hex), so I extracted the message part (from the bytestring 50 4b 03 to the end, which is right after the length header) and use <a href="https://gchq.github.io/CyberChef/">Cyberchef</a> to figure out what it is. (cyberchef.helps was another clue from mDNS packet.)</p><p>When you use <a href="http://bit.ly/2hNEYae">the Detect File Type operation</a>, you’ll see that this is a zip file!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Cr3vol8Nl6kyUHE2.png" /></figure><p>Cool, let’s just unzip it — this is <a href="http://bit.ly/2hLf1I6">the unzipped file</a> in Cyberchef.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/957/0*HDAQrxbgaEgJPfE2.png" /></figure><p>Umm, it’s still not really readable… But remember, you still have two remaining clues: key.version from mDNS and xor from the instruction page.</p><p>Aha, maybe XORing this file with the Encrypted Alert’s version (0x00de)?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*bzdeVRRUcEOVssry.png" /></figure><p>And here it is, found the flag!</p><p><em>Originally published at </em><a href="https://taishi8117.github.io/2017/08/09/awake-pcap-challenge/"><em>taishi8117.github.io</em></a><em> on August 9, 2017.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=48006c0628fe" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Exploiting Simple Buffer Overflow (3) — Writing a simple Metasploit module]]></title>
            <link>https://medium.com/@taishinojima/exploiting-simple-buffer-overflow-3-writing-a-simple-metasploit-module-a922ebd042ba?source=rss-68b87bc59967------2</link>
            <guid isPermaLink="false">https://medium.com/p/a922ebd042ba</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[buffer-overflow]]></category>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[hacking]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <dc:creator><![CDATA[Taishi Nojima]]></dc:creator>
            <pubDate>Sun, 24 Jul 2016 00:00:00 GMT</pubDate>
            <atom:updated>2017-08-17T05:58:06.379Z</atom:updated>
            <content:encoded><![CDATA[<p>Hello! It’s been a while since the last post, but I’m currently doing a summer internship at Twitter, which has been extremely fun. And what’s more, I will probably be able to go to DEF CON 24 in Las Vegas! I will definitely post about it after the conference, but for this article, I want to continue the sequence of Exploiting Simple Buffer Overflow.</p><p>Today, I will show you how to exploit a simple buffer overflow against my custom vulnerable TCP server, by developing a custom exploit module for Metasploit Framework. As you know, <a href="http://metasploit.com/">Metasploit Framework</a> is the most popular pentesting tool out there. It is extremely powerful and covering exploits of most public vulnerabilities, and thanks to its user-friendly framework, it is also really easy to develop your own exploit module.</p><p>So, let’s dive in. I have prepared a stupidly simple and vulnerable TCP server in C, as well as its client in Python — you can download them from <a href="https://github.com/taishi8117/bof_lab/tree/master/vuln_server">here</a>.</p><h3>Vulnerable Server</h3><p>The source code for the vulnerable server is located <a href="https://github.com/taishi8117/bof_lab/blob/master/vuln_server/tcp_server.c">here</a>. It is a very simple TCP server, so I will omit explaining what each code does, but below is the vulnerable part, which is called as soon as a client socket is accepted.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e2767dd293b49ec1ba529f8c6baf485/href">https://medium.com/media/2e2767dd293b49ec1ba529f8c6baf485/href</a></iframe><p>First, it reads 4-byte header, indicating how many bytes to read for the body. And using that value, it then reads the body into buffer. As you can guess, it is obvious that int read_bytes = read(cli_fd, buffer, to_read); has a buffer overflow vulnerability, since to_read can be much larger than 1024!</p><p>Let’s try it out. I will use Ubuntu 14.04 64-bit for this experiment, although I will compile the server in 32-bit mode for the sake of simplicity.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cbd96959dd9b531f808612f1254379d9/href">https://medium.com/media/cbd96959dd9b531f808612f1254379d9/href</a></iframe><p>For the Makefile, make sure that if you are compiling in 32-bit mode on a 64-bit machine, CFLAGS=-m32 is turned on, as well as -fno-stack-protector and -z execstac.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b4aeffe99b26a4efb6bc599ad1b0a78c/href">https://medium.com/media/b4aeffe99b26a4efb6bc599ad1b0a78c/href</a></iframe><p>Also, I will do this experiment without ASLR. If you want to learn how to bypass ASLR, you can check the <a href="http://taishi8117.github.io/2015/11/11/stack-bof-2/">previous article</a>.</p><p>Let’s look at how this server behaves. Open a terminal, and compile and start the server by ./tcp_server 1234. On another terminal, modify the <a href="https://github.com/taishi8117/bof_lab/blob/master/vuln_server/simple_client.py">Python client</a>, so MESSAGE = &quot;Hello\n&quot; and executes it. You should see the following.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e4d6989f748a29f71cebb5218ebbf28/href">https://medium.com/media/4e4d6989f748a29f71cebb5218ebbf28/href</a></iframe><p>And on the server terminal, you should see the following.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e0e977b9309ee1abd35ea2f86694db1b/href">https://medium.com/media/e0e977b9309ee1abd35ea2f86694db1b/href</a></iframe><p>Cool! Let’s also check the offset for EIP, using GDB and <a href="https://github.com/longld/peda">peda</a>’s pattern_create function. In this case, since the buffer size is 1024, I created a pattern with 1200 characters, so it will be large enough to overflow.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/00c5cd4003213602041beb8ea5823dd3/href">https://medium.com/media/00c5cd4003213602041beb8ea5823dd3/href</a></iframe><p>Copy the pattern to the value of MESSAGE for the client script. Then start the server inside the GDB by run 1234, and on another terminal, start the client. The output on the client terminal should look like the following.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/616725bde110ca1890c9d429f77a9503/href">https://medium.com/media/616725bde110ca1890c9d429f77a9503/href</a></iframe><p>On the other hand, the output for the server side should look like the following.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9003a848a6d566a39ce4d8fb16d1d68/href">https://medium.com/media/c9003a848a6d566a39ce4d8fb16d1d68/href</a></iframe><p>Using pattern_search, we now know that EIP will be overwriten by 4 bytes starting from 1040th character in the body. This means that including the 4-byte header, the RET address needs to start from 1044th character. We also know that the address of the top of the stack is 0xffffcfe0, from which our payload will be written and executed. Now, we know everything for the exploit!</p><p>Writing an exploit module for Metasploit Framework is quite simple, but you need to first set up the development environment. Although I will not elaborate on this, you can refer to <a href="https://github.com/rapid7/metasploit-framework/wiki/Setting-Up-a-Metasploit-Development-Environment">this page</a>, which has a perfect instruction!</p><p>Now, I assume that you have successfully set up the development environment for Metasploit :-)</p><p>Just to give you some sense of how the codes are organized, exploit modules are located under metasploit-framework/modules/exploits directory, payloads are under metasploit-framework/modules/payloads, and the core library for Metasploit is located under metasploit-framework/lib/msf.</p><p>For this article, I will create a module called bof_lab.rb under metasploit-framework/modules/exploits/custom directory. It can be downloaded from <a href="https://github.com/taishi8117/bof_lab/blob/master/vuln_server/bof_lab.rb">here</a>. If you want to test it with Metasploit Framework, make sure bof_lab.rb is copied somewhere under metasploit-framework/modules/exploits, so it is automatically loaded!</p><p>I will also not elaborate on the details of how to develop a custom module, since there are a lot of documentation out there. You should probably read <a href="https://github.com/rapid7/metasploit-framework/wiki/How-to-get-started-with-writing-an-exploit">this</a> first, if you are interested. Instead, I will briefly explain the code.</p><p>First, I included Metasploit’s TCP library (include Msf::Exploit::Remote::Tcp), as I need to connect to a TCP server.</p><p>In <a href="https://github.com/taishi8117/bof_lab/blob/master/vuln_server/bof_lab.rb#L14">initialize()</a>, you need to define some information about the exploit module, such as name, description, payload encoding and target architecture. In this case, I will not use any payload encoding for the sake of simplicity, and the target architecture would be linux x86 (this is because I compiled the server in 32-bit mode). I will also define Ret to indicate where the payload will be located (which is 0xffffcfe0 as we found earlier).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1dc365dcf30e2ad3af2b74f15932cc5b/href">https://medium.com/media/1dc365dcf30e2ad3af2b74f15932cc5b/href</a></iframe><p><a href="https://github.com/taishi8117/bof_lab/blob/master/vuln_server/bof_lab.rb#L47">check()</a> method is for testing whether a target is vulnerable. But for the sake of simplicity, I made it to return that the target host is always vulnerable.</p><p>Now, <a href="https://github.com/taishi8117/bof_lab/blob/master/vuln_server/bof_lab.rb#L51">exploit()</a> is the core part of this module, as the name suggests obviously.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b4300934fbdd49facbad52607cce656/href">https://medium.com/media/0b4300934fbdd49facbad52607cce656/href</a></iframe><p>It basically connects to the server, sends A for 1044 times (which is the offset), and Ret and finally the payload. That’s it, super simple!!</p><h3>Exploit!</h3><p>Now, let’s try an exploit!</p><p>Note that my environment was the following:</p><ul><li>Server (10.0.1.130) listening on port 1234, username: sirius</li><li>MSF (127.0.0.1), reverse handler listening on port 8888</li></ul><p>First, start the msfconsole and direct to the module that you just created.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a016f495ba76551c96a2b1f45a28a6db/href">https://medium.com/media/a016f495ba76551c96a2b1f45a28a6db/href</a></iframe><p>Let’s specify options, according to the environment. For payloads, I will use linux/x86/shell/reverse_tcp, so I can get a shell of the server. You can also specify other payloads, if you want to play around with different ways.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc8f1145196c2c11cc3d557ce2ff0c90/href">https://medium.com/media/fc8f1145196c2c11cc3d557ce2ff0c90/href</a></iframe><p>Here we go! You got it!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3c46825f477ce85446c3f82cdfd5d2f/href">https://medium.com/media/c3c46825f477ce85446c3f82cdfd5d2f/href</a></iframe><p><em>Originally published at </em><a href="https://taishi8117.github.io/2016/07/24/bof-metasploit/"><em>taishi8117.github.io</em></a><em> on July 24, 2016.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a922ebd042ba" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Exploiting Simple Buffer Overflow (2) — Shellcode + ASLR Bruteforcing]]></title>
            <link>https://medium.com/@taishinojima/exploiting-simple-buffer-overflow-2-shellcode-aslr-bruteforcing-e2f967d4206c?source=rss-68b87bc59967------2</link>
            <guid isPermaLink="false">https://medium.com/p/e2f967d4206c</guid>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[buffer-overflow]]></category>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[hacking]]></category>
            <dc:creator><![CDATA[Taishi Nojima]]></dc:creator>
            <pubDate>Wed, 11 Nov 2015 00:00:00 GMT</pubDate>
            <atom:updated>2017-08-17T05:49:10.064Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi! For my second article on exploiting simple buffer overflow, I want to talk about bruteforcing against ASLR (Address Space Layout Randomization).</p><p>For those who are not so familiar with ASLR, it is a common countermeasure technique against traditional stack-based stack buffer overflow exploit — it randomizes the memory address layout (e.g. stack) every time a program is run so that an attacker cannot know the exact location of shellcodeo or variable etc. beforehand. It is enabled by default for most recent operation systems, and it is fairly effective for 64-bit systems (the range of randomization of address layout is 28-bit). However, for 32-bit systems, the range of randomization of address layout is so much smaller that bruteforcing is feasible.</p><p>So, in this article, I will demonstrate a bruteforce against ASLR in 32-bit system, so that an arbitrary shellcode can be run. Note that other countermeasure techniques (e.g. DEP, Canary) are not used for the sake of simplicity in this experiment. (I promise that I will post about attacks against these techniques in the future!) Also, source code used in this article can be downloaded from <a href="https://github.com/taishi8117/bof_lab/tree/master/bof">here</a>.</p><p>I will use Ubuntu 14.04 32-bit for this experiment.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ecb2c46ac3de0814e35063008250d33/href">https://medium.com/media/8ecb2c46ac3de0814e35063008250d33/href</a></iframe><p>And here is a simple vulnerable program that I will use. As you can see, vuln() function has a buffer overflow vulnerability when strcpy is called.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fb93a13c2ee5722e7659f46c4ddda921/href">https://medium.com/media/fb93a13c2ee5722e7659f46c4ddda921/href</a></iframe><p>And for Makefile, make sure that -fno-stack-protector and -z execstack are included in the flags.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/73d66d17667603164033884e811540a6/href">https://medium.com/media/73d66d17667603164033884e811540a6/href</a></iframe><p>After it was compiled, make sure that ASLR is turned on. Note that echo 1 | sudo tee /proc/sys/kernel/randomize_va_space can turn on ASLR. (echo 0 to disable, echo 2 to enable full ASLR)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56b1ab0c768e3c4cc5e8187adc084f32/href">https://medium.com/media/56b1ab0c768e3c4cc5e8187adc084f32/href</a></iframe><p>Let’s examine how the program behaves when its buffer is overflowed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6c392f4a94ea4f003c7a37a50cc667d/href">https://medium.com/media/d6c392f4a94ea4f003c7a37a50cc667d/href</a></iframe><p>As you can see, 4 bytes starting from 112th character in the input string will overwrite EIP.</p><p>Let’s also check how ASLR affects the address of buffer.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/405334e79ec97186111eeca5d0329c5e/href">https://medium.com/media/405334e79ec97186111eeca5d0329c5e/href</a></iframe><p>As you can see, the location of buffer is randomized every time the program is run, and it is impossible to guess its exact location for the next execution. However, if you examine the location of buffer enough times, you will realize that its location is within the range of 0xbf*****c. In this case, there are 1048576 possible locations where buffer is loaded. So, you can use a strategy — randomly pick a memory address within the valid range, and run the program repeatedly until buffer happens to be located at that location and that’s when the shellcode is executed.</p><p>Let’s also write a shellcode that executes ‘/bin/sh’ so you can get a shell. I will not explain the details of how to write shellcode, but there are lots of resources on the web that you can find very useful.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/86ba2bb5f47efbf8a9ba0fd780759728/href">https://medium.com/media/86ba2bb5f47efbf8a9ba0fd780759728/href</a></iframe><p>Now, we have everything that’s needed to exploit this program. Below is the exploit code written in Python. Note that in order to increase the chance of shellcode being executed, I added 4096 bytes of NOP instruction right before the inserted shellcode. In this way, if the returned address (=addr_buffer+bufsize+offset+4) happens to be within this NOP range, the shellcode is properly executed as well (the longer the NOP sled, the less number of attempts necessary for a successful attack).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/071e339ed7ec95cde582ec47d80bbe55/href">https://medium.com/media/071e339ed7ec95cde582ec47d80bbe55/href</a></iframe><p>And here it is! The shell was given after 998 attempts in my case. You can experiment in your environment too. Normally for this program with 4096 bytes of NOP sled, around 1000 times of attempts are necessary for a successful attack, which is significantly less than 1048576 (the number of possible address location of buffer)!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/89987ab8d0d14c959b72e5799d477c1e/href">https://medium.com/media/89987ab8d0d14c959b72e5799d477c1e/href</a></iframe><p><em>Originally published at </em><a href="https://taishi8117.github.io/2015/11/11/stack-bof-2/"><em>taishi8117.github.io</em></a><em> on November 11, 2015.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e2f967d4206c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Exploiting Simple Buffer Overflow (1) — Super Basics]]></title>
            <link>https://medium.com/@taishinojima/exploiting-simple-buffer-overflow-1-super-basics-e2eb207be41?source=rss-68b87bc59967------2</link>
            <guid isPermaLink="false">https://medium.com/p/e2eb207be41</guid>
            <category><![CDATA[buffer-overflow]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[hacking]]></category>
            <category><![CDATA[ctf]]></category>
            <dc:creator><![CDATA[Taishi Nojima]]></dc:creator>
            <pubDate>Sat, 07 Nov 2015 00:00:00 GMT</pubDate>
            <atom:updated>2017-08-17T05:49:27.726Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently I’ve been revising the concept of buffer overflow and its exploitation, so I will post a sequence of articles on this topic with varying settings.</p><p>In this article, let’s exploit a simple SUID-ed program with a stack-based Buffer Overflow to add a new root-priviledged user with a chosen password. This example is referenced from the book <a href="http://www.amazon.com/Hacking-Art-Exploitation-Jon-Erickson/dp/1593271441/ref=sr_1_sc_1?ie=UTF8&amp;qid=1446946770&amp;sr=8-1-spell&amp;keywords=hacking+the+art+of+exploitaitn">Hacking: the Art of Exploitation</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47a7a74e7894fd49136e15f580d0e52c/href">https://medium.com/media/47a7a74e7894fd49136e15f580d0e52c/href</a></iframe><p>I’m using x86_64, but I will compile the program in 32bit mode.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7edb6380fd6a8cf481496a94e624a0c1/href">https://medium.com/media/7edb6380fd6a8cf481496a94e624a0c1/href</a></iframe><p>The program shown above simply appends buffer to the file /var/notes. When strcpy is called to copy argv[1] to buffer, it can cause a buffer overflow — when more than 100 characters are stored in argv[0], then the remaining string would be overflowed. The Makefile shown below is used to compile and chown/chmod the program.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/84d6732082e33def6549d89e4d7a2991/href">https://medium.com/media/84d6732082e33def6549d89e4d7a2991/href</a></iframe><p>When argv[1] is not over 100 characters, it works perfectly.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/974bd267a1642274f09b0eb590a56ef2/href">https://medium.com/media/974bd267a1642274f09b0eb590a56ef2/href</a></iframe><p>Since the offset between buffer and datafile is 100 (same as the size of buffer), the overflowed strings of argv[1] will be overwritten to datafile.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3f580c1c7634bca02d19c97ef330fe07/href">https://medium.com/media/3f580c1c7634bca02d19c97ef330fe07/href</a></iframe><p>By exploiting this BOF, let’s create a new user with a chosen password by appending to /etc/passwd file. Following the format of /etc/passwd in UNIX system, I will add username myroot with password password — myroot:AA6tQYSfGxd/A:0:0:me:/root:/bin/bash. As shown below, password is encrypted using Crypto() OpenSSL cryptographic library with a salt “AA”. Note that the 3rd &amp; 4th entry (i.e. “0:0”) indicates root privilege. If you want to know the format of /etc/passwd more in detail, refer to a link <a href="http://www.cyberciti.biz/faq/understanding-etcpasswd-file-format/">here</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5569b4fb873aa15d27931917e53e3e4e/href">https://medium.com/media/5569b4fb873aa15d27931917e53e3e4e/href</a></iframe><p>The content of datafile needs to be /etc/passwd, but the last entry in /etc/passwd needs to specify the user’s default shell. So, let’s create a symbolic link with the name /tmp/etc/passwd pointing to /bin/bash. Then, adjust the length of the user ID info (comment section), so that the location of /etc/passwd would be at 100.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/17574330ab2f25717eceb9c37e520edc/href">https://medium.com/media/17574330ab2f25717eceb9c37e520edc/href</a></iframe><p>Good. And here it is, you get the root shell.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8896e66d8388e89f5a3954fdd55377b6/href">https://medium.com/media/8896e66d8388e89f5a3954fdd55377b6/href</a></iframe><p><em>Originally published at </em><a href="https://taishi8117.github.io/2015/11/07/stack-bof-1/"><em>taishi8117.github.io</em></a><em> on November 7, 2015.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e2eb207be41" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ksnctf Villager A write-up]]></title>
            <link>https://medium.com/@taishinojima/ksnctf-villager-a-write-up-5a627b71e9cd?source=rss-68b87bc59967------2</link>
            <guid isPermaLink="false">https://medium.com/p/5a627b71e9cd</guid>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[hacking]]></category>
            <dc:creator><![CDATA[Taishi Nojima]]></dc:creator>
            <pubDate>Tue, 20 Oct 2015 00:00:00 GMT</pubDate>
            <atom:updated>2017-08-17T06:15:02.049Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/772/0*x917WOXi9irj1TwF.png" /></figure><p><a href="http://ksnctf.sweetduet.info/">ksnctf</a> is one of the beginner level CTF websites. This article is the write-up for the question 4 <a href="http://ksnctf.sweetduet.info/problem/4">Villager A</a>, in which you need to exploit the <em>Format String Vulnerability</em> to capture the flag!</p><p><strong>Note that this article includes a complete solution. If you are looking for some hints for Villager A, you should refer to these keywords: </strong><strong>Format String Vulnerability and </strong><strong>GOT overwrite.</strong></p><h3>Write-up</h3><h4>Connect to the server</h4><p>Using given information, access to the server ssh -p 10022 q4@ctfq.sweetduet.info<br> In the server, you can find</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d6fb16de492763ef8e40b2fdc0789dc/href">https://medium.com/media/2d6fb16de492763ef8e40b2fdc0789dc/href</a></iframe><h4>Analysis</h4><p>Since I don’t have an access to read flag.txt, it seems that I need to somehow exploit q4 (SUID=root) to read the file. Let’s disassemble main().</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/59721baa21f6850ed6d91996332aa454/href">https://medium.com/media/59721baa21f6850ed6d91996332aa454/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/207f8f27dbff14d7ad79612ffbfcdfe9/href">https://medium.com/media/207f8f27dbff14d7ad79612ffbfcdfe9/href</a></iframe><p>It seems that at &lt;main+48&gt;, fgets() is called to get a string from stdin; at &lt;main+72&gt;, printf() is called to output the string. But, it’s kind of weird that printf() was called at &lt;main+60&gt; to output “Hi, “, and after that, putchar() was called to output “\n” (0xa). Why are they called separately, instead of calling just one like printf(&quot;Hi, %s\n&quot;, input); as you probably more familiar to write. Now, I’m getting suspicious that there is a format string vulnerability in this program. (I mean, it’s a CTF program)<br> What if I input some kind of <em>format string</em> at &lt;main+48&gt;?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/09bb40ca695fab46566eb7049f8f0c5e/href">https://medium.com/media/09bb40ca695fab46566eb7049f8f0c5e/href</a></iframe><p>It’s now clear that there is a format string vulnerability at <strong>&lt;main+72&gt;</strong>. So, let’s think about how to exploit it to read flag.txt.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec4c29352e14924f549a9c2a3957c0fd/href">https://medium.com/media/ec4c29352e14924f549a9c2a3957c0fd/href</a></iframe><p>Realize that 0x1 is moved into [esp+0x418] right before the jump to &lt;main+205&gt;. Then, 0x1 is brought back to eax, followed by jne at &lt;main+219&gt; – if the value of eax is not 0 (or ZF = 0), it jumps to &lt;main+102&gt; that is a loop asking “Do you want the flag?”, as shown below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/44db0f8772ddaeba13ef5aa168f73b7b/href">https://medium.com/media/44db0f8772ddaeba13ef5aa168f73b7b/href</a></iframe><p>On the other hand, if the jump was not taken, then it opens flag.txt and print it out (from &lt;main+212&gt; onwards) One possibility to attack this program is by using format string attack to change the value of [esp+0x418] to 0 before jne at &lt;main+219&gt;, but it wouldn’t work because mov [esp+0x418], 0x1 happens after the string vulnerability. Moreover, ASLR (Address Space Layout Randomization) is enabled on this system, so guessing the stack address of [esp+0x418] is very hard.</p><h4>Exploitation</h4><p>Remember that ASLR doesn’t disable the randomization of memory address of code section. If I can somehow set eip to &lt;main+221&gt; (0x08048691), I should be able to read flag.txt.<br> Right after the format string vulnerability, putchar@plt is called, using PLT. That means, if I modify the address referred at putchar@plt to &lt;main+221&gt;, I can modify eip!<br> Let’s examine.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/76e99d8d290c9c29992151944e459f3a/href">https://medium.com/media/76e99d8d290c9c29992151944e459f3a/href</a></iframe><p>So, it seems that if I change the value stored at <strong>0x80499e0</strong> to be <strong>0x08048691</strong> (&lt;main+219&gt;), I can read flag.txt.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6bdc1f9235ca55cc37078838d5982bec/href">https://medium.com/media/6bdc1f9235ca55cc37078838d5982bec/href</a></iframe><p>As examining the format vulnerability, it seems that the input string is stored at <strong>6th place</strong> from the top of the stack.</p><h3>Capture the flag!</h3><p>Now, we got all the informatin needed.</p><ul><li>modify the value at <strong>0x80499e0</strong> to <strong>0x8048691</strong></li><li>offset is 6</li></ul><p>Some calculation</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1a71d7dc6b1198ad835ddcc6cb9607ad/href">https://medium.com/media/1a71d7dc6b1198ad835ddcc6cb9607ad/href</a></iframe><p>Thus, the exploit code is</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/067e6385439159cf7699eaabc8ce5df9/href">https://medium.com/media/067e6385439159cf7699eaabc8ce5df9/href</a></iframe><p>Then, the flag was: <strong>FLAG_nwW6eP503Q3QI0zw</strong></p><p><em>Originally published at </em><a href="https://taishi8117.github.io/2015/10/20/ksnctf-villager-A/"><em>taishi8117.github.io</em></a><em> on October 20, 2015.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5a627b71e9cd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>