Skip to content

Commit

Permalink
Deployed 002af7e with MkDocs version: 1.5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
M0rtale committed Feb 10, 2024
1 parent 9fbabdc commit 8ada1e1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
28 changes: 14 additions & 14 deletions reverse-engineering/what-is-assembly-machine-code/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3268,7 +3268,7 @@


<h1 id="assemblymachine-code">Assembly/Machine Code</h1>
<p>Machine Code or Assembly is code which has been formatted for direct execution by a CPU. Machine Code is the why readable programming languages like C, when compiled, cannot be reversed into source code (well <a href="">Decompilers</a> can sort of, but more on that later).</p>
<p>Machine Code or Assembly is code which has been formatted for direct execution by a CPU. Machine Code is the reason why readable programming languages like C, when compiled, cannot be reversed into source code (well <a href="">Decompilers</a> can sort of, but more on that later).</p>
<h2 id="from-source-to-compilation">From Source to Compilation</h2>
<p><a href="https://godbolt.org">Godbolt</a> shows the differences in machine code generated by various compilers.</p>
<p>For example, if we have a simple C++ function:</p>
Expand All @@ -3284,7 +3284,7 @@ <h2 id="from-source-to-compilation">From Source to Compilation</h2>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>We can see the compilation results in some verbose instrucitons for the CPU:</p>
<p>We can see the compilation results in some verbose instructions for the CPU:</p>
<div class="highlight"><pre><span></span><code>.LC0:
.string &quot;/etc/passwd&quot;
main:
Expand Down Expand Up @@ -3319,30 +3319,30 @@ <h2 id="from-source-to-compilation">From Source to Compilation</h2>
leave
ret
</code></pre></div>
<p>This is a one way process for compiled languages as there is no way to generate source from machine code. While the machine code may seem unintelligble, the extremely basic functions can be interpreted with some practice.</p>
<p>This is a one way process for compiled languages as there is no way to generate source from machine code. While the machine code may seem unintelligible, the extremely basic functions can be interpreted with some practice.</p>
<h2 id="x86-64">x86-64</h2>
<p>x86-64 or amd64 or i64 is a 64-bit Complex Instruction Set Computing (CISC) architecture. This basically means that the registers used for this architecture extend an extra 32-bits on Intel's x86 architecture. CISC means that a single instruction can do a bunch of diferent things at once such as memory accesses, register reads, etc. It is also a variable-length instruction set which means diferent instructions can be diferent sizes ranging from 1 to 16 bytes long. And finally x86-64 allows for multi-sized register access which means that you can access certain parts of a register which are diferent sizes.</p>
<p>x86-64 or amd64 or i64 is a 64-bit Complex Instruction Set Computing (CISC) architecture. This basically means that the registers used for this architecture extend an extra 32-bits on Intel's x86 architecture. CISC means that a single instruction can do a bunch of different things at once, such as memory accesses, register reads, etc. It is also a variable-length instruction set, which means different instructions can be different sizes ranging from 1 to 16 bytes long. And finally x86-64 allows for multi-sized register access, which means that you can access certain parts of a register which are different sizes.</p>
<h3 id="x86-64-registers">x86-64 Registers</h3>
<p>x86-64 registers behave similarly to other architectures. A key component of x86-64 registers is multi-sized access which means the register RAX can have its lower 32 bits accessed with EAX. The next lower 16 bits can be accessed with AX and the lowest 8 bits can be accessed with AL which allows for the compuler to make optimizations which boost program execution.
<p>x86-64 registers behave similarly to other architectures. A key component of x86-64 registers is multi-sized access which means the register RAX can have its lower 32 bits accessed with EAX. The next lower 16 bits can be accessed with AX and the lowest 8 bits can be accessed with AL which allows for the compiler to make optimizations which boost program execution.
<img alt="Multi-access Register" src="../images/multi-access-register.png" /></p>
<p>x86-64 has plenty of registers to use including rax, rbx, rcx, rdx, rdi, rsi, rsp, rip, r8-r15, and more! But some registers serve special purposes.</p>
<p>x86-64 has plenty of registers to use, including rax, rbx, rcx, rdx, rdi, rsi, rsp, rip, r8-r15, and more! But some registers serve special purposes.</p>
<p>The special registers include:
- RIP: the instruction pointer
- RSP: the stack pointer
- RBP: the base pointer</p>
<h3 id="instructions">Instructions</h3>
<p>An <strong>instruction</strong> represents a single operation for the CPU to perform.</p>
<p>There are diferent types of instructions including:</p>
<p>There are different types of instructions including:</p>
<ul>
<li>Data movement: <code>mov rax, [rsp - 0x40]</code></li>
<li>Arithmetic: <code>add rbx, rcx</code></li>
<li>Control-flow: <code>jne 0x8000400</code></li>
</ul>
<p>Because x86-64 is a CISC architecture, instructions can be quite complex for machine code such as <code>repne scasb</code> which repeats up to ECX times over memory at EDI looking for NULL byte (0x00), decrementing ECX each byte (Essentially strlen() in a single instruction!)</p>
<p>It is important to remember that an instruction really is just memory, this idea will become useful with Return Oriented Programming or ROP.</p>
<p>Because x86-64 is a CISC architecture, instructions can be quite complex for machine code, such as <code>repne scasb</code> which repeats up to ECX times over memory at EDI looking for a NULL byte (0x00), decrementing ECX each byte (essentially strlen() in a single instruction!).</p>
<p>It is important to remember that an instruction really is just memory; this idea will become useful with Return Oriented Programming or ROP.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Instructions, numbers, strings, everything! Always represented in hex.</p>
<p>Instructions, numbers, strings, everything are always represented in hex!</p>
</div>
<div class="highlight"><pre><span></span><code>add rax, rbx
mov rax, 0xdeadbeef
Expand All @@ -3357,7 +3357,7 @@ <h3 id="examples">Examples</h3>
<ol>
<li><code>mov rax, 0xdeadbeef</code></li>
</ol>
<p>Here the operation <code>mov</code> is moving the "immeadiate" <code>0xdeadbeef</code> into the register <code>RAX</code></p>
<p>Here the operation <code>mov</code> is moving the "immediate" <code>0xdeadbeef</code> into the register <code>RAX</code></p>
<ol>
<li><code>mov rax, [0xdeadbeef + rbx * 4]</code></li>
</ol>
Expand Down Expand Up @@ -3421,15 +3421,15 @@ <h3 id="control-flow">Control Flow</h3>
<li><code>jle &lt;address&gt;</code></li>
<li>etc.</li>
</ul>
<p>They jump if their condition is true, and just go to the next instruction otherwise. These conditionals are checking EFLAGS which are special registers which store flags on certain instructions such as <code>add rax, rbx</code> which sets the o (overflow) flag if the sum is greater than a 64-bit register can hold, and wraps around. You can jump based on that with a <code>jo</code> instruction. The most important thing to remember is the cmp instruction:
<p>They jump if their condition is true, and just go to the next instruction otherwise. These conditionals are checking EFLAGS, which are special registers which store flags on certain instructions such as <code>add rax, rbx</code> which sets the o (overflow) flag if the sum is greater than a 64-bit register can hold, and wraps around. You can jump based on that with a <code>jo</code> instruction. The most important thing to remember is the cmp instruction:
<div class="highlight"><pre><span></span><code>cmp rax, rbx
jle error
</code></pre></div>
This assembly jumps if RAX &lt;= RBX</p>
<h3 id="addresses">Addresses</h3>
<p>Memory acts similarly to a big array where the indices of this "array" are memory addresses. Remember from earlier:</p>
<p><code>mov rax, [0xdeadbeef]</code></p>
<p>The square brackets mean "get the data at this address". This is analagous to the C/C++ syntax: <code>rax = *0xdeadbeef;</code></p>
<p>The square brackets mean "get the data at this address". This is analogous to the C/C++ syntax: <code>rax = *0xdeadbeef;</code></p>



Expand All @@ -3452,7 +3452,7 @@ <h3 id="addresses">Addresses</h3>
<span class="md-icon" title="Last update">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1-2.1-2M12.5 7v5.2l4 2.4-1 1L11 13V7h1.5M11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2v1.8Z"/></svg>
</span>
<span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date">January 26, 2024</span>
<span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date">February 8, 2024</span>
</span>


Expand Down
16 changes: 8 additions & 8 deletions reverse-engineering/what-is-c/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3251,14 +3251,14 @@

<h1 id="the-c-programming-language">The C Programming Language</h1>
<h2 id="history">History</h2>
<p>The C programming language iwas written by Dennis Ritchie in the 1970s while he was working at Bell Labs. It was first used to reimplement the Unix operating system which was purely written in assembly language. At first, the Unix developers were considering using a language called "B" but because B wasn't optimized for the target computer, the C language was created.</p>
<p>The C programming language was written by Dennis Ritchie in the 1970s while he was working at Bell Labs. It was first used to reimplement the Unix operating system which was purely written in assembly language. At first, the Unix developers were considering using a language called "B" but because B wasn't optimized for the target computer, the C language was created.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>C is the letter and the programming language after B!</p>
</div>
<p>C was designed to be close to assembly and is still widely used in lower level programming where speed and control are needed (operating systems, embedded systems). C was also very influential to other programming langauges used today. Notable languages include C++, Objective-C, Golang, Java, JavaScript, PHP, Python, and Rust.</p>
<p>C was designed to be close to assembly and is still widely used in lower level programming where speed and control are needed (operating systems, embedded systems). C was also very influential to other programming languages used today. Notable languages include C++, Objective-C, Golang, Java, JavaScript, PHP, Python, and Rust.</p>
<h2 id="hello-world">Hello World</h2>
<p>C is an ancestor of many other programming languages and if you are familiar with programming, it's likely that C will be at least someewhat familiar.</p>
<p>C is an ancestor of many other programming languages and if you are familiar with programming, it's likely that C will be at least somewhat familiar.</p>
<div class="highlight"><pre><span></span><code><span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;stdio.h&gt;</span>
<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
Expand All @@ -3267,7 +3267,7 @@ <h2 id="hello-world">Hello World</h2>
<span class="p">}</span>
</code></pre></div>
<h2 id="today">Today</h2>
<p>Today C is widely used either as a low level programming langauge or is the base language that other programming languages are implemented in.</p>
<p>Today C is widely used either as a low level programming language or is the base language that other programming languages are implemented in.</p>
<p>While it can be difficult to see, the C language compiles down directly into machine code. The compiler is programmed to process the provided C code and emit assembly that's targetted to whatever operating system and architecture the compiler is set to use.</p>
<p>Some common compilers include:</p>
<ul>
Expand All @@ -3279,7 +3279,7 @@ <h2 id="today">Today</h2>
<p>In regards to CTF, many reverse engineering and exploitation CTF challenges are written in C because the language compiles down directly to assembly and there are little to no safeguards in the language. This means developers must manually handle both. Of course, this can lead to mistakes which can sometimes lead to security issues.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Other higher level langauges like Python manage memory and garbage collection for you. Google Golang was inspired by C but adds in functionality like garbage collection, and memory safety.</p>
<p>Other higher level langauges like Python manage memory and garbage collection for you. Google Golang was inspired by C, but adds in functionality like garbage collection and memory safety.</p>
</div>
<p>There are some examples of famously vulnerable functions in C which are still available and can still result in vulnerabilities:</p>
<ul>
Expand Down Expand Up @@ -3311,7 +3311,7 @@ <h2 id="pointers">Pointers</h2>
<p class="admonition-title">Note</p>
<p>The <code>*</code> character allows us to declare pointer variables but also allows us to access the value stored at a pointer. For example, entering <code>*y</code> allows us to access the 4 value instead of 0x1000.</p>
</div>
<p>Whenever we use the <code>y</code> variable we are using the memory address, but if we use the x variable we use the value stored at the memory address.</p>
<p>Whenever we use the <code>y</code> variable we are using the memory address, but if we use the <code>x</code> variable we use the value stored at the memory address.</p>
<h2 id="arrays">Arrays</h2>
<p>Arrays are a grouping of objects of the same type. They are typically created with the following syntax:</p>
<div class="highlight"><pre><span></span><code><span class="n">type</span><span class="w"> </span><span class="n">arrayName</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="n">arraySize</span><span class="w"> </span><span class="p">];</span>
Expand All @@ -3320,7 +3320,7 @@ <h2 id="arrays">Arrays</h2>
<div class="highlight"><pre><span></span><code><span class="kt">int</span><span class="w"> </span><span class="n">integers</span><span class="p">[</span><span class="w"> </span><span class="mi">10</span><span class="w"> </span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">};</span>
</code></pre></div>
<p>Arrays allow programmers to group data into logical containers.</p>
<p>To access the indiviual elements of an array we access the contents by their "index". Most programming langauges today start counting from 0. So to take our previous example:</p>
<p>To access the individual elements of an array we access the contents by their "index". Most programming langauges today start counting from 0. So to take our previous example:</p>
<div class="highlight"><pre><span></span><code><span class="kt">int</span><span class="w"> </span><span class="n">integers</span><span class="p">[</span><span class="w"> </span><span class="mi">10</span><span class="w"> </span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">};</span>
<span class="cm">/* indexes 0 1 2 3 4 5 6 7 8 9</span>
</code></pre></div>
Expand Down Expand Up @@ -3360,7 +3360,7 @@ <h2 id="memory-management">Memory Management</h2>
<span class="md-icon" title="Last update">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1-2.1-2M12.5 7v5.2l4 2.4-1 1L11 13V7h1.5M11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2v1.8Z"/></svg>
</span>
<span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date">January 26, 2024</span>
<span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date">February 8, 2024</span>
</span>


Expand Down
2 changes: 1 addition & 1 deletion search/search_index.json

Large diffs are not rendered by default.

Binary file modified sitemap.xml.gz
Binary file not shown.

0 comments on commit 8ada1e1

Please sign in to comment.