<?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 Tettra Coins on Medium]]></title>
        <description><![CDATA[Stories by Tettra Coins on Medium]]></description>
        <link>https://medium.com/@zlkidda?source=rss-6a0f609662fe------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*WjQaObcXgUiZ60Xj.</url>
            <title>Stories by Tettra Coins on Medium</title>
            <link>https://medium.com/@zlkidda?source=rss-6a0f609662fe------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 08:19:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@zlkidda/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[Quest For My First Zero Day]]></title>
            <link>https://medium.com/series/quest-for-my-first-zero-day-74a033aad6df?source=rss-6a0f609662fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/74a033aad6df</guid>
            <dc:creator><![CDATA[Tettra Coins]]></dc:creator>
            <pubDate>Tue, 14 May 2019 19:44:24 GMT</pubDate>
            <atom:updated>2019-05-14T19:44:24.232Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*dPqIgeUqO8A8pNcMz6TvOw.jpeg" /></figure><h3>Day 0: Writing my first shell code — Quest for my first Zero Day</h3><p>I’m on my mission to getting my first zero day exploit. I’m really interested in binary exploitation and reverse engineering but have found it very difficult to independently study this. I’m pretty much a complete beginner in this area. So this is going to be a series of blog posts of me studying the art of binary exploitation.</p><h4>Skip ahead</h4><p>Day 1: <a href="https://medium.com/@zlkidda/day-1-quest-for-my-first-zero-day-dealing-with-stripped-binaries-8501460e2781">Quest for my first Zero day — Dealing with stripped binaries</a></p><p>The goal of this is to keep me motivated and on track to learning binary exploitation. While also being a reference point that someone else who is in my situation can use to follow along with and learn.</p><p>I feel quite comfortable writing programs in Python, but struggle to write anything in C. So I will also be studying C along the way. Hopefully learning to use GDB, IDA, radare2, klang and other stuff that I’ve heard wonderful things about. Although I know that GDB alone has a demanding learning curve.</p><p><strong>Challenge 1 — OWR</strong></p><p>I’m a big fan of <a href="https://www.hackthebox.eu/home/machines/list">https://www.hackthebox.eu/</a> they offer webbased CTF’s through a VPN (free or paid). But they do not offer much for learning simple binary exploitation. After some googling I found a site laid out similarly to Hack The Box. I advice that you follow along with the challenge.</p><p><a href="https://pwnable.tw/challenge/#2">https://pwnable.tw/challenge/#2</a></p><p>Why challenge 2, instead of challenge 1? Because, the first one looked harder. :/</p><p>The idea of this site is that they provide you a link to download a binary, they you create a local exploit for the binary. After that you create a remote a exploit that you can test on their servers through netcat. If you remotely exploit the binary correctly, you will get a flag.</p><p>Download binary: <a href="https://pwnable.tw/static/chall/orw">https://pwnable.tw/static/chall/orw</a></p><p>I will be doing this on Kali Linux, although it won’t be difficult to follow along on a different Linux OS. Let’s see what we are dealing with. We can get some basic information about the file using the file command. This command uses magic numbers to guess what type of file you’re dealing with. If it’s a standard file format then file is normally pretty accurate. If it’s proprietary then file probably won’t be that accurate.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dhsc5RWoCYQWiFF-rmQ6Pw.png" /></figure><p>The output from file shows that we’re dealing with an ELF (Executable Linkable Format). This, to my knowledge, is the dominant format that is used on UNIX like systems, so it’s not directly compatible with Windows. The ELF format seems quite complex so I will leave that to another day.</p><p>The next thing that I did was execute the binary. The Binary printed the string<em>“Give me your shellcode:”. </em>Once I entered in anything, we received a segmentation fault. Segmentation faults suck balls for programmers but to hacker they’re often used to exploit a binary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/503/1*OUasp_zTkDYd3qKRBIHHPw.png" /></figure><p>Next I will load the binary into IDA so that we can see the assembly. To see what is going on behind the scene.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/1*zQTv9GsJwFso1IHiaAHfbg.png" /></figure><p>I downloaded IDA from here: <a href="https://out7.hex-rays.com/files/idafree70_linux.run">https://out7.hex-rays.com/files/idafree70_linux.run</a></p><p>Then did chmod +x idafree70_linux.run; ./idafree70_linux.run . This started the installer. I then chose to install IDA in opt. To run IDA did the following. cd /opt/idafree-7.0; ./ida64 . Then loaded the binary in through the Wizzard.</p><p>Initially I thought that to exploit this binary I would need to do a buffer overflow but after looking at the assembly we can see that it’s actually easier than that.</p><p>First thing that happens is something called the function prologue, this is a few lines of assembly at the start of every function. The goal of these lines of assembly is to set up the registers and the stack frame for the rest of the function to work correctly. The first interesting bit is the call to orw_seccomp. After googling “seccomp” I found this.</p><blockquote>“<strong>seccomp</strong> (short for <strong>secure computing mode</strong>) is a computer security facility in the <a href="https://en.wikipedia.org/wiki/Linux_kernel">Linux kernel</a>. seccomp was first devised by Andrea Arcangeli in January 2005 for use in public <a href="https://en.wikipedia.org/wiki/Grid_computing">grid computing</a> and was originally intended as a means of safely running untrusted compute-bound programs.”</blockquote><p>Sounds interesting, a bit more research showed that the idea behind this is to limit what syscalls the program can do while the process is in this <em>secure mode</em>. Seccomp allows a process to make a one-way transition into a “secure” state where it cannot make any system calls except exit(), sigreturn(), read(), write(). Should it attempt any other system calls, the kernel will terminate the process with SIGKILLor SIGSYS .</p><p>Syscalls or system calls, are a way of accessing something from the kernel. For example if we want to read a file, we would use the read syscall. Applications such as chrome, notepad etc. run in <em>user-mode. </em>When in user mode the program may require access to something that requires a higher level privilege. To do this a system call is required. The program basically asks the kernel to do the work for it. Then the kernel will either do it, or deny the program access to it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/0*GY7xhgLWCW5C8u9A.png" /></figure><p>Seeing as the goal of the challenge is to read the flag, I think that this is added to binary to prevent people from pwning their servers too hard. Although I would imagine that the servers are vitalized anyway.</p><p>After the call to orw_seccomp we then get two more syscalls. One to printfand another to read . We can see that the string “Give me your shellcode” is loaded as the argument to the printf call. The we can see that the binary is accepting some user input.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/676/1*6CR-QLz4LPn82n7zPrDoaQ.png" /></figure><p>I’m not quite sure how much input is accepted. But I think it’s 0C8h , were the h means hex. Convert that to decimal giving us 200 bytes. This seems about right. This bit confused me because I remember getting a segmentation fault when I first ran the program. But in fact it was not because I gave the program more than 200 bytes. It was because of a few lines down we see the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*DmLy9pwZ3lMRoGkdIuDoNw.png" /></figure><p>Here we’re calling what ever is in eax. This is what caused the segmentation fault. Before stating this I thought that a segmentation fault was just a fancy way of saying that a buffer had been overflow. But It’s not. Wikipedia gives the following description.</p><blockquote>In <a href="https://en.wikipedia.org/wiki/Computing">computing</a>, a <strong>segmentation fault</strong> (often shortened to <strong>segfault</strong>) or <strong>access violation</strong> is a <a href="https://en.wikipedia.org/wiki/Fault_(computing)">fault</a>, or failure condition, raised by hardware with <a href="https://en.wikipedia.org/wiki/Memory_protection">memory protection</a>, notifying an <a href="https://en.wikipedia.org/wiki/Operating_system">operating system</a> (OS) the software has attempted to access a restricted area of memory (a memory access violation).</blockquote><p>So before when I ran the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*OUasp_zTkDYd3qKRBIHHPw.png" /></figure><p>I was not overflowing a buffer, I was actually trying to access a piece of memory that was invalid. Then the memory management unit (MMU), raises the illegal access error.</p><blockquote>At the hardware level, the fault is initially raised by the <a href="https://en.wikipedia.org/wiki/Memory_management_unit">memory management unit</a> (MMU) on illegal access (if the referenced memory exists), as part of its memory protection feature, or an <a href="https://en.wikipedia.org/wiki/Invalid_page_fault">invalid page fault</a></blockquote><p>Now it seems pretty obvious what I have to do. The goal is write some shellcode that only uses the sys_open sys_read sys_write. Then load this shellcode when the read syscall is called. This is because of the the “seccomp”. Another hint to this is in the challenge description.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/348/1*VALgKKDV7AYQcC0mLsfzpw.png" /></figure><p>The binary orw refers to open-&gt;read-&gt; write. Basically replicating the Python code below but in assembly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/377/1*HdToz2YlVxrzVAigrRFgPA.png" /></figure><p>Now is the part that get’s scary. I know what I have to do but actually starting it is daunting. I found this cool Python module called pwn.</p><blockquote><em>pwntools</em> is a CTF framework and exploit development library. Written in Python, it is designed for rapid prototyping and development, and intended to make exploit writing as simple as possible.</blockquote><pre>apt-get update<br>apt-get install python2.7 python-pip python-dev git libssl-dev libffi-dev build-essential<br>pip install --upgrade pip<br>pip install --upgrade pwntools</pre><p>This seems pretty perfect for what we need.</p><p>The first thing that we need to do is open the file. The man page shows the following syntax. Here we give the the file location and then the additional flag.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/355/1*C6m-qQAJefRhQKYcGULvyg.png" /></figure><p>By looking at the Linux syscall reference we can see the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QfN91z91s36tyqmszES_2Q.png" /></figure><p>The shellcode has to place the pointer to the location of the file in the ebxregister, then the flags and mode in the ecx and the edx register. The eaxregister is the the syscall number.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/804/1*XqD1OxgA8G2CEj3_XOzdGA.png" /><figcaption>fp = open(“/home/orw/flag”);</figcaption></figure><p>Here I create a variable called <em>shellcode</em>, this is where I will be putting my shellcode. I started by clearing the eax register with xor eax eax. XOR means exclusive or, this is used a lot in the crypto world. Anything XOR’ed with it’s self is always zero.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/127/1*QDExPnUIIYF1lMCzymYFQg.png" /><figcaption>XOR</figcaption></figure><p>Next we push eax on the stack and add 5 to it’s value. Why don’t we just use the move instruction, such as mov eax, 5? The answer is because we want to avoid null bytes. Null bytes are used to signify the end of data. In this case if our shell code had a Null byte, then everything up to that null bytes would get executed. But everything after that would not get executed. I found this handy stack overflow response that explained it pretty clearly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/757/1*YZ01kQLG-lRLbmZ-9o09KQ.png" /></figure><p>Ok, now comes the tricky part. For the second part of our open syscall we have to place the string /home/orw/flag into the stack. Then load a pointer into the ebx register. First I converted the string into hex with the following Python. Then split up the string into 4 bytes each, with the list comprehension on the last line.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gKbjLyi3FSuwop5gQm-aSw.png" /></figure><p>This gave us the following output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/525/1*UAJ1ghoWiLLuwVSn2mco-w.png" /></figure><p>There are a few things to note with this. First thing is that the final 4 bytes is not long enough. This is bad because we either have to fill those final two bytes with the evil null bytes or something else. This is an easy problem to solve. UNIX is quite forgiving with how you specify file paths, meaning the following is valid. ///home/orw/flag.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/507/1*wz2k9-ni_XJFVdovPlD2JQ.png" /></figure><p>Great, we now have no null bytes. We can now add some more assembly to add the string to the stack and put the pointer into ebx.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/804/1*XqD1OxgA8G2CEj3_XOzdGA.png" /><figcaption>fp = open(“/home/orw/flag”);</figcaption></figure><p>Readers with a keen eye will notice that the string is loaded into memory backwards. This is because something called endianness.</p><blockquote><strong>Endianness</strong> is the sequential order in which <a href="https://en.wikipedia.org/wiki/Byte">bytes</a> are arranged into larger numerical <a href="https://en.wikipedia.org/wiki/Word_(data_type)">values</a> when stored in <a href="https://en.wikipedia.org/wiki/Computer_data_storage">memory</a> or when transmitted over digital links. Endianness is of interest in <a href="https://en.wikipedia.org/wiki/Computer_science">computer science</a> because two conflicting and incompatible formats are in common use: <a href="https://en.wikipedia.org/wiki/Word_(computer_architecture)">words</a> may be represented in big-endian or little-endian format, depending on whether bits or bytes or other components are ordered from the big end (<a href="https://en.wikipedia.org/wiki/Most_significant_bit">most significant bit</a>) or the little end (<a href="https://en.wikipedia.org/wiki/Least_significant_bit">least significant bit</a></blockquote><p>We can check what endiannes we need with the readelf command to show the headers of the binary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/552/1*WRtjifNTXl44rgtEXADB5g.png" /></figure><p>Then finaly the stack pointer is pointing to the string, so we just have to load the esp into the ebx register. Next we just set the flag to 0 with xor ecx ecx . Then to actually make the syscall we do int 0x80. Now we need to make the read syscall</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/855/1*9xpwgSPtbIb6Z9aMw9BHFw.png" /><figcaption>Read syscall</figcaption></figure><p>I will speed things up now that we have the general idea how how things work. Here I cleared the eax register then added 3 to it, so we don’t have null bytes. Then after that I placed the pointer to were the pointer was. After that I set the file descriptor to 3 in edx. Then I added 64 to the edx register signifying that I want to read 64 bytes from memory. Then I initiated the syscall with int 0x80.</p><p>Now lets do the write syscall.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/799/1*cBVFnOwjosWCYRFJyWZASg.png" /><figcaption>write</figcaption></figure><p>The write syscall was the easiest. This is because the ecx and edx register are still set from the previous syscall. Therefore all we have to do is add 4 to the eax register and then add 1 to the ebx register, before initiating the syscall.</p><p>At this point I tried getting the exploit to work locally but could not. But getting it work work remotely was easy using the pwntool libary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/988/1*KExFXJAve34y-5iOJ2c63g.png" /><figcaption>Final exploit</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/868/1*GpO94KA5ww2NCPtJL1ThKg.png" /></figure><p>I think that reading 64 bytes was a bit overkill but there we go, we have the flag.</p><p>I have a lot of <em>wasted</em> time on a train to work (5 hours a week minimum) that I want to spend doing something useful. So I will also be reading books to aid my journey. As another part of studying in case anyone cares.</p><p><strong>Book number one</strong>: Modern Operating Systems</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/495/0*rOwSNOaf__m3A4D8.jpg" /></figure><p>I did not choose this book, someone on Reddit recommended it to me on a thread that I made. I did however choose the topic. One of the main functions of a operating system is to provide the programmer abstractions so they don’t have to deal with the messy low level stuff. This book goes over how those abstractions work. From how MMU (memory management unit) works, to boring stuff like file permissions. So far the book has been quite good.</p><p><a href="https://www.amazon.co.uk/Modern-Operating-Systems-Andrew-Tanenbaum/dp/1292061421/ref=sr_1_1?crid=9DJDAYVPGBIU&amp;keywords=modern+operating+systems&amp;qid=1557085028&amp;s=gateway&amp;sprefix=modern+oper%2Cclothing%2C132&amp;sr=8-1">https://www.amazon.co.uk/Modern-Operating-Systems-Andrew-Tanenbaum/dp/1292061421/ref=sr_1_1?crid=9DJDAYVPGBIU&amp;keywords=modern+operating+systems&amp;qid=1557085028&amp;s=gateway&amp;sprefix=modern+oper%2Cclothing%2C132&amp;sr=8-1</a></p><p>See you in the next post.</p><p>Day 1: <a href="https://medium.com/@zlkidda/day-1-quest-for-my-first-zero-day-dealing-with-stripped-binaries-8501460e2781">Quest for my first Zero day — Dealing with stripped binaries</a></p><h3>Day 1: Quest for my first Zero day — Dealing with stripped binaries</h3><p>Day 0: <a href="https://medium.com/@zlkidda/day-0-quest-for-my-first-zero-day-writing-my-shell-code-847f493b71d0">Writing my first shell code — Quest for my first Zero Day</a></p><p>Last post i did the second challenge in the the <a href="https://pwnable.tw/">pwnable.tw</a> binary exploitation CTF’s. Today I will will be doing the first challenge. I initially went to try the first challenge but it look much more challenging than the second.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/1*_YGWoOWHwlAOVmr3NYmaWw.png" /><figcaption>The challenge is called “<em>start”</em>.</figcaption></figure><p>First let’s download the binary and do some preliminary basic analysis to get a fell with what we’re dealing with. First I downloaded the binary with the wget command, next the file command estimates what the file is and then the readelf -h command is used to look at the binary headers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*T9pxu15GdM9ANoCGo6Xhdg.png" /></figure><p>Great, it looks like we’re dealing with another small ELF binary. Let’s see what happens when we execute the binary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/645/1*f9yaskla_XRa3LTx71kmEw.png" /></figure><p>Without disassembling the binary, we can see that there is a good chance that the binary is vulnerable to a buffer-overflow. We can guess this because we supplied the binary with a large amount of input and then received the segmentation fault. Let’s take a closer look at this in gdb to see what is happening under the hood. Here I used the-q flag to hide the welcome screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/475/1*2WPyoDe9d3mW-Jijs_AIvQ.png" /></figure><h4>Stripped Binaries and debugging symbols</h4><p>Strange, gdb is not meant to do that. This is first binary that I’ve seen where gdb cannot handle the binary by default. No debugging symbols? What are debugging symbols? After a bit of stack overflowing I came to the conclusion that when the binary was compiled (probably with gcc) the -s flag was used. The -s flag is used to remove all symbol table and relocation information from the executable. I would imagine that this makes the binary smaller, a good thing for companies to do to save space. But I would also imagine that this makes debugging and reverse engineering the binary much harder. But then I looked at the output from the file command a little closer .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/711/1*H2Wsbp0-gv1zh1LFSYmBKg.png" /></figure><p>It clearly says that the binary is not stripped, this might be a false positive it might not be. A bit more time on stack overflow showed me how to test if a binary was in fact stripped or not. We can use objdump --sym start . Here is a truncated version of the help page for the--sym flag, as it’s quite long.</p><pre>Print the symbol table entries of the file. This is similar to the information provided by the nm program…</pre><pre>The other common output format, usually seen with ELF based files, looks like this:</pre><pre>00000000 l d .bss 00000000 .bss<br> 00000000 g .text 00000000 fred</pre><pre>Here the first number is the symbol’s value (sometimes refered to as its address). The next field is actually a set of<br> characters and spaces indicating the flag bits that are set on the symbol. These characters are described below. Next is<br> the section with which the symbol is associated or *ABS* if the section is absolute (ie not connected with any section),<br> or *UND* if the section is referenced in the file being dumped, but not defined there.</pre><p>Cool, so let’s have a and see if there are any symbols in the binary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/492/1*H0uJN6PsLCkYIzFllxM8kg.png" /></figure><p>We can see that the symbol table is pretty generic, containing just about everything that you would expect from a binary. At this point I’m pretty curious if the binary is stripped or not. So I decided to do a quick test. Compile a 32 bit stripped binary and compile a 32 bit non stripped binary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*jxfYLfLAGT64VRd_iAA42g.png" /></figure><p>Next I did the same objdump command on both binaries to look at the differences. We can see that the stripped binary has zero entries in it’s symbol table, the non-stripped binary has around a hundred. Then where does the CTF binary fit in? My guess is that it is a binary that has had just enough stripped to make make the challenge harder. This is the easiest challenge and they’re already making it harder.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/1*TW1NlhyijFCSM9MCqtTZkQ.png" /></figure><h4>How to deal with stripped binary in gdb?</h4><p><em>(semi stripped)</em></p><p>Although I’m going to shove this binary in IDA, I want to learn how to deal with stuff like this in future before having more complex software handle it for me. I found a post by <a href="http://twitter.com/tr0id">@tr0id</a> showing you how to deal with stripped binaries in gdb.</p><p><a href="https://medium.com/@tr0id/working-with-stripped-binaries-in-gdb-cacacd7d5a33">Working With Stripped Binaries in GDB</a></p><p>Here we can load the binary into gdb then execute info file. This displays information about debugged binary and loaded sections. Showing us precisely where the entry point to the binary is. Next we can issue the following command x/50i 0x80048060. This command bassically translates to <em>examine the next 50 instructions from the address 0x0804060.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*FU0DRJmz95LvmTncthkCyQ.png" /></figure><p>I’ve forgot that the goal of the challenge is to exploit the binary, I’ve all ready learnt quite a lot and we’ve only just seen the assembly for the first time. I’m going to load the binary into IDA anyway now that I’ve proved that I can get the binary working in gdb… Mainly so I can comment the code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/644/1*2ZMu31VH4keAB4Tzg6sgMw.png" /></figure><p>The binary does not seem that complex</p><ul><li>we first clear the registers.</li><li>Push the string “Let’s start the CTF:” onto the stack.</li><li>Then push the stack pointer on the stack?</li><li>Write 20 bytes using sys_write.</li><li>Read 60 bytes using sys_read.</li><li>Add 20 Bytes to the stack pointer.</li></ul><p>Cool so now we know that we’re trying to place 60 bytes in a 20 byte buffer. The problem is that the smallest shellcode I can find is 23 bytes long. Every byte counts so it’s not possible to use this shellcode.</p><p><a href="http://shell-storm.org/shellcode/files/shellcode-827.php">Linux/x86 - execve /bin/sh shellcode - 23 bytes</a></p><p>So there is obviously more to the challenge than the bog standard buffer overflow. Tomorrow I will be exploiting the binary.</p><h3>Day 2: Quest for my first Zero day — Memory leak</h3><p>Last post I started the static analysis for the “<em>start</em>” <a href="https://pwnable.tw/">pwnable.tw</a> challenge. In this post we will be mocking up the exploit and then performing the exploit. The exploit includes a memory leak, followed by a buffer overflow.</p><p>Day 0: <a href="https://medium.com/@zlkidda/day-0-quest-for-my-first-zero-day-writing-my-shell-code-847f493b71d0">Writing my first shell code</a></p><p>Day 1: <a href="https://medium.com/@zlkidda/day-1-quest-for-my-first-zero-day-dealing-with-stripped-binaries-8501460e2781">Dealing with stripped binaries</a></p><p>This challenge hurt my head, it really did. But I got there in the end. Let’s quickly go over the assembly code again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/508/1*FtHJ7B33VrrdVHImLAjb1g.png" /></figure><p>First we push <em>esp </em>onto the stack, then we push exit onto the stack. After that we clear our general purpose registers. Then we push the string “<em>Let’s start the CTF:</em>” onto the stack. After that we make the following syscall. sys_write(stdout, top_of_stack, 20 bytes); followed by another syscall. sys_read(stdin, top_of_stack, 60 bytes);. Therefore the program does not do much. The strange behavior here is that the sys_read call reuses the space allocated on the stack from the “<em>Let’s start the CTF:</em>” string. The size of that string is 20 bytes long. This means that writing anything longer than 20 bytes will overwrite the return address _exit. The smallest usable shell code I can find is 23 bytes.</p><p>We need to decided what to overwrite the return address with. If this was a local exploit we could just use gdb on the stack. But this is a remote exploit. This gives us two options.</p><h4>Spray attack</h4><p>We could try and guess the location of the stack, although this machine has ASLR enabled. This means it would not be very feasibly to try this approach.</p><h4>Memory Leak</h4><p>We could try and get the sys_write call to write the location of the stack.</p><p>The memory leak option is defiantly the better option here. To do the memory leak, we have to overwrite the return address with the location of the mov ecx, esp instruction. This is cause the control flow of the program to change to re invoke the sys_write call again followed by the sys_read call. But this time the stack will be arranged differently. So instead of writing out <em>“Let’s start the CTF:</em>”, the program will binary will actually write out the address of the stack. This will allow us to reattempt are buffer overflow from the sys_read call. But instead of overwriting the return address with the offset of the instruction mov ecx, esp we can point to the top of the stack.</p><p>Okay let’s start the POC code.</p><p>First we connect to the server hosting the challenge, then we are going to send the payload below causing the binary to got to the mov ecx, esp instruction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e9H7fSCD-r2erIQtSOf4kA.jpeg" /></figure><p>This will then invoke the second sys_write call containing the address of the stack. Here we catch this address with the new_return_address = SERVER.recv(4) .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/434/1*wZXqjGWobKKuG8bPupdBtg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/457/1*s2_TAFyl3mSHvPfPWCJn7g.png" /></figure><p>Great, now we just have to write the second stage. We have 60 Bytes to play with for the payload. But 20 of the bytes are going to A’s and 4 bytes are going to be the return address. This leaves us with 60-20-4=36 bytes of the payload for the shell code. This still not a lot, but it is enough to invoke the execve(&quot;/bin/sh&quot;) to get a shell.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hnGC_9XSBB896MIXcg87gg.jpeg" /></figure><p>Let’s start writing the second stage of the exploit. Notice that we added 20 bytes to return address. This is because we don’t want to point to the location of the A’s.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/653/1*8HPMCGVDzqGkJoGQhY0hKw.png" /></figure><p>I slightly modified the shell code taken from <a href="http://shell-storm.org/shellcode/files/shellcode-827.php">here</a> to correctly set up the execve call as edx should point to NULL.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/497/1*L9lw1BNT1tOXmbBek6F5CA.png" /><figcaption>flag retracted</figcaption></figure><h3>Day 3: Quest for my first Zero day — Fuzzing</h3><p>Day 0: <a href="https://medium.com/@zlkidda/day-0-quest-for-my-first-zero-day-writing-my-shell-code-847f493b71d0">Writing my first shell code</a></p><p>Day 1: <a href="https://medium.com/@zlkidda/day-1-quest-for-my-first-zero-day-dealing-with-stripped-binaries-8501460e2781">Dealing with stripped binaries</a></p><p>Day 2: <a href="https://medium.com/@zlkidda/day-2-quest-for-my-first-zero-day-memory-leak-293416198d59">Memory leak</a></p><p>Today I will be starting a new challenge, this challenge is still hosted on <a href="https://pwnable.tw/challenge/#3">pwnable.tw</a> but is going to be the harder challenge called <strong>calc</strong>. Let’s go ahead and download the binary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0xeshxRDGSuUtwbgB7k5vA.png" /></figure><p>Look’s like we have another 32 bit ELF to poke at. I decided to have a go at using Ghidra for this challenge. It was surprising simple to get Ghidra working, guess that’s what the NSA wants though. When Ghidra opened the binary, I was overwhelmed with how many functions where in the symbol tree. So I decided to run the program first. Since the program was called calc it’s pretty obvious that it’s going to be a command line calculator. I decided to enter in some expressions, valid and invalid ones. I noticed that some of the answers are just wrong, I also got some exceptions, expression error and a merry Christmas.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/567/1*Mlg37eWJUKySul9E9QB7Ew.png" /></figure><p>I decided that this would be the perfect program to have a go at fuzzing with. I remembered a live overflow video about a tool <em>radamsa. </em>So I downloaded the tool.</p><p>$ git clone https://gitlab.com/akihe/radamsa.git &amp;&amp; cd radamsa &amp;&amp; make &amp;&amp; sudo make install</p><p>Radamsa takes some input then mutates it to try and stress the limits of the binary’s logic in the hopes of breaking it, so that the bug can be quickly fixed. It was pretty quick that I got a segfault with radamsa. I’ve not even looked at the asm yet.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*nb2MWYEJqEm_s1ERnWCN1A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/337/1*ljEBtsvTFRNmeW3thS4IoQ.png" /></figure><p>I mutated the input myself but still have no idea what’s going on. Although I have Ghidra open I decided to load the binary into GDB first to see where the segmentation fault occurs.</p><pre>echo &quot;-98020-1&quot;&gt; segfault<br>gdb -q calc<br>run &lt; segfault</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*4jYAKTB_adBix84h3dAM8Q.png" /></figure><p>The segmentation fault occurred at the address 0x08049130 in a function called “<em>parse_expr”. </em>Let’s finally have a look the assembly in Ghidra then, by jumping into the entry point of the binary.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=74a033aad6df" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Day 2: Quest for my first Zero day — Memory leak]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@zlkidda/day-2-quest-for-my-first-zero-day-memory-leak-293416198d59?source=rss-6a0f609662fe------2"><img src="https://cdn-images-1.medium.com/max/600/1*FtHJ7B33VrrdVHImLAjb1g.png" width="600"></a></p><p class="medium-feed-snippet">Last post I started the static analysis for the &#x201C;start&#x201D; pwnable.tw challenge. In this post we will be mocking up the exploit and then&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/@zlkidda/day-2-quest-for-my-first-zero-day-memory-leak-293416198d59?source=rss-6a0f609662fe------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@zlkidda/day-2-quest-for-my-first-zero-day-memory-leak-293416198d59?source=rss-6a0f609662fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/293416198d59</guid>
            <category><![CDATA[binary-exploitation]]></category>
            <category><![CDATA[buffer-overflow]]></category>
            <category><![CDATA[exploitation]]></category>
            <category><![CDATA[memory-leak]]></category>
            <category><![CDATA[hacking]]></category>
            <dc:creator><![CDATA[Tettra Coins]]></dc:creator>
            <pubDate>Sun, 12 May 2019 17:23:09 GMT</pubDate>
            <atom:updated>2019-05-12T17:23:09.137Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Day 1: Quest for my first Zero day — Dealing with stripped binaries]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@zlkidda/day-1-quest-for-my-first-zero-day-dealing-with-stripped-binaries-8501460e2781?source=rss-6a0f609662fe------2"><img src="https://cdn-images-1.medium.com/max/601/1*_YGWoOWHwlAOVmr3NYmaWw.png" width="601"></a></p><p class="medium-feed-snippet">Day 0: Day 0: Writing my first shell code&#x200A;&#x2014;&#x200A;Quest for my first Zero Day</p><p class="medium-feed-link"><a href="https://medium.com/@zlkidda/day-1-quest-for-my-first-zero-day-dealing-with-stripped-binaries-8501460e2781?source=rss-6a0f609662fe------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@zlkidda/day-1-quest-for-my-first-zero-day-dealing-with-stripped-binaries-8501460e2781?source=rss-6a0f609662fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/8501460e2781</guid>
            <category><![CDATA[memory-leak]]></category>
            <category><![CDATA[buffer-overflow]]></category>
            <category><![CDATA[pwnable]]></category>
            <category><![CDATA[hacking]]></category>
            <category><![CDATA[stripped-binaries]]></category>
            <dc:creator><![CDATA[Tettra Coins]]></dc:creator>
            <pubDate>Thu, 09 May 2019 11:43:47 GMT</pubDate>
            <atom:updated>2019-05-14T19:37:14.342Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Day 0: Quest for my first Zero Day — Writing my shell code.]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@zlkidda/day-0-quest-for-my-first-zero-day-writing-my-shell-code-847f493b71d0?source=rss-6a0f609662fe------2"><img src="https://cdn-images-1.medium.com/max/988/1*KExFXJAve34y-5iOJ2c63g.png" width="988"></a></p><p class="medium-feed-snippet">I&#x2019;m on my mission to getting my first zero day exploit. I&#x2019;m really interested in binary exploitation and reverse engineering but have&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/@zlkidda/day-0-quest-for-my-first-zero-day-writing-my-shell-code-847f493b71d0?source=rss-6a0f609662fe------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@zlkidda/day-0-quest-for-my-first-zero-day-writing-my-shell-code-847f493b71d0?source=rss-6a0f609662fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/847f493b71d0</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[reverse-engineering]]></category>
            <category><![CDATA[binary-exploitation]]></category>
            <category><![CDATA[hacking]]></category>
            <category><![CDATA[shellcode]]></category>
            <dc:creator><![CDATA[Tettra Coins]]></dc:creator>
            <pubDate>Mon, 06 May 2019 10:22:49 GMT</pubDate>
            <atom:updated>2019-05-09T11:46:10.777Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[10 Tips To Write Cleaner Code.]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@zlkidda/10-tips-to-write-cleaner-code-b256aed95d92?source=rss-6a0f609662fe------2"><img src="https://cdn-images-1.medium.com/max/700/0*YB2g0Lrbega5evV3.jpg" width="700"></a></p><p class="medium-feed-snippet">Have you ever looked back at some of the old code that you wrote when you first started programming? I have, it left a look of confusion&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/@zlkidda/10-tips-to-write-cleaner-code-b256aed95d92?source=rss-6a0f609662fe------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@zlkidda/10-tips-to-write-cleaner-code-b256aed95d92?source=rss-6a0f609662fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/b256aed95d92</guid>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Tettra Coins]]></dc:creator>
            <pubDate>Sat, 13 Apr 2019 08:01:15 GMT</pubDate>
            <atom:updated>2019-04-13T08:01:31.522Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Hack The Box: Popcorn]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@zlkidda/hack-the-box-popcorn-11f5c7f77f5e?source=rss-6a0f609662fe------2"><img src="https://cdn-images-1.medium.com/max/630/0*sCXYCHWKlpokLuMx.jpg" width="630"></a></p><p class="medium-feed-snippet">Today we will be tackling the Popcorn CTF hosted on Hack The Box. This machine is Linux based CTF.</p><p class="medium-feed-link"><a href="https://medium.com/@zlkidda/hack-the-box-popcorn-11f5c7f77f5e?source=rss-6a0f609662fe------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@zlkidda/hack-the-box-popcorn-11f5c7f77f5e?source=rss-6a0f609662fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/11f5c7f77f5e</guid>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[hackthebox]]></category>
            <category><![CDATA[hacking]]></category>
            <category><![CDATA[exploit]]></category>
            <category><![CDATA[kali]]></category>
            <dc:creator><![CDATA[Tettra Coins]]></dc:creator>
            <pubDate>Sun, 07 Apr 2019 21:53:33 GMT</pubDate>
            <atom:updated>2019-04-07T21:53:33.353Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Multilayer Blockchain Scalability]]></title>
            <link>https://medium.com/@zlkidda/multilayer-blockchain-scalability-1747c677629f?source=rss-6a0f609662fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/1747c677629f</guid>
            <category><![CDATA[bitcoin]]></category>
            <category><![CDATA[blockchain-technology]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[cryptocurrency]]></category>
            <category><![CDATA[sharding]]></category>
            <dc:creator><![CDATA[Tettra Coins]]></dc:creator>
            <pubDate>Sun, 07 Apr 2019 13:56:13 GMT</pubDate>
            <atom:updated>2019-04-07T13:56:13.454Z</atom:updated>
            <content:encoded><![CDATA[<p>We all know that Bitcoins is not the most scalable cryptocurrency, with around 6.5 transactions per second. Well, that’s a bit of a lie since Bitcoin has the lightning network and has accepted SegWit, but for simplicity sake Bitcoin is still not that scalable. I love how really smart people in the community are actively developing Bitcoin and the project is growing. Today I will be discussing some ideas that I had in 2017. These ideas were the basis of my University dissertation.</p><p>I will briefly describe some of the current solutions to the Bitcoin scalability problem.</p><h3>Lightning network.</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*q0dhAbUmYcWF7UsH.png" /></figure><p>By far the coolest solution currently available. The lightning network can be thought of as another layer to Bitcoin. The Lightning Network allows near-instant transactions between two parties that do not congest the main Bitcoin layer. These transactions between two parties happen on a payment channel established on the Lightning Network. Once a payment channel has been established the two parties can send thousands of transactions that do not need to appear on the main blockchain. This obviously reduces congestion on the blockchain. Then when either party once to close the contract they just broadcast the contract to the main net and miners validate it as a single transaction.</p><p>I love the Lightning Network and I might even set up my own node at some point. There are some downsides to Lightning networks. For example, they are only really suitable for a small transaction.</p><h3>SegWit</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/740/0*_tEGQjCw8WrKh96g.png" /></figure><p>Another solution is something called a segregated witness (SegWit for short). The idea of this solution is to split up signatures into a separate structure. Since signatures take up around 65% of the block size this allows for more room for transactions.</p><h3>Bitcoin Cash</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eqpR-2VMsYmz16L2.png" /></figure><p>Bitcoin Cash has opted for a simpler solution. Just increase the block size. Bitcoins blocksize is 1MB while Bitcoin Cash’s block size is 8MB. This allows eight times more transactions to fit in the block increasing its throughput compared to Bitcoin. This solution is not compatible with the original Bitcoin network. Therefore Bitcoin Cash and Bitcoin are two separate currencies when changes like this happen its called a hard fork. Critics of Bitcoin Cash say that this is only a bandaid solution that delays the scaling issue and splits up the community.</p><h3>Background</h3><p>For a quick background on me, I’m a recent graduate with a degree in Cyber Security. For my dissertation, I wrote a paper on Bitcoin’s scalability issues. I have since lost the original document and can only find a rough copy pictured below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*thRYpXa3MPVkbfLNiKvcGw.jpeg" /></figure><p>This paper has a collection of different ideas I had that could directly or indirectly help address the Bitcoins scaling problem. I would like to do a quick overview of them.</p><h3>Compression of the ledger.</h3><p>One concern with the blocksize size debate is that the ledger would quickly grow in size. For example, a block in BTC is 1MB compared to 8MB on BHC. Considering a block gets mined roughly every 10 minutes, the ledger will slowly grow over time. I’ve created a chart to show you the growth of the ledger.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/1*ZvbJTUNs_IrmNseka-B8mw.png" /></figure><p>I would like to quickly point out that this is assuming that all blocks are filled to their maximum capacity. Bitcoin Cash current has never reached its 8MB block size capacity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oVQxj_haTt7VDd0_X49gaA.png" /></figure><p>Although the math is simple the results are quite surprising. Assuming that Bitcoin will be around for 100 years or even 1000 years, we can quickly see that the ledger size is going to get ridiculously large. After 100 Years Bitcoins ledger would be around 5000Gb’s.</p><p>There are two types of nodes, <strong>full nodes</strong> and <strong>light nodes</strong>. Full node store the entire blockchain while Lightweight nodes use a method called Simрlifiеd рауmеnt vеrifiсаtiоn (SPV) to verify transactions. Lightweight node wallets skip several security steps which can leave the user vulnerable. An example of a situation where this was important was the <a href="https://en.bitcoin.it/wiki/July_2015_chain_forks#Wallet_Advice">4th July 2015 accidental chain fork</a> where updated full node wallets were unaffected while lightweight node wallets were in danger.</p><p>This a problem that will obviously grow over time of blockchain based projects. One way to address this is to <strong>compression </strong>of this data. I will make the assumption that as the ledger grows in size the ratio between full nodes and light nodes will lean towards full nodes. This is because it will become increasingly expensive to store the entire ledger. This is a bad thing as smaller the number of full nodes the greater <strong>centralization </strong>the network will have. Since Bitcoin is a <strong>decentralized </strong>currency, it is in the communities interest to address this problem before it’s too late.</p><p>My idea is to somehow implement compression into Bitcoins ledger, reducing the size of the ledger. One problem with this is entropy. Entropy in layman terms is the measure of randomness.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/966/1*a5yq2gGVC56Deww0WRNS2g.png" /></figure><p>Entropy is an import aspect of compression algorithms. This is because data with high entropy is much harder to compress. Around 65% of a block contains signatures. Signatures and hashes are going to very high entropy, this means that they will not compress well. Another issue is that the compression algorithm used will have to be lossless. Lossless compression simply means that no data is lost during the compression. This is paramount as the information being compressed is vital. This means that around 35% of the data can be compressed. I have no data to back this up but I will make an assumption that the over data can have a compression ratio of 2:1. With the numbers I described above we could see a 17.5% decrease in ledger size over time. For BCH it can save several terabytes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*USEq-98FDTK00OjJG_eCNg.png" /></figure><h4>How could we implement this?</h4><p>Currently, Bitcoins block formation looks like a straight line of blocks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/969/1*3wqFaCcbUC2GvUFvdIxo7w.png" /><figcaption>BTC Blockchain structure</figcaption></figure><p>The pattern is simple. When a new block is found add it to the blockchain and then repeat. What if we added <em>Checkpoint Blocks. </em>I defined a checkpoint block as a merger of <em>n</em> blocks. When <em>n </em>blocks are found they’re used to create a checkpoint block. Then blocks one to <em>n </em>can be deleted.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EWMjthnL0_JfgjBV-5hTAg.png" /><figcaption>Checkpoint blocks</figcaption></figure><h4>What are the advantages of this?</h4><ul><li>Well, first thing is you can compress the checkpoint block.</li><li>This extra step also allows the blocks to be <strong>pruned </strong>of redundant data. <em>(You can prune or remove historical information from the database and reduce the database size)</em></li><li>No hard-fork required.</li><li>Reduces costs of running a full node.</li><li>More efficient implementation of sharding.</li></ul><h3>What is Sharding?</h3><blockquote>A <strong>database shard</strong> is a <a href="https://en.wikipedia.org/wiki/Partition_(database)">horizontal partition</a> of data in a <a href="https://en.wikipedia.org/wiki/DBMS">database</a> or <a href="https://en.wikipedia.org/wiki/Search_engine">search engine</a>. Each individual partition is referred to as a <strong>shard</strong> or <strong>database shard</strong>. Each shard is held on a separate <a href="https://en.wikipedia.org/wiki/Database_server">database server</a> instance, to spread the load.</blockquote><blockquote>Some data within a database remains present in all shards,<a href="https://en.wikipedia.org/wiki/Shard_(database_architecture)#cite_note-1">[notes 1]</a> but some appear only in a single shard. Each shard (or server) acts as the <em>single</em> source for this subset of data</blockquote><p>So basically different nodes can store a small number of checkpoint blocks instead of the Gb’s or even Tb’s of the blockchain. Then when a miner wants a certain Checkpoint Block, they can just broadcast the block that they want and another node that has the correct block will respond with valid data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/942/1*g0JS45m8RAFA_HGWhfZsZw.png" /></figure><p>This technique works great on centralized systems where you can trust over nodes or servers. But as you know Bitcoin works as a trustless system. How do we know that node 4 is not malicious and gave us an unaltered Checkpoint Block. Currently, we cannot know. What&#39;s the solution to this? Merkel roots are how Bitcoin makes sure that the transactions have not been tampered with. If the Merkel root for checkpoint block 4 is in checkpoint block 5’s previous Merkel root header, then we assume that this is a valid block. For an attacker to generate the false block, he would have to mine 2<em>n </em>blocks.</p><h3>Splitting up the blocks to increase throughput.</h3><p>Now imagine if we could have different teams or pools of nodes working on different problems. For example, there could be a team for each block in the checkpoint block. The teams could all be solving different problems allowing for an increase in throughput by <em>n</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DP0TFZpbdpF85a14nV4eMw.png" /></figure><p>This solution could co-exist with other scaling solutions such as the lightning network, SegWit or increased block size.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1747c677629f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How many different answers are there to the Countdown number game?]]></title>
            <link>https://medium.com/@zlkidda/how-many-different-answers-are-there-to-the-countdown-number-game-f8220d9290d2?source=rss-6a0f609662fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/f8220d9290d2</guid>
            <category><![CDATA[discrete-maths]]></category>
            <category><![CDATA[statistics]]></category>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[combinatorics]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Tettra Coins]]></dc:creator>
            <pubDate>Sun, 31 Mar 2019 22:31:55 GMT</pubDate>
            <atom:updated>2019-03-31T22:31:55.521Z</atom:updated>
            <content:encoded><![CDATA[<h3>Have you ever heard of Combinatorics?</h3><p>This article will involve some basic Combinatorics. I’d never heard of Combinatorics before last week, but I did an online course in statistics and thought it was interesting.</p><h3>What Combinatorics?</h3><blockquote><strong>Combinatorics</strong> is an area of <a href="https://en.wikipedia.org/wiki/Mathematics">mathematics</a> primarily concerned with counting, both as a means and an end in obtaining results, and certain properties of <a href="https://en.wikipedia.org/wiki/Finite_set">finite</a> <a href="https://en.wikipedia.org/wiki/Mathematical_structure">structures</a>. It is closely related to many other areas of mathematics and has many applications ranging from <a href="https://en.wikipedia.org/wiki/Logic">logic</a>to <a href="https://en.wikipedia.org/wiki/Statistical_physics">statistical physics</a>, from <a href="https://en.wikipedia.org/wiki/Evolutionary_biology">evolutionary biology</a> to <a href="https://en.wikipedia.org/wiki/Computer_science">computer science</a>, etc.</blockquote><p>Or in layman&#39;s terms its a fancy way of counting sets and their variations, combinations and permutations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iXSFl252sxAt7c4O9xJdfQ.png" /></figure><p>As a daily commuter to work on a busy train, it’s safe to say that I’m not a fan of British trains. I’m currently stuck on a train with no internet, but I do have a seat and two hours to kill. <br>So I thought I would set my self a challenge. Some old lady next to me is playing a Countdown app on her old beaten up iPhone. I was never any good at mental arithmetic, but she did give me an idea of how to kill the next two hours.</p><p>Hello everyone, in this article I will be writing a formula that will count the number of variations of a symptomatically correct Counter down number game answers then implementing this program into C to get the result. But first lets go over the rules as quick as possible.</p><h3>The Rules</h3><ul><li>A contestant chooses a set of random numbers from a predefined collection of numbers.</li><li>A computer randomly generates a number.</li><li>The contestant then has to calculate the randomly generated number using the the random numbers that they selected.</li><li>They can use any of the following operators: x % + —</li></ul><h3>Let’s give a quick example.</h3><p>Contestant chooses the following list of numbers: 2, 8, 2, 24<br>The computer chooses the following: 48</p><p>A valid answer would be: 8 x 24 x 2 x 2 = 48</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/515/0*BX1j9NrtQkENa18H.jpg" /></figure><p>Let’s figure out how many possible combinations, that a contestant could choose from.</p><h3>Define the set</h3><p>Let’s define <em>S </em>as the numbers that the contestant chose and then let’s define <em>a </em>as the final answer. Let’s also define <em>OP </em>as the list of operators that can be used.</p><p>We can formally define <em>S </em>as a set of size <em>i </em>containing integers less than or equal to one thousand.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/303/0*zl5dbRy4eb9eVv4n" /></figure><h3>Permutations Vs Combinations.</h3><p>Before we carry on, I just want to clarify the difference between permutations and combinations, because before today I had no idea the difference between the two. But the difference is quite important.</p><h4>Combinations</h4><p>Combinations do not take ordered into account, for example if I go to McDonald and order a Big Mac and a Pepsi, it does not really matter if I say Big Mac and then Pepsi, or Pepsi then Big Mac.</p><h4>Permutations</h4><p>Permutations do take order into account, expanding on the previous example. If Bob and Jake go to McDonald, Jake and Bob both order a Big Mac and a Pepsi. In this scenario it does make a difference if Jake ordered first or Bob ordered first because there could be one Big Mac ready.</p><h3><strong>Permutations of the set</strong></h3><p>Next we need to find out how many permutations of these numbers we can make. Since each value in the set can only be used once, this limits the number of combinations that there are.</p><p>Lets give a basic example using a set containing three numbers 1,2,3. By only using each number once, we get the following number of permutations.</p><p>1,2,3 or 1,3,2 or 2,3,1 or 2,1,3 or 3,1,2 or 3,2,1</p><p>This gets more complicated, the more numbers in a set there are. But we can define the number of permutations with replacement as the factorial of the carnality of the set. In our simple example it would give the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/143/0*ia_lQoXoo51hHJJX" /></figure><p>Number of permutations between contestants choices of of numbers in set <em>S </em>can be define as followed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/24/0*f_MdMSavZGVA-Q2j" /></figure><h3>Permutations of the operators</h3><p>Next we need to find out how many different permutations of operators there are. This time operators can be used more than once, unlike the numbers above. The number of operators that can be used is always, the number of elements in the set minus one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/51/0*H4SlZC5t9xv2MFuT" /></figure><p>Here is a simple example. Image that the numbers below are a maths calculation and you have to replace “_” with an operator. There are five numbers there but there is only 4 spaces for operators. 1 _ 2 _ 3 _ 4 _ 5 .</p><p>Cool, now we know how many spaces there are for the operators but how many different combinations are there?</p><p>Let’s look at an easy example. Since in our scenario, we can reuse operators in the gaps between the numbers. We can think of this problem as how many different combinations as a bike lock has. Imagine a four pin lock where each pin can be either 0,2,3,4,5,6,7,8,9 . If you add this up you ten different unique values for the first pin location. But we have four pins locations so all we have to do is raise it to the power four. 10 x 10 x 10 x 10 = 10,000 . This is the same as our problem so we can describe the number of possible permutations of operators as <em>op </em>to the power <em>g </em>where <em>op </em>is the number of operators and <em>g </em>is the number of gaps between the digits (|S|-1). This gives us the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/33/0*tVgNTTTJOHt4ODnr" /></figure><h4>Permutation of <strong>Precedence.</strong></h4><p><em>This is where I had a bit of a brain fart and completely over complicated the problem but I still learnt allot. See if you can spot the problem. If you just want to skip my blunder go to the “Did You Spot The Problem?” section.</em></p><p>Next we need to define the precedence or in other terms, where the brackets need to go. Because brackets are important and need to be considered.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/162/0*v3Xo-h_6CDT_EJ9e" /></figure><p>The number of bracket placements is going to be equal to the following. Where <em>Nd</em> is equal to the number of digits chosen for the answer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/79/0*GVQb-sOGlXXmaBg1" /></figure><p>(a/b)/c/d, a/(b/c)/d, a/b/(c/d), (a/b/c)/d, a/(b/c/d), (a/b/c/d)</p><p><strong>Or so I thought at first</strong>, but I forgot that you can have multiple brackets. Making this much more challenging.</p><p>(a/b)/c/d, a/(b/c)/d, a/b/(c/d), (a/b/c)/d, a/(b/c/d), (a/b/c/d), (a/b)/(c/d) ((a/b)/c)/d …</p><h3>Catalan Number.</h3><p>What is a Catalan Number? Well it’s not a number but infact but a way of calculating a sequence of numbers for a Combinatorics problem. Such as this one. They are extremely exponential as you can imagine.</p><p>The notation for Catalan Number is as follows. But I prefer the product notation. To be fair, I thought that I would have to use binomial coefficients for this problem but I did not want to. Where <em>n </em>is the number of digits. The bracket placements have to be valid <em>Dyck </em>language.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/176/0*8TUfwMWqdODOnzsz" /></figure><h3><strong>What is Dyck Language?</strong></h3><blockquote>In the theory of <a href="https://en.wikipedia.org/wiki/Formal_languages">formal languages</a> of <a href="https://en.wikipedia.org/wiki/Computer_science">computer science</a>, <a href="https://en.wikipedia.org/wiki/Mathematics">mathematics</a>, and <a href="https://en.wikipedia.org/wiki/Linguistics">linguistics</a>, a <strong>Dyck word</strong> is a balanced <a href="https://en.wikipedia.org/wiki/String_(computer_science)#Formal_theory">string</a> of square brackets [ and ]. The set of Dyck words forms <strong>Dyck language</strong>.</blockquote><p>Named after the mathematician <a href="https://en.wikipedia.org/wiki/Walther_von_Dyck">Walther von Dyck</a>. They have applications in the <a href="https://en.wikipedia.org/wiki/Parsing">parsing</a> of expressions that must have a correctly nested sequence of brackets, such as arithmetic or algebraic expressions. I found the proof for it on Wikipedia and it’s just what we need.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7MYhpgp6DWb4OW6h6_xSlA.png" /><figcaption><a href="https://en.wikipedia.org/wiki/Catalan_number">https://en.wikipedia.org/wiki/Catalan_number</a></figcaption></figure><h3>Combining everything together.</h3><p>Combining everything together we get the following formula where <em>d </em>is the number of digits, <em>op</em> is the number of operators.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/183/0*c4OKn6x70x0ZQG2w" /></figure><h3>Converting the formula into C code.</h3><p>We can convert this to C code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/642/1*mqMQ0lV1hmfosO0oG9k5Pw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*icqM_8z3PII7jCM2P3HhBg.png" /></figure><h3>Did You Spot The Problem?</h3><p>I really over complicated things here. Below you can see the wrong answer followed by the correct answer. I made the fatal error of thinking that you needed to include all permutations of bracket placements to get every unique answer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/183/0*c4OKn6x70x0ZQG2w" /></figure><p>In actual fact you do not, need to do so and can just remove the brackets entirely. This results in the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/90/0*noxvFOmIT1uoRHg1" /></figure><p>Remember reading the following statement? This what caused me to calculate bracket placements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/162/0*v3Xo-h_6CDT_EJ9e" /></figure><p>The problem is that doing it this way has loads of edge cases like the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/162/0*vXjMCPiLlert1tqC" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/188/0*Cn-A7p8BX7iC-CPx" /></figure><p>The problem is the formula will calculate them as different, when in fact they are the same. <em>If I was trying to calculate the number of unique answers where brackets placement matters not where precedent matter, I think that this would be the right solution. </em>Okay, brain fart over with. Here are the real results</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/724/1*piDrfw3j_AzIEBBi35j5Jw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*ATdPJziKjs15Rx-nRRtL8Q.png" /></figure><h3>References</h3><ul><li><a href="http://mathworld.wolfram.com/BinomialCoefficient.html">http://mathworld.wolfram.com/BinomialCoefficient.html</a></li><li><a href="https://en.wikipedia.org/wiki/Catalan_number">https://en.wikipedia.org/wiki/Catalan_number</a></li><li><a href="https://en.wikipedia.org/wiki/Dyck_language">https://en.wikipedia.org/wiki/Dyck_language</a></li><li><a href="http://mathworld.wolfram.com/CatalanNumber.html">http://mathworld.wolfram.com/CatalanNumber.html</a></li><li><a href="https://betterexplained.com/articles/easy-permutations-and-combinations/">https://betterexplained.com/articles/easy-permutations-and-combinations/</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f8220d9290d2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating My Own Trading bot — Week -2]]></title>
            <link>https://medium.com/@zlkidda/creating-my-own-trading-bot-week-2-ffc207d10867?source=rss-6a0f609662fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/ffc207d10867</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[iex]]></category>
            <category><![CDATA[trading]]></category>
            <category><![CDATA[pandas]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Tettra Coins]]></dc:creator>
            <pubDate>Sun, 17 Feb 2019 22:37:13 GMT</pubDate>
            <atom:updated>2019-02-17T22:37:13.011Z</atom:updated>
            <content:encoded><![CDATA[<h3>Creating My Own Trading bot — Week -2</h3><h3>Fetching the price data.</h3><p>In case you did not read last weeks article, I just layout of the foundation for the graphical user interface.</p><p><a href="https://medium.com/@zlkidda/creating-my-own-trading-bot-week-1-2f8caaf8d16d">https://medium.com/@zlkidda/creating-my-own-trading-bot-week-1-2f8caaf8d16d</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*CFinoe6idfKj_XVe-txnyg.png" /></figure><p>This week I did not have much time to devote to the project. But I got quite a critical step out the way, and that step is <strong>fetching the data needed for trading bot</strong>. Since the project is done in python, I needed to find some API that could be interacted with via Python or HTTP. I also needed the data to contain daily information at the minimum. The bot is going to be a trend following bot so I don’t really care about having minutes or hours in the data set. I also needed the API to contain most the stocks that I want to trade without manually creating a ticker list. The final specification for choosing the API was that it needed to either be free or cheap. Some charge you $60 a month. Thanks, but no thanks.</p><p>This lead me to quandl and IEX. I chose IEX because I could automate importing the ticker list. Using, sublime and excel, I could quickly convert the following html into a CSV that is python readable.</p><p><a href="https://iextrading.com/trading/eligible-symbols/">https://iextrading.com/trading/eligible-symbols/</a></p><p>If you want the CSV you can get it from the paste bin below. I really wish API would maintain a total list of tickers.</p><p><a href="https://pastebin.com/s6SGE70W">Plus-Ticker,Name,API CGC,Canopy Growth,IEX ACB,Aurora Cannabis,IEX NWL,Newell - Pastebin.com</a></p><p>Now that I have a ticker list that is compatible with IEX, I can start fetching the data. Once an update button is pressed it will start downloading the data.</p><p>The following code reads the CSV. Then using multi threading downloads a complete copy of the price data. This price data is stored as a pandas dataframe and then pickled to a file for later use. After each file is downloaded the progress bar on my UI will be updated.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/123c74337efa0be556a7d699158275a5/href">https://medium.com/media/123c74337efa0be556a7d699158275a5/href</a></iframe><p>Without multi-threading this process would take around 10 minutes. But was around a minute with multi-threading. This is still rather slow, but I don’t even get 1Mb/s download. First world problems. Below is the list of tickers downloaded.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/953/1*IJuwoQ-cCByblVUFgmD8ng.png" /><figcaption>List of tickers</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ffc207d10867" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating My Own Trading Bot — Week- 1]]></title>
            <link>https://medium.com/@zlkidda/creating-my-own-trading-bot-week-1-2f8caaf8d16d?source=rss-6a0f609662fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/2f8caaf8d16d</guid>
            <category><![CDATA[trading-system]]></category>
            <category><![CDATA[graphic-design]]></category>
            <category><![CDATA[qt]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[algorithmic-trading]]></category>
            <dc:creator><![CDATA[Tettra Coins]]></dc:creator>
            <pubDate>Sun, 10 Feb 2019 12:26:27 GMT</pubDate>
            <atom:updated>2019-02-10T12:29:33.640Z</atom:updated>
            <content:encoded><![CDATA[<h3>Creating My Own Trading Bot — Week- 1- Designing the UI</h3><p>This is going to be my biggest project so far. I’ve never been so excited to write some code. First of lets out line the goals of the project.</p><ul><li>Have a Graphical User Interface (GUI) that does not look like poop.</li><li>Display price information in a well thought out graph.</li><li>Be able to trade many different markets on many different exchanges.</li><li>Perform complex statistical analysis on price of many different financial products.</li><li>Cross platform</li><li>Created in such a way that the trading system can be altered easily.</li><li>Back test trading systems and perform analysis on their performance.</li><li>Manage my portfolio.</li><li>Display portfolio returns over time, risk, leverage etc.</li><li>Calculate risk and adjust my positions accordingly.</li><li>Price updates, note taking and prediction logging.</li><li>Be able to layer indicators that I like to use such as RSI, Moving averages etc, on a graph.</li></ul><p>I’ve never programmed a GUI before, so one of the biggest issue is going to be creating the GUI and being able to integrate the front end of the system with the back end of the system. I wanted the project to be written in python so I also wanted to design the GUI using python. After some initial research I decided to use tkinter for the user interface. I thought that I would try it, creating a few elements and what not.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/504/1*Utv5Y20SU7Lbls6dzl_LwQ.png" /><figcaption>Trail run of tkinter</figcaption></figure><p>I quickly realized that Tkinter, is not something I wanted to develop a complex piece of software in. It also looks like poop, and I didnt want to focus to much of my time polishing a turd. Next I looked at designing the project in C#. Again I decided against this as I wanted every to be done in python. After doing some more research I came across this reddit post.</p><iframe src="https://cdn.embedly.com/widgets/media.html?type=text%2Fhtml&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;schema=reddit&amp;url=https%3A//www.reddit.com/r/Python/comments/3d9ge9/what_does_your_gui_look_like_post_your_screenshots/&amp;image=" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/bca6b9cea8be612e9bb1a28ff525f82e/href">https://medium.com/media/bca6b9cea8be612e9bb1a28ff525f82e/href</a></iframe><p>This one from fabioz really caught my eye.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/781/0*Bz84OgA6OPVUVjgj.png" /><figcaption><a href="https://www.reddit.com/user/fabioz">fabioz</a> — <a href="http://www.pyvmmonitor.com/images/main.png">http://www.pyvmmonitor.com/images/main.png</a></figcaption></figure><p>I quickly fell in love with the QT designs displayed on their and knew that QT is what I was going to use.</p><p>Great, I had mentally mapped my design in my head and decided to draw it out on a white board. Would post a picture but, the whole philosophy of a white board is that they are mutable. Either way. I found a nice dark theme for QT that looks similar to the one from fabioz. From here I started programming. I bellow is the final GUI, (the column on the left and the graph are just pictures as I have not round to programming them yet).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CFinoe6idfKj_XVe-txnyg.png" /><figcaption>Final design for UI</figcaption></figure><p>This a shell of a program that does nothing, yet. Next week I will be slowly adding functionality to this shell. I cannot wait. :) The GUI initialization code is over a 1000 lines of code already.</p><p>See you next week.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2f8caaf8d16d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>