<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Gealber Morales</title>
    <description>The latest articles on Forem by Gealber Morales (@gealber).</description>
    <link>https://forem.com/gealber</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F250139%2Fe9bbbd9e-ff83-4409-817c-45ee701d5c8d.jpeg</url>
      <title>Forem: Gealber Morales</title>
      <link>https://forem.com/gealber</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/gealber"/>
    <language>en</language>
    <item>
      <title>EVM Reverse Engineering Challenge 0x03</title>
      <dc:creator>Gealber Morales</dc:creator>
      <pubDate>Fri, 05 Jul 2024 14:17:06 +0000</pubDate>
      <link>https://forem.com/gealber/evm-reverse-engineering-challenge-0x03-2jia</link>
      <guid>https://forem.com/gealber/evm-reverse-engineering-challenge-0x03-2jia</guid>
      <description>&lt;p&gt;Here I am, this time with the fourth challenge. This one it's a bit more interesting. Let me know if you found it interesting as well. As always here is the address of the smart contract. Feel free to get that USDT!!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0x48f1F0095a91Ed8d3b788AB4c8998640cb5d4463
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hint: Can I call myself?&lt;/p&gt;

</description>
      <category>evm</category>
      <category>ethereum</category>
      <category>reverseengineering</category>
      <category>smartcontract</category>
    </item>
    <item>
      <title>EVM Reverse Engineering Challenge 0x02</title>
      <dc:creator>Gealber Morales</dc:creator>
      <pubDate>Tue, 02 Jul 2024 16:34:19 +0000</pubDate>
      <link>https://forem.com/gealber/evm-reverse-engineering-challenge-0x02-5o4</link>
      <guid>https://forem.com/gealber/evm-reverse-engineering-challenge-0x02-5o4</guid>
      <description>&lt;p&gt;The third challenge it's here. I'll keep it simple like the other ones, but in order to help you out a bit I will always give you super useful hint like in this case. Also I'll suggest you to make the challenges in order because you will possible need to use previous challenges exploits. Here is the address of the contract, remember always to simulate before actually making the transaction. For that you can use &lt;a href="https://tenderly.co/"&gt;Tenderly&lt;/a&gt;, or &lt;a href="https://github.com/EnsoFinance/temper"&gt;Temper&lt;/a&gt;. No more description, exploit this contract and get 1 USDT!!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0xd436932352dECf38720b04Ef172F7FafdEe7Ab6F
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hint: I was born in a cross-fire hurricane...&lt;/p&gt;

</description>
      <category>evm</category>
      <category>ethereum</category>
      <category>challenge</category>
      <category>reverseengineer</category>
    </item>
    <item>
      <title>EVM Reverse Engineering Challenge 0x01</title>
      <dc:creator>Gealber Morales</dc:creator>
      <pubDate>Sun, 30 Jun 2024 20:29:11 +0000</pubDate>
      <link>https://forem.com/gealber/evm-reverse-engineering-challenge-0x01-462d</link>
      <guid>https://forem.com/gealber/evm-reverse-engineering-challenge-0x01-462d</guid>
      <description>&lt;p&gt;The idea on this one is quite similar as the previous one, just with slight variation. The contract address for this challenge is this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0xA0BEC25Cd1d2b22aa428AbEf23F899506acf9Fff
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hint: How much would be 0 - 1 = ?&lt;/p&gt;

</description>
      <category>evm</category>
      <category>re</category>
      <category>ethereum</category>
      <category>smartcontract</category>
    </item>
    <item>
      <title>EVM Reverse Engineering Challenge 0x00</title>
      <dc:creator>Gealber Morales</dc:creator>
      <pubDate>Wed, 26 Jun 2024 16:43:31 +0000</pubDate>
      <link>https://forem.com/gealber/evm-reverse-engineering-challenge-0x00-21fa</link>
      <guid>https://forem.com/gealber/evm-reverse-engineering-challenge-0x00-21fa</guid>
      <description>&lt;p&gt;It's been a while since I don't blog, so I think it's time. I've been writing an Sandwich bot, mostly in the night after work, that according to my forecast it will take me my whole life :). In case you don't know what is a sandwich bot let me tell you is not a robot that makes sandwich, that would be awesome. Just google &lt;code&gt;MEV sandwich Ethereum&lt;/code&gt;, that should give you a good start. Thanks to this I've learned more and more about EVM(Ethereum Virtual Machine) in general, learning how to optimize a contract to spend less and less gas. This post is not about &lt;a href="https://ethereum.org/en/developers/docs/mev/"&gt;MEV&lt;/a&gt; or explaining what it is, there's a lot of information about it online. Instead, is more to start doing something similar to &lt;a href="https://challenges.re/"&gt;Reverse Engineering challenges&lt;/a&gt; by &lt;a href="https://yurichev.com/"&gt;Dennis Yurichev&lt;/a&gt;, but focus on EVM instead of traditional reverse engineering.&lt;/p&gt;

&lt;p&gt;This blog will be the first of a series of blog, that I'll try to keep up, if laziness or another millionaire idea, like a sandwich bot, doesn't distract me. The format of the challenges will be quite simple, I'll share with you one contract addresses in the Ethereum Mainnet or TON Mainnet(not sure about this yet), the contract will have 1 USDT, your goal will be to exploit this contract and get the 1 USDT for you. Given that I'm far from being rich, I'll limit the number of challenges to 100, I hope I'll have enough material for it. After someone exploits the contract, the 1 USDT won't be in the contract removing the "incentive" for someone else to exploit the contract again. I know 1 USDT is not such a big incentive neither. Is better to be the first, you'll get a whole 1 USDT, WAOOO.&lt;/p&gt;

&lt;p&gt;The challenges will be from easier to harder, I'll try by all means to keep it in that way, but given that I'll be also learning with this I cannot assure you that. In order to exploit this contracts is recommended that you firstly simulate your transaction, in Ethereum that can be done quite precise with &lt;a href="https://tenderly.co/"&gt;Tenderly&lt;/a&gt;, &lt;a href="https://github.com/EnsoFinance/temper"&gt;temper&lt;/a&gt;, in TON I really don't know of a good tool for that. Nevertheless this first challenge is on Ethereum. Please simulate before submitting a transaction, don't spend gas for nothing.&lt;/p&gt;

&lt;p&gt;Enough talk, show me the stuff I want my 1 USDT!!! I'm sure this what you are thinking at the moment, you want to crack this shit and get the 1 USDT to show off with your friends. Don't worry my friend, here is the contrat address:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0xC3fA91C26307EA9c59334c55e23d8bf19f555Ea8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Feel free to exploit it, and earn that 1 USDT.&lt;/p&gt;

&lt;p&gt;HINT: 2 + 2 = ?&lt;/p&gt;

&lt;p&gt;PS: possible the txn will cost you more than 1 USDT, because well is Ethereum...&lt;/p&gt;

</description>
      <category>ethereum</category>
      <category>evm</category>
    </item>
    <item>
      <title>Challenge #31</title>
      <dc:creator>Gealber Morales</dc:creator>
      <pubDate>Sun, 08 Oct 2023 21:51:59 +0000</pubDate>
      <link>https://forem.com/gealber/challenge-31-57bd</link>
      <guid>https://forem.com/gealber/challenge-31-57bd</guid>
      <description>&lt;p&gt;This challenge can be an easy one if you start with the &lt;a href="https://challenges.re/31"&gt;Optimizing MSVC 2012 x64&lt;br&gt;
&lt;/a&gt; assembly code example, instead of the &lt;a href="https://challenges.re/31"&gt;Optimizing MSVC 2010 x86&lt;/a&gt;. The problem with the &lt;a href="https://challenges.re/31"&gt;Optimizing MSVC 2010 x86&lt;/a&gt; example, is the use of &lt;a href="https://en.wikibooks.org/wiki/X86_Assembly/Floating_Point#FPU_Register_Stack"&gt;FPU Register Stack&lt;/a&gt; which makes kinds of hard to follow the logic. If you want to know more about floating points, I highly recommend you read the chapter from &lt;a href="https://beginners.re/"&gt;Reverse Engineering for Beginners&lt;/a&gt;, from the same author of the challenge. At the end of the articles, I'll link other sources as well. As always the code can be found in the original &lt;a href="https://challenges.re/31"&gt;website&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In case you are kind of lost and don't have an idea of what to consult to learn more about floating points, take a look at the Bibliography.&lt;/p&gt;
&lt;h2&gt;
  
  
  Analysis
&lt;/h2&gt;

&lt;p&gt;The code we need to analyze has three float constants, &lt;strong&gt;1.0, 0.001, and 0.5&lt;/strong&gt;. These constants are defined in the following lines&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;__real@3fe0000000000000 DQ 03fe0000000000000r ;; 0.5
__real@3f50624dd2f1a9fc DQ 03f50624dd2f1a9fcr ;; 0.001
__real@3ff0000000000000 DQ 03ff0000000000000r ;; 1.0

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Apart from this, the logic is straightforward, and not too complicated. Here's the code in C language:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mo"&gt;001&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// same as dividing by 2&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Printing the result of calling this function, you can notice that we will receive basically &lt;code&gt;a / 2 + 0.5&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Bibliography
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://beginners.re/"&gt;Chapter Code Patterns, Floating unit&lt;/a&gt; from &lt;a href="//beginners.re"&gt;beginners.re&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://cdrdv2.intel.com/v1/dl/getContent/671200"&gt;Intel Manual. Volume 1, Chapter 8 Programming with the x87 FPU&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Also, the author of the challenge recommends getting familiar with the &lt;a href="https://www.forth.com/forth/"&gt;Forth Programming Language&lt;/a&gt;, it is not to get a super pro on this programming language, it's just to get familiar with the way of programming with a stack-based approach. This language plays also an inspiration in the &lt;a href="https://ton.org/fiftbase.pdf"&gt;Fift Programming Language&lt;/a&gt; of the &lt;a href="https://ton.org"&gt;TON Blockchain&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Books recommendations</title>
      <dc:creator>Gealber Morales</dc:creator>
      <pubDate>Wed, 04 Oct 2023 21:48:30 +0000</pubDate>
      <link>https://forem.com/gealber/books-recommendations-2g3k</link>
      <guid>https://forem.com/gealber/books-recommendations-2g3k</guid>
      <description>&lt;p&gt;Here is a short list of books I've read, and still remember. I've read many more, but these are the ones that I wouldn't bother reading again or that I recommend reading.&lt;/p&gt;

&lt;h2&gt;
  
  
  Technical books
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://amzn.to/46gPvy6"&gt;The Practice of programming&lt;/a&gt;, by Brian W. Kernighan and Rob Pike.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://amzn.to/3PKJBhI"&gt;Introduction to Algorithms, third edition&lt;/a&gt;, by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein. This book is one that you always go back always when you have to implement an algorithm, really useful book. I've read the majority of it.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://amzn.to/46icusE"&gt;The Go Programming Language&lt;/a&gt;, by Alan A. Donovan and Brian W. Kernighan. I think this is one of the best books I've ever read about a programming language, with practical examples from the beginning to the end. &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://amzn.to/3rJdbfx"&gt;Learn C the hard way&lt;/a&gt;, by Zed Shaw. This guy managed to teach C in an entertaining way.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://amzn.to/46ielO7"&gt;64-bit assembly programming for Linux: The easy guide to get started&lt;/a&gt;, by Mark B. This book is not such a great book, but what I wanted was perfect. This is a book you can read, in one weekend, to learn the basics of Assembly through examples. For a more deep understanding, there are way better options.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://amzn.to/3Q4Vr7G"&gt;Python Tricks: A Buffet of Awesome Python Features&lt;/a&gt;, by Dan Bader. Oh, I loved this book. The author has a natural skill to keep you reading with small, but quite instructive recipes about Python.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://amzn.to/3rAyQXr"&gt;Reversing: Secrets of Reverse Engineering&lt;/a&gt;, by Eldad Eilam. I read this one quite recently, and I've been coming back to it every time I want to consult something about Reverse Engineering.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://beginners.re/"&gt;Reverse Engineering for Beginners&lt;/a&gt;, by Denis Dennis Yurichev. Haven't read it completely, but a great part of it. I like the approach the author took to teach you about this subject, has a lot a lot of examples.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://amzn.to/45jn5T4"&gt;Practical SQL A Beginner’s Guide to Storytelling with Data&lt;/a&gt;, by Anthony DeBarros. Another book that I loved, it doesn't make you lose time you start writing SQL queries from the beginning.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://amzn.to/3Q2mUXE"&gt;Automate the boring stuffs with Python&lt;/a&gt;, by Al Sweigart. One of the first books I read, and still one of my favorites. For a beginner, this book is absolute treasure.&lt;/li&gt;
&lt;li&gt;Recreational Mathematics, by Yakov Perelman. This is the first book about math that I read.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Nontechnical books
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://amzn.to/3LPeYqe"&gt;Atlas Shrugged&lt;/a&gt;, by Ayn Rand. By far the best book I've ever read.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://amzn.to/3ZFWz4S"&gt;Ghost In The Wires: My Adventures as the World's Most Wanted Hacker&lt;/a&gt;, by Kevin Mitnick. Interesting story.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://amzn.to/46D0qSS"&gt;Crime and Pubishment&lt;/a&gt;, by Fyodor Dostoevsky.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://amzn.to/3ZEZkUd"&gt;The Black Swan: Second Edition: The Impact of the Highly Improbable: With a new section: "On Robustness and Fragility": 2&lt;/a&gt;, by Nassim Taleb. This book should be a must to read for all guys making statistics. In this case, I didn't &lt;strong&gt;read it&lt;/strong&gt; but listened to the audiobook.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://amzn.to/3PHb6ce"&gt;Ada or ardor&lt;/a&gt;, by Vladimir Nabokob. Way better book than Lolita, in my opinion. Is not about the same theme. This author wrote so well, but so good, I haven't found an author that writes like this man.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://amzn.to/3LKZdR6"&gt;1984&lt;/a&gt;, by George Orwell. This book doesn't need an introduction, this guy was describing Cuba...in the present and the past 64 years.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://amzn.to/3ZPle7h"&gt;The Fountainhead&lt;/a&gt;, by Ayn Rand. This is the book that more times I've read in my life, more than 3 or 4. Especially during college. This book gives me fuel. It's just awesome!&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://amzn.to/3teXKMx"&gt;"Surely You're Joking, Mr. Feynman!": Adventures of a Curious Character&lt;/a&gt;, by Richard P. Feynman. One of the few books I've enjoyed most.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Whishlist
&lt;/h2&gt;

&lt;p&gt;Here is a short list of books I plan to read in the future.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Technical books&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://amzn.to/46zkpS5"&gt;Applied Cryptography: Protocols, Algorithms, and Source Code in C&lt;/a&gt;, by Bruce Schneier. As soon as I finish my one month of learning the basics of Reverse engineering, I'll jump to this book. This is a subject that I've always been interested in. As always I'll take a practical approach.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Nontechnical&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://amzn.to/46FIebf"&gt;Atomic Habits&lt;/a&gt;, by James Clear. I've heard good things about this book.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>books</category>
    </item>
    <item>
      <title>Challenge RE #30</title>
      <dc:creator>Gealber Morales</dc:creator>
      <pubDate>Wed, 04 Oct 2023 09:45:52 +0000</pubDate>
      <link>https://forem.com/gealber/challenge-re-30-27hh</link>
      <guid>https://forem.com/gealber/challenge-re-30-27hh</guid>
      <description>&lt;p&gt;I'll jump to challenge #30, the #29 is an obfuscated one, and I'll need more time to work on that. The #30 is a simple one and is in the format of finding the password. For the disassembly of this program I'll use &lt;a href="https://hex-rays.com/ida-free/"&gt;Ida&lt;/a&gt;, from the one I've tried is honestly the more user-friendly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Analysis
&lt;/h2&gt;

&lt;p&gt;Let's analyze the main thread only, here is the code associated with it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;; int __cdecl main(int, char **, char **)
main            proc near               ; DATA XREF: start+17↑o
; __unwind {
                push    ebp
                mov     ebp, esp
                and     esp, 0FFFFFFF0h
                sub     esp, 0A0h
                mov     eax, large gs:14h
                mov     [esp+9Ch], eax
                xor     eax, eax
                mov     dword ptr [esp], offset aEnterPassword ; "enter password:" 1st argument, only one argument it's required
                call    _puts

                lea     eax, [esp+1Ch]
                mov     [esp+4], eax
                mov     dword ptr [esp], offset aS ; "%s"
                call    ___isoc99_scanf

                cmp     eax, 1 ;; number of items successfully matched
                jz      short loc_8048539

                mov     dword ptr [esp], offset aNoPasswordSupp ; "no password supplied"
                call    _puts

loc_8048539:                            ; CODE XREF: main+3E↑j
                mov     dword ptr [esp+4], offset aMetallica ; "metallica"
                lea     eax, [esp+1Ch]
                mov     [esp], eax
                call    _strcmp

                test    eax, eax
                jnz     short loc_804855F ;; INCORRECT PASSWORD PRINT IT

                mov     dword ptr [esp], offset aPasswordIsCorr ; "password is correct"
                call    _puts
                jmp     short loc_804856B
; ---------------------------------------------------------------------------

loc_804855F:                            ; CODE XREF: main+62↑j
                mov     dword ptr [esp], offset aPasswordIsNotC ; "password is not correct"
                call    _puts

loc_804856B:                            ; CODE XREF: main+70↑j
                mov     edx, [esp+9Ch]
                xor     edx, large gs:14h
                jz      short locret_8048580
                call    ___stack_chk_fail

locret_8048580:                         ; CODE XREF: main+8C↑j
                leave
                retn
; } // starts at 80484ED
main            endp

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code we have first a call to &lt;code&gt;puts&lt;/code&gt; printing the text &lt;code&gt;enter password:&lt;/code&gt;, we know that is this text thanks to &lt;a href="//hex-rays.com/ida-free/"&gt;Ida&lt;/a&gt;. What follows after that it's a call to &lt;code&gt;scanf&lt;/code&gt;, for waiting for the input of the password. Nothing too hard here, so we need to look for the part of the code that validate this password. Right after the call to &lt;code&gt;scanf&lt;/code&gt; we have the following code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                lea     eax, [esp+1Ch]
                mov     [esp+4], eax
                mov     dword ptr [esp], offset aS ; "%s"
                call    ___isoc99_scanf

                cmp     eax, 1 ;; number of items successfully matched
                jz      short loc_8048539
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the call to &lt;code&gt;scanf&lt;/code&gt; we check if the user supplied something, in positive case we jump to &lt;code&gt;loc_8048539&lt;/code&gt;, that is where our validation it's present. Let's see how this password it's validated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;loc_8048539:                            ; CODE XREF: main+3E↑j
                mov     dword ptr [esp+4], offset aMetallica ; "metallica"
                lea     eax, [esp+1Ch]
                mov     [esp], eax
                call    _strcmp

                test    eax, eax
                jnz     short loc_804855F ;; INCORRECT PASSWORD PRINT IT

                mov     dword ptr [esp], offset aPasswordIsCorr ; "password is correct"
                call    _puts
                jmp     short loc_804856B
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Easy! We have a comparison with &lt;code&gt;strcmp&lt;/code&gt; to the text &lt;code&gt;metallica&lt;/code&gt;, seems Denis is a fan of Metallica, and the printing of the text &lt;code&gt;password is correct&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That's it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I'm becoming better at this, some progress.&lt;/p&gt;

</description>
      <category>reverseengineering</category>
      <category>assembly</category>
      <category>c</category>
    </item>
    <item>
      <title>Challenge RE #28</title>
      <dc:creator>Gealber Morales</dc:creator>
      <pubDate>Tue, 03 Oct 2023 09:15:21 +0000</pubDate>
      <link>https://forem.com/gealber/challenge-re-28-2o8l</link>
      <guid>https://forem.com/gealber/challenge-re-28-2o8l</guid>
      <description>&lt;p&gt;Let's start with challenge #28. In this case we have a bigger code than our previous two challenges, but I think will be more understandable than those.&lt;br&gt;
The code as always can be found the &lt;a href="https://challenges.re/28/"&gt;original website&lt;/a&gt;. This challenge is an example of a useful code, I mean something you might use. I'll start analyzing it by parts as always. &lt;/p&gt;
&lt;h2&gt;
  
  
  Analysis
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;f1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here something to notice is that in this case we have a function &lt;code&gt;f_main&lt;/code&gt; that will be our main thread, but let's start analyzing function &lt;code&gt;f1&lt;/code&gt;. Seems quite simple. We have the following(commented code by myself)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;;; this function compares two 32-bit integers
;; returning:
;; -1 in case a &amp;lt; b
;; 0 in case a = b
;; 1 in case a &amp;gt; b
;; int f(const void *a, const void *b) NOTE this method it's passed as an the comparison function to qsort
f1:
    mov eax, DWORD PTR [rsi]
    xor edx, edx
    cmp DWORD PTR [rdi], eax ;; compare first char with second string
    mov eax, -1
    setg    dl ;; set byte in dl if str1[0] &amp;gt; str2[0]; this could be a ternary operation [edx]dl = str1[0] &amp;gt; str2[0] ? 1 : 0;
    cmovge  eax, edx ;; copy 0 to eax if greater of equal
    ret
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see from the comments I added, this method seems to compare two elements from &lt;code&gt;rsi&lt;/code&gt; and &lt;code&gt;rdi&lt;/code&gt;, which seems to be array of integers. The output number will be &lt;code&gt;-1, 0 or 1&lt;/code&gt; depending on the comparison result. Looking down in the code you can notice that this method, it's passed to &lt;code&gt;qsort&lt;/code&gt; as the comparison function. Then we can infer that must hold the signature &lt;code&gt;int (*compar)(const void *, const void *)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;code in C&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int f1(const void *a, const void *b)
{
    if (*(int *)a &amp;lt; *(int *)b) return -1;

    return *(int *)a &amp;gt; *(int *)b ? 1 : 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;my_memdup&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The next method to analyze is &lt;code&gt;my_memdup&lt;/code&gt;, here is the snippet for the code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_memdup:
    push    rbp
    mov rbp, rdi ;;  arr, 1st argument of my_memdup
    mov rdi, rsi ;; 1st arg to malloc and 2nd from this method
    push    rbx
    mov rbx, rsi ;; copy same number to rbx, to be used in the call to memcpy
    sub rsp, 8
    call    malloc ;; the reserved memory is in rax

    add rsp, 8
    mov rdx, rbx ;; 3rd arg (size_t n) number of bytes to copy
    mov rsi, rbp ;; 2nd arg (src)
    pop rbx
    pop rbp
    mov rdi, rax ;; 1st arg (dst)
    jmp memcpy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looking at this code snippet we can infer a couple of things. We have a call to &lt;code&gt;malloc&lt;/code&gt; followed by a &lt;code&gt;memcpy&lt;/code&gt;. This code can be put in C code very shortly in this way&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;my_memdup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;rax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sz&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;memcpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Basically this method make a &lt;code&gt;duplicate&lt;/code&gt; of memory in &lt;code&gt;arr&lt;/code&gt;. I don't see the use of this code in the main thread or any other function, so no idea why it's here. Maybe the compiler added? They do weird stuffs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;f2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This method is the simpler of all of them, we receive an 32-bit integer as argument. The operations followed is a right shift to 31 bits, which will make it literally &lt;code&gt;0&lt;/code&gt;. I don't understand why the compiler decided to to this...compilers are dark magicians. In this case I think all 5 operations are just a right shift to one position.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int f2(int a)
{
    return a &amp;gt;&amp;gt; 1;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;f1_main&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, the main thread. Here is the code for &lt;code&gt;f_main&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;;;int f_main(int *arr, int nmemb)
f_main:
    push    r12
    mov r12, rdi
    push    rbp
    lea rbp, [0+rsi*4] ;; rsi contains nmemb
    push    rbx
    mov rdi, rbp ;; amount of memory to reserve nmemb*4 (4 is the sizeof(int))
    mov rbx, rsi ;; rbx will contain nmemb
    call    malloc

    mov rdx, rbp ;; 3rd arg, size
    mov rsi, r12 ;; 2nd arg, src (rdi)
    mov rdi, rax ;; 1st arg, dst
    call    memcpy

    mov ecx, OFFSET FLAT:f1 ;; 4th arg comparison function
    mov edx, 4 ;; 3rd arg
    mov rsi, rbx ;; 2nd arg (nmemb)
    mov rdi, rax ;; 1st arg
    mov rbp, rax ;; reserved memory
    call    qsort

    test    bl, 1 ;; checking for parity, nmemb % 2
    jne .L12 ;; odd
    ;; nmemb is even
    shr rbx
    mov eax, DWORD PTR [rbp+0+rbx*4]
    add eax, DWORD PTR [rbp-4+rbx*4]

    pop rbx
    pop rbp
    pop r12
    mov edx, eax
    shr edx, 31
    add eax, edx
    sar eax
    ret
.L12: ;; 
    shr rbx ;; nmemb &amp;gt;&amp;gt; 1; division by 2
    mov eax, DWORD PTR [rbp+0+rbx*4] ;; rbp contains the memory reserved, and array just sorted with qsort
    pop rbx
    pop rbp
    pop r12
    ret
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An equivalent code in C of this case would be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;f_main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;nmemb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;rax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nmemb&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

    &lt;span class="n"&gt;memcpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nmemb&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="n"&gt;qsort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nmemb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nmemb&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;nmemb&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;


    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;nmemb&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;nmemb&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looking at this, it's easy to infer that we are extracting the &lt;strong&gt;median&lt;/strong&gt; of the array &lt;code&gt;arr&lt;/code&gt;, the formal description for this code is&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;formal description&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Computes the median in an array of integers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Something that I didn't understand here, is the declaration of &lt;code&gt;f2&lt;/code&gt; and &lt;code&gt;my_memdup&lt;/code&gt;, they are not used in the code.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Challenge RE #26</title>
      <dc:creator>Gealber Morales</dc:creator>
      <pubDate>Mon, 02 Oct 2023 08:25:30 +0000</pubDate>
      <link>https://forem.com/gealber/challenge-re-26-3mb3</link>
      <guid>https://forem.com/gealber/challenge-re-26-3mb3</guid>
      <description>&lt;p&gt;This challenge comes in the form of &lt;a href="https://learn.microsoft.com/en-us/dotnet/standard/managed-execution-process"&gt;Microsoft Intermediate Language&lt;/a&gt; which is &lt;strong&gt;extremely&lt;/strong&gt; readable compared to previous assembly code used in these challenges. If you are interested in more about the specifics of reversing .NET read &lt;a href="https://amzn.to/3PCRUvZ"&gt;Chapter 12, of Reversing. Secrets of Reverse Engineering&lt;/a&gt; there's a quite detailed explanation there. Also for a quick search of the available opcodes for each instruction recommend you to use &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.reflection.emit.opcodes.add?view=net-7.0"&gt;Official doc&lt;/a&gt;. This challenge will be quite short, I included comments on each line of the code, so the analysis won't be a big deal.&lt;/p&gt;

&lt;h2&gt;
  
  
  Analysis
&lt;/h2&gt;

&lt;p&gt;Here is the code to understand&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  .method public hidebysig static uint8  f(uint8 a) cil managed
  {
    // Code size       36 (0x24)
    .maxstack  2
    .locals init (uint8 V_0)
    IL_0000:  nop
    IL_0001:  ldarg.0 ;; first argument
    IL_0002:  conv.u8 ;; converts value on top of stack and extends it to int64
    IL_0003:  ldc.i8     0x202020202 ;; push this value on the stack as an int64
    ;; multiply two first values on stack, and push result on the stack. In this case
    ;; this is int64(arg_0) * int64(0x202020202)
    IL_000c:  mul     
    IL_000d:  ldc.i8     0x10884422010 ;; again push this value into the stack
    IL_0016:  and ;; perform AND between the result of multiplication and this recent value pushed on the stack
    IL_0017:  ldc.i4     0x3ff ;; push value into stack
    IL_001c:  conv.i8  ;; convert it to int64
    IL_001d:  rem      ;; push into the stack  val1 % 0x3ff, where val1 is the result of our previous operation
    IL_001e:  conv.u1  ;; convert value to int8 but push int32, so it extends the converted value
    IL_001f:  stloc.0  ;; pop value on top of stack and store it in local variable 0
    IL_0020:  br.s       IL_0022 ;; this jumps to IL_0022

    IL_0022:  ldloc.0  ;; loads variable on top of stack
    IL_0023:  ret ;; return
  } // end of method e25::f
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's not too much to add here, take a look that even the signature it's given. Following these comments the equivalent code in C, I don't know C# guys, would be like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt;&lt;span class="p"&gt;(((&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mh"&gt;0x202020202&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0x10884422010&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mh"&gt;0x3ff&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would be the code, now what this does? Well for that task I have Google. Making a search for this constant I found a book called &lt;a href="https://amzn.to/3LKUOxP"&gt;Hacker's Delight&lt;/a&gt; which has this exact formula. You can find it in Chapter 7, about Rearranging bits and bytes.&lt;/p&gt;

&lt;p&gt;According to the description given in the book this&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Reverse an 8-bit integer&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I don't find too much utility for this kind of hacks, are obscure as fuck. At least you are writing malware, that for some reason needs to perform this operation, and you want to write obscure code, yes is useful otherwise...not so much.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Google is your friend, use it!&lt;/p&gt;

</description>
      <category>reverseengineering</category>
      <category>assembly</category>
      <category>c</category>
    </item>
    <item>
      <title>Challenge RE #24</title>
      <dc:creator>Gealber Morales</dc:creator>
      <pubDate>Sun, 01 Oct 2023 11:56:20 +0000</pubDate>
      <link>https://forem.com/gealber/challenge-re-24-g44</link>
      <guid>https://forem.com/gealber/challenge-re-24-g44</guid>
      <description>&lt;p&gt;This challenge it's more interesting. This time we have a binary file with the following description&lt;/p&gt;

&lt;h2&gt;
  
  
  Challenge Description
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Here is the simplest possible calculator program, a simplified version of bc UNIX utility:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;123+456
&lt;span class="o"&gt;(&lt;/span&gt;unsigned&lt;span class="o"&gt;)&lt;/span&gt; dec: 579 hex: 0x243  bin: 1001000011

120/10
&lt;span class="o"&gt;(&lt;/span&gt;unsigned&lt;span class="o"&gt;)&lt;/span&gt; dec: 12 hex: 0xC  bin: 1100

&lt;span class="nt"&gt;-10&lt;/span&gt;
&lt;span class="o"&gt;(&lt;/span&gt;unsigned&lt;span class="o"&gt;)&lt;/span&gt; dec: 4294967286 hex: 0xFFFFFFF6  bin: 11111111111111111111111111110110
&lt;span class="o"&gt;(&lt;/span&gt;signed&lt;span class="o"&gt;)&lt;/span&gt; dec: &lt;span class="nt"&gt;-10&lt;/span&gt; hex: &lt;span class="nt"&gt;-0xA&lt;/span&gt;  bin: &lt;span class="nt"&gt;-1010&lt;/span&gt;

&lt;span class="nt"&gt;-10&lt;/span&gt;+20
&lt;span class="o"&gt;(&lt;/span&gt;unsigned&lt;span class="o"&gt;)&lt;/span&gt; dec: 10 hex: 0xA  bin: 1010
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;It is known that it supports 4 arithmetic operations and negative numbers can be denoted with a minus before the number. But it's also known that there are at least 7 undocumented features. Try to find them all.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In order to approach this problem, we need to take a look at the assembly code of this binary. For this you have several tools to extract the disassembled code, here is a list of them&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://hex-rays.com/ida-free/"&gt;Ida free&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/NationalSecurityAgency/ghidra/releases"&gt;Ghidra&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/radareorg/radare2"&gt;radare2&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For this challenge, I'll just use Ida because is the one I'm more familiar with. The great advantage of these tools is that they even draw you through the flow of the program, and even disassemble the code to C/C++ code. I'm not going to disassemble anything, but I'm not mad for them to draw me some arrow flows, that's sick, good stuff.&lt;/p&gt;

&lt;h2&gt;
  
  
  Analysis
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Exploring the binary functionalities&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's start our reversing process running the binary, as always don't do this if you are running with malware. Let's run this program&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./calc_x64

1
&lt;span class="o"&gt;(&lt;/span&gt;unsigned&lt;span class="o"&gt;)&lt;/span&gt; dec: 1 hex: 0x1  bin: 1
1+1
&lt;span class="o"&gt;(&lt;/span&gt;unsigned&lt;span class="o"&gt;)&lt;/span&gt; dec: 2 hex: 0x2  bin: 10
1&lt;span class="k"&gt;*&lt;/span&gt;1
&lt;span class="o"&gt;(&lt;/span&gt;unsigned&lt;span class="o"&gt;)&lt;/span&gt; dec: 1 hex: 0x1  bin: 1
1/1
&lt;span class="o"&gt;(&lt;/span&gt;unsigned&lt;span class="o"&gt;)&lt;/span&gt; dec: 1 hex: 0x1  bin: 1
1-1
&lt;span class="o"&gt;(&lt;/span&gt;unsigned&lt;span class="o"&gt;)&lt;/span&gt; dec: 0 hex: 0x0  bin: 
1&lt;span class="k"&gt;**&lt;/span&gt;1
Error: unexpected token. Exiting.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you see in this terminal output I explored the program running basic arithmetic operations, &lt;code&gt;+, -, *, /&lt;/code&gt;. I also tried &lt;strong&gt;power&lt;/strong&gt; in the Python style, and we received an error. The output in all cases, except the error one, is in the format &lt;code&gt;(unsigned) dec: &amp;lt;number in decimal representation&amp;gt; hex: &amp;lt;number in hex representation&amp;gt; bin: &amp;lt;number in bin representation&amp;gt;&lt;/code&gt;. In all the cases we tried with positive numbers, let's try with negative ones&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nt"&gt;-1&lt;/span&gt;
&lt;span class="o"&gt;(&lt;/span&gt;unsigned&lt;span class="o"&gt;)&lt;/span&gt; dec: 4294967295 hex: 0xFFFFFFFF  bin: 11111111111111111111111111111111
&lt;span class="o"&gt;(&lt;/span&gt;signed&lt;span class="o"&gt;)&lt;/span&gt; dec: &lt;span class="nt"&gt;-1&lt;/span&gt; hex: &lt;span class="nt"&gt;-0x1&lt;/span&gt;  bin: &lt;span class="nt"&gt;-1&lt;/span&gt;
&lt;span class="nt"&gt;-1&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt; 2
&lt;span class="o"&gt;(&lt;/span&gt;unsigned&lt;span class="o"&gt;)&lt;/span&gt; dec: 4294967294 hex: 0xFFFFFFFE  bin: 11111111111111111111111111111110
&lt;span class="o"&gt;(&lt;/span&gt;signed&lt;span class="o"&gt;)&lt;/span&gt; dec: &lt;span class="nt"&gt;-2&lt;/span&gt; hex: &lt;span class="nt"&gt;-0x2&lt;/span&gt;  bin: &lt;span class="nt"&gt;-10&lt;/span&gt;
&lt;span class="nt"&gt;-1&lt;/span&gt; + 2
&lt;span class="o"&gt;(&lt;/span&gt;unsigned&lt;span class="o"&gt;)&lt;/span&gt; dec: 1 hex: 0x1  bin: 1
&lt;span class="nt"&gt;-1&lt;/span&gt; / 2
&lt;span class="o"&gt;(&lt;/span&gt;unsigned&lt;span class="o"&gt;)&lt;/span&gt; dec: 0 hex: 0x0  bin: 
&lt;span class="nt"&gt;-1&lt;/span&gt; - 2
&lt;span class="o"&gt;(&lt;/span&gt;unsigned&lt;span class="o"&gt;)&lt;/span&gt; dec: 4294967293 hex: 0xFFFFFFFD  bin: 11111111111111111111111111111101
&lt;span class="o"&gt;(&lt;/span&gt;signed&lt;span class="o"&gt;)&lt;/span&gt; dec: &lt;span class="nt"&gt;-3&lt;/span&gt; hex: &lt;span class="nt"&gt;-0x3&lt;/span&gt;  bin: &lt;span class="nt"&gt;-11&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, we can see that the calculator can handle &lt;code&gt;signed&lt;/code&gt; numbers as well. Ok, everything is as expected. These are 4 arithmetics operations mentioned in the description of the challenge, we are missing 3 more. One thing we can try is using bitwise operators and see what we get.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;1 ^ 2
&lt;span class="o"&gt;(&lt;/span&gt;unsigned&lt;span class="o"&gt;)&lt;/span&gt; dec: 3 hex: 0x3  bin: 11
1 &amp;amp; 2
Error: bad character. Exiting.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we see the calculator supports &lt;code&gt;XOR with ^ operator&lt;/code&gt;, but does not support &lt;code&gt;AND with &amp;amp;&lt;/code&gt;. We have 5 operations, we are still missing 2 more. I also tried &lt;code&gt;|&lt;/code&gt; but we received an error output as well. The error for this case it's quite different, has the following output &lt;code&gt;junk after expression&lt;/code&gt;, which might mean that we are just using badly this operator. For now, this exploration it's quite good we will need to look at the assembly code to find out the other two.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reading the code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's start our analysis with the main thread of the program. Right away you can notice that we have a call to &lt;code&gt;sub_400A3B&lt;/code&gt;, which we are going to look at a bit later. Followed by printings of a text with the following format, using the result of this subroutine to populate the formatted text. This is the formatted text &lt;code&gt;(unsigned) dec: %u hex: 0x%X bin: &amp;lt;number in binary representation&amp;gt;\n&lt;/code&gt;. This matches with our previous exploration of the program. Here is the code snippet for the assembly code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;; int __fastcall main(int, char **, char **)
main proc near
; __unwind {
push    rbx
call    sub_400A3B

loc_400696:
call    sub_400AEE ;; ???
;; PREPARING FOR PRINTING RESULT OF sub_400AEE on different formats
;; decimal, and hexadecimal
mov     esi, offset aUnsignedDecUHe ; "(unsigned) dec: %u hex: 0x%X "
mov     ecx, eax ;; result from sub_400AEE
mov     edx, eax
mov     ebx, eax
mov     edi, 1
xor     eax, eax
call    ___printf_chk

;; printing just 'bin: '
mov     esi, offset aBin ; " bin: "
mov     edi, 1
xor     eax, eax
call    ___printf_chk

mov     edi, ebx
call    sub_40082D
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now I personally don't want to read the whole assembly code, I just want to find out these missing operations. One smart approach would be to look in Ida for the signs of operations we already know. For example, let's look for the parsing of character &lt;code&gt;+&lt;/code&gt;. In this way, we might identify other symbols as well in the code. The ASCII value for &lt;code&gt;+&lt;/code&gt; it's &lt;code&gt;0x2b&lt;/code&gt;, another way to represent this is &lt;code&gt;2bh&lt;/code&gt; which is the way Ida represents the hexadecimal numbers. To make the search you can press &lt;code&gt;Alt+T&lt;/code&gt; or go to &lt;code&gt;Search &amp;gt; Text&lt;/code&gt;, another way could be that if you previously disassembled the binary to a file you can use &lt;code&gt;grep&lt;/code&gt; to search in the file where the disassembled code. When we made that search Ida added a comment with the value &lt;code&gt;'+'&lt;/code&gt;, this is done in the subroutine &lt;code&gt;sub_400C07&lt;/code&gt;. Let's analyze that particular function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sub_400C07 proc near
; __unwind {
push    rbx
xor     eax, eax
call    sub_400BBB

loc_400C0F:
mov     ebx, eax

loc_400C11:
cmp     cs:dword_602490, 1
mov     al, cs:byte_602090
jnz     short loc_400C69

loc_400C20:
cmp     al, 2Ah ; '*'
jz      short loc_400C43
cmp     al, 2Fh ; '/'
jz      short loc_400C54
cmp     al, 25h ; '%'
jnz     short loc_400C20
call    advance
xor     eax, eax
call    sub_400BBB
mov     esi, eax
mov     eax, ebx
cdq
idiv    esi
mov     ebx, edx
jmp     short loc_400C11

;; ... method continues

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Right away in &lt;code&gt;loc_400C20&lt;/code&gt; we can see that apart from &lt;code&gt;* and /&lt;/code&gt;, operations that we already know, there's as well &lt;code&gt;%&lt;/code&gt;. Let's rerun our calc_x64 to see what this operation gives us, possibly just the remainder between two numbers, but let's check.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;5 % 2
&lt;span class="o"&gt;(&lt;/span&gt;unsigned&lt;span class="o"&gt;)&lt;/span&gt; dec: 1 hex: 0x1  bin: 1
4 % 2
&lt;span class="o"&gt;(&lt;/span&gt;unsigned&lt;span class="o"&gt;)&lt;/span&gt; dec: 0 hex: 0x0  bin: 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nice, so this is another supported operation, at this point, we have the &lt;code&gt;+, -, *, /, ^ and %&lt;/code&gt;, just one operation it's remaining. Now that we know Ida commented to us these special signs it would be smart on our part to make a smarter search, in this case, let's look for the following regular expression:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="s1"&gt;'.{1}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this, we are asking Ida to look for comments that are enclosed in &lt;code&gt;''&lt;/code&gt; and have exactly one character.&lt;/p&gt;

&lt;p&gt;This search gave us several results, we only need our extra operation in order to solve the puzzle so I'm going to look right for that. Doing so, I spotted the following character &lt;code&gt;~&lt;/code&gt;, so this could be our last operation, which could be the negation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;~ 1
&lt;span class="o"&gt;(&lt;/span&gt;unsigned&lt;span class="o"&gt;)&lt;/span&gt; dec: 4294967294 hex: 0xFFFFFFFE  bin: 11111111111111111111111111111110
&lt;span class="o"&gt;(&lt;/span&gt;signed&lt;span class="o"&gt;)&lt;/span&gt; dec: &lt;span class="nt"&gt;-2&lt;/span&gt; hex: &lt;span class="nt"&gt;-0x2&lt;/span&gt;  bin: &lt;span class="nt"&gt;-10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yep!! Now we have all the operations, &lt;code&gt;+, -, *, /, ^, % and ~&lt;/code&gt; are our seven arithmetics operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I think the lesson to learn here is to &lt;strong&gt;focus on your goal&lt;/strong&gt; try to find what you want and not analyze every single line of code.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Challenge RE #23</title>
      <dc:creator>Gealber Morales</dc:creator>
      <pubDate>Fri, 29 Sep 2023 22:13:20 +0000</pubDate>
      <link>https://forem.com/gealber/challenge-re-23-37g2</link>
      <guid>https://forem.com/gealber/challenge-re-23-37g2</guid>
      <description>&lt;p&gt;&lt;a href="https://challenges.re/23/"&gt;Challenge #23&lt;/a&gt; description can be found on the original website, as well as the assembly code. Here I'll put the same code but with some personal comments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;;; int f(long long int *rdi)
f:
        ;; rdi array of 64-bit numbers
        mov     rdx, QWORD PTR [rdi]
        ;; this initial part of the code here
        ;; will return i if the supplied number
        ;; had the last i*8 bits equal to zero, and the first 8 to one
        test    dl, dl ;; dl register 8 bit, major register rdx 
        je      .L18 ;; EXIT PROGRAM 0
        test    dh, 255 ;; 8 bits = 1, from 0 to 8
        je      .L19 ;; EXIT PROGRAM 1
        test    edx, 16711680 ;; 8 bits = 1, and 16 bits = 0
        je      .L20 ;; EXIT PROGRAM 2
        test    edx, 4278190080 ;; 8 bits = 1, and 24 bits = 0
        je      .L21 ;; EXIT PROGRAM 3
        movabs  r8, 1095216660480 ;; 8 bits = 1, and 32 bits = 0
        test    rdx, r8
        je      .L22 ;; EXIT PROGRAM 4
        movabs  r9, 280375465082880 ;; 8 bits = 1, and 40 bits = 0
        test    rdx, r9
        je      .L26 ;; EXIT PROGRAM 5

        xor     eax, eax
        ;; this number it's stored in the counter
        movabs  rcx, 71776119061217280 ;; 8 bits = 1, and 48 bits = 0
        movabs  rsi, -72057594037927936 ;; ? no idea why is this here
        jmp     .L14

;; eax = 0
.L15:
        test    rdx, rsi
        je      .L27 ;; EXIT PROGRAM 7
        add     rax, 8
        ;; load next element of array in rdx
        mov     rdx, QWORD PTR [rdi+rax]
        test    dl, dl
        je      .L28 ;; EXIT PROGRAM WITH rax+i
        test    dh, 255
        je      .L29 ;; EXIT PROGRAM with rax+1
        test    edx, 16711680
        je      .L30 ;; EXIT PROGRAM with rax+2
        test    edx, 4278190080
        je      .L31 ;; EXIT PROGRAM with rax+3
        test    rdx, r8
        je      .L32 ;; EXIT PROGRAM with rax+4
        test    rdx, r9
        je      .L33 ;; EXIT PROGRAM with rax+5
.L14:
        test    rdx, rcx
        jne     .L15
        add     rax, 6 ;; EXIT PROGRAM WITH rax+6
        ret

;; NO MORE LOGIC HERE JUST RETURNS
.L27 ;; EXIT PROGRAM 7:
        add     rax, 7
        ret
.L28 ;; EXIT PROGRAM WITH rax+i:
        rep ret
.L29 ;; EXIT PROGRAM with rax+1:
        add     rax, 1
        ret
.L30 ;; EXIT PROGRAM with rax+2:
        add     rax, 2
        ret
.L31 ;; EXIT PROGRAM with rax+3:
        add     rax, 3
        ret
.L32 ;; EXIT PROGRAM with rax+4:
        add     rax, 4
        ret
.L33 ;; EXIT PROGRAM with rax+5:
        add     rax, 5
        ret

.L18 ;; EXIT PROGRAM 0:
        xor     eax, eax
        ret
   ;; EXIT PROGRAM WITH 2;
.L20 ;; EXIT PROGRAM 2:
        mov     eax, 2
        ret
.L19 ;; EXIT PROGRAM 1:
        mov     eax, 1
        ret
.L21 ;; EXIT PROGRAM 3:
        mov     eax, 3
        ret
.L26 ;; EXIT PROGRAM 5:
        mov     eax, 5
        ret
.L22 ;; EXIT PROGRAM 4:
        mov     eax, 4
        ret
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Analysis
&lt;/h2&gt;

&lt;p&gt;In this code you can find several constants, taking these constants into binary representation will give the following table&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Number&lt;/th&gt;
&lt;th&gt;First 8-bits&lt;/th&gt;
&lt;th&gt;Rest of bits&lt;/th&gt;
&lt;th&gt;Total of bits&lt;/th&gt;
&lt;th&gt;Returns&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0xff&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;i+1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0xff0000&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;24&lt;/td&gt;
&lt;td&gt;i+2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0xff000000&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;32&lt;/td&gt;
&lt;td&gt;i+3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0xff00000000&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;40&lt;/td&gt;
&lt;td&gt;i+4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0xff0000000000&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;48&lt;/td&gt;
&lt;td&gt;i+5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0xff000000000000&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;56&lt;/td&gt;
&lt;td&gt;i+6&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The next thing to notice is the loop inside the function. First, we are iteration over elements of an array of 64-bit numbers in &lt;code&gt;rdi&lt;/code&gt;, and performing &lt;code&gt;AND&lt;/code&gt; operations against the constants. The peculiarity of these constants is that they have &lt;strong&gt;1s&lt;/strong&gt; on sections from &lt;strong&gt;1 to 6&lt;/strong&gt;, and from the &lt;strong&gt;eights 8-bit&lt;/strong&gt; sections a &lt;strong&gt;64-bit&lt;/strong&gt; number has. Then when we perform &lt;code&gt;AND&lt;/code&gt; operations against these constants, we are checking if the number on this particular &lt;strong&gt;section&lt;/strong&gt; has &lt;strong&gt;0&lt;/strong&gt; in all the bits of the section. &lt;/p&gt;

&lt;p&gt;Putting this code into &lt;code&gt;C&lt;/code&gt; syntax it's straightforward&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Given an array of 64-bit numbers this method&lt;/span&gt;
&lt;span class="c1"&gt;// returns the index plus the 8-bit section where it's zero.&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xff000000000000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xff&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xff0000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xff000000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xff00000000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xff0000000000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this, we can give our formal description of what the function does.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Formal description&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In case any of these operations it's &lt;strong&gt;0&lt;/strong&gt; we return &lt;code&gt;index + 8-bit section that it's 0&lt;/code&gt;. For example, when we evaluate &lt;code&gt;test dh, 0xff&lt;/code&gt; we are checking if &lt;strong&gt;8-bit&lt;/strong&gt; section at position &lt;strong&gt;1&lt;/strong&gt; is &lt;strong&gt;0&lt;/strong&gt;. Keep in mind that we are dealing with &lt;strong&gt;64-bit numbers&lt;/strong&gt;, so each one of them has an 8 &lt;strong&gt;8-bit&lt;/strong&gt; section.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;According to the author of the challenge, this is&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is another implementation of a well-known library function...&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For me doesn't ring a bell at the moment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional questions
&lt;/h2&gt;

&lt;p&gt;The challenge comes with three questions&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The code may crash under some specific circumstances. Which are...?&lt;/li&gt;
&lt;li&gt;The code can be easily optimized using SSEx. How?&lt;/li&gt;
&lt;li&gt;The code will not work correctly on big-endian architectures. How to fix it?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The &lt;strong&gt;first question&lt;/strong&gt; it's easy I would say, the main problem I see with this code is the stop condition of the loop. If we pass an array with all elements equal &lt;code&gt;0x1ffffffffffffff&lt;/code&gt; the loop won't finish. The problem is that this number, &lt;code&gt;0x1ffffffffffffff&lt;/code&gt;, is a &lt;strong&gt;57-bit&lt;/strong&gt; number with all its bits equal &lt;strong&gt;1&lt;/strong&gt;. Then none of the current stop conditions, &lt;code&gt;arr[i] &amp;amp; constant = 0&lt;/code&gt;, will be met. This will have a consequence when we reach the next element of the array we will be out of bounds in the array.&lt;/p&gt;

&lt;p&gt;For the rest of the questions, I have no response really.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I will come back to this challenge later, but for now, it's okay, I'm happy with what I managed to infer.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Challenge RE #22</title>
      <dc:creator>Gealber Morales</dc:creator>
      <pubDate>Thu, 28 Sep 2023 09:44:48 +0000</pubDate>
      <link>https://forem.com/gealber/challenge-re-22-38hn</link>
      <guid>https://forem.com/gealber/challenge-re-22-38hn</guid>
      <description>&lt;p&gt;In this article let's dive into the &lt;a href="https://challenges.re/22/"&gt;#22 RE Challenge&lt;/a&gt;. As in previous challenges we have assembly code examples that we have to understand and infer what it's doing. Sometimes this process it's straightforward, if you understand assembly language, of course, other times it's hard as fuck like in &lt;a href="https://challenges.re/19/"&gt;Challenge #19&lt;/a&gt;...I haven't forgotten this baby, going back to you another day. Enough of dirty talk with programming challenges Gealber. Back to our goal here, the assembly code can be found in &lt;a href="https://challenge.re/22"&gt;challenge.re&lt;/a&gt;, take a look there's a fair amount of challenges there.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note&lt;/em&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I know we programmers like a lot the Ctr+C and Ctr+V combination keys, but I don't recommend you to do so with this code here. The main point of these challenges is to understand what this code does, but that doesn't mean that all the code here runs smoothly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Analysis
&lt;/h2&gt;

&lt;p&gt;The first two things that you should identify in this code, we have two functions here, &lt;code&gt;f1&lt;/code&gt; and &lt;code&gt;f2&lt;/code&gt;. Given that &lt;code&gt;f1&lt;/code&gt; has calls to &lt;code&gt;f2&lt;/code&gt;,  we can assume that this is our main entry point. Let's start with this.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;f1&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;;; SIGNATURE OF F1
;; ARGS: rdi, esi, edx
;; void f1(int *arr,int start,int end);
f1:
        push    r13
        push    r12
        ;; r12d
        mov     r12d, edx
        push    rbp
        ;; rbp
        mov     rbp, rdi
        push    rbx
        ;; ebx
        mov     ebx, esi
        push    rcx

.L12:
        ;; ebx &amp;gt;= r12d (two numbers, then)
        ;; start &amp;gt;= end
        cmp     ebx, r12d
        jge     .L10

        ;; copy back original parameters
        ;; to their original registers
        ;; to pass them as argument for f2
        ;; from this we know that rdi is an array of integers as well
        ;; f2(arr, start, end)
        mov     esi, ebx
        mov     edx, r12d
        mov     rdi, rbp
        call    f2

        ;; end arg
        lea     edx, [rax-1]
        mov     r13d, eax
        ;; start arg
        mov     esi, ebx
        ;; arr
        mov     rdi, rbp
        lea     ebx, [r13+1]

        call    f1

        jmp     .L12
.L10:
        pop     rax
        pop     rbx
        pop     rbp
        pop     r12
        pop     r13
        ret
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looking at this code, you can notice first that &lt;code&gt;f1&lt;/code&gt; calls itself, so it's a recursive function. Also, we have a &lt;strong&gt;loop&lt;/strong&gt; which stops condition it's that &lt;code&gt;ebx &amp;gt;= r12d&lt;/code&gt;. Putting this into C code, we have&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;f1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;f1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I renamed the parameters to something meaningful. This is one of the most painful things of assembly, you have just registers with weird names :).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;f2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now &lt;code&gt;f2&lt;/code&gt;, this is the tricky one here I won't explain the whole code only the most tricky part. The signature we already know it from  &lt;code&gt;f1&lt;/code&gt;, we will receive an &lt;strong&gt;array of ints&lt;/strong&gt; with and two &lt;strong&gt;ints&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;xor     r11d, ebx
;; arr[i+1] = r11d;
mov     DWORD PTR [rcx+4+r8], r11d
;; r11d = arr[j-1] ^ arr[i+1] ^ arr[j-1] = arr[i+1];
xor     r11d, DWORD PTR [r9]
;; arr[j-1] = arr[i+1]
mov     DWORD PTR [r9], r11d
;; arr[i+1] ^= arr[i+1];
xor     DWORD PTR [rcx+4+r8], r11d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This was a real pain because if it was with normal variables you would know what it does. Look this is the equivalent of normal C code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;^=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;^=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;^=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;😅 not so obvious either, but if you take into consideration that &lt;strong&gt;XOR&lt;/strong&gt; it's a &lt;strong&gt;commutative&lt;/strong&gt; operation you can figure out that this is a swap of variables, but without using a temporary variable. Actually, it's called &lt;a href="https://en.wikipedia.org/wiki/XOR_swap_algorithm"&gt;XOR Swap&lt;/a&gt;, please don't use this to swap your variables. First, this is not intuitive, and also when you provide equal variables, you will end up with values &lt;strong&gt;0&lt;/strong&gt; on both variables. Even if you put an &lt;strong&gt;if statement&lt;/strong&gt; to discard this case, will run slower than the normal temporary approach.&lt;/p&gt;

&lt;p&gt;This is the most tricky part of this function and the two loops it has inside as well. I managed to get this into C code, would be something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;f2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;esi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;esi&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;esi&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;esi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="n"&gt;swap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;swap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note&lt;/em&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Let me put this here again. This code it's my &lt;strong&gt;literal translation of the assembly code&lt;/strong&gt;, so I might be wrong, please don't copy this as an implementation of the algorithm &lt;strong&gt;I think it is&lt;/strong&gt;. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When you combine these two functions we have what seems to be a recursive sorting algorithm. If you take a look at &lt;code&gt;f2&lt;/code&gt; you can notice that it's quite similar to the partition process in &lt;a href="https://en.wikipedia.org/wiki/Quicksort"&gt;quick sort algorithm&lt;/a&gt;. This is my guess.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Formal description&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Performs quick sort on an array.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The point of reversing is not to reverse detail by detail each instruction. Instead, is to get a broad idea of what the program does.&lt;/p&gt;

</description>
      <category>reverseengineering</category>
      <category>assembly</category>
      <category>c</category>
    </item>
  </channel>
</rss>
