Answer
stringlengths 38
29k
⌀ | Id
stringlengths 1
5
| CreationDate
stringlengths 23
23
| Body
stringlengths 55
28.6k
| Title
stringlengths 15
145
| Tags
stringlengths 3
68
|
---|---|---|---|---|---|
<p>If the original class was not using StackMapTables, then there should be no problem, assuming you modified the bytecode correctly. Even if it is using them, it is usually possible to just remove them and revert to the old behavior. Assuming that the class does not use <code>invokedynamic</code>, you can just change the version back to <code>49.0</code> and delete the StackMapTable attributes.</p>
<p>Unfortunately, version 51.0 mandates usage of StackMapTable, which is a pain to create when manually editing bytecode. If your class actually is making use of 51.0 features (i.e. <code>invokedynamic</code>) then your only option is to create the appropriate stack frames. In a simple case like this, you could do it by hand, but in general you're best off using a tool to generate the stack frames automatically.</p>
| 3787 | 2014-03-04T19:49:02.390 | <p>I'm using <a href="http://dirty-joe.com/" rel="nofollow">dirtyJOE</a> to edit a method of a class file.</p>
<p>The original class file had some encryption method calls and such.
I've changed the byte-code of the method ldc (byte-code: <code>12 1E</code>)
to load true and return(byte-code: <code>12 1E</code>)</p>
<p>apparently, Java's verifier is upset with my changes and it complains of verification error:</p>
<p>java.lang.VerifyError: Expecting a stack map frame in method <em>[methodName]</em> at offset 2
at..
at..</p>
<p>I was wondering if there is a way to fool the jvm to think that there is a stack map frame?</p>
<p>thank you </p>
| VerifyError after editingg class file with dirtJOE | |java|byte-code| |
<p>When dealing with multi-threaded program, you may end up getting instructions from different threads. You can for example check the current thread ID to make sure you're getting what you need.</p>
<p>Even if the program itself does not use threads, they may be created by system libraries. One of the most common examples is the <a href="https://stackoverflow.com/questions/12670097/what-is-the-purpose-of-this-mysterious-tppwaiterpthread-thread">TppWaiterpThread</a>. RPC functions also often create threads.</p>
| 3796 | 2014-03-05T15:58:41.157 | <p>In fuzzing applications with Pin (the Pintool source code is hosted <a href="https://github.com/tathanhdinh/PathExplorer/tree/windows_version/version_1" rel="nofollow">here</a>, I am sorry for the self-advertisement), I get a very bizarre situation in tracing executed instructions, the following trace:</p>
<pre><code>...
0x404f94 test edx, edx
0x404f96 jnz 0x404fe1
0x404fe1 pop ebp
0x404f89 rep cmpsb byte ptr [esi], byte ptr [edi]
0x404f8b pop edi
0x404f8c pop esi
0x404f8d jz 0x404f94
0x404f8f sbb edx, edx
...
</code></pre>
<p>is extracted from the execution of wget. The bizarre observation is that the instruction after 0x404f8f is somehow arbitrary, normaly it should be:</p>
<pre><code>0x404f91 sbb edx, 0xffffffff
</code></pre>
<p>but "sometimes" it is:</p>
<pre><code>0x779a015d add esp, 0x4
</code></pre>
<p>which is located in the NtWaitForMultipleObjects. To say "sometimes", I mean that it does not always happen but it happens and that is 100% reproducible.</p>
<p>I still cannot figure out what happened here. First, the observation means that the control-flow of the program has been changed in a unpredictable way. Second, the bizarre instruction located in NtWaitForMultipleObjects, namely it must be in the kernel-space, but here I have observed it in the program (i.e. in the user-space).</p>
<p>I know that this question is quite specific but feel free to request me more information. I really appreciate any help.</p>
| Strange behavior in the reverse execution of traces | |fuzzing|instrumentation| |
<p>Actually you can do the following:</p>
<p>Assuming that you know when and where this instruction is located and where it can appear:</p>
<pre><code>#I didn't check this code, use carefully, beware errors
#You can use idc.FindBinary instead of text search if you know
#how your assembly instruction is encoded
import idautils
import idc
#static nopification of ARM address
def static_nopify_arm(ea):
nop = [0xe1, 0xa0, 0x00, 0x00] # it is a nop encoding taken from wikipedia
for i in range(len(nop)):
idc.PatchByte(ea + i, nop[i])
#searches assembly command by its text
#generally bad idea, but should work
#start and end means search area boundaries
def static_search_text_and_nopify(asmline, start, end):
for h in idautils.Heads(start, end):
disasm = idc.GetDisasm(h)
if asmline == disasm:
static_nopify_arm(h)
#The same with dynamic (memory during debugging)
#Dynamic nopification of ARM address
def dynamic_nopify_arm(ea):
nop = [0xe1, 0xa0, 0x00, 0x00] # it is a nop encoding taken from wikipedia
for i in range(len(nop)):
#I'm not sure that it will work, may be you should do something with memory protection
idc.PatchDbgByte(ea + i, nop[i])
#searches assembly command by its text
#generally bad idea, but should work
#start and end means search area boundaries
#Code should be recognized by IDA as code before running the function
def dynamic_search_text_and_nopify(asmline, start, end):
for h in idautils.Heads(start, end):
disasm = idc.GetDisasm(h)
if asmline == disasm:
dynamic_nopify_arm(h)
</code></pre>
| 3797 | 2014-03-05T17:56:23.717 | <p>These are my first steps in IDA script, so please be kind.</p>
<p>I want to create some sort of script that, every time it finds the following instruction:</p>
<p><code>MOV R1, #0x4D080</code></p>
<p>to automatically replace it with</p>
<p><code>NOP</code></p>
<p>I could do it statically (hex edit), but I'm looking for a way to do it on the fly during dynamic debugging.</p>
<p>Any ideas?</p>
| automatically find and nop an instruction in IDA | |ida|patching|patch-reversing| |
<p>I have been searching for an answer to this question for a while and I know this question is years old but on the off chance someone else comes across this question I will leave my response here. </p>
<p>The above answer is referring to non-canonicalized device paths where as you are asking about <code>\??\</code> not <code>\\?\</code> there may only be a small difference in the question but the answer is completely different.</p>
<p><code>\??</code> is a "fake" prefix which refers to per-user Dos devices, so here is an image that will hopefully help you understand what the process is: <a href="https://i.stack.imgur.com/LOeeO.png" rel="noreferrer"><img src="https://i.stack.imgur.com/LOeeO.png" alt="enter image description here"></a></p>
<p>Some definitions that may help you understand:</p>
<ul>
<li>\Device - Default location for kernel driver Device Objects </li>
<li>\GLOBAL?? - System location for symbolic links to devices including drive letters</li>
<li>\BaseNamedObjects - System location for named resources</li>
<li>\Sessions\X - Directory for the login session X </li>
<li>\Session\0\DosDevices -
Directory for the “Dos Devices” for each logged in user.</li>
</ul>
| 3798 | 2014-03-05T20:16:28.183 | <p>I often come across paths similar to <code>\??\C:\Windows</code> when looking in memory. I have been unable to understand why the double '<code>?</code>' is appended to some paths. My google-fu is failing me with the ability to find a reason for the double '<code>?</code>'.Any assistance would be appreciated.</p>
| \??\C:\ Question Marks in Paths | |windows|memory| |
<p>By far, the established method for binary rewriting is <strong>dynamic</strong> rewriting where the binary is rewritten while being run on real inputs. Think of instrumentation tools like <strong>PIN</strong>, <strong>DynamoRIO</strong> and <strong>Dyninst</strong> and also binary translators like <strong>qemu</strong>. </p>
<p>Static rewriting tools have a fundamental challenge compared to dynamic rewriting which is precise Control Flow Graph recovery. That is, for each basic block in the binary we need to know the set of possible targets of its jump instruction. The difficulty is that binaries have many <strong>indirect</strong> jump instructions. For example, if we face a basic block that ends with <code>bx r3</code> then we need to have a <em>precise</em> and <em>reliable</em> Value Set Analysis (VSA) that can tell us the possible values that <code>r3</code> can take at run-time. Unfortunately, such analysis is, generally, undecidable. However, well-behaving compilers produce binaries that are <em>structured</em> somehow which is a fact that can be useful to a large extent. </p>
<p>Note that the solving CFG recovery problem would allow us to solve the code/data separation problem as a by product. That is, recursive descent disassembly would allow us in that case to perfectly seperate code from data in the code byte stream.</p>
<p>I can refer here to the following paper introduced in last year's USENIX Security:</p>
<blockquote>
<p>Shuai Wang, Pei Wang, Dinghao Wu:
<strong>Reassembleable Disassembling</strong>. USENIX Security 2015: 627-642</p>
</blockquote>
<p>Their tool <code>Uroboros</code> is <strike>not</strike> open source. It's based on iterative linear sweep disassembly using <strong>objdump</strong>. The disassembly technique itself is discussed in an earlier paper. Nonetheless, it provides interesting techniques for static binary rewriting that actually works (or at least that is their claim). They even rewrite the same binary multiple times without breaking it. Finally, note that static binary rewriting is largely inapplicable to binaries with run-time code generation.</p>
<p><strong>Update</strong>:</p>
<p>It seems like many of the shortcomings of <code>Uroboros</code> has been addressed in <code>Ramblr</code> which is discussed here:</p>
<blockquote>
<p>Wang et. al. "<strong>Ramblr: Making Reassembly Great Again</strong>", Proceedings of the Network and Distributed System Security Symposium (NDSS'17). 2017</p>
</blockquote>
<p>Particularly, they mention that their reassembled binaries have no execution overhead or size expansion.</p>
| 3800 | 2014-03-06T00:50:44.160 | <p>I am struggling on this problem for around three months:</p>
<p><em>How to use disassemblers (IDA Pro and others...) to generate re-assemblable asm code and assemble it back</em></p>
<p>My experience is that:</p>
<ol>
<li><p>There is NO tool that can generate re-assemblable asm code on 32-bit x86.</p></li>
<li><p>You need to adjust/heuristically modify the asm code created by IDA Pro to make it re-assemblable.</p></li>
<li><p>It is doable to <strong>automatically</strong> adjust/heuristically modify process on <strong>benign program</strong> (one without obfuscation).</p></li>
<li><p>It is very tedious, and VS compiled PE binary is much more complex than GCC compiled ELF binary.</p></li>
</ol>
<p>So my questions are:</p>
<ol>
<li><p>Why there are not any disassemblers that can generate re-assemblable asm code targeting on <strong>benign program</strong> (one without obfuscation) ?</p></li>
<li><p>If I want to implement such a tool (without the help of IDA Pro, sketching from the beginning), is it possible?</p></li>
<li><p>Are there any other concerns related to this that I may have missed?</p></li>
</ol>
| Why there are not any disassemblers that can generate re-assemblable asm code? | |ida|disassembly|x86|disassemblers|reassembly| |
<p>You can use <a href="http://mynav.googlecode.com" rel="nofollow">MyNav</a>. In <a href="https://www.hex-rays.com/contests/2010/MyNav/tutorials/exportimport.htm" rel="nofollow">this video</a> you can check how it can be done with this tool.</p>
| 3806 | 2014-03-06T12:44:34.993 | <p>Do anyone know about an automatic way to transfer function names from one IDA file to another while:</p>
<ol>
<li>First IDA file is based on version 1 of the executable.</li>
<li>Second IDA file is based on the updated, version 2, of the same executable.</li>
</ol>
<p>I'm aware of Zynamics BinDiff. I'm looking for alternatives.</p>
| Transfer function names from one IDA DB to another | |ida|code-modeling| |
<p>Just about all the major tools will take source files / directories and produce namespace (a Package), class and interface definitions, attributes, properties, and operation signatures. For a variety of languages.</p>
<p>However, not all dependencies nor all relationships between classes will be discovered as operation definition bodies are not parsed, thus Activity model of an operation cannot be derived ( nor its Activity Diagram ).</p>
<p>Microsoft in Visual Studio 2010 ( I believe, through to 2015 ) included an Operation to Sequence Diagram generator for any operation selected in the editor. It has since been dropped, probably due to it being restricted to managed languages.</p>
<p>To derive State Machine model / diagram would also need to be cognitive of the coding convention originally employed in the source to be meaningful. Perhaps this is an area that Machine Learning might have impact.</p>
<p>Collaboration Diagrams have been dropped from UML.</p>
<p>I'm suprised that parsing op bodies has not been up taken by vendors, EA, MagicDraw, not even Eclipse Papyrus for Java.</p>
<p>Hope that helps,
Regards</p>
| 3808 | 2014-03-06T13:20:10.840 | <p>Are there tools that would create: </p>
<ul>
<li>UML Structural Diagrams from Source Code. </li>
<li>UML Behavioral Diagrams from Binary executing on a arm / x86 system.<br>
Sources would be in C &/ C++ &/ Python.</li>
</ul>
<p>Such tools would boost productivity while re-engineering (understanding existing software and modifying it.) on a Linux platform.</p>
| Re-engineering to create UML Diagrams from Source and Binary | |tools|static-analysis|dynamic-analysis|processhacker| |
<p>If you just want the answer without the explanation, scroll to the bottom of this post.</p>
<hr>
<p>The <code>[register]</code> notation stands for "<em>take the value stored in <code>register</code> and interpret it as an address</em>". If the addressed entity size is ambiguous, it can be clarified using <code>DWORD PTR [register]</code> for <code>DWORD</code>-sized pointers (and similarly for other pointer sizes).</p>
<blockquote>
<pre><code>mov BL, [EAX]
</code></pre>
</blockquote>
<p>This line treats the value in the <code>EAX</code> register as a pointer to a single byte (the size of <code>BL</code>), reads a byte from that address and stores it in <code>BL</code>. </p>
<blockquote>
<pre><code>inc EAX
</code></pre>
</blockquote>
<p>This line increments the value of <code>EAX</code>, effectively advancing to the next byte.</p>
<blockquote>
<pre><code>mov [EDX], BL
</code></pre>
</blockquote>
<p>This line treats the value in the <code>EDX</code> register as a pointer to a single byte (again, the size of the other operand tells us this), and writes a byte that is stored in <code>BL</code> to that address.</p>
<blockquote>
<pre><code>inc EDX
</code></pre>
</blockquote>
<p>This line increments the value of <code>EDX</code>, advancing to the next byte.</p>
<p>With all this information, we can see that this sequence basically copies a byte from one address to another. Most likely it is used in a loop such as string copy or memory copy. If there's a line similar to <code>test BL, BL</code> afterwards to determine if the copied byte was NULL, it's most likely a string copy; if there's a length/address check instead - it's probably a memory/buffer copy that works on a specified amount of bytes.</p>
<hr>
<p>In C parlance, this can be represented as:</p>
<pre><code>char t; // BL
char *src; // EAX
char *dst; // EDX
// initialize src and dst here
t = *src;
++src;
*dst = t;
++dst;
</code></pre>
<p>Or, as K&R put it ever so tersely:</p>
<pre><code>*dst++ = *src++;
</code></pre>
| 3811 | 2014-03-06T16:54:46.473 | <p>I have the following disassembly:</p>
<pre><code>mov BL, [EAX]
inc EAX
mov [EDX], BL
inc EDX
</code></pre>
<p>I could see this being the result of:</p>
<pre><code>uint8_t foo = bar;
++bar;
uint8_t tmp = foo;
++foo;
</code></pre>
<p>But this assumes C99. Is there some other C construct that could produce the code above?</p>
| What C construct could generate this assembly sequence? | |disassembly|x86|static-analysis|c| |
<h2>Getting the entrypoint</h2>
<p>If you have no useful symbol, you first need to find the entrypoint of the executable. There are several ways to do it (depending on the tools you have or the tools you like the best):</p>
<ol>
<li><p>Using <code>readelf</code></p>
<pre><code>$> readelf -h /bin/ls
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x40489c
Start of program headers: 64 (bytes into file)
Start of section headers: 108264 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 9
Size of section headers: 64 (bytes)
Number of section headers: 27
Section header string table index: 26
</code></pre>
<p>So, the entrypoint address is <code>0x40489c</code>.</p></li>
<li><p>Using <code>objdump</code></p>
<pre><code>$> objdump -f /bin/ls
/bin/ls: file format elf64-x86-64
architecture: i386:x86-64, flags 0x00000112:
EXEC_P, HAS_SYMS, D_PAGED
start address 0x000000000040489c
</code></pre>
<p>Again, the entrypoint is <code>0x000000000040489c</code>.</p></li>
<li><p>Using <code>gdb</code></p>
<pre><code>$> gdb /bin/ls
GNU gdb (GDB) 7.6.2 (Debian 7.6.2-1)
...
Reading symbols from /bin/ls...(no debugging symbols found)...done.
(gdb) info files
Symbols from "/bin/ls".
Local exec file:
`/bin/ls', file type elf64-x86-64.
Entry point: 0x40489c
0x0000000000400238 - 0x0000000000400254 is .interp
0x0000000000400254 - 0x0000000000400274 is .note.ABI-tag
0x0000000000400274 - 0x0000000000400298 is .note.gnu.build-id
0x0000000000400298 - 0x0000000000400300 is .gnu.hash
0x0000000000400300 - 0x0000000000400f18 is .dynsym
0x0000000000400f18 - 0x00000000004014ab is .dynstr
0x00000000004014ac - 0x00000000004015ae is .gnu.version
0x00000000004015b0 - 0x0000000000401640 is .gnu.version_r
0x0000000000401640 - 0x00000000004016e8 is .rela.dyn
0x00000000004016e8 - 0x0000000000402168 is .rela.plt
0x0000000000402168 - 0x0000000000402182 is .init
0x0000000000402190 - 0x00000000004028a0 is .plt
0x00000000004028a0 - 0x0000000000411f0a is .text
0x0000000000411f0c - 0x0000000000411f15 is .fini
0x0000000000411f20 - 0x000000000041701c is .rodata
0x000000000041701c - 0x0000000000417748 is .eh_frame_hdr
...
</code></pre>
<p>Entrypoint is still <code>0x40489c</code>.</p></li>
</ol>
<h2>Locating the <code>main</code> procedure</h2>
<p>Once the entrypoint is known, you can set a breakpoint on it and start looking for the <code>main</code> procedure. Because, you have to know that all the programs will start by a <code>_start()</code> procedure in charge of initializing the memory for the process and loading the dynamic libraries. In fact, this first procedure is a convention in the Unix World.</p>
<p>What exactly does this initialization procedure is quite tedious to follow and, most of the time, of no interest at all to understand your program. The <code>main()</code> procedure will only start after all the memory is set-up and ready to go. </p>
<p>Lets see how to do that (I assume that the executable has been compile with <code>gcc</code>):</p>
<pre><code>(gdb) break *0x40489c
Breakpoint 1 at 0x40489c
(gdb) run
Starting program: /bin/ls
warning: Could not load shared library symbols for linux-vdso.so.1.
Breakpoint 1, 0x000000000040489c in ?? ()
</code></pre>
<p>Okay, so we stopped at the very beginning of the executable. At this time, nothing is ready, everything need to be set-up. Let see what are the first steps of the executable:</p>
<pre><code>(gdb) disas 0x40489c,+50
Dump of assembler code from 0x40489c to 0x4048ce:
=> 0x000000000040489c: xor %ebp,%ebp
0x000000000040489e: mov %rdx,%r9
0x00000000004048a1: pop %rsi
0x00000000004048a2: mov %rsp,%rdx
0x00000000004048a5: and $0xfffffffffffffff0,%rsp
0x00000000004048a9: push %rax
0x00000000004048aa: push %rsp
0x00000000004048ab: mov $0x411ee0,%r8
0x00000000004048b2: mov $0x411e50,%rcx
0x00000000004048b9: mov $0x4028c0,%rdi
0x00000000004048c0: callq 0x4024f0 <__libc_start_main@plt>
0x00000000004048c5: hlt
0x00000000004048c6: nopw %cs:0x0(%rax,%rax,1)
End of assembler dump.
</code></pre>
<p>What follow the <code>hlt</code> is just rubbish obtained because of the linear sweep performed by <code>gdb</code>. So, just ignore it. What is relevant is the fact that we are calling <code>__libc_start_main()</code> (I won't comment on the <code>@plt</code> because it would drag us out of the scope of the question). </p>
<p>In fact, the procedure <code>__libc_start_main()</code> initialize the memory for a process running with the <code>libc</code> dynamic library. And, once done, jump to the procedure located in <code>%rdi</code> (which usually is the <code>main()</code> procedure). See the following picture to have a global view of what does the <code>__libc_start_main()</code> procedure [<a href="http://dbp-consulting.com/tutorials/debugging/linuxProgramStartup.html" rel="noreferrer">1</a>]</p>
<p><a href="https://i.stack.imgur.com/4S3MC.png" rel="noreferrer"><img src="https://i.stack.imgur.com/4S3MC.png" alt="ELF complete callgraph"></a></p>
<p>So, indeed, the address of the <code>main()</code> procedure is at <code>0x4028c0</code>. Let disassemble a few instructions at this address:</p>
<pre><code>(gdb) x /10i 0x4028c0
0x4028c0: push %r15
0x4028c2: push %r14
0x4028c4: push %r13
0x4028c6: push %r12
0x4028c8: push %rbp
0x4028c9: mov %rsi,%rbp
0x4028cc: push %rbx
0x4028cd: mov %edi,%ebx
0x4028cf: sub $0x388,%rsp
0x4028d6: mov (%rsi),%rdi
...
</code></pre>
<p>And, if you look at it, this is indeed the <code>main()</code> procedure. So, this where to really start the analysis.</p>
<h2>Words of warning</h2>
<p>Even if this way of looking for the <code>main()</code> procedure will work in most the cases. You have to know that we strongly rely on the following hypothesis:</p>
<ol>
<li><p>Programs written in pure assembly language and compiled with <code>gcc -nostdlib</code> (or directly with <code>gas</code> or <code>nasm</code>) won't have a first call to <code>__libc_start_main()</code> and will start straight from the entrypoint. Therefore, for these programs, the <code>_start()</code> procedure is the <code>main()</code> procedure. In fact, it is important to understand that the <code>main()</code> procedure is just a convention introduced by the C language as the first function (written by the programmer) to be run in the program. Of course, you can find this convention replicated in many other languages such as Java, C++, and others. But, all these languages derive from C.</p></li>
<li><p>We also strongly rely on a knowledge on the way <code>__libc_start_main()</code> works. And, how this procedure has been designed by the <code>gcc</code> team. So, if the program you are analyzing has been compiled with another compiler, you may have to investigate a bit further about this compiler and how it perform the set-up of the memory before running the <code>main()</code> procedure.</p></li>
</ol>
<p>Anyway, you should now be able to track down a program with no symbol at all if you read this answer carefully.</p>
<p>Finally, you can find an excellent summary about the starting of an executable by reading "<a href="http://dbp-consulting.com/tutorials/debugging/linuxProgramStartup.html" rel="noreferrer">Linux x86 Program Start Up or - How the heck do we get to <code>main()</code>?</a>" by Patrick Horgan.</p>
| 3815 | 2014-03-07T06:55:02.097 | <p>I'm a newbie and just got into RE.
I got a ELF 64-bit LSB executable, x86-64. I'm trying to reverse it.
First I tried to set a break point on line 1 using</p>
<pre><code>gdb ./filename
break 1
</code></pre>
<p>The gdb says</p>
<pre><code>No symbol table is loaded. Use the "file" command.
</code></pre>
<p>OKie so gave out file command</p>
<pre><code>(gdb) file filename
Reading symbols from /media/Disk/filename...(no debugging symbols found)...done.
</code></pre>
<p>How could a set a break point to see the execution..?</p>
| Reversing ELF 64-bit LSB executable, x86-64 ,gdb | |gdb|elf|x86-64| |
<p>In fact, they probably used the <code>sstrip</code> software from the package <a href="http://www.muppetlabs.com/~breadbox/software/elfkickers.html" rel="nofollow">ElfKicker</a>. According to the <code>sstrip</code> <code>README</code> file:</p>
<blockquote>
<p>sstrip is a small utility that removes the contents at the end of an
ELF file that are not part of the program's memory image.</p>
<p>Most ELF executables are built with both a program header table and a
section header table. However, only the former is required in order
for the OS to load, link and execute a program. sstrip attempts to
extract the ELF header, the program header table, and its contents,
leaving everything else in the bit bucket. It can only remove parts of
the file that occur at the end, after the parts to be saved. However,
this almost always includes the section header table, along with a few
other sections that are not involved in program loading and execution.</p>
<p>It should be noted that most programs that work with ELF files are
dependent on the section header table as an index to the file's
contents. Thus, utilities such as gdb and objdump will often have
limited functionality when working with an executable with no section
header table. Some other utilities may refuse to work with them at
all.</p>
</blockquote>
<p>In fact, <code>sstrip</code> remove all section information from the executable and keep the executable still usable.</p>
<p>But let see the different levels is strip that we can reach.</p>
<h2>No stripping</h2>
<p>Let consider a program (similar to the one looked at in the question) with no stripping a all. </p>
<pre><code>$> objdump -tT ./crackme
./crackme: file format elf32-i386
SYMBOL TABLE:
08048134 l d .interp 00000000 .interp
08048148 l d .note.ABI-tag 00000000 .note.ABI-tag
08048168 l d .note.gnu.build-id 00000000 .note.gnu.build-id
0804818c l d .gnu.hash 00000000 .gnu.hash
080481ac l d .dynsym 00000000 .dynsym
0804822c l d .dynstr 00000000 .dynstr
...
080497dc g .bss 00000000 _end
08048390 g F .text 00000000 _start
080485f8 g O .rodata 00000004 _fp_hw
080497d8 g .bss 00000000 __bss_start
08048490 g F .text 00000000 main
00000000 w *UND* 00000000 _Jv_RegisterClasses
080497d8 g O .data 00000000 .hidden __TMC_END__
00000000 w *UND* 00000000 _ITM_registerTMCloneTable
080482f4 g F .init 00000000 _init
DYNAMIC SYMBOL TABLE:
00000000 DF *UND* 00000000 GLIBC_2.0 strcmp
00000000 DF *UND* 00000000 GLIBC_2.0 read
00000000 DF *UND* 00000000 GLIBC_2.0 printf
00000000 DF *UND* 00000000 GLIBC_2.0 system
00000000 w D *UND* 00000000 __gmon_start__
00000000 DF *UND* 00000000 GLIBC_2.0 __libc_start_main
080485fc g DO .rodata 00000004 Base _IO_stdin_used
</code></pre>
<h2>Stripping with <code>strip</code></h2>
<pre><code>$> strip ./crackme-striped
$> objdump -tT ./crackme-striped
./crackme-striped: file format elf32-i386
SYMBOL TABLE:
no symbols
DYNAMIC SYMBOL TABLE:
00000000 DF *UND* 00000000 GLIBC_2.0 strcmp
00000000 DF *UND* 00000000 GLIBC_2.0 read
00000000 DF *UND* 00000000 GLIBC_2.0 printf
00000000 DF *UND* 00000000 GLIBC_2.0 system
00000000 w D *UND* 00000000 __gmon_start__
00000000 DF *UND* 00000000 GLIBC_2.0 __libc_start_main
080485fc g DO .rodata 00000004 Base _IO_stdin_used
</code></pre>
<p>As you see, the dynamic symbols are still here when <code>strip</code> is applied. The rest is just removed cleanly.</p>
<h2>Stripping with <code>sstrip</code></h2>
<p>Finally, lets take a look at what happen when using <code>sstrip</code>.</p>
<pre><code>$> sstrip ./crackme-sstriped
$> objdump -tT ./crackme-sstriped
./crackme-sstriped: file format elf32-i386
objdump: ./crackme-sstriped: not a dynamic object
SYMBOL TABLE:
no symbols
DYNAMIC SYMBOL TABLE:
no symbols
</code></pre>
<p>As you can notice, all symbols, including dynamic symbols have been removed. In fact, all the symbols pointing towards the PLT are removed and addresses are left as static addresses. Here is an example with the <code>_start</code> procedure preamble, first all the symbols:</p>
<pre><code> 0x8048390 <_start>: xor %ebp,%ebp
0x8048392 <_start+2>: pop %esi
0x8048393 <_start+3>: mov %esp,%ecx
0x8048395 <_start+5>: and $0xfffffff0,%esp
0x8048398 <_start+8>: push %eax
0x8048399 <_start+9>: push %esp
0x804839a <_start+10>: push %edx
0x804839b <_start+11>: push $0x80485e0
0x80483a0 <_start+16>: push $0x8048570
0x80483a5 <_start+21>: push %ecx
0x80483a6 <_start+22>: push %esi
0x80483a7 <_start+23>: push $0x8048490
0x80483ac <_start+28>: call 0x8048380 <__libc_start_main@plt>
0x80483b1 <_start+33>: hlt
</code></pre>
<p>And, then <code>strip</code>ep:</p>
<pre><code>0x8048390: xor %ebp,%ebp
0x8048392: pop %esi
0x8048393: mov %esp,%ecx
0x8048395: and $0xfffffff0,%esp
0x8048398: push %eax
0x8048399: push %esp
0x804839a: push %edx
0x804839b: push $0x80485e0
0x80483a0: push $0x8048570
0x80483a5: push %ecx
0x80483a6: push %esi
0x80483a7: push $0x8048490
0x80483ac: call 0x8048380 <__libc_start_main@plt>
0x80483b1: hlt
</code></pre>
<p>And, finally, the <code>sstrip</code> version:</p>
<pre><code>0x8048390: xor %ebp,%ebp
0x8048392: pop %esi
0x8048393: mov %esp,%ecx
0x8048395: and $0xfffffff0,%esp
0x8048398: push %eax
0x8048399: push %esp
0x804839a: push %edx
0x804839b: push $0x80485e0
0x80483a0: push $0x8048570
0x80483a5: push %ecx
0x80483a6: push %esi
0x80483a7: push $0x8048490
0x80483ac: call 0x8048380
0x80483b1: hlt
</code></pre>
<p>Surprisingly the executable is still functional. Let's compare what ELF headers are left after <code>strip</code> and <code>sstrip</code> (as suggested Igor). First, after a <code>strip</code>:</p>
<pre><code>$> readelf -l crackme-striped
Elf file type is EXEC (Executable file)
Entry point 0x8048390
There are 8 program headers, starting at offset 52
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
PHDR 0x000034 0x08048034 0x08048034 0x00100 0x00100 RWE 0x4
INTERP 0x000134 0x08048134 0x08048134 0x00013 0x00013 RWE 0x1
[Requesting program interpreter: /lib/ld-linux.so.2]
LOAD 0x000000 0x08048000 0x08048000 0x006b4 0x006b4 RWE 0x1000
LOAD 0x0006b4 0x080496b4 0x080496b4 0x00124 0x00128 RWE 0x1000
DYNAMIC 0x0006c0 0x080496c0 0x080496c0 0x000e8 0x000e8 RWE 0x4
NOTE 0x000148 0x08048148 0x08048148 0x00044 0x00044 RWE 0x4
GNU_EH_FRAME 0x000600 0x08048600 0x08048600 0x00024 0x00024 RWE 0x4
GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RWE 0x10
Section to Segment mapping:
Segment Sections...
00
01 .interp
02 .interp .note.ABI-tag .note.gnu.build-id .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rel.dyn .rel.plt .init .plt .text .fini .rodata .eh_frame_hdr .eh_frame
03 .init_array .fini_array .jcr .dynamic .got .got.plt .data .bss
04 .dynamic
05 .note.ABI-tag .note.gnu.build-id
06 .eh_frame_hdr
07
</code></pre>
<p>And, then the version that went through with <code>sstrip</code>:</p>
<pre><code>$> readelf -l ./crackme-sstriped
Elf file type is EXEC (Executable file)
Entry point 0x8048390
There are 8 program headers, starting at offset 52
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
PHDR 0x000034 0x08048034 0x08048034 0x00100 0x00100 RWE 0x4
INTERP 0x000134 0x08048134 0x08048134 0x00013 0x00013 RWE 0x1
[Requesting program interpreter: /lib/ld-linux.so.2]
LOAD 0x000000 0x08048000 0x08048000 0x006b4 0x006b4 RWE 0x1000
LOAD 0x0006b4 0x080496b4 0x080496b4 0x00124 0x00128 RWE 0x1000
DYNAMIC 0x0006c0 0x080496c0 0x080496c0 0x000e8 0x000e8 RWE 0x4
NOTE 0x000148 0x08048148 0x08048148 0x00044 0x00044 RWE 0x4
GNU_EH_FRAME 0x000600 0x08048600 0x08048600 0x00024 0x00024 RWE 0x4
GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RWE 0x10
</code></pre>
<p>As you can see, the name of the sections have also been removed (as announced in README file).</p>
<p>Note that, applying <code>sstrip</code> on an executable that went through <code>upx</code> render the final executable unusable (I tried).</p>
| 3823 | 2014-03-07T21:45:15.150 | <p>I want to find how can I identify calls to shared libraries in GDB only. On a stripped binary, I cannot found the dynamic symbol table:</p>
<pre><code>$> objdump -tT crackme-01
crackme-01: file format elf32-i386
objdump: crackme-01: not a dynamic object
SYMBOL TABLE:
no symbols
DYNAMIC SYMBOL TABLE:
no symbols
</code></pre>
<p>But still the dynamic library resolution is present, for instance before the call to <code>strcmp</code>:</p>
<pre><code>0x08048330 in ?? ()
...
0xb7ff2420 in _dl_runtime_resolve () from /lib/ld-linux.so.2
0xb7fec020 in _dl_fixup () from /lib/ld-linux.so.2
0xb7ff6678 in __x86.get_pc_thunk.bx () from /lib/ld-linux.so.2
0xb7fec033 in _dl_fixup () from /lib/ld-linux.so.2
0xb7fe7600 in _dl_lookup_symbol_x () from /lib/ld-linux.so.2
0xb7fe6df9 in do_lookup_x () from /lib/ld-linux.so.2
0xb7fedb70 in _dl_name_match_p () from /lib/ld-linux.so.2
...
0xb7ff5d74 in strcmp () from /lib/ld-linux.so.2
</code></pre>
<p>My question is how the symbol table is hidden from <code>readelf</code> but still be used during execution ?</p>
| No dynamic symbol table but resolution of method from shared libraries is working | |elf| |
<p>Because I can't comment, I'll try to fix some inaccuracies in pnak4j's answer.</p>
<p><code>dec ebx</code> is really a 1-byte instruction (I don't know why it appears to be 2-bytes). <code>DEC</code> sets the <code>ZF</code> flag accordingly to the result of (<code>ebx-1</code>) when: zero or not zero. Then, <code>JNE</code> does the jump if not zero (<code>JNE</code>/<code>JNZ</code> are the same). <code>JMP</code> is not a conditional jump, therefore it would not make much sense after <code>CMP</code>/<code>TEST</code>. </p>
| 3845 | 2014-03-11T01:16:10.523 | <p>I have been looking at some simple C code and the different output from GCC using different optimization levels.</p>
<p><strong>C code</strong></p>
<pre><code>#include <stdio.h>
int main() {
int i = 0;
while(i<10) {
printf("Hello\n");
i++;
}
i = 0;
while(i<10) {
printf("i: %d\n", i);
i++;
}
</code></pre>
<p>}</p>
<p>When I compile the code using <code>-Os</code> or <code>-O2</code> the first loop works a bit differently. It it decrements instead of incrementing, and it is in two different ways. I am wondering why it decrements instead of incrementing like in the code, and the the small difference between <code>-Os</code> and <code>-O2</code>.</p>
<p><strong>-Os compiled</strong></p>
<pre><code>0x400486 <main+6> mov edi,0x40068c
0x40048b <main+11> call 0x400450 <puts@plt>
0x400490 <main+16> dec ebx
0x400492 <main+18> jne 0x400486 <main+6>
</code></pre>
<p><strong>-O2 compiled</strong></p>
<pre><code>0x400490 <main+16> mov edi,0x40069c
0x400495 <main+21> call 0x400450 <puts@plt>
0x40049a <main+26> sub ebx,0x1
0x40049d <main+29> jne 0x400490 <main+16>
</code></pre>
| GCC Loop optimization | |disassembly| |
<p>since this has nothing to do with original query and<br />
is an experiment of sorts with the labeller windbg extension that i edited in in my first answer<br />
i am adding this as a new answer and not editing my original answer<br />
since the question of performance of AddSyntheticSymbol for bulk Addition Came up i cooked up a small 6 figure long windbg script file that i could use to test the performance</p>
<p>it appears there is a logarithmic increase in the time required to add symbols<br />
if windbg could add 500 symbols in 11 seconds on the first round<br />
it would take 13 seconds for next 500 and<br />
16 seconds for the third round of 500 symbols<br />
i cut off the test when i added the 20 th round of symbols it took about 50 seconds to load the symbols 9500 to 10000</p>
<p>here is the small python script to cook up a windbg scriptfile</p>
<pre><code>buff = []
j=0
k=1
for i in range(0,100000,1):
j=j+1
buff.append("!label str_%s %08x 1 xul\n" % ( str(i).rjust(8,'0') , i ) )
if(j == 500):
buff.append("%s %d symbols added\n" % (".echotime;.echo ",j*k))
j = 0
k = k+1
with open ("lab100k.txt","w") as txt:
txt.writelines(buff)
</code></pre>
<p>this creates a file with 100200 windbg commands that uses !label extcmd</p>
<pre><code>wc -l lab100k.txt
100200 lab100k.txt
grep -c echotime lab100k.txt
200
tail -n 3 lab100k.txt
!label str_00099998 0001869e 1 xul
!label str_00099999 0001869f 1 xul
.echotime;.echo 100000 symbols added
</code></pre>
<p>loaded the label extension and executed this windbg script and cut it off after labelling 10000 address the timeframe isas follows</p>
<pre><code>0:021> $$>a< lab100k.txt
Debugger (not debuggee) time: Tue Sep 22 23:58:52.053 2020
500 symbols added
Debugger (not debuggee) time: Tue Sep 22 23:59:03.468 2020
1000 symbols added
Debugger (not debuggee) time: Tue Sep 22 23:59:16.983 2020
1500 symbols added
Debugger (not debuggee) time: Tue Sep 22 23:59:32.546 2020
2000 symbols added
Debugger (not debuggee) time: Tue Sep 22 23:59:50.225 2020
2500 symbols added
Debugger (not debuggee) time: Wed Sep 23 00:00:10.133 2020
3000 symbols added
Debugger (not debuggee) time: Wed Sep 23 00:00:32.502 2020
3500 symbols added
Debugger (not debuggee) time: Wed Sep 23 00:00:57.303 2020
4000 symbols added
Debugger (not debuggee) time: Wed Sep 23 00:01:23.955 2020
4500 symbols added
Debugger (not debuggee) time: Wed Sep 23 00:01:52.593 2020
5000 symbols added
Debugger (not debuggee) time: Wed Sep 23 00:02:22.930 2020
5500 symbols added
Debugger (not debuggee) time: Wed Sep 23 00:02:55.546 2020
6000 symbols added
Debugger (not debuggee) time: Wed Sep 23 00:03:30.068 2020
6500 symbols added
Debugger (not debuggee) time: Wed Sep 23 00:04:06.521 2020
7000 symbols added
Debugger (not debuggee) time: Wed Sep 23 00:04:45.134 2020
7500 symbols added
Debugger (not debuggee) time: Wed Sep 23 00:05:25.709 2020
8000 symbols added
Debugger (not debuggee) time: Wed Sep 23 00:06:08.456 2020
8500 symbols added
Debugger (not debuggee) time: Wed Sep 23 00:06:53.301 2020
9000 symbols added
Debugger (not debuggee) time: Wed Sep 23 00:07:40.663 2020
9500 symbols added
Debugger (not debuggee) time: Wed Sep 23 00:08:29.967 2020
10000 symbols added
</code></pre>
<p>the result of symbols are as follows</p>
<pre><code>0:021> x xul!str_000100*
50462710 xul!str_00010000 = <no type information>
50462711 xul!str_00010001 = <no type information>
50462712 xul!str_00010002 = <no type information>
50462713 xul!str_00010003 = <no type information>
50462714 xul!str_00010004 = <no type information>
50462715 xul!str_00010005 = <no type information>
50462716 xul!str_00010006 = <no type information>
50462717 xul!str_00010007 = <no type information>
50462718 xul!str_00010008 = <no type information>
50462719 xul!str_00010009 = <no type information>
5046271a xul!str_00010010 = <no type information>
5046271b xul!str_00010011 = <no type information>
5046271c xul!str_00010012 = <no type information>
5046271d xul!str_00010013 = <no type information>
5046271e xul!str_00010014 = <no type information>
5046271f xul!str_00010015 = <no type information>
50462720 xul!str_00010016 = <no type information>
50462721 xul!str_00010017 = <no type information>
50462722 xul!str_00010018 = <no type information>
50462723 xul!str_00010019 = <no type information>
50462724 xul!str_00010020 = <no type information>
50462725 xul!str_00010021 = <no type information>
50462726 xul!str_00010022 = <no type information>
50462727 xul!str_00010023 = <no type information>
50462728 xul!str_00010024 = <no type information>
50462729 xul!str_00010025 = <no type information>
5046272a xul!str_00010026 = <no type information>
5046272b xul!str_00010027 = <no type information>
5046272c xul!str_00010028 = <no type information>
</code></pre>
| 3850 | 2014-03-11T10:45:24.523 | <p>When I have a kernel module without symbols, I'd typically first open it in IDA and give names to some of the subroutines (those I'm interested in).</p>
<p>Since I prefer my kernel debugging with plain WinDbg (and not the IDA-integrated WinDbg), I'd like WinDbg to recognize the names IDA (and me) gave to those addresses. That way, a) I could break on those functions by name, change variables by name, and b) WinDbg's output and views would read better (in stack traces etc.).</p>
<p>Unfortunately, IDA has no "create PDB" feature, and I don't even see a non-PDB way of importing addresses into WinDbg.</p>
<p>Ideas, anyone?</p>
| Importing list of functions and addresses into WinDbg | |ida|windbg|symbols| |
<blockquote>
<p>how does recv know that the setsockopt locates at this address?</p>
</blockquote>
<p>The relative virtual address of the function <code>setsockopt</code> is in <code>WSOCK32.dll</code>'s Export Table, so your disassembler/debugger was smart enough to match the virtual address <code>0x74381000</code> to the relative virtual address of <code>setsockopt</code>.</p>
<blockquote>
<p>"recv calls always WSARecv"</p>
</blockquote>
<p>That's correct; in Winsock, <code>recv</code> is a wrapper around <code>WSARecv</code>.</p>
| 3854 | 2014-03-12T01:35:57.983 | <p>In tracing how the recv function works on Windows, I observe the following trace:</p>
<pre><code>...
0x743817d2 push eax C:\windows\system32\WSOCK32.dll recv
0x743817d3 push dword ptr [ebp+0x8] C:\windows\system32\WSOCK32.dll recv
0x743817d6 call 0x7438193e C:\windows\system32\WSOCK32.dll recv
0x7438193e jmp dword ptr [0x74381000] C:\windows\system32\WSOCK32.dll setsockopt
0x77287089 mov edi, edi C:\windows\syswow64\WS2_32.dll WSARecv
0x7728708b push ebp C:\windows\syswow64\WS2_32.dll WSARecv
0x7728708c mov ebp, esp C:\windows\syswow64\WS2_32.dll WSARecv
...
</code></pre>
<p>Unfortunately, once again I found it quite strange. First, there is a direct call: </p>
<pre><code>call 0x7438193e
</code></pre>
<p>inside <code>recv</code>. I still do not understand why that works: since the <code>WSOCK32.dll</code> (containing <code>recv</code>) will be loaded "arbitrarily" in the user-space, how does <code>recv</code> guarantee that the <code>setsockopt</code> locates at this address?. </p>
<p>Second, I see nowhere in the application (here it is wget) can modify the memory at <code>0x74381000</code> (that is the target of <code>jmp</code> inside <code>setsockopt</code>), so normally the value at this address is always <code>0x77287089</code> and that means <em><code>recv</code> calls always <code>WSARecv</code></em>(!!!). I doubt that is not true because there is no official document (i.e. MSDN) saying that. </p>
<p>Many thanks for any consideration.</p>
| Directed call inside recv | |binary-analysis|debugging| |
<p>First, you should begin by reading through pat.txt which is included with the IDA flair utilities. It describes the format of .pat files. </p>
<p>Second it would be helpful if you can clarify what you are trying to do. What processor/file type are you working with? In many cases you do not need to directly manipulate .pat files at all since Hex-Rays provides tools to generate .pat files and .sig files from those .pat files. </p>
<p>Third, it is not clear exactly what type of signatures you are trying to work with and why. IDA "startup" signatures are a special form of signature used the when you first create a database for a new binary file. The purpose of startup signatures is to identify startup routines and the compiler used to create the binary. This may in turn trigger the loading of additional library signature files. Library signature files are the ones used to recognize the presence of library code that has been statically linked into a binary. Signatures are usually generated by parsing a copy of the static link libraries that were linked into the binary you are interested in. The most common case for needing to understand the .pat file format is when the parsers that Hex-Rays ships with flair are not capable of parsing the link libraries that you are trying to create patterns from.</p>
<p>This link may also be useful: <a href="https://www.hex-rays.com/products/ida/tech/flirt/in_depth.shtml" rel="nofollow">Hex-Rays FLIRT</a></p>
| 3856 | 2014-03-12T08:51:01.533 | <p>IDA startup signatures are sets of signatures used in FLIRT to detect what library is used in a static linked executable .<br>
I am about to use these signatures to detect which library is used in an executable file .
<br>I must use pattern files in IDA FLAIR in startup folder .<br>but i don't know it's format and what does each field mean (:?)<br>
here are a few sample pattern :</p>
<pre><code>1)8BFF558BECE8........E8........5DC3.............................. 00 0000 0011 :0000 s=A/- ^0006 ___security_init_cookie ^000B ___tmainCRTStartup
2)6A0C68........E8........8BF98BF28B5D0833C0408945E485F6750C3915.. 00 0000 00F6 :0000 o=2:a=108:vc32rtf:l=vc32mfc/vcextra/vc8atl:m=+67^[_DllMain@12]~msmfc2d/~@vc32mfc@; :00E4@ $LN18 :00E3@ $LN25 :00D3@ $LN29 :00D3@ $LN17 ^0003 __sehtable$___DllMainCRTStartup ^0008 __SEH_prolog4 ^001F ___proc_attached ^0037 __pRawDllMain ^0055 __CRT_INIT@12 ^0068 _DllMain@12 ^00DD ___CppXcptFilter ^00F1 __SEH_epilog4 ......0F84C50000008365FC003BF0740583FE02752EA1........85C07408575653FFD08945E4837DE4000F8496000000575653E8........8945E485C00F8483000000575653E8........8945E483FE01752485C07520575053E8........576A0053E8........A1........85C07406576A0053FFD085F6740583FE037526575653E8........85C075032145E4837DE4007411A1........85C07408575653FFD08945E4C745FCFEFFFFFF8B45E4EB1D8B45EC8B088B095051E8........5959C38B65E8C745FCFEFFFFFF33C0E8........C3
</code></pre>
<p>i could 't find any relevant content on the internet.
can any body help me about the format?
<br>
More about my need:
<br>
I want to find library functions in PE files (on my own program not by IDA) and ,as there are a huge amount of FLAIR pattern for all libraries , I cannot load them altogether because of memory and speed issues .<br>
I have also read pat.txt in IDA FLAIR and i know the patterns format but startup signatures format is a bit different and has new parts so I want to know about these new parts like :
<code>
o=2:a=108:vc32rtf:l=vc32mfc/vcextra/vc8atl:m=+67^[_DllMain@12]~msmfc2d/~@vc32mfc@;
</code></p>
| IDA startup signatures format | |ida| |
<p>Yes. In <code>StructB</code>, select the field you want to convert to a sub-structure or create a new field by pressing <kbd>D</kbd>. With the given field selected, press <kbd>Alt</kbd>+<kbd>Q</kbd> and select <code>StructA</code> for the field.</p>
| 3859 | 2014-03-12T21:15:15.260 | <p>I'm looking to define a structure in IDA like:</p>
<pre><code>struct StructA {
int a;
int b;
} StructA;
struct StructB {
StructA a;
int b;
} StructB;
</code></pre>
<p>Can I do this in IDA's structure definition box without having to redefine all the members from <code>StructA</code> in <code>StructB</code>?</p>
| When defining a structure in IDA, can I define a field that is of another struct type? | |ida|struct| |
<p>There is no SPI specification that dictates things like read codes or address lengths, AFAIK; these are chip-specific and have been generally standardized by vendors of SPI EEPROMs and flash chips (though I"m not aware of any formal agreement among vendors).</p>
<p>Most SPI EEPROMs use two bytes to specify the read address, because they are so small that they only need two bytes to address all of the memory on the chip. Most SPI flash chips are larger and use three bytes (they also use the same READ and WRITE codes that SPI EEPROMs do).</p>
<p>So what you are seeing appears to be valid; if I had to guess, I'd say that the chip under the epoxy is an SPI flash chip rather than an EEPROM, but from the standpoint of reading the data from the chip there's little difference, besides the length of the address.</p>
<p>This does not explain why your data appears to be repeating every 0x4000 bytes though. One thing you can try is the RDID command (0x9F) that is implemented by most SPI flash chips (though not EEPROMs to my knowledge). This should return a 3-byte value identifying the manufacturer ID, the device ID and the memory density (aka, the size of the chip). <a href="http://www.mxic.com.tw/Lists/DataSheet/Attachments/1599/MX25L1006E,%203V,%201Mb,%20v1.3.pdf">Here</a> is a datasheet from a typical SPI flash chip which provides more detail on the typical RDID implementation.</p>
<p>If this is an SPI flash chip, I would expect the RDID command to work, and you'll know for sure what the size of the chip is; if it doesn't work, then I would venture to guess that this is an EEPROM chip whose vendor decided (for whatever reason) to use 3-byte addresses instead of 2.</p>
| 3863 | 2014-03-13T06:15:19.727 | <p>I am looking at a SPI EEPROM chip on board which is unfortunately hidden under an epoxy blob. However, I was able to determine the pinout thanks to the silkscreen.</p>
<p>Dumping the EEPROM with a Bus Pirate, I figured that the EEPROM is 16K since the dumped data seems to repeat every 0x4000 bytes.</p>
<p>I hooked up a logic analyzer to the pins and monitored communication with the EEPROM while the device booted. The sniffed communication looks very much like SPI (as it should), however there is a slight discrepancy from the spec I haven't figured out yet.</p>
<p>I've screenshotted three examples of what seems to be a READ operation:</p>
<p><img src="https://i.stack.imgur.com/PFQ2t.png" alt="Screenshot 1">
<img src="https://i.stack.imgur.com/nrxvL.png" alt="Screenshot 2">
<img src="https://i.stack.imgur.com/9cWYB.png" alt="Screenshot 3"></p>
<p>The first byte is 0x03 (READ), followed by three bytes, and a two-byte response on MISO. According to the SPI protocol, a READ opcode should be immediately followed by a two-byte address. Then, data is returned from that address.</p>
<p>However, <em>three</em> bytes follow the READ opcode, not two. Looking at each example, the first byte always seems to be 0x00. The next two bytes may conceivably be the address.</p>
<p>My questions boil down to:</p>
<ul>
<li>Is this normal behavior for SPI chips?</li>
<li>How should this seemingly non-standard communication be interpreted?</li>
<li>Are there more effective ways of decoding this captured traffic into human-readable SPI commands?</li>
</ul>
| Potentially non-standard SPI communication | |hardware|spi| |
<p>Try <code>ctrl-P</code> it will take you to the <code>"Choose function to jump to"</code> window which sounds like what you want.</p>
| 3866 | 2014-03-13T21:18:31.593 | <p>I'm in the IDA View-A within a subroutine. I've identified the subroutine, and now I want to navigate to the subroutine in the functions window so that I can jump around some of the other subroutines in the vicinity rather quickly.</p>
<p>I know that I can search the functions window with <kbd>Alt + T</kbd>, but I was wondering if there was a shortcut to automatically jump to the currently selected subroutine.</p>
<p>There are more than 200,000 subroutines in the functions window, so searching is slow.</p>
<p>Is it possible to navigate directly to the current subroutine, or to navigate directly to a subroutine based on address? If so, how?</p>
| Is it possible/How can I select in the functions window the current subroutine in the IDA View? | |ida| |
<p>Code before <code>This is my string</code> relies on addresses/offsets of data (and possibly code) after <code>This is my string</code>. When you insert the string <code>new</code>, you're effectively shifting the code/data after <code>This is my string</code> to the right by 4 bytes. When code before <code>This is my string</code> tries to access that content, it access the wrong content since the location has been shifted.</p>
| 3872 | 2014-03-14T20:19:20.423 | <p>I created a simple Cocoa app (Mac 64bit) in Xcode, and in it I created a string object, and then outputted the contents of the string in a NSLog statement.</p>
<p><img src="https://i.stack.imgur.com/98uC6.png" alt="enter image description here" /></p>
<p>Then I decided to see if I could modify the contents of the binary (exe) in the .app directory of the application. I used 0xED to change, <em>This is my string.</em> to <em>This is my new string.</em> I did this by typing the word <em>new</em> in the right portion of the 0xED editor.</p>
<p><img src="https://i.stack.imgur.com/6UTQt.png" alt="enter image description here" /></p>
<p>Finally, I saved the file, then tried to launch it, but it appears to crash. The crash report appears somewhat cryptic to me, so I am not exactly sure why the app is crashing.</p>
| Editing a Mach-O x86_64 binary with 0xED results in a app crash | |mach-o|patch-reversing| |
<p>With your cursor on the given instruction, press <kbd>Shift</kbd>+<kbd>F2</kbd> to bring up the IDC window, paste the following script into the script body pane, and press the <kbd>Run</kbd> button in the dialog window.</p>
<pre><code>auto x = Rfirst0(ScreenEA());
while (x != BADADDR)
{
Message("0x%08X\n", x);
x = Rnext0(ScreenEA(), x);
}
</code></pre>
| 3876 | 2014-03-15T09:34:50.660 | <p>How do I list all destinations from relative jump instructions (e.g. the jmpr instruction) in IDA?</p>
| List Relative Jump Destinations in IDA | |ida| |
<p>The general rule of thumb is that every prefix is applied in sequence. <strike>For example, two <code>66</code> prefixes cancel each other, and <code>66 66 66</code> is the same as single <code>66</code>.</strike></p>
<p><strong>EDIT</strong>: it seems I was wrong, and repeated prefixes <a href="http://www.reddit.com/r/ReverseEngineering/comments/20i4l8/what_is_the_proper_way_to_deal_with_an_x8664/cg6dg0v" rel="nofollow">are ignored.</a></p>
<p>The situation becomes somewhat muddled for SSE instructions where some prefixes are mandatory and some are optional, and their order may matter. For example, if both F2 and F3 are used with an SSE instruction, then the last one "wins", and 66 is ignored if other prefixes are present (except for a few instructions like CRC32).</p>
<pre><code>66 f3 f2 0f 59 ff ; mulsd xmm7, xmm7
66 f2 f3 0f 59 ff ; mulss xmm7, xmm7
66 0f 59 ff ; mulpd xmm7, xmm7
f2 66 0f 59 ff ; mulsd xmm7, xmm7
</code></pre>
<p>In general, docs don't describe the corner cases very well. So the best thing to do is to put some bytes into a file, run it on the actual CPU, and observe what happens.</p>
| 3879 | 2014-03-15T20:21:10.273 | <p>Olly, specifically, will only accept the last prefix it finds if there are more than one from the same group. For example</p>
<pre><code>lock rep add dword ptr[eax],eax ; f0 f3 01 00
</code></pre>
<p>becomes</p>
<pre><code>??? ; f0
rep add dword ptr[eax],eax ; f3 01 00
</code></pre>
<p>Page 28 of the Intel architecture manual simply states "it is only useful to include up to one prefix code from each of the four groups ..." but no where does it say it's required to only have, at most, one from each group. Yes, there are cases where some of the prefixes are required, must not be present, or are only valid depending on the instruction, but I'm not referring to any of those scenarios.</p>
<p>I'm choosing to interpret Intel's guidance as meaning "it's valid but there aren't many reasons to do it" and I'll continue to leave the prefix listed with the instruction (again, barring any specific rules that say otherwise.)</p>
<p>Is this the correct way to handle the situation or should I be doing something different?</p>
| What is the proper way to deal with an x86/64 instruction containing multiple prefixes from the same group? | |disassembly|x86|x86-64| |
<p>I think you should read from original file (exe, dll) of exported API function and compare with the data in the loading memory. To avoid the fake result in which the read API functions are also hooked, you should use some tricks to get the original read API function from memory.</p>
<p>Hope this help!</p>
| 3882 | 2014-03-15T23:30:27.723 | <p>One of the many features of Kernel Detective is the possibility to retrieve the original addresses of the native apis functions implemented in the driver win32k.sys and checking if they are hooked. What are the possible ways to achieve the same ? </p>
| How does Kernel Detective check if API functions are hooked? | |kernel-mode| |
<p>Well the good news is that it's barely been obfuscated. I don't see any string encryption, which is the one thing that every single commercial obfuscator does. The only thing I see is basic class/method renaming. Most likely they just ran it through Proguard.</p>
<p>The bad news is that even with weak obfuscation, it's still a pain to read through. (Heck, with a large codebase it's often a pain just to read through the original commented source code!) But there's not much you can do except slog through it. Coming up with meaningful names to rename everything is obviously not something that can be done automatically. This is where the human traditionally steps in.</p>
<p>If they happen to have included an open source library in the renamed part, you can match it up and automatically rename all those parts back to the original, but I don't see any signs of this.</p>
<p>Ultimately, it's just a matter of reverse engineering. At least you're a lot better off than with x86 reverse engineering.</p>
| 3886 | 2014-03-17T02:52:04.340 | <p>I have a web application written in JSP running on a tomcat 6 server that I want to RE. I've decompiled the .jars associated with the web application. The source code is heavily obfuscated, making it very time consuming to understand the execution flow. Since I'm under a time constraint reverse engineering this way isn't feasible. </p>
<p>Is there a way to force the JVM to log all method calls with parameters?
Can this be done through a profiler such as VisualVM?</p>
| Reverse engineering close sourced JSP applets | |debugging|dynamic-analysis|java| |
<p>Libraries like STL or Boost are tricky. Because they're heavily template-based and most of their code is generated at compile time, it's pretty difficult to make FLIRT-style signatures for them. Too much depends on the specific compiler, build options, optimization settings and so on, so unless you match them pretty closely when generating signatures, you're unlikely to get many good hits.</p>
<p>However, you may be able to find some signs of them. For example, the typical <code>std::string</code> implementation in some cases throws exceptions <code>length_error</code> or <code>out_of_range</code>. You might be able to find references to the error text or the exception names. Other than that I think there's not much you can look for besides recognizing a specific implementation from the actual code.</p>
<p>However, since you mention it's an RTOS, I highly doubt it's using STL. In an OS, any non-deterministic behavior is a bad thing, and with STL you can get an exception basically at any time. They may use some limited C++ for better encapsulation but any high-level classes are likely to be custom-made and not from STL or Boost.</p>
| 3889 | 2014-03-17T16:36:44.183 | <p>I have a binary file (actually, an operating system for an ARM embedded device which also contains some high-level apps (hard coded in the user interface)).</p>
<p>I know some parts of the operating system are from C++ code, so it is likely the binary contains the C++ STL.</p>
<p>However, I don't know much about the STL.</p>
<p>Would you have a method to find the address of the STL functions?
(the basic method of searching for the "map", "vector", ... string was unsuccessful and I don't know any specific feature I could search for in this case)</p>
<p>Is there some kind of signature for the STL functions?</p>
<p>Thanks!</p>
<p>Additional informations: I use IDA. I can run the OS with a GDB. I know the address of much of the standard C functions (ctype/stddio/...).</p>
| Find the C++ STL functions in a binary | |ida|binary-analysis|c++|arm|operating-systems| |
<p>It's guaranteed that given enough time and resources anything can be reverse engineered.</p>
| 3891 | 2014-03-17T19:11:51.150 | <p>If I have an unknown file format and</p>
<ul>
<li>someone can still run the program and save any number of files</li>
<li>someone can modify all options of the program individually</li>
<li>it is known that the file format does not use compression</li>
<li>it is known that the file format does not use encryption</li>
<li>it doesn't seem to have a checksum</li>
<li>I have "unlimited" money to pay someone</li>
</ul>
<p>is it then guaranteed that someone can reverse engineer the file format?</p>
| Is it guaranteed that someone can reverse engineer this file format? | |file-format| |
<p>What you see is offsets modified to reflect a changed base address. Anything that's not relative to the base (be it global variables or calls or whatever) needs to be adjusted if the image base changes.</p>
<p>PE files can have a relocation table for that. Can, as in standard .exe files usually don't have it, as it was not necessary until ASLR (random base addresses for modules) came along.</p>
<p>For DLLs the base address is likely to change as it depends on what other files are already loaded so they almost always have a base relocation table that says "If the base address differs from XYZ, add the delta to the following locations".</p>
<p>If you are curios how that base relocation table looks like, I'll recommend checking out the following:</p>
<ul>
<li><p>See the following code implementing a <a href="http://secureimplugin.googlecode.com/svn/trunk/CryptoPP/dllloader.cpp" rel="nofollow">custom PE file loader</a>.</p></li>
<li><p>Search for <code>IMAGE_DIRECTORY_ENTRY_BASERELOC</code> to see code handling the relocation directory.</p></li>
</ul>
| 3895 | 2014-03-18T13:32:35.357 | <p>I'm running a 64-bit MS Windows 7 with all updates installed. About 1 or 2 weeks ago I've noticed that whenever I restart the OS, the virtual memory pages (of whatever process), corresponding to system libraries like <code>ntdll.dll</code> and <code>kernel32.dll</code> are slightly different. </p>
<p>I'm not talking about the base address of the loaded modules, because I know that changes due to ASLR. I'm talking about the actual contents of the loaded modules, which as far as I know was not affected by ASLR implementations on Windows.</p>
<p>To illustrate what I mean, let me show you the following screenshot that compares 2 binary instances of <code>ntdll.dll</code> captured before (top-half) and after (bottom-halt) one OS restart:
<img src="https://i.stack.imgur.com/xHpGR.png" alt="enter image description here"></p>
<p>The picture shows just a small part of <code>ntdll.dll</code> and therefore just a few differences. However, there are more. The size of these DLLs don't change, only some bytes at particular locations.</p>
<p>I obtained the 2 binary instances which are compared in the previous picture using Process Hacker like so:</p>
<ul>
<li>Right-click a process and select <strong>Properties</strong></li>
<li>Go to the <strong>Memory</strong> tab and scroll-down until you find several entries having the name: <em>ntdll.dll: Image (Commit)</em></li>
<li>Double-click one of these entries (I chose the one that had 856kB)</li>
<li>Press the <strong>Save...</strong> button to store the contents to a binary file on the HDD.</li>
</ul>
<p><strong>Question 1:</strong> What is causing these bytes to change? </p>
<ul>
<li><strong>Sub-question 1.1:</strong> Is is some sort of protection mechanism? </li>
<li><strong>Sub-question 1.2:</strong> Was it recently introduced? </li>
<li><strong>Sub-question 1.3:</strong> Can it be disabled?</li>
</ul>
<p><strong>Question 2:</strong> What do these changing bytes represent in the code of the DLLs? </p>
| What changes in MS Windows system libraries after restart? | |windows|dll| |
<p>Last Instruction Opcode is set mainly for the use of exception handlers, but it's set for every non-control FPU instruction (that is, set by loads, stores, compares, etc).</p>
<p>The value is non-zero on my system, but it is different value, and it's not constant.</p>
<p>Olly 1 doesn't request as many context fields as Olly 2 does, and that's part of the reason why the values are different. The context structure initialization leaves some fields unassigned. It's not specific to Olly that some of the bits are set. Also note that the value of (0xd800+)0x7ff does not decode to any meaningful instruction.</p>
<p>In short, not a reliable detection method.</p>
| 3907 | 2014-03-19T12:16:30.563 | <p>While playing around with FPU instructions I discovered an anti-debugging trick for
OllyDbg. I haven't found it in popular references so far.</p>
<p>First of all here it is.</p>
<pre><code>fnsave [esp-100h]
cmp word ptr [esp-0EEh], 07FFh ; 07FFh (all bits set) in Olly 2
; 0000h in Olly 1
; something different otherwise
je @being_debugged
</code></pre>
<p>The code fragment checks the "Last Instruction Opcode" at +0x12. According to Intel manuals it's set if and only if an exception occured.</p>
<p>The value is set only once, it doesn't work when restarting (<<) the program.</p>
<p>But I don't know where the values come from. When Olly calls GetThreadContext() of debuggee, the value is already set. Other debuggers does not seem to behave like this.</p>
<p>Can anyone confirm this? Tested on Win XP SP3, Intel P4.</p>
| OllyDbg FPU anti-debug | |ollydbg|anti-debugging| |
<p>Look in IDA menu: </p>
<p>Edit->Segments->Rebase program.</p>
| 3909 | 2014-03-19T14:29:36.547 | <p>If a binary file is open with ROM starting at 0x10A000, how do I easily change the ROM starting address of the file to 0x109000? I always had to restart IDA to do it. I also tried to "rebase the program using shift delta" but I don't know how to use a negative shift delta, plus it's not automated.</p>
| Rebase program with negative shift delta in IDA | |ida| |
<p>To dump memory to a file, see the <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ee416349%28v=vs.85%29.aspx" rel="nofollow">DbgHelp MiniDumpWriteDump</a> function. You'll get a snapshot of the memory as a dump file (.DMP) which you can then analyze with various tools or by yourself using the <a href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff561475%28v=vs.85%29.aspx" rel="nofollow">DbgEng debugging engine</a>.</p>
| 3915 | 2014-03-19T18:05:42.550 | <p>I've been looking for any Windows functions to view or dump memory, or the process to do this manually. I can not find info on this anywhere online.</p>
<p>How would I get a dump of a process's memory like the one in Olly's memory window?</p>
<p><strong>Edit for clarification:</strong></p>
<p>I wanted to be able to retrieve an address's base page address. The memory map was the best relation I had to this, as it gives page info such as size, starting address, permissions, etc. <code>VirtualQueryEx()</code> solved the problem.</p>
<p>I ended up iterating through the pages until I found one in which my target address fell.</p>
<p>I tend to ask for what I think would be the solution rather than laying out my problem and taking suggestions. </p>
<p>Thank you all for the help.</p>
<pre><code>// Iterate through pages
for(base = NULL;
WINDOWS::VirtualQuery(base, info, sizeof(*info)) == sizeof(*info);
base += info->RegionSize) {
if(p > base && p < base + info->RegionSize) {
found = true;
break;
}
}
</code></pre>
| How does Ollydbg obtain the memory map (alt+m)? | |windows|memory|dumping| |
<p><a href="http://www.google.com/search?q=vmware%20gdb%20stub" rel="nofollow">VMware exposes a GDB stub</a> specifically for this purpose. Using this stub, one can connect GDB (or any GDB front-end) to manipulate the guest OS, as if it were a process.</p>
<ul>
<li><a href="http://wiki.osdev.org/VMware" rel="nofollow">VMware</a> - OSDev Wiki</li>
<li><a href="https://www.hex-rays.com/products/ida/support/tutorials/debugging_gdb_windows_vmware.pdf" rel="nofollow">Debugging Windows kernel under VMWare using
IDA's GDB debugger</a> - HexRays</li>
</ul>
| 3930 | 2014-03-21T16:34:12.243 | <p>I would like to set up a virtual machine monitor using Microsoft Hyper-V, VMware vSphere/ESX, Xen, or any other alternative hypervisor solution that is able to monitor the execution of a guest OS (a VM within the VMM). </p>
<p>In particular, I'd like to be able to view the guest OS' instructions before they are run by the virtual CPU. Additionally, I'd also like to be able to set monitors on some of the guest OS' CPU registers so if they are modified (e.g. on write with a MOV instruction), the guest's state will be suspended at the location where the write occurred (and the rip/eip will point to that instruction).</p>
<p>I've been searching for a VMM solution that exposes a way to accomplish my goals, but I couldn't find anything.</p>
<p>Does anyone know what the best way of approaching such a thing is? Any recommendations on the hypervisor that I should be using? (I'm guessing that Xen would probably be easiest to modify and debug since it's open source)</p>
<p>Thanks a lot.</p>
<p>Cheers,</p>
<hr>
<p><strong>EDIT:</strong> I'll try to explain myself better. I'm attempting to debug code running in kernel-mode (x86-64). Right now, I'm using WinDbg to debug the ring0 code in a virtual machine (VMware). </p>
<p>If I could, I would be more than happy in setting a hardware breakpoint (ba) on a memory location and seeing what reads/writes to it. The problem is that my hardware breakpoints are being disabled by the code running in ring0. Basically, this code is saving the contents of the dr7 register off, clearing it, accessing the memory that I'm watching (the memory I set the hardware breakpoint on), and finally is restoring the dr7 register after access. Thus, my hardware breakpoint is never hit. This means I never figure out where the memory was being manipulated from. Suffice it to say that software breakpoints are not an acceptable alternative. Additionally, I've also tried mucking around with the general-detect bit in dr7 and installed my own trap handler to understand if someone's screwing with my hardware breakpoints. However, this code is quite smart and handles this attempt nicely (re: restores the old hook before accessing any of the debug registers).</p>
<p>I hope you now see my conundrum. I need to be running at a lower level than ring0 to deal with this code running at ring0. The only way to do that is in a hypervisor. </p>
<p>I would like to monitor the contents of say the dr7 register and see what's accessing it (read/writes). If I find the RIP of the faulting code, I'll be able to step through it and understand more of what it's doing.</p>
| Using a VMM/hypervisor to monitor guest OS execution? | |debugging|kernel-mode|driver|virtual-machines|hypervisor| |
<p>IDA don't detect type of packer. Also, signatures are not used. Instead, it analyze PE-header: .idata section, entry point and import entries.
This method is very accurate, has low false positives.</p>
| 3934 | 2014-03-22T12:11:32.157 | <p>So, how does IDA understand that the file was packed by a packer? </p>
<p>How does it distinguishes between different types of packers? </p>
<p>Are they leave some significant signatures or some patterns of byte code?</p>
<p>Or maybe they do modify the header of the file in some way?</p>
| How does IDA understand that the file was packed by packer? | |ida|packers| |
<p>The entrypoint can be found using the info command <code>i?</code> especially the entrypoint info command <code>ie</code></p>
<pre><code>[0x00404890]> ie
[Entrypoints]
vaddr=0x00404890 paddr=0x00004890 baddr=0x00400000 laddr=0x00000000
1 entrypoints
</code></pre>
<p>Alternatively you can use <code>rabin2 -e <file></code>.</p>
| 3938 | 2014-03-24T11:59:50.163 | <p>How do I get to the entry point of a .exe file using radare2?
I tried using aa then pdf@sym.main, but a prompt showed saying "Cannot find function at 0x00000000"</p>
| Getting to the entry point using radare2 | |radare2| |
<p>See <a href="http://pof.eslack.org/2011/02/18/from-apk-to-readable-java-source-code-in-3-easy-steps/">this</a>. You can unzip the classes.dex from your apk file on the mac, and dex2jar should work on a mac as well. </p>
<p>The last part, jd, is a windows program, but they have an online demo <a href="http://jd.benow.ca/">on their project site</a> - click "live demo" in the header, then drag and drop your jar file on the <code>input files</code> box.</p>
<p><strong>Edit</strong>
I just noticed there are download links for jd on mac as well, so you don't need to use the online version. You can even use the jd for eclipse plugin on a mac.</p>
| 3942 | 2014-03-24T15:30:15.400 | <p>I accidentally erased my project from Eclipse, and all I have left is the APK file which I transferred to my phone. Is there a way to reverse the process of exporting an application to the .apk file, so I can get my project back? by using MacOS X ?</p>
| Reverse engineering from an APK file to a project by MAC | |android| |
<p>When looking for files containing executable code in a well known format, you could search using <code>find</code> and <code>file</code>:</p>
<pre><code>find . -type f -print0 | xargs -0 file | grep -i "i386\|x86\|arm\|ar archive"
</code></pre>
<p>This will get you all files which <code>file</code> labeled with the processor name for i386, x86 or arm.</p>
<p>Note that there are many filetypes which <code>file</code> does not recognize.
It will for instance not recognize java jar files, or android apk files as executable.
Nor will it recognize raw firmware images.</p>
<p>What I actually usually do when researching an unknown system:</p>
<ul>
<li>do <code>find . | xargs file</code> to get a large list of everything</li>
<li>then filter out known files, like audio, images, html, text, xml files.</li>
<li>then manually inspect what is left over.</li>
<li><code>file</code> also makes lots of mistakes, i usually get quite some number of files labeled as DOS executable, which aren't, also i often see files mislabeled as DBase.</li>
</ul>
| 3947 | 2014-03-25T05:57:47.163 | <p>Is there a open-source Linux tool / utility for Linux platform that would recursively search for binary files (executable, shared / static objects, etc), in a folder and display?</p>
| Binary file search tools | |tools|linux| |
<p>The Calculator app is stripped as can be seen by running <code>nm</code>.
You will need to find the address of the method using <code>class-dump</code>:</p>
<pre><code>$ class-dump -A /Applications/Calculator.app | grep showAbout
- (void)showAbout:(id)arg1; // IMP=0x0000000100009939
</code></pre>
<p>However as the Calculator application is already running, the address has been slided because of ASLR.
To find the ASLR slide you can use my tool called <a href="https://github.com/Tyilo/get_aslr" rel="nofollow">get_aslr</a>, like so:</p>
<pre><code>$ sudo get_aslr $(pgrep Calculator)
ASLR slide: 0x9508000
</code></pre>
<p>You then add the two numbers together:</p>
<pre><code>0x0000000100009939 + 0x9508000 = 0x109511939
</code></pre>
<p>That is the current address of the <code>showAbout:</code> method.
Now you just need to set the breakpoint in <code>lldb</code>:</p>
<pre><code>b *0x109511939
</code></pre>
<p>And it works!</p>
| 3948 | 2014-03-25T06:00:32.007 | <p>I'm trying to hit a breakpoint that I set in LLDB (CLI), but for whatever reason I'm not hitting my breakpoint. I am messing around with the stock Calculator.app on OS X, and am trying to call / hit my breakpoint when I open the About dialog box of the Calculator.app.</p>
<p>I launch the Calculator.app, then I start lldb from a Terminal window. I find the process of the Calculator.app using ps and grep. I attach to the running process using LLDB. I then issue the <code>continue</code> command in LLDB to allow the Calculator.app to continue running.</p>
<p>Then I set a breakpoint in LLDB when the following method is called, <code>showAbout</code></p>
<p>I type the following command into LLDB,
<code>(lldb) breakpoint set --method showAbout</code></p>
<p>However when I click <code>About Calculator</code> from the menu bar it doesn't halt the program, but rather shows the About dialog box for the Calculator.</p>
| How to set a breakpoint when a method is called using LLDB on OS X? | |debuggers|osx|lldb| |
<p>You can disassemble a DOS 16-bit executable using <a href="http://www.radare.org" rel="noreferrer">radare2</a> as follows:</p>
<p>Example using <code>TEXTINST.EXE</code> from the FreeDOS 1.1 CDROM image:</p>
<pre><code>bash$ radare2 TEXTINST.EXE
SS : 214f
SP : 4c00
IP : 0
CS : 0
NRELOCS: 1
RELOC : 1c
CHKSUM : 0
[0000:0020]> aa
[0000:0020]> pd 10
; [0] va=0x00000020 pa=0x00000020 sz=67272 vsz=67272 rwx=-rwx .text
;-- section..text:
0000:0020 b93b03 mov cx, 0x33b
0000:0023 be7406 mov si, 0x674
0000:0026 89f7 mov di, si
0000:0028 1e push ds
0000:0029 a9b580 test ax, 0x80b5
| 0000:002c 8cc8 mov ax, cs
| 0000:002e 050510 add ax, 0x1005
| 0000:0031 8ed8 mov ds, ax
| 0000:0033 05f010 add ax, 0x10f0
| 0000:0036 8ec0 mov es, ax
(etc)
</code></pre>
<p>If required, you can force the disassembler, etc. to assume DOS as follows:</p>
<pre><code>e asm.arch=x86
e asm.os=dos
</code></pre>
<p>If you think you have a DOS executable and the above doesn't work, there may be something more subtle going on, and you should post a question showing specifically what you tried, what you expect and what is not working. </p>
<p><em>Note: showing what you tried, etc. is more likely to elicit answers on the SE sites...</em></p>
| 3949 | 2014-03-25T07:02:55.783 | <p>How do I specify to radare2 that what I'm disassembling when I know it is a DOS MZ executable?</p>
<p>As it does not auto-detect this for me.</p>
| Disassembling an unknown DOS MZ executable using radare2 | |disassembly|radare2| |
<p>You cannot step into kernel mode from Ollydbg. You need a kernel debugger like <code>windbg</code>, as <code>ollydbg</code> is a user mode debugger. </p>
<p>Since you posed the question, I assume you neither have a kernel debugging connection,
nor the driver where that control code is sent for analyzing it, as answered by Jonathon.</p>
<p>Usage of proper security measures to deal with malware assumed and emphasized from here onward.</p>
<p>I assume the malware is running already as your query states that you are on <code>DeviceIoControl</code>.</p>
<p>I am going to use ollydbg 2.01.</p>
<p>Attach ollydbg to an unknown process </p>
<p>Press <kbd>Ctrl</kbd>+<kbd>G</kbd> and start typing <code>ntdll.ntDeviceIo</code> and the list box will show
<code>ntdll.ntDeviceIoControl</code> now select it and <code>follow label</code>.</p>
<p>Press <kbd>F2</kbd> to set a breakpoint, and <kbd>F9</kbd> to run the attached process.</p>
<pre><code>INT3 breakpoints, item 0
Address = 7C90D27E NtDeviceIoControlFile
Module = ntdll
Status = Active
Disassembly = MOV EAX, 42
Comment = ntdll.NtDeviceIoControlFile(guessed Arg1,Arg2,Arg3,Arg4,Arg5,Arg6,Arg7,Arg8,Arg9,Arg10)
</code></pre>
<p>Ollydbg should break when a control code is sent and stack should look like this:</p>
<pre><code>CPU Stack
Address Value ASCII Comments
0013EF50 [7C801675 u€| ; /RETURN from ntdll.NtDeviceIoControlFile to kernel32.DeviceIoControl+4C
0013EF54 /00000090 ; |Arg1 = 90
0013EF58 |00000000 ; |Arg2 = 0
0013EF5C |00000000 ; |Arg3 = 0
0013EF60 |00000000 ; |Arg4 = 0
0013EF64 |0013EF88 ˆï ; |Arg5 = 13EF88
0013EF68 |83050024 $ ƒ ; |Arg6 = 83050024
0013EF6C |00000000 ; |Arg7 = 0
0013EF70 |00000000 ; |Arg8 = 0
0013EF74 |0013EFEC ìï ; |Arg9 = 13EFEC
0013EF78 |00000004 ; \Arg10 = 4
</code></pre>
<p>From the specs of <code>DeviceIoControlCode</code>, the first argument points to <code>handle</code> and the sixth argument is the <code>control code</code></p>
<p>Having windbg installed can make things easier from here, but we will not use windbg at this moment as it has a steep learning curve.</p>
<p>Open ollydbg handle window and find what does the handle point to (<code>0x90</code> in the above paste) - it points to a device:</p>
<pre><code>Handles, item 9
Handle = 00000090
Type = File (dev) <-------- it is a _FILE_OBJECT
Refs = 2.
Access = 0012019F (FILE_GENERIC_READ|FILE_GENERIC_WRITE)
Tag =
Info =
Translated name = \Device\Dbgv
</code></pre>
<p>Now run <a href="http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx" rel="noreferrer">Process Explorer</a> from <code>SysInternals</code>. Select the unknown process, and press <kbd>Ctrl</kbd>+<kbd>H</kbd> to make the lower pane show handles.</p>
<p>Select the handle <code>90</code>, right click and select <code>properties</code>. <em>Process Explorer</em> will show the address of the <em>Device Object</em> as noted by Ollydbg. This device object is a <em>File Object</em>:</p>
<p><img src="https://i.stack.imgur.com/ZFEXl.png" alt="enter image description here"></p>
<p><code>ollydbg 2.01</code> can show memory above <code>0x7fffffff</code> (kernel mode memory) we will use that feature to find the driver associated with this device </p>
<p>Read about the <a href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff545834.aspx" rel="noreferrer"><code>FILE_OBJECT</code></a> structure relevant to your OS. In Windows XP you will find the address of device object at +4 from file object.</p>
<p>Select the dump window press <kbd>Ctrl</kbd>+<kbd>G</kbd> and type in the address process explorer showed viz </p>
<pre><code>863c87f0 and follow expression
CPU Dump
Address Hex dump ASCII
863C87F0 05 00 70 00|A8 D3 51 86|00 00 00 00|00 00 00 00| p ¨ÓQ†
</code></pre>
<p>So <code>8651d3a8</code> in the paste above points to the device object for this file object.</p>
<p>Read about <a href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff543147.aspx" rel="noreferrer"><code>DEVICE_OBJECT</code></a> structure. In Windows XP, the address of the driver object is at device object + 8. Following there with <kbd>Ctrl</kbd>+<kbd>G</kbd> -> <code>follow expression</code> as done earlier</p>
<pre><code>CPU Dump
Address Hex dump ASCII
8651D3A8 03 00 B8 00|01 00 00 00|48 2C F1 86|00 00 00 00| ¸ H,ñ†
</code></pre>
<p>notice <code>86f12c48</code> is driver object. Read about <a href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff544174.aspx" rel="noreferrer"><code>DRIVER_OBJECT</code></a> structure.</p>
<p><code>+0xc is Driver Start</code> and <code>+0x10 is Driver Size</code> --> <kbd>Ctrl</kbd>+<kbd>G</kbd> to follow:</p>
<pre><code>CPU Dump
Address Hex dump ASCII
86F12C48 04 00 A8 00|A8 D3 51 86|12 00 00 00|00 30 2A A9| ¨ ¨ÓQ† 0*©
86F12C58 00 3D 00 00|48 9C D8 86|F0 2C F1 86|18 00 18 00| = HœØ†ð,ñ†
</code></pre>
<p><code>a92a3000</code> is <code>DriverStart</code> and size of driver is <code>3d00</code>.</p>
<p><kbd>Ctrl</kbd>+<kbd>G</kbd> to follow:</p>
<pre><code>CPU Dump
Address Hex dump ASCII
A92A3000 4D 5A 90 00|03 00 00 00|04 00 00 00|FF FF 00 00| MZ ÿÿ
A92A3010 B8 00 00 00|00 00 00 00|40 00 00 00|00 00 00 00| ¸ @
</code></pre>
<p>The famous <code>IMAGE_DOS_HEADER</code> is noticeable.</p>
<p>Select from <code>a92a3000</code> to <code>a92a6d00</code>, <kbd>Right-Click</kbd> -> <code>Edit</code> -> binary copy and paste the bytes to a new file in any hex editor (HxD for example), and save it as <code>malwaredriver.sys</code> :)</p>
<pre><code>foo:\>dir malwaredriver.sys
26/03/2014 14:04 15,616 malwaredriver.sys
\>set /a 0x3d00
15616
\>file malwaredriver.sys
malwaredriver.sys; PE32 executable for MS Windows (native) Intel 80386 32-bit
:\>
</code></pre>
<p>You can now analyze the driver statically.</p>
<p>Read about <a href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff544174.aspx" rel="noreferrer"><code>DRIVER_OBJECT</code></a> structure carefully to find you can readily know the handler for <a href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff550744.aspx" rel="noreferrer"><code>IRP_MJ_DEVICE_IO_CONTROL</code></a> handler in ollydbg. It would be at <code>+0x70</code> from Driverobject in XP. For this particular driver, the <code>IRP_MJ_DEVICE_IO_CONTROL_HANDLER</code> is at </p>
<pre><code>CPU Dump
Address Value ASCII Comments
86F12CB8 A92A4168 hA*©
</code></pre>
<p>You can follow and disassemble this memory in ollydbg. Select cpu dump press ctrl+g to disassemble an unknown driver </p>
<pre><code>CPU Disasm
Address Command Comments
A92A4168 MOV EDI, EDI ; IRP_MJ_DEVICE_IO_CONTROL_HANDLER(pdevob,pirp)
A92A416A PUSH EBP
A92A416B MOV EBP, ESP
A92A416D SUB ESP, 24
A92A4170 PUSH EBX
A92A4171 PUSH ESI
A92A4172 MOV ESI, DWORD PTR SS:[EBP+0C] ; irp
A92A4175 MOV EAX, DWORD PTR DS:[ESI+60] ; nt!_irp -y Tail.overlay.cur->maj
A92A4178 MOV EDX, DWORD PTR DS:[ESI+0C] ; nt!_irp Tail.overlay.cur->par.DeviceIo.ty
A92A417B XOR EBX, EBX
A92A417D PUSH EDI
A92A417E LEA EDI, [ESI+18]
A92A4181 MOV DWORD PTR DS:[EDI], EBX
A92A4183 MOV DWORD PTR DS:[ESI+1C], EBX
A92A4186 MOVZX ECX, BYTE PTR DS:[EAX]
A92A4189 SUB ECX, EBX
A92A418B JE SHORT A92A41F7 ; case 0 create
A92A418D DEC ECX
A92A418E DEC ECX
A92A418F JE SHORT A92A41CA ; case 2 close
A92A4191 SUB ECX, 0C
A92A4194 JNE A92A42BB ; unhandled
A92A419A MOV ECX, DWORD PTR DS:[EAX+0C] ; ioctl
A92A419D MOV EBX, ECX
A92A419F AND EBX, 00000003
A92A41A2 CMP BL, 3
A92A41A5 JNE SHORT A92A41AC ; buff align
A92A41A7 MOV EBX, DWORD PTR DS:[ESI+3C]
A92A41AA JMP SHORT A92A41AE
A92A41AC MOV EBX, EDX ; inbuff
A92A41AE PUSH DWORD PTR SS:[EBP+8] ; devobj
A92A41B1 PUSH EDI
A92A41B2 PUSH ECX
A92A41B3 PUSH DWORD PTR DS:[EAX+4] ; bufflen
A92A41B6 PUSH EBX
A92A41B7 PUSH DWORD PTR DS:[EAX+8] ; ioctlcode
A92A41BA PUSH EDX
A92A41BB PUSH 1
A92A41BD PUSH DWORD PTR DS:[EAX+18] ; fobj
A92A41C0 CALL A92A3EEC ; actual_handler
</code></pre>
<p>Here the ioctl code is handled:</p>
<pre><code>CPU Disasm
Address Command Comments
A92A3EF8 MOV ESI, DWORD PTR SS:[EBP+24]
A92A3EFB XOR EBX, EBX
A92A3EFD MOV DWORD PTR DS:[ESI], EBX
A92A3EFF MOV DWORD PTR DS:[ESI+4], EBX
A92A3F02 MOV ECX, DWORD PTR SS:[EBP+20]
A92A3F05 MOV EAX, 83050020
A92A3F0A CMP ECX, EAX
A92A3F0C JA A92A40F1 this code will take this path as 24 > 20
</code></pre>
<p>===========</p>
<pre><code>CPU Disasm
Address Hex dump Command Comments
A92A40F1 81F9 24000583 CMP ECX, 83050024
A92A40F7 74 3F JE SHORT A92A4138 path taken
</code></pre>
<p>The control code does this work and sends some 320 on some condition</p>
<pre><code>[code]
CPU Disasm
Address Command Comments
A92A4138 PUSH 4
A92A413A POP EAX
A92A413B CMP DWORD PTR SS:[EBP+1C], EAX
A92A413E JB SHORT A92A4152
A92A4140 MOV ECX, DWORD PTR SS:[EBP+18]
A92A4143 CMP ECX, EBX
A92A4145 JE SHORT A92A4152
A92A4147 MOV DWORD PTR DS:[ECX], 320 <------------
A92A414D MOV DWORD PTR DS:[ESI+4], EAX
A92A4150 JMP SHORT A92A4158
A92A4152 MOV DWORD PTR DS:[ESI], C000000D
A92A4158 MOV AL, 1
A92A415A CALL A92A46D5
A92A415F RETN 24
</code></pre>
<p>The driver analysed here is the driver loaded by dbgview (dbgv.sys) from SysInternals. This control code checks for an incompatible version of driver loaded in memory. Follow through can be practiced with the specific driver and specific version:</p>
<pre><code>(4.75) build time Thu Aug 07 04:51:27 2008
</code></pre>
<h2>EDIT</h2>
<p>With windbg inastalled you can do the same with just few commands as posted below</p>
<pre><code>C:\>tlist | grep -i dbgview
3724 Dbgview.exe DebugView on \\ (local)
C:\>kd -kl -c ".foreach /pS 1 /ps 4 (place { .shell -ci \"!handle 0 3 0n3724 Fi
le \" grep File } ) {dt nt!_FILE_OBJECT -y Dev->Dri->DriverS place};q" | grep -i
+0x00c
+0x00c DriverStart : 0xf7451000 Void
+0x00c DriverStart : 0xf73c5000 Void
+0x00c DriverStart : 0xa87f9000 Void
+0x00c DriverStart : 0xf7451000 Void
+0x00c DriverStart : 0xf7451000 Void
C:\>kd -kl -c ".reload ; lm a (0xf7451000); lm a (0xf73c5000) ; lm a (0xa87f9000);q" | grep -i defer
f7451000 f746f880 ftdisk (deferred)
f73c5000 f73db880 KSecDD (deferred)
a87f9000 a87fcd00 Dbgv (deferred)
C:\>kd -kl -c ".writemem malware.sys a87f9000 a87fcfff ;q" | grep -A 1 lkd
lkd> kd: Reading initial command '.writemem malware.sys a87f9000 a87fcfff ;q'
Writing 4000 bytes........
C:\>file malware.sys
malware.sys; PE32 executable for MS Windows (native) Intel 80386 32-bit
</code></pre>
<p>An explanation for the commands above as follows</p>
<pre><code>kd -kl is local kernel debugging
!handle command has the capability to search for kernel handle
when you input a Pid
The explanation for arguments for this commands are
0 = all handles
3 = default flag provides basic+object information
0n3724 = pid (can be from task manager
File = type of Object to search for
!dt can display memebers of nested structures with ease
-y with dt takes partial inputs (wild card entries )
and displays all matching structure members
and the output is manipulatable with any text / stream handling
software like grep , sed . awk . find etc
</code></pre>
| 3955 | 2014-03-25T16:55:19.460 | <p>I have started reversing this piece of malware. At some point it creates a service and starts it, then immediately it calls the function <code>DeviceIoControl</code> and the malware went from "paused" to "running" under ollydbg. I've searched a little bit, and I understand that this function serves to communicate with the service it just had created.</p>
<p>But how exactly do I reverse it? How do I know what it does? And how can I continue stepping under ollydbg? Or do I have to move to windbg or some other kernel-mode debugger?</p>
| how to reverse DeviceIoControl? | |malware|kernel-mode| |
<p>There's now (from 2020) a plugin for IDA Pro that helps in working with obfuscated binaries. It is called <a href="https://eshard.com/posts/d810_blog_post_1/" rel="nofollow noreferrer">D810: Creating an extensible deobfuscation plugin for IDA Pro</a>.</p>
| 3969 | 2014-03-26T15:31:27.673 | <p>VM packers like Code Virtualizer and VMProtect seem challenging to existing reverse engineering work, especially static approach like IDA Pro.</p>
<p>According to this slides</p>
<p>www.hex-rays.com/products/ida/support/ppt/caro_obfuscation.ppt</p>
<p>from Hex-rays, IDA Pro requires experienced reverse engineer to manually recognize the opcode array and understand the semantic, then decode the bytecode array..</p>
<p>I myself use IDA Pro to deal with simple quicksort program using Code Virtualizer, and I can share two pics.</p>
<p><img src="https://i.stack.imgur.com/vZqaZ.png" alt="enter image description here"></p>
<p>See, I use Code Virtualizer to protect this part and IDA Pro can not go to 0X599050h.</p>
<p><img src="https://i.stack.imgur.com/UPTnc.png" alt="enter image description here"></p>
<p>See, the size of relocation section has a significant growth.</p>
<p>So my questions:</p>
<ol>
<li>Can IDA Pro automatically deal with VM obfuscated binaries?</li>
<li>Any other interesting materials on the state-of-art in this area?</li>
</ol>
<p>Thank you!</p>
| Can IDA Pro automatically deal with VM obfuscated binaries? | |ida|virtual-machines|virtualizers| |
<p>The extra space on the stack is there to support the Edit and Continue functionality and can be eliminated by changing /Zl to /Zi. The saved ebx and initialization of the stack to 0xcc is done by the <a href="http://msdn.microsoft.com/en-us/library/8wtf2dfz.aspx" rel="nofollow noreferrer">/RTC Runtime Checking Option</a>.</p>
<p>There was a <a href="https://stackoverflow.com/questions/2077074/understanding-the-c-function-call-prolog-with-cdecl-on-windows">similar question</a> asked on SO.</p>
<p>The windows example, by the way, is clearly a 32 bit binary. x64 windows calling convention uses RCX, RDX, R8, and R9 as the first 4 integer/pointer arguments (instead of the stack).</p>
| 3971 | 2014-03-26T20:58:54.170 | <p>I was debugging a simple x86-64 program in Visual Studio 2010 and I noticed that the <code>main</code> function preamble is different from the GNU GCC compiled version of the same C program.</p>
<p>To illustrate what I mean here is the C code for the <code>main</code> function:</p>
<pre><code>int main() {
int a,b,c;
a=1;
b=2;
c=proc(a,b);
return c;
}
</code></pre>
<p>The Visual Studio 2010 disassembly of the <code>main</code> function <strong>preamble</strong> for the VC++ version is:</p>
<pre><code>01211410 push ebp
01211411 mov ebp,esp
01211413 sub esp,0E4h
01211419 push ebx
0121141A push esi
0121141B push edi
0121141C lea edi,[ebp-0E4h]
01211422 mov ecx,39h
01211427 mov eax,0CCCCCCCCh
0121142C rep stos dword ptr es:[edi]
</code></pre>
<p>The rest of the function code of the VC++ version is:</p>
<pre><code>0081141E mov dword ptr [a],1
00811425 mov dword ptr [b],2
0081142C mov eax,dword ptr [b]
0081142F push eax
00811430 mov ecx,dword ptr [a]
00811433 push ecx
00811434 call proc (81114Fh)
00811439 add esp,8
0081143C mov dword ptr [c],eax
0081143F mov eax,dword ptr [c]
00811442 pop edi
00811443 pop esi
00811444 pop ebx
00811445 add esp,0E4h
0081144B cmp ebp,esp
0081144D call @ILT+300(__RTC_CheckEsp) (811131h)
00811452 mov esp,ebp
00811454 pop ebp
00811455 ret
</code></pre>
<p>The disassembly of the <code>main</code> function <strong>preamble</strong> for the GCC compiled version is:</p>
<pre><code>00400502 push rbp
00400503 mov rbp,rsp
00400506 sub rsp,0x10
</code></pre>
<p>The rest of the <code>main</code> function code of the GCC version is:</p>
<pre><code>004004e8 mov DWORD PTR [rbp-0xc],0x1
004004ef mov DWORD PTR [rbp-0x8],0x2
004004f6 mov edx,DWORD PTR [rbp-0x8]
004004f9 mov eax,DWORD PTR [rbp-0xc]
004004fc mov esi,edx
004004fe mov edi,eax
00400500 call 0x4004cc <proc>
00400505 mov DWORD PTR [rbp-0x4],eax
00400508 mov eax,DWORD PTR [rbp-0x4]
0040050b leave
0040050c ret
</code></pre>
<p>The same disassembly is given by <code>objdump</code> version 2.22.90.20120924.</p>
<p>I realize that the first 3 instructions for both preambles do the following:</p>
<ol>
<li>Save old EBP (later needed to remove stack frame)</li>
<li>Top of old stack frame becomes EBP of new frame</li>
<li>Reserve space for local variables. The function has 3 integer local variables.</li>
</ol>
<p><strong>Question 1:</strong> What is the purpose of 4th instruction in the VC++ version? I see its saving EBX, but why? It never uses it afterwards.</p>
<p>For the remaining instructions of the VC++ version preamble, I relized that it initializes <code>39h</code> dwords with the value <code>0CCCCCCCCh</code>. Which makes sense because <code>39h * 4h = 0E4h</code>.</p>
<p><strong>Question 2:</strong> Why is this space initialized with the value <code>0CCCCCCCCh</code>? Is this value better than <code>00000000h</code> in some way?</p>
<p><strong>Question 3:</strong> Why does the VC++ version allocate <code>0E4h</code> bytes for 3 local variables? Is this number random? If not, how is it computed?</p>
<p><strong>Question 4:</strong> Is this space used for something else beside local variables? If yes, for what?</p>
| Understanding x86 C main function preamble created by Visual C++ | |windows|decompilation|x86|c| |
<p>In case it is running into this error <code>Received a SIGTRAP: Trace/breakpoint trap</code> even though there are no breakpoints in the program, check the power of Hardware Debugger e.g. J-Link. </p>
<p>In my case, J-Link was powered on but the port wasn't delivering enough power. Changing the port to high-power one fixed this.</p>
| 3974 | 2014-03-27T01:36:42.070 | <p>I am trying to debug an iOS app with gdb and when I hit a breakpoint I get this error, and can not continue.</p>
<pre><code>Program received signal SIGTRAP, Trace/breakpoint trap.
[Switching to process 190 thread 0x6fa7]
0x000ae150 in dyld_stub_pthread_key_create ()
</code></pre>
<p>Does anyone know how I can continue debugging without closing and opening back gdb?</p>
| Can not continue debugging after SIGTRAP | |ida|disassembly|assembly|gdb|ios| |
<p>Thanks to the hints of blabb and Jonathon Reinhart I was able to write a <code>get_thread_start_address()</code> function. It reads the same address used by <code>pthread_start_thread()</code> to call the start routine. In Kernel 3.2.0-4-686-pae this address is <code>GS+0x234</code>. I use <code>ptrace</code> to get the <code>GS register</code> and the actual <code>GS segment address</code>. Here is my code:</p>
<pre><code>#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ptrace.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <asm/ldt.h>
#include <asm/ptrace.h>
int attach(int tid);
int detach(int tid);
void print_error(char* func_name, int errnumber);
int get_gs_register(int tid){
struct user_regs_struct regs;
int ret = ptrace(PTRACE_GETREGS, tid, NULL, &regs);
if(ret == -1){
print_error("PTRACE_GETREGS", errno);
return -1;
}
return regs.xgs;
}
// This is needed to get the actual GS segment address from the GS register value
int get_thread_area_base(tid, gs){
struct user_desc desc;
memset(&desc, 0, sizeof(desc));
int ret = ptrace(PTRACE_GET_THREAD_AREA, tid, gs / LDT_ENTRY_SIZE, &desc);
if(ret == -1){
print_error("PTRACE_GET_THREAD_AREA", errno);
return -1;
}
return desc.base_addr;
}
void* get_start_address(tid, start_address_pointer){
char start_addr_str[4];
int mem_file;
char mem_file_path[255];
snprintf(mem_file_path, sizeof(mem_file_path), "/proc/%i/mem", tid);
mem_file = open(mem_file_path, O_RDONLY);
if(mem_file == -1){
print_error("open()", errno);
return (void*) -4;
}
int ret = lseek(mem_file, start_address_pointer, SEEK_SET);
if(ret == -1){
print_error("lseek()", errno);
return (void*) -5;
}
ret = read(mem_file, start_addr_str, 4);
if(ret == -1){
print_error("read()", errno);
return (void*) -6;
}
return (void*) *((int*)start_addr_str);
}
int main(int argc, char* argv[]){
if(argc <= 1){
printf("Usage: %s TID\n", argv[0]);
return -1;
}
int tid = atoi(argv[1]);
int gs;
int thread_area_base;
int start_address_offset = 0x234;
void* start_address;
int ret = attach(tid);
if(ret == -1) return -1;
gs = get_gs_register(tid);
if(gs == -1){
detach(tid);
return -2;
}
thread_area_base = get_thread_area_base(tid, gs);
if(thread_area_base == -1){
detach(tid);
return -3;
}
printf("thread_area_base: %p\n", (void*) thread_area_base);
unsigned int start_address_pointer = thread_area_base + start_address_offset;
printf("start_address_pointer: %p\n", (void*) start_address_pointer);
start_address = get_start_address(tid, start_address_pointer);
printf("start_address: %p\n", start_address);
detach(tid);
return 0;
}
int attach(int tid){
int status;
int ret = ptrace(PTRACE_ATTACH, tid, NULL, NULL);
if(ret == -1){
print_error("PTRACE_ATTACH", errno);
}
ret = waitpid(-1, &status, __WALL);
if(ret == -1){
print_error("waitpid()", errno);
}
return ret;
}
int detach(int tid){
int ret = ptrace(PTRACE_DETACH, tid, NULL, NULL);
if(ret == -1){
print_error("PTRACE_DETACH", errno);
}
return ret;
}
void print_error(char* func_name, int errnumber){
printf("%s failed. %i, %s\n", func_name, errnumber, strerror(errnumber));
}
</code></pre>
| 3982 | 2014-03-27T15:52:45.423 | <h2>Problem Statement</h2>
<p>I'm trying to get the address of a running thread's <code>start_routine</code> as passed in the <code>pthread_create()</code> call.</p>
<h2>Research so far</h2>
<p>It is apparently not in <code>/proc/[tid]/stat</code> or <code>/proc/[tid]/status</code>.</p>
<p>I found that <code>start_routine</code> is a member of <code>struct pthread</code> and gets set by <code>pthread_create</code>[<a href="http://fxr.watson.org/fxr/source/nptl/pthread_create.c?v=GLIBC27;im=excerpts#L455">1</a>].
If I knew the address of this <code>struct</code>, I could read the <code>start_routine</code> address.</p>
<p>I also found <code>td_thr_get_info</code> defined in the debugging library <code>thread_db.h</code> [<a href="http://fxr.watson.org/fxr/source/nptl_db/td_thr_get_info.c?v=GLIBC27#L27">2</a>]. It fills a <code>struct</code> with information about the thread, including the start function [<a href="http://fxr.watson.org/fxr/source/nptl_db/thread_db.h?v=GLIBC27#L259">3</a>]. But, it needs a <code>struct td_thragent</code> as an argument and I don't know how to create it properly.</p>
| How can I get a running thread's start address on linux? | |linux|thread| |
<p>It may be late but:
If you use WinDBG(kd) to debug the kernel use</p>
<pre><code>sxe -c ".echo fdisk loaded;" ld:fdisk.sys
</code></pre>
<p>this is usable in user and kernel mode and cause the debugger break-in after module loaded and before entry-point.</p>
| 3983 | 2014-03-28T12:50:55.777 | <p>I've been reversing a sample of the Uroborus trojan for my own learning joy. I'm having a hard time following it once it loads a windows kernel driver that implements the rootkit. I've set up my environment for Kernel debugging (using IDA's windbg plugin) and set a breakpoint for the new driver (it's called Fdisk.sys, so I've been typing "bu fdisk.sys!DriverEntry"). However, IDA never breaks when the driver is loaded. I can tell that it has run because it starts hiding a registry key (Ultra3), and dumping the memory and using Volatility to look at unloaded modules, I can see that fdisk.sys was unloaded. I can also confirm that it's installed hooks into a number of kernel API's. How do I get IDA/windbg to break on the driver before it gets to run? </p>
| How to break on not-yet-loaded kernel driver | |ida|malware|debugging|kernel-mode| |
<p>You asked this a couple of months ago, but I'm going to answer anyway.</p>
<p>First, I fear that your decompression code is buggy. There are several horizontal lines of noise, and I'm pretty sure they're not supposed to be there.</p>
<p>Your <code>A+B=C</code> guess was correct. A tell-tale sign are the visible horizontal edges but hidden vertical edges. I think your mistake was simply to do the addition with the wrong endianness.</p>
<p>I wrote a little Python script:</p>
<pre><code>from PIL import Image
im = Image.open('orig.png')
w, h = im.size
pix = im.load()
def add(p, q):
# Convert (R,G,B) tuples to RGB integers, add them, and convert back
pp = (p[0] << 16) + (p[1] << 8) + p[2]
qq = (q[0] << 16) + (q[1] << 8) + q[2]
r = pp + qq
return ((r >> 16) & 0xFF, (r >> 8) & 0xFF, r & 0xFF)
for y in range(1, h):
for x in range(w):
pix[x,y] = add(pix[x, y], pix[x, y-1])
im.save('out.png')
</code></pre>
<p>The result is this image. You can see that the top third matches the PSP image, but starting from the line of noise in the source image, the picture becomes distorted. Fix your decompression code and the picture will come out right :)</p>
<p><img src="https://i.stack.imgur.com/c2y15.png" alt="processed image"></p>
| 3992 | 2014-03-30T12:00:12.417 | <p>All right, I have a PS3 image compressed with a variation of the LZ algoritm (magic bytes <code>43 5A 32 00</code>) (I have successfully decompressed it) and what I get from the decompressed form is a set of ARGB colors/pixels (The number of bytes in the compressed file is <code>4*width*height</code> thus I know that the decompressed file problably only contain the raw ARGB data). </p>
<p>Here is an image illustrating the result of rendering the ARGB data (transparency omitted to makes thing easier to make out).</p>
<p><strong>Decompressed image:</strong>
<img src="https://i.stack.imgur.com/N7DIl.png" alt="enter image description here"></p>
<p><strong>PS3 edition screenshot:</strong></p>
<p><img src="https://i.stack.imgur.com/ibduR.png" alt="enter image description here"></p>
<p><strong>PSP edition screenshot:</strong></p>
<p><img src="https://i.stack.imgur.com/pcJHM.jpg" alt="enter image description here"></p>
<p>What I want to know is if any of you know what sort of logic this color structure use? To me, it looks like it looks at the last color in terms of the y-axis. Where zero value just gets the value of the last color of the y-axis. It kind of looks like I have to dif/sum colors together.</p>
<p>I have tried varies methods. For example, here I try storing a color array with zero color and for each line, sum the unsigned integer representation of ARGB of x position in the array with the new color' unsigned integer -> <code>A+B = C</code>.</p>
<p><img src="https://i.stack.imgur.com/UXS2C.png" alt="enter image description here"></p>
<p>A-B=C, just gives pinkish giberish..</p>
<p>Here are some samples of ARGB values that I use to try and find a solution:</p>
<pre><code>x:0, y:24-27
R G B
137 195 255
240 254 000
014 000 255
001 001 001
</code></pre>
<p>This should give a blueish color (comparing to Game screenshot) like: <code>145, 184, 219</code>.</p>
<p>*Please notice that my image for comparison isn't exact. That is to say that I got it from the same game of another platform where another format is used. Since the image sized are different. I can say that the colors aren't 100% the same as the new one.</p>
<pre><code>x:4, y:22-28
R G B
137 195 255
118 168 201
255 204 161
</code></pre>
<p>This too should give a dark bluerish color like: <code>020, 068, 096</code>.</p>
<p>*Please notice that the compared color in the game isn't exact, the image has transparency
too, so the game has problably messed with the color values of the screenshot.</p>
<p>So if anybody have encountered stuff like this before and could give me a tip of some sort I would be very happy. :)</p>
| What is this way of representing color in this unknown image format | |file-format| |
<p>The immediate value is not checked by the CPU but may be checked by the exception handler, e.g. to distinguish between breakpoints inserted by the debugger from those added by the programmer/compiler. For example, the <a href="http://www.keil.com/support/man/docs/ARMCC/armcc_pge1358787048379.htm" rel="nofollow noreferrer">ARM semihosting interface</a> uses <code>BKPT 0xAB</code> on ARMv7-M and presumably won't work with other immediates.</p>
| 3995 | 2014-03-31T15:25:19.527 | <p>Assembler syntax
BKPT #
where:
See Standard assembler syntax fields on page A6-7.
Specifies an 8-bit value that is stored in the instruction. This value is ignored by the ARM
hardware, but can be used by a debugger to store additional information about the
breakpoint.</p>
<p>what should i pass to this function? where can i see that value when i am debugging? </p>
<p>BKPT #0 vs BKPT #1 - is their a difference? </p>
| what does the imediate value of the BKPT opcode is being used for? | |disassembly|arm| |
<p>Just because you haven't seem them does not mean they don't exist. In my experience the decompiler produces <code>for</code> loops all the time.</p>
<p><strong>EDIT</strong>: Here's just one example:</p>
<pre><code> loc_804B520:
xor edx, edx
jmp short loc_804B52B
loc_804B524:
mov al, [edi+edx]
mov [ebx+edx], al
inc edx
loc_804B52B:
cmp edx, esi
jl short loc_804B524
</code></pre>
<p>Output:</p>
<pre><code> for ( i = 0; i < a2; ++i )
*(_BYTE *)(v2 + i) = *(_BYTE *)(a1 + i);
</code></pre>
| 3999 | 2014-04-01T02:55:05.873 | <p>Question came up in a reverse engineering class. The prof asked this question. I mean, in the grand scheme of things, while loops == for loops, I don't have a problem with that. </p>
<p>IDA Pro book... Google... not seeing anything online here. </p>
<p>So why does IDA pro never produce for loops in its pseudocode generator?</p>
| Why does IDA Pro's pseudocode function not produce for loops? | |ida|ida-plugin| |
<p>dynamoRIO does expose an IR (see <a href="http://dynamorio.org/docs/dr__ir__instr_8h.html" rel="nofollow">documentation</a>).
There is just no "written textual" form of it, it is basically a 1:1 mapping of the underlying assembly language and thus very close to the underlying architecture.</p>
| 4003 | 2014-04-01T13:04:04.667 | <p>There are some excellent DBI frameworks for Windows (Intel PIN, DynamoRIO...) but unfortunately none of them expose an IR afaik.
I am looking for something like Valgrind's VEX that works on Windows.</p>
<p>Any pointers / references would be greatly appreciated. Thanks in advance!</p>
| Is there any Dynamic Binary Instrumentation frameworks for Windows exposing an Intermediate Representation? | |windows|dynamic-analysis| |
<blockquote>
<p>So I am wondering what does ollydbg write to memory to set a BP? and
is this detectable?</p>
</blockquote>
<p>By default, OllyDbg overwrites the beginning of an instruction with <code>INT 3</code> (machine code <code>0xCC</code>) for a software breakpoint (though it hides this from your view in order to simplify things). This is configurable in OllyDbg v2, which allows you to instead use <code>INT 1</code>, <code>HLT</code>, <code>CLI</code>, etc. for software breakpoints.</p>
<p>Both OllyDbg v1 and OllyDbg v2 allow you to use hardware breakpoints or memory breakpoints instead of software breakpoints. All three types of breakpoints are detectable by the debuggee.</p>
| 4008 | 2014-04-02T16:29:13.983 | <p>For the sake of this question I am going to simplify things. Basically I have a CMP that is comparing a memory address to a constant value. I want to change the constant value to force a match.</p>
<p>I tried changing the constant value by hex editing and changing the value in ollydbg. Both methods cause the application to behave in an undesirable way so there must be a double check which checks the constant value. I have not found this second check yet.</p>
<p>Now this is where it gets interesting, if I set a BP with ollydbg on the CMP command without changing anything the application behaves as if there was a change. I have confirmed this multiple times, I do not even have to stop or restart the application, once a BP is set the application behaves in a very distinguishable way and then returns to normal as soon as I remove the BP. This BP is never being hit! I have done some further testing and this only happens if the BP is set on the CMP command or the following 2 commands which are a JE and a MOV</p>
<p>So I am wondering what does ollydbg write to memory to set a BP? and is this detectable?</p>
| Ollydbg breakpoint causing application to perform differently without BP being hit | |ollydbg|memory| |
<pre><code>:\>cat memaccess.py
from pydbg import *
from pydbg.defines import *
def handler_breakpoint (pydbg):
if pydbg.first_breakpoint:
return DBG_CONTINUE
context = dbg.get_thread_context(dbg.h_thread)
print "eip = %08x" % context.Eip
print "edi = %08x" % context.Edi
return DBG_CONTINUE
dbg = pydbg()
dbg.set_callback(EXCEPTION_BREAKPOINT, handler_breakpoint)
dbg.load("c:\\windows\\system32\\calc.exe")
dbg.bp_set(0x101248a)
dbg.resume_all_threads()
pydbg.debug_event_loop(dbg)
:\>python memaccess.py
eip = 0101248a
edi = 7c80b741
</code></pre>
<p>confirming with windbg </p>
<p><strong>:>cdb -c "bp 0x101248a;g;r Edi;q" calc</strong></p>
<pre><code>0:000> cdb: Reading initial command 'bp 0x101248a;g;r Edi;q'
Breakpoint 0 hit
edi=7c80b741
quit:
:\>
</code></pre>
| 4010 | 2014-04-03T01:58:01.033 | <p>How can I find the register value of another process at a specific address using python pydbg or any other module that can do that?</p>
<p>Lets say that:</p>
<pre><code>Address opcode
006122CB mov eax, [ecx+08]
</code></pre>
<p>So, I want to find the value of the ecx register at this address.</p>
<p>Windows 7 x64 bit</p>
<p>Program x32 bit</p>
| Find register value of a remote process - pydbg | |debugging|python| |
<p>confirm if addres is accessible and correct </p>
<pre><code>u 0x1```401172f1
or
db 0x1`401172f1 `
</code></pre>
<p>then set a bp 0x1 <code>``401172f1</code></p>
<p>preferably sign/zero extend the address using <code>backticks</code> when you are not using a symbol to set breakpoints </p>
<p>deferred breakpoints <code>(Bu breakpoints)</code> are more versatile than bp breakpoints as they are tied to symbol and not to specific address and thus are not influenced by aslr / loading / unloading of images</p>
| 4019 | 2014-04-04T19:57:12.843 | <p>My issue is all about setting the breakpoint on x64 Windbg.
For x86 to combine IDA and Windbg analysis nothing is required. Just copy and paste the IDA address value and it works just fine.
For instance, in x86 I have <code>sub_401000</code>, so, the corresponding breakpoint is set by <code>bp 401000</code><br>
In x64 I have address <code>sub_140001000</code>, so, I'm trying to set as follows <code>bp 140001000</code> and get no result at all. Application won't reach my breakpoint. That's why, I've just set <code>bp 1401172F1</code> that reaches the address of interest for sure.</p>
<p>But now I've faced with error </p>
<pre><code>Unable to insert breakpoint 0 at 00000001`401172f1, Win32 error 0n998
The breakpoint was set with BP. If you want breakpoints
to track module load/unload state you must use BU.
bp0 at 00000001`401172f1 failed
WaitForEvent failed
</code></pre>
<p>All these answers make sense, but setting breakpoint for this rva leads to error for some reason. Maybe I should try something else?</p>
<pre><code>0:000> lmi
start end module name
00000000`77570000 00000000`77719000 ntdll (pdb symbols) C:\Windows\SYSTEM32\ntdll.dll
00000000`77740000 00000000`77743000 normaliz (deferred)
00000001`3fd90000 00000001`402e0000 module_of_interest (no symbols)
</code></pre>
| Windbg x64 setting breakpoint | |ida|dynamic-analysis|windbg|x86-64| |
<p>I'm not sure, but you probably experiencing the following issue:
Usually page addresses are rounded by page size. It depends on page size in system you working with. So you can calculate this address by making AND operation with constant <code>~(PAGE_SIZE-1)</code>.</p>
<p>If it doesn't work, try to call call <code>mprotect</code> twice:</p>
<ol>
<li>first do read and write without execute, </li>
<li>change the memory, and after that call <code>mprotect</code> with execute bit but without write bit.</li>
</ol>
| 4021 | 2014-04-05T05:06:24.430 | <p>I was trying out a ROP exploit -- I'm trying to make an mprotect system call using ROP(not a libc call), to try and make the buffer executable. I tried stracing the server process and ran the exploit, only to see that results in an error as follows :- </p>
<pre><code>mprotect(0xbffdda10, 65536, PROT_READ|PROT_WRITE|PROT_EXEC) = -1 EINVAL(Invalid argument)
</code></pre>
<p>Later on, I tried a different address <code>0x08048000</code> and I could see that the call worked(strace output and /proc/pid/maps confirms this). <code>Given a buffer address how could I guess/find the correct page address that I could use to make a call to mprotect.</code></p>
<p>I think I figured out the reason why this happens. ASLR is enabled and the page that starts at 0x08048000 does not change addresses. However, the page that corresponds to the buffer changes addresses.
The buffer address can be leaked -- so I tried checking if the difference between the start of the page and the buffer address remains constant, it does not.</p>
| Calculating the page address a buffer belongs to | |linux|exploit| |
<p>After unziping the arhive, run <code>binwalk</code> with</p>
<pre><code>binwalk -eM *.chm
</code></pre>
<p>Flag e stands for extract automatically,and M for <code>--matryoshka</code> or recursively scanning and extracting.</p>
<p>After a few recursive unpackings, you'll get a squashfs-root dir with what you want I guess. </p>
<p>For what it's worth, I tried this with binwalk version 1.2.1.</p>
| 4034 | 2014-04-07T16:12:04.103 | <p>I am trying to extract the squashfs filesystem of my router. <a href="http://www.downloads.netgear.com/files/GDC/WNDR3400V2/WNDR3400v2_V1.0.0.38_1.0.61.zip" rel="noreferrer">Here is firmware</a>.</p>
<p>First, I unzipped the file. Next, I ran <code>binwalk</code> to get some information about the file.</p>
<pre><code>DECIMAL HEX DESCRIPTION
-------------------------------------------------------------------------------------------------------
58 0x3A TRX firmware header, little endian, header size: 28 bytes, image size: 6656000 bytes, CRC32: 0x2B1713B2 flags/version: 0x10000
86 0x56 LZMA compressed data, properties: 0x5D, dictionary size: 65536 bytes, uncompressed size: 3614368 bytes
1282426 0x13917A Squashfs filesystem, little endian, non-standard signature, version 3.0, size: 5367357 bytes, 853 inodes, blocksize: 65536 bytes, created: Wed Aug 7 05:08:47 2013
</code></pre>
<p>I then ran <code>binwalk -e</code> to extract the contents of the file. I got three files:</p>
<pre><code>.
├── 13917A.squashfs
├── 56
└── 56.7z
</code></pre>
<p>Running <code>unsquashfs</code> on <code>13917A.squashfs</code> failed, saying it was unable to find the magic block. This was because the file was using a non standard magic. Changing the magic to a standard one made <code>unsquashfs</code> and <code>file</code> detect it as a squashfs file system. But <code>unsquashfs</code> did not complete successfully:</p>
<pre><code>caff@UbunutuX2:~/Netgear/test$ unsquashfs 13917A.squashfs
Reading a different endian SQUASHFS filesystem on 13917A.squashfs
Filesystem on 13917A.squashfs is (768:0), which is a later filesystem version than I support!
</code></pre>
<p>I also tried extracting the file system by using <a href="https://code.google.com/p/firmware-mod-kit" rel="noreferrer">Jeremy Collake’s Firmware Mod Kit</a>. This failed also:</p>
<pre><code>caff@UbunutuX2:~/Netgear/test$ /opt/firmware-mod-kit/trunk/unsquashfs_all.sh 13917A.squashfs
Attempting to extract SquashFS .X file system...
Trying ./src/others/squashfs-3.4-nb4/unsquashfs-lzma... Skipping others/squashfs-hg55x-bin (wrong version)...
File extraction failed!
</code></pre>
<p>How do I extract this squashfs file system?</p>
| Router Decompiling | |firmware| |
<p>Regarding the disassembling part, you may want to check <strong>JEB</strong> (version >= 2.3.0), which provides advanced disassembly and decompilation of MIPS 32-bit code.</p>
<p>The MIPS debugger is only available for Android Linux though, so you won't be able to debug your program using this tool.</p>
<p>There is a demo available on <a href="https://www.pnfsoftware.com/jeb2/mips" rel="nofollow noreferrer">PNF Software's website</a>.</p>
| 4040 | 2014-04-08T19:04:04.390 | <p>I am searching for security vulnerabilities in the firmware of <a href="http://www.downloads.netgear.com/files/GDC/WNDR3400V2/WNDR3400v2_V1.0.0.38_1.0.61.zip" rel="nofollow noreferrer">this router</a>, its architecture is mips. <a href="https://reverseengineering.stackexchange.com/questions/4034/router-decompiling">I have successfully unpacked the file system</a>. I would like to disassemble the http daemon, located at <code>/usr/sbin/httpd</code>. How can I disassemble this program and run it in <code>spim</code>?</p>
| Disassembling MIPS Binaries | |disassembly|mips| |
<p>Open the stack frame window (<kbd>Ctrl-K</kbd> or double-click on a variable), then delete bogus arguments with <kbd>U</kbd>.</p>
| 4047 | 2014-04-09T16:31:47.100 | <p>In IDA I can easily change the size of the local variables using <code>Alt+P</code> and then changing the "Local Variables area" field to the desired value.</p>
<p>However, how can I do this with the parameters size? IDA has misanalyzed the function and got the result that it has about 30 kilobytes arguments when it actually just has 30 kilobytes variables.</p>
| How do I adjust the length of the parameters in IDA? | |ida|functions| |
<p>You may want to start with something simple like monitoring the API calls the controller application is doing. I'm not sure how old your Windows version is but it might be worth giving <a href="http://www.rohitab.com/apimonitor" rel="nofollow">API Monitor</a> a try. It gives you a good start to monitoring the application at least and you get to trivially see how it interacts with the operating system.</p>
<p>Essentially what you're looking for is probably <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx" rel="nofollow">CreateFile</a> followed by <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa363216%28v=vs.85%29.aspx" rel="nofollow">DeviceIoControl</a> calls which use the same handle as returned from CreateFile. This is one way for an application to interact with kernel mode software. Another option is to look for a CreateFile call followed by <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa365467%28v=vs.85%29.aspx" rel="nofollow">ReadFile</a> and <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa365747%28v=vs.85%29.aspx" rel="nofollow">WriteFile</a> which is another common way for user space applications to communicate with kernel mode drivers.</p>
<p>If your software is so old that we're talking Windows 9x you should probably use something like SoftICE.</p>
<p>Once you've figured out these things you'd need to examine the driver for the card and look for the handlers for the DeviceIoControl calls you saw before or the ReadFile, WriteFile handling. We've had a similar discussion <a href="https://reverseengineering.stackexchange.com/questions/3955/how-to-reverse-deviceiocontrol">here</a> and there is a decent resource available as <a href="http://www.reverse-engineering.info/SystemCoding/SkeletonKMD_Tutorial.htm" rel="nofollow">Kernel Mode Driver Tutorial by Clandestiny</a> which is getting a bit long in the tooth but still applies.</p>
<p>This is quite a bit of work and I would strongly advice you to buy something like <a href="http://rads.stackoverflow.com/amzn/click/1118787315" rel="nofollow">Practical Reverse Engineering</a>, as well as <a href="http://rads.stackoverflow.com/amzn/click/1593273851" rel="nofollow">A bug hunter's diary</a>. They both discuss the issues of how to reverse engineer user space and kernel mode interaction. If you're very serious about this I would recommend also getting a copy of <a href="http://rads.stackoverflow.com/amzn/click/0735648735" rel="nofollow">Windows Internals, Part 1</a> and <a href="http://rads.stackoverflow.com/amzn/click/073566587" rel="nofollow">Windows Internals, Part 2</a>. </p>
<p>If you want to look at the actual PCI traffic, which is probably not what you want to do, I think you have to go the hardware route. There's a PCI bus analyzer available from <a href="http://www.silicon-control.com/pci650.htm" rel="nofollow">Silicon Control</a>, then there's the expensive solutions from the big names, LeCroy and Agilent. Although I'm not sure if they still make PCI bus analyzers anymore or if everyone has moved on to PCIe. You also have the option of breaking out the bus yourself and using an FPGA to sniff the signals. ElectroFriends has <a href="http://electrofriends.com/articles/computer-science/protocol/introduction-to-pci-protocol/" rel="nofollow">a short introduction to the PCI bus</a> available.</p>
| 4053 | 2014-04-10T13:33:11.543 | <p>I have an old device connected to personal computer via specific PCI card. Device is handled with C++ control application, which is not able to run on new versions of Windows. Manufacturer of that device was consumed by big company a while ago and do not continue on development of such devices.</p>
<p>What I want to do is disassemble communication protocol between this device and computer, respective between the software and PCI card; however, I am complete beginner. I downloaded IDA tool. I am able to trace application and find couple of subroutines that are often trigerred when application is sending commands to the device, but these subroutines contains others etc etc.
Also I could not find any meaningful strings in the disassembled application (i was expecting many short string instructions with numeric parameters).</p>
<p>I would like to ask you for some advice, what I should do at first, or what to be careful on when I want to detect communication protocol.</p>
| Disassemble communication protocol for an old device | |ida|c++|communication| |
<p><strong>This fix doesn't save to database or is overwritten upon loading IDB database it always reverts back to messed up! (I think there is something I missed to change)</strong></p>
<p>Not the right way to do it.. but I managed to solve this using <code>IDC Scripts</code></p>
<p>Open a fresh IDA PRO let it analyze then go to <code>File -> Produce File -> Dump Database to IDC File.</code></p>
<p>Open dumped <code>idc</code> file in notepad and search for the start of Imports comments such as <code>; Imports from GDI32.dll</code> since thats where the first Import starts.</p>
<p>Now just copy+paste from the <code>idc</code> file all the way to the end of the function before the bracket.</p>
<p>Backup your messed up <code>project.idb</code> file because this may mess up it even more if you are not careful!.</p>
<p>Now go to <code>File->IDC Command...</code> and paste something I posted at the end of this post.</p>
<p><strong>IDC Command Text box has limitations you can't paste too much so you need to split up by blocks, I advise make sure your block starts with <code>MakeDword</code> don't end with that.</strong></p>
<p>Here was my first Imports from <code>idc</code> file what it generated below.<br>
<code>(This will only work for my application only obviously, just showing you what you need to look for.)</code></p>
<pre><code>auto x;
#define id x
MakeArray (0X4DA0EC, 0XF14);
ExtLinA (0X4DB200, 0, "; ");
ExtLinA (0X4DB200, 1, "; Imports from GDI32.dll");
ExtLinA (0X4DB200, 2, "; ");
ExtLinA (0X4DB200, 3, "; Section 4. (virtual address 000DB000)");
ExtLinA (0X4DB200, 4, "; Virtual size : 0000090E ( 2318.)");
ExtLinA (0X4DB200, 5, "; Section size in file : 00000A00 ( 2560.)");
ExtLinA (0X4DB200, 6, "; Offset to raw data for section: 0002FC00");
ExtLinA (0X4DB200, 7, "; Flags C0000040: Data Readable Writable");
ExtLinA (0X4DB200, 8, "; Alignment : default");
MakeDword (x=0X4DB200);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB200, "GetObjectA");
MakeDword (x=0X4DB204);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB204, "DeleteObject");
MakeByte (0X4DB208);
MakeArray (0X4DB208, 0X4);
ExtLinA (0X4DB20C, 0, "; ");
ExtLinA (0X4DB20C, 1, "; Imports from KERNEL32.dll");
ExtLinA (0X4DB20C, 2, "; ");
MakeDword (x=0X4DB20C);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB20C, "GetModuleFileNameA");
MakeDword (x=0X4DB210);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB210, "WritePrivateProfileStringA");
MakeDword (x=0X4DB214);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB214, "GetTickCount");
MakeDword (x=0X4DB218);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB218, "CloseHandle");
MakeDword (x=0X4DB21C);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB21C, "GetFileTime");
MakeDword (x=0X4DB220);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB220, "CreateFileA");
MakeDword (x=0X4DB224);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB224, "GetPrivateProfileIntA");
MakeDword (x=0X4DB228);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB228, "GetPrivateProfileStringA");
MakeDword (x=0X4DB22C);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB22C, "VirtualAlloc");
MakeDword (x=0X4DB230);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB230, "VirtualFree");
MakeDword (x=0X4DB234);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB234, "TerminateProcess");
MakeDword (x=0X4DB238);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB238, "GetExitCodeProcess");
MakeDword (x=0X4DB23C);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB23C, "CreateProcessA");
MakeDword (x=0X4DB240);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB240, "GetCommandLineA");
MakeDword (x=0X4DB244);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB244, "SetConsoleTitleA");
MakeDword (x=0X4DB248);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB248, "Sleep");
MakeDword (x=0X4DB24C);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB24C, "SetEndOfFile");
MakeDword (x=0X4DB250);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB250, "SetStdHandle");
MakeDword (x=0X4DB254);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB254, "GetFileType");
MakeDword (x=0X4DB258);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB258, "ExitProcess");
MakeDword (x=0X4DB25C);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB25C, "GetNumberOfConsoleInputEvents");
MakeDword (x=0X4DB260);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB260, "PeekConsoleInputA");
MakeDword (x=0X4DB264);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB264, "GetConsoleMode");
MakeDword (x=0X4DB268);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB268, "SetConsoleMode");
MakeDword (x=0X4DB26C);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB26C, "ReadConsoleInputA");
MakeDword (x=0X4DB270);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB270, "SetEnvironmentVariableA");
MakeDword (x=0X4DB274);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB274, "CompareStringW");
MakeDword (x=0X4DB278);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB278, "CompareStringA");
MakeDword (x=0X4DB27C);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB27C, "LoadLibraryA");
MakeDword (x=0X4DB280);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB280, "WaitForSingleObject");
MakeDword (x=0X4DB284);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB284, "GetStringTypeW");
MakeDword (x=0X4DB288);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB288, "GetStringTypeA");
MakeDword (x=0X4DB28C);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB28C, "IsBadCodePtr");
MakeDword (x=0X4DB290);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB290, "IsBadWritePtr");
MakeDword (x=0X4DB294);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB294, "IsBadReadPtr");
MakeDword (x=0X4DB298);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB298, "GetOEMCP");
MakeDword (x=0X4DB29C);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB29C, "GetACP");
MakeDword (x=0X4DB2A0);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2A0, "GetCPInfo");
MakeDword (x=0X4DB2A4);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2A4, "GetEnvironmentStringsW");
MakeDword (x=0X4DB2A8);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2A8, "GetTimeZoneInformation");
MakeDword (x=0X4DB2AC);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2AC, "GetSystemTime");
MakeDword (x=0X4DB2B0);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2B0, "GetLocalTime");
MakeDword (x=0X4DB2B4);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2B4, "__imp_RtlUnwind");
MakeDword (x=0X4DB2B8);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2B8, "GetLastError");
MakeDword (x=0X4DB2BC);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2BC, "GetEnvironmentStrings");
MakeDword (x=0X4DB2C0);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2C0, "FreeEnvironmentStringsW");
MakeDword (x=0X4DB2C4);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2C4, "GetCurrentProcess");
MakeDword (x=0X4DB2C8);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2C8, "HeapAlloc");
MakeDword (x=0X4DB2CC);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2CC, "HeapReAlloc");
MakeDword (x=0X4DB2D0);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2D0, "HeapFree");
MakeDword (x=0X4DB2D4);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2D4, "RaiseException");
MakeDword (x=0X4DB2D8);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2D8, "GetVersion");
MakeDword (x=0X4DB2DC);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2DC, "ReadFile");
MakeDword (x=0X4DB2E0);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2E0, "WriteFile");
MakeDword (x=0X4DB2E4);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2E4, "SetFilePointer");
MakeDword (x=0X4DB2E8);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2E8, "HeapDestroy");
MakeDword (x=0X4DB2EC);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2EC, "LCMapStringW");
MakeDword (x=0X4DB2F0);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2F0, "SetHandleCount");
MakeDword (x=0X4DB2F4);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2F4, "GetStdHandle");
MakeDword (x=0X4DB2F8);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2F8, "GetStartupInfoA");
MakeDword (x=0X4DB2FC);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB2FC, "MultiByteToWideChar");
MakeDword (x=0X4DB300);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB300, "WideCharToMultiByte");
MakeDword (x=0X4DB304);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB304, "LCMapStringA");
MakeDword (x=0X4DB308);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB308, "UnhandledExceptionFilter");
MakeDword (x=0X4DB30C);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB30C, "FreeEnvironmentStringsA");
MakeDword (x=0X4DB310);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB310, "HeapCreate");
MakeDword (x=0X4DB314);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB314, "SetUnhandledExceptionFilter");
MakeDword (x=0X4DB318);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB318, "GetFileAttributesA");
MakeDword (x=0X4DB31C);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB31C, "FlushFileBuffers");
MakeDword (x=0X4DB320);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB320, "GetProcAddress");
MakeDword (x=0X4DB324);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB324, "GetModuleHandleA");
MakeByte (0X4DB328);
MakeArray (0X4DB328, 0X4);
ExtLinA (0X4DB32C, 0, "; ");
ExtLinA (0X4DB32C, 1, "; Imports from USER32.dll");
ExtLinA (0X4DB32C, 2, "; ");
MakeDword (x=0X4DB32C);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB32C, "MessageBoxA");
MakeDword (x=0X4DB330);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB330, "LoadImageA");
MakeByte (0X4DB334);
MakeArray (0X4DB334, 0X4);
ExtLinA (0X4DB338, 0, "; ");
ExtLinA (0X4DB338, 1, "; Imports from WSOCK32.dll");
ExtLinA (0X4DB338, 2, "; ");
MakeDword (x=0X4DB338);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB338, "__imp_ioctlsocket");
MakeDword (x=0X4DB33C);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB33C, "__imp_inet_ntoa");
MakeDword (x=0X4DB340);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB340, "__imp_WSACleanup");
MakeDword (x=0X4DB344);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB344, "__imp_WSAStartup");
MakeDword (x=0X4DB348);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB348, "__imp_recvfrom");
MakeDword (x=0X4DB34C);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB34C, "__imp_sendto");
MakeDword (x=0X4DB350);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB350, "__imp_recv");
MakeDword (x=0X4DB354);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB354, "__imp_closesocket");
MakeDword (x=0X4DB358);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB358, "__imp_socket");
MakeDword (x=0X4DB35C);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB35C, "__imp_inet_addr");
MakeDword (x=0X4DB360);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB360, "__imp_setsockopt");
MakeDword (x=0X4DB364);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB364, "__imp_htons");
MakeDword (x=0X4DB368);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB368, "__imp_htonl");
MakeDword (x=0X4DB36C);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB36C, "__imp_bind");
MakeDword (x=0X4DB370);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB370, "__imp_gethostbyname");
MakeDword (x=0X4DB374);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB374, "__imp_connect");
MakeDword (x=0X4DB378);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB378, "__imp_send");
MakeByte (0X4DB37C);
MakeArray (0X4DB37C, 0X4);
ExtLinA (0X4DB380, 0, "; ");
ExtLinA (0X4DB380, 1, "; Imports from zlib.dll");
ExtLinA (0X4DB380, 2, "; ");
MakeDword (x=0X4DB380);
OpOff (x, 0, 0);
OpOff (x, 128, 0);
MakeName (0X4DB380, "__imp_compress");
MakeByte (0X4DB384);
MakeArray (0X4DB384, 0X4);
</code></pre>
| 4059 | 2014-04-12T09:43:28.897 | <p>I have put alot of work into renaming and fixing lvar types to better understand the code I made a mistake when I was converting some <code>unk_dword</code> to array I missed that it wasn't set to db but rather dd and it overwritten lots of stuff I lost many my own stuff which I know I won't get back but I can live with that, but I can't seem to get the imports to evaluate with the proper names like they were before.</p>
<p>I tried <code>Load->FLIRT signature file</code> with <code>Microsoft VisualC 2-10/net runtime</code> which seems to be right, didn't do anything.</p>
<p>In output Window I'm getting</p>
<p><code>004DB200: Already data or code (hint: make 'unexplored')</code></p>
<p>I guess I need to make it unexplored? how would I do that? I tried Undefining the Imports with the <code>U Key</code> it doesn't work.</p>
<p>Here is some screenshots how it looks</p>
<p>Before looked like this
<br>
<img src="https://i.stack.imgur.com/rUkHC.png" alt="before imports">
<br>
After looks like this
<br>
<img src="https://i.stack.imgur.com/XJOXy.png" alt="after imports">
<br>
How it really looks like
<br>
<img src="https://i.stack.imgur.com/VHy63.png" alt="imports declares">
<br>
Here is how the bytes used to look like and how they look now (8 extra bytes)
<br>
<img src="https://i.stack.imgur.com/WFrws.png" alt="import bytes"></p>
| IDA PRO Imports Names Lost in a accident, Reanalyze does nothing. How to make unexplored imports? | |ida| |
<p>Well, after spending lots of time in NVIDIA's documentation, and the recent update to 6.0, they added the ability for their program <code>cuobjdump</code> to extract *.cubin files from any given target executable. So you can extract *.cubin files from PE or ELF files like this:</p>
<pre><code>C:\ProgramData\NVIDIA Corporation\CUDA Samples\v6.0\Bin\win32\Debug>cuobjdump -xelf all simpleAtomicIntrinsics.exe
</code></pre>
<p>Then use <code>nvdisasm</code> on your target *.cubin file:</p>
<pre><code>C:\ProgramData\NVIDIA Corporation\CUDA Samples\v6.0\Bin\win32\Debug>nvdisasm NVIDIA-CUDA-simpleAtomicIntrinsics.sm_50.cubin
</code></pre>
<p>Oh, by the way, <code>nvdisasm</code> does actually use control-flow disassembly. <code>cuobjdump</code> uses linear. </p>
<p>By playing with the provided tools you can extract opcodes, but unfortunately there's snakey instructions like <code>S2R</code> which according to NVIDIA's <a href="http://docs.nvidia.com/cuda/cuda-binary-utilities/index.html#fermi" rel="nofollow">documentation</a> does this: </p>
<p><code>S2R Special Register to Register</code></p>
<p>With no documentation I've seen yet as to what the special registers even ARE on this architecture. The best documentation on this so far is <a href="https://code.google.com/p/asfermi/wiki/FAQ" rel="nofollow">here</a>.</p>
| 4065 | 2014-04-14T03:22:30.643 | <p>I've recently started exploring the world of GPU-based malware. Academia doesn't have a ton of papers here, but there's some powerful ones. I'm looking at trying to improve the communities tools in terms of NVIDIA's fermi ISA, (since AMD published theirs!) </p>
<p>To date, I've not found any fermi disassemblers, but one ONE fermi assembler:</p>
<p><a href="http://code.google.com/p/asfermi/" rel="nofollow">asfermi</a></p>
<p>Are their any plugins/disassemblers that this community is aware of, that my google searches have apparently turned up zilch? NVIDIA's supplied tools don't work for binaries compiled with vs2010. </p>
<p>=============[Updated Context]===================</p>
<p>Appears that to a certain extent, my assumptions were wrong. NVIDIA's disassembly tools (nvobjdump, nvdisasm) are designed only to work with their *.cubin (maybe *.ptx) intermediate assemblies. Which is bad, from a malware analysis perspective. </p>
<p>After searching extensively I found a dead project called <a href="https://github.com/laanwj/decuda" rel="nofollow">decuda</a>. (I say dead because it hasn't had a commit in years.) AND it doesn't seem to be able to handle disassembling *.cubin binaries from the latest releases, 5.5 and 6.0. </p>
| Are there ANY supported Fermi Disassemblers out there? | |disassembly|windows|linux|nvidia| |
<p>It's possible, but not in the freeware version:</p>
<ol>
<li>Put cursor on the <code>jmp</code></li>
<li>Edit->Other->Specify switch idiom...</li>
</ol>
| 4074 | 2014-04-15T09:51:15.820 | <p>I'm disassembling a function that seems to use a switch statement, resulting in an indexed indirect jump, in two different places (same function!):</p>
<pre><code>0005FA58 mov al, [eax+112h]
0005FA5E cmp al, 4
0005FA60 ja loc_5FBED
0005FA66 and eax, 0FFh
0005FA6B jmp cs:off_5F98C[eax*4]
0005FA73
00060011 mov al, byte ptr unk_A4E30[eax]
00060017 cmp al, 3
00060019 ja short loc_60053
0006001B and eax, 0FFh
00060020 jmp cs:off_5F9A0[eax*4]
0005F98C off_5F98C dd offset loc_5FBCC, offset loc_5FA73, offset loc_5FAC5, offset loc_5FB14, offset loc_5FB66
0005F98C ; DATA XREF: FunctionStart+BB
0005F9A0 off_5F9A0 dd offset loc_60049, offset loc_6003E, offset loc_60031, offset loc_60028
0005F9A0 ; DATA XREF: FunctionStart+670
</code></pre>
<p>The first jump table has 5 entries, the second one 4. Unfortunately, the compiler placed them directly behind each other in memory. This seems to confuse IDA when calculating the graph connections for those nodes:</p>
<p><img src="https://i.stack.imgur.com/3ld8y.png" alt="Graph for 1st jump table"></p>
<p><em>Edit, as the the wording of the question was probably misleading</em></p>
<p>The graph node shows 9 outgoing connections. 5 of them are true connections, the next 4 are not - they belong to a different jump table that happens to be directly after this one in memory. I'd like to tell IDA "This jump table has 5 valid entries, ignore the other ones when creating the graph node". Is there any way to do this?</p>
<p><em>The original question was:</em></p>
<blockquote>
<p>The first jump table has 9 connections, 4 of which don't belong to
<em>that</em> statement. (The second one, ommited here for brevity, has only one connection). Is there any way to tell IDA to remove the extra
connections from the first table, and possibly create new connections
from the 2nd table to the corresponding targets?</p>
</blockquote>
<p>I already tried defining the jump tables as arrays, then undefine the function and re-define it as code, but that didn't change anything.</p>
<p>(I'm using IDA 5.0, as this is a hobby project, and i don't want to spend several hundred bucks on something i'll use a few times a year).</p>
| Any way to fix misinterpreted case jump tables in Ida Pro? | |ida|disassembly| |
<p>VBX files are standard NE (New Executable) files which use a few special entrypoints and structures used by VB. IDA Free does not support NE file format, you need to use the full version (at least Starter) or another disassembler .You can find the details of the VBX API in the VB Control Development Kit (CDK) shipped with <a href="https://winworldpc.com/download/4DA81FE7-57D8-11E4-A90F-7054D21A8599" rel="nofollow noreferrer">Visual Basic Pro 3.0</a> or by searching for "vbapi.h". Some additional information is available <a href="https://msdn.microsoft.com/en-us/library/aa268983" rel="nofollow noreferrer">in MSDN</a></p>
| 4077 | 2014-04-15T13:06:03.097 | <p>I have a 16 bit VBX file for Visual Basic 3 which I'd like to disassembly. Which program can analyze it? </p>
<p>I tried IDA Free, but it seems only OCX are supported.</p>
| How to disassemble VBX files? | |disassembly| |
<p>It seems to me that you're wrong if you assume v7 is an index - v6 is.</p>
<p>The original function was probably something like</p>
<pre><code>int v6=0;
while (v6 < result->ArenaSubStruct.ArenaPlayerCount) { // <-- what is a if/do while in your disassembly was originally a while
eachPlayer = result->ArenaSubStruct.PlayerPointers[v6];
if (eachPlayer != player) // <-- v4 and player are identical
SendPlayerReliablePacket(eachPlayer, buf, len, a4);
++v6;
}
</code></pre>
<p>To optimize this, the compiler introduced v7 as a pointer to the array element corresponding to the loop variable. This way, the processor just needs to access a pointer (and increment the pointer by the size of the int it points to) instead of a multiplication and add per loop:</p>
<pre><code>int v6=0;
int v7=offsetof(*result, ArenaSubStruct.PlayerPointers);
while (v6 < result->ArenaSubStruct.ArenaPlayerCount) {
eachPlayer = *(int *)((char *)result+v7)
if (eachPlayer != player)
SendPlayerReliablePacket(eachPlayer, buf, len, a4);
v6++;
v7+=sizeof(ArenaSubStruct.PlayerPointers[0]); // which is an it so sizeof returns 4
}
</code></pre>
<p>So your v7 is not an index into the PlayerPointers structure that should be brought down to 0, instead, it's an "index" into the ArenaSubStruct structure, that starts with the offset of PlayerPointers within ArenaSubStruct. You can't get that back nicely in IDA, because there's no nice C representation of what the compiler did - look at the ugly type casts i had to do.</p>
| 4082 | 2014-04-16T01:36:27.100 | <p>I put the whole question in 2 images. From research, it seems I need to use <kbd>Ctrl</kbd>+<kbd>R</kbd>, but I don't think that's what I need since I couldn't lower the number a bit further in order to reach 0.</p>
<p>I think the problem is that I'm not creating the <code>structs</code> properly.</p>
<p><strong>I'd like to add a few guesses the number went down to 16072 because the array of PlayerPointers is 4 bytes per element. So it went down <code>64288 / 4 = 16072</code>.</strong>
<br>
I still don't know what that means.</p>
<p><img src="https://i.stack.imgur.com/q4K9S.png" alt="problem 1">
<img src="https://i.stack.imgur.com/2odeD.png" alt="asm code"></p>
<p>ASM Code:</p>
<pre><code>.text:0040E040 ; =============== S U B R O U T I N E =======================================
.text:0040E040
.text:0040E040
.text:0040E040 ; struct_ARENA *__thiscall code(struct_PLAYER *player, const void *buf, unsigned int len, int a4)
.text:0040E040 sub_40E040 proc near
.text:0040E040
.text:0040E040
.text:0040E040 buf = dword ptr 4
.text:0040E040 len = dword ptr 8
.text:0040E040 a4 = dword ptr 0Ch
.text:0040E040
.text:0040E040 push ebx
.text:0040E041 push esi
.text:0040E042 mov esi, ecx
.text:0040E044 mov eax, [esi+1Ch]
.text:0040E047 test eax, eax
.text:0040E049 jz short loc_40E093
.text:0040E04B mov ecx, [eax+0FF0Ch]
.text:0040E051 xor ebx, ebx
.text:0040E053 test ecx, ecx
.text:0040E055 jle short loc_40E093
.text:0040E057 push edi
.text:0040E058 push ebp
.text:0040E059 mov ebp, [esp+10h+a4]
.text:0040E05D mov edi, 0FB20h
.text:0040E062
.text:0040E062 loc_40E062:
.text:0040E062 mov eax, [edi+eax]
.text:0040E065 cmp eax, esi
.text:0040E067 jz short loc_40E082
.text:0040E069 mov ecx, [eax+38h]
.text:0040E06C test ecx, ecx
.text:0040E06E jnz short loc_40E082
.text:0040E070 mov ecx, [esp+10h+len]
.text:0040E074 mov edx, [esp+10h+buf]
.text:0040E078 push ebp ; a4
.text:0040E079 push ecx ; len
.text:0040E07A push edx ; buf
.text:0040E07B mov ecx, eax ; this
.text:0040E07D call SendPlayerReliablePacket
.text:0040E082
.text:0040E082 loc_40E082:
.text:0040E082
.text:0040E082 mov eax, [esi+1Ch]
.text:0040E085 inc ebx
.text:0040E086 add edi, 4
.text:0040E089 cmp ebx, [eax+0FF0Ch]
.text:0040E08F jl short loc_40E062
.text:0040E091 pop ebp
.text:0040E092 pop edi
.text:0040E093
.text:0040E093 loc_40E093:
.text:0040E093
.text:0040E093 pop esi
.text:0040E094 pop ebx
.text:0040E095 retn 0Ch
.text:0040E095 sub_40E040 endp
.text:0040E095 ; ---------------------------------------------------------------------------
.text:0040E098 align 10h
</code></pre>
<p>Here is one that looks better only 1 struct instead of 2 but still same problem</p>
<p><img src="https://i.stack.imgur.com/89F9H.png" alt="enter image description here"></p>
<p>Here is the amount of players who are not watching but playing the game.</p>
<pre><code>int __thiscall TotalPlayingPlayers(struct_ARENA *arena)
{
int ArenaPlayerCount; // edx@1
int result; // eax@1
struct_PLAYER **eachPlayer; // ecx@2
ArenaPlayerCount = arena->ArenaPlayerCount;
result = 0;
if ( ArenaPlayerCount > 0 )
{
eachPlayer = arena->playerPointersForSomething;// How could this be like this? this would only hold 251 4 bytes not enough for all players.
do
{
if ( (*eachPlayer)->Ship != 8 )
++result;
++eachPlayer; // This means it really has to go up by 4 bytes the small 251 array.
--ArenaPlayerCount;
}
while ( ArenaPlayerCount );
}
return result;
}
</code></pre>
<p><img src="https://i.stack.imgur.com/a4W5E.png" alt="here is image of above"></p>
| IDA PRO Struct Pointer Counter big number not starting from address offset 0, Lowers a bit slightly but not all the way to 0 | |ida|struct|functions| |
<p><strong>Alignment.</strong></p>
<p>Note that all of the NOPs end (and the next function begins) at ...C0, ...F0. The compiler and/or linker inserted padding bytes so that the functions begin at 0x10 aligned addresses.</p>
<p>Different compilers / linkers will use different values for these bytes. I've seen 90 (nop), CC (int3), as well as multi-byte NOPs that exactly fill the space between the functions.</p>
<p>You should check out this great answer on the same question over at <a href="https://stackoverflow.com/a/7912617/119527">Stack Overflow</a>. </p>
<p>In short, this is done for performance reasons, as processors typically fetch instructions in 16- or 32-byte strides, so it makes sense to have functions begin at one of these boundaries.</p>
| 4084 | 2014-04-16T15:07:06.240 | <p>The test platform is on Linux 32bit, x86.
So basically I wrote a simple C program like this:</p>
<pre><code>void main()
{
double a = 10.0;
printf("hello world %f\n", a);
}
</code></pre>
<p>I use gcc to compile to into ELF binary, and use objdump to disassemble it. I solve the reference to .rodata section, and refine the asm code in this :</p>
<pre><code>extern printf
section .rodata
S_80484d0 db 0x68
db 0x65
db 0x6c
db 0x6c
db 0x6f
db 0x20
db 0x77
db 0x6f
db 0x72
db 0x6c
db 0x64
db 0x20
db 0x25
db 0x66
db 0x0a
S_80484f0 db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0x40
section .text
global main
main:
push ebp
mov ebp,esp
and esp,0xfffffff0
sub esp,0x20
fld qword [S_80484f0]
fstp QWORD [esp+0x18]
fld QWORD [esp+0x18]
fstp QWORD [esp+0x4]
mov DWORD [esp],S_80484d0
call printf
leave
ret
</code></pre>
<p>Then I re-compile this asm code to get a new ELF binary, and comparing the .text section of these two binaries.</p>
<p>Here is the confusing thing: The only different I can find is that there are more leading nop in front of the main function like this:</p>
<p>new ELF binary leading nop:</p>
<p><img src="https://i.stack.imgur.com/4CiRE.png" alt="new ELF binary"></p>
<p>new ELF binary ending nop:</p>
<p><img src="https://i.stack.imgur.com/nNKht.png" alt="new elf binary 2"></p>
<p>old ELF binary:</p>
<p><img src="https://i.stack.imgur.com/goaAd.png" alt="old ELF binary"></p>
<p>Basically I don't think it is kinda of "alignment" issue, because there are just too much nop.</p>
<p>What's more, when I change the original code into just a simple helloworld code(without double number a), then basically there is <strong>no difference</strong> between these two ELF binaries.</p>
<p>Could anyone give me some help on why there are so many nop generated?</p>
<p>Thank you </p>
| Why ther are some many padding/leading nop instructions in my binary code? | |disassembly|assembly|elf| |
<p>Hmm this code will probably not work if optimized because the shared globals are not pointers nor are they marked as volatile. This means that the compiler is free to assume the globals are accessed from one thread only so most likely the</p>
<p><code>while(!passcode)</code></p>
<p>will be optimized to always be true and therefore result in an eternal loop. Really should mark this volatile if they're intended to be shared between threads.</p>
<p>I think you're right about the race condition being what they expect you to exploit here. The key is to realize that the <em>read</em> from <em>code</em> happens in two instructions in the main thread and the <em>write</em> to <em>code</em> happens in two instructions in the spawned thread due to this being a 64 bit variable. So what you want to do is to hit it like this:</p>
<ul>
<li>The spawned thread writes two 32 bit parts of <em>code</em>. The lower 32 bits are equal to the lower 32 bits of <em>passcode</em> while the upper aren't. </li>
<li>The main thread then reads the lower 32 bit part of <em>code</em> into a register.</li>
<li>The spawned thread preempts (simply due to the processor being multicore), writes two 32 bit parts of <em>code</em>. The lower 32 bits are not equal to the lower 32 bits of <em>passcode</em> while the upper are.</li>
<li>The main thread then reads the upper 32 bit part of <em>code</em> into a register.</li>
<li>This means that the main thread will have the 64 bit value of <em>passcode</em> in its registers without that value being what's written by the spawned thread.</li>
</ul>
<p>In order to do this you need a very very long argument list with values such that this condition can happen. Possibly generating a few competing processes for processor time to maximize preemption. </p>
<p>The order of lower and upper bits being correct depend on the specifics of the machine code the threads are running. Which you probably know since I assume you have access to the binary. </p>
<p>The two load and store instructions are also likely to be positioned extremely close to each other so whether the exact sequence of events is likely to occur will be very dependent on the CPU architecture. It's possible that the instructions are seen as independent and scheduled on two different pipelines due to <a href="https://en.wikipedia.org/wiki/Superscalar" rel="nofollow">superscalarity</a>. This of course depends on how the CPU schedules loads and stores..</p>
<p>The issue seems much easier to exploit on a multicore system due to you not having to depend on super accurate preemption. It might also be harder to exploit on a 64 bit OS running a 32 bit process.</p>
<p>All of this doesn't really matter because in the end all you can really do is throw a huge number of arguments at it and pray as far as I can see.</p>
<h1>Spoilers</h1>
<p>As I was kind of curious under what conditions this was reliably exploitable I had to test some stuff. If you're doing the same, here's the fixed source and the exploit. This should allow you to do some local experimentation. Like I expected it seems to be extremely unlikely to hit a context switch at the right moment so on a single core CPU this is a nightmare. On a multicore CPU both threads are spinning in parallel so it's much more likely to hit the right conditions.</p>
<h2>Code</h2>
<pre><code>#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
volatile unsigned long long passcode = 0xbadc0dedecadeull;
volatile unsigned long long code = 0;
volatile unsigned long long v;
volatile int finished = 0;
void* tryAllCodes(void* ptr) {
char** codes = (char**) ptr;
while(*++codes) {
printf(".");
v = strtoull(*codes, 0, 16); // v is stored in ebx:ecx
code = (v != passcode)? v : 0;
}
finished = 1;
return 0;
}
int main(int argc, char** argv) {
char *args[] = { "/bin/sh", 0};
pthread_t t;
pthread_create(&t, NULL, tryAllCodes, argv);
while(!finished)
if(code == passcode) { // code is stored in ebx:ecx
printf("Win!\n");
execve(*args, args, 0);
}
pthread_join(t, NULL);
return 0;
}
</code></pre>
<h2>Compile with</h2>
<pre><code>gcc -std=c99 -Wall -Wpedantic -pthread -m32 -O2 -o race race.c
</code></pre>
<h2>Exploit with</h2>
<pre><code>./race `perl -e "print 'dedecade badc000000000 'x90870"`
</code></pre>
| 4101 | 2014-04-18T23:36:42.637 | <p>I've been trying for the past week to solve a wargame - where I am given a program and need to find the correct input (usually malformed, malicious, or otherwise) to get it to execute code that I want. I've been struggling with the following problem and wanted to see if anyone here has advice or ideas to try.
The code is the following</p>
<pre><code>unsigned long long passcode = 0xbadc0dedecadeull, code = 0, v;
int finished = 0;
void* tryAllCodes(void* ptr) {
char** codes = (char**) ptr;
while(*++codes) {
printf(".");
v = strtoull(*codes, 0, 16); // v is stored in ebx:ecx
code = (v != passcode)? v : 0;
}
finished = 1;
}
int main(int argc, char** argv) {
char *args[] = { "/bin/sh", 0};
pthread_t t;
pthread_create(&t, NULL, tryAllCodes, argv);
while(!finished)
if(code == passcode) { // code is stored in ebx:ecx
printf("Win!\n");
execve(*args, args, 0);
}
pthread_join(t, NULL);
return 0;
}
</code></pre>
<p>I believe there is a race condition here because there is no mutex around the access to the variable code, and I've gotten it to happen by setting breakpoints in the tryAllCodes function with gdb and letting the main thread continue to run. I believe this was because the registers that v and code are stored in when checking their value are the same, so if the context jumps out of tryAllCodes with v set to passcode before it gets zeroized I enter the win block.
Unfortunately I need to get this same behavior without using a debugger - so my question is does my approach of exploiting a race condition seem correct, or is there something else I'm overlooking? If so, is there a way on linux to cause a thread to yield execution more often? I tried renice on the tryAllCodes thread but it seems to just cause to program to spin in the main loop.</p>
<p>Thanks a bunch</p>
| Help with identifying race condition in wargame | |exploit|vulnerability-analysis| |
<ol>
<li><p><code>Edit -> Add struct type...</code></p></li>
<li><p><code>idagui.cfg</code>:</p>
<pre><code>//
// Keyboard hotkey definitions
// ---------------------------
//
"ReloadFile" = 0 // Reload the same input file
[....]
"AddStruct" = "Ins" // add struct type
</code></pre></li>
</ol>
| 4103 | 2014-04-19T09:50:42.553 | <p>I use the free version of IDA and I'm trying to define <code>structs</code>. Unfortunately, I'm using Windows XP as a VM on OSX and am having trouble remapping <kbd>Insert</kbd> to something else.</p>
<p>I'm hoping someone has solved this before. Any hints on fixing this would be mega useful !</p>
| osx + IDA + virtualbox, keyboard mapping for "Insert" | |ida|osx|virtual-machines| |
<p>There is a legitimate situation where this can occur, which is called "import forwarding". That is, a DLL (such as kernel32.dll) might export a symbol, for which the implementation is held in another DLL (such as ntdll.dll). A typical example of that is the heap allocation functions: HeapAlloc and HeapFree. If you examine kernel32.dll, you will see that they are redirected to ntdll.RtlAllocateHeap and ntdll.RtlFreeHeap. The reason for this is behavior is probably not particularly interesting (it's to do with maintaining compatibility).</p>
<p>That is not to say that all such cases of direct calls into ntdll.dll are legitimate, but it might explain what you are seeing.</p>
| 4106 | 2014-04-19T21:38:17.213 | <p>For fun I downloaded an installer from a "driver downloads" website. I do NOT intend on installing it, but I was interested to check it out just to see what it looked like. I tossed it into Ollydbg (without advancing the pointer) and just browsed the assembly. </p>
<p>There are multiple different calls/jumps/far jumps to various <code>ntdll.addr</code>. </p>
<p>I know <code>ntdll</code> is a pretty low-level library, but I just don't have enough experience in windows x86 disassembly to know what's '<em>normal</em>'. This is also ostensibly, a driver installer, so it seems reasonable that there would be calls into <code>ntdll</code>. Are jumps like that considered normal behavior? Usually I'm used to seeing calls that expressly name the function... not a specific address in <code>ntdll</code>. </p>
<h2>Extra detail/context</h2>
<p>The file wasn't packed, but there were a few spots where the full printable ASCII <code>[A-Za-z]</code> appeared in the hex view, which I recall could be a sign that there's base64 encoding/decoding going on somewhere. Maybe shift-ciphers. </p>
<p>For reference, the precise binary I'm peering into is <a href="http://driverscollection.com/?H=GM-M7700&By=Gigabyte" rel="nofollow">here</a>. Windows 7 64bits. (The installer itself appears to be 32bits... Ollydbg loaded it fine, and IDA free too.)</p>
<p>PEBrowse64 also showed me something possibly suspicious under <code>Resources->STRING->4085</code>.</p>
<p>In this section there appears to be strings set aside for <kbd>Pageup</kbd>/<kbd>Down</kbd>/<kbd>Backspace</kbd>/<kbd>Esc</kbd>/<kbd>Enter</kbd> keys, which I know could be a sign of a keylogger... but again, I'm new, so I'm unsure. </p>
| Are jump instructions targeting addresses in ntdll a sign of malware? | |disassembly|windows|malware| |
<p>I don't know about that version but you can find the latest version (0.4.8.892) here</p>
<p><a href="https://tuts4you.com/download.php?view.2028" rel="nofollow noreferrer">https://tuts4you.com/download.php?view.2028</a></p>
<p>For more information about OllyDbg plugin system see <strong><em><a href="https://stackoverflow.com/questions/14574155/how-to-setup-plugins-for-ollydbg-2-x-x">this</a></em></strong> and <strong><em><a href="http://www.ollydbg.de/Help/i_Plugins.htm" rel="nofollow noreferrer">this</a></em></strong></p>
| 4114 | 2014-04-21T14:47:36.340 | <p>I want plugin for OllyDbg - strongOD 0.3.4.639 version. Where I find it for download? Thank for response.</p>
| Where I download plugin StrongOD 0.3.4.639? | |ollydbg| |
<p>I'm not sure that it is possible to answer this question without seeing all the function because correctness of local variable type recovery can be done only by understanding of the context where variables are used. </p>
<p>However, I'd suggest the following algorithm for dealing with local variables
in Hex-Rays:</p>
<ol>
<li><p>As your friend said, do nothing with variables allocated on the registers.</p></li>
<li><p>For all other variables allocated on the stack, do the following:</p></li>
<li><p>Look where this variable is used.</p></li>
<li><p>If you see one of patterns below, act accordingly, but press F5 after each change and review results. Remember, you can revert each type back by pressing Y and entering previous type on it.</p></li>
<li><p>If you see usage of some <strong>different</strong> offsets relative to this variable it may be a structure. Try to create one by right-click on the variable and choosing corresponding menu item. Usage of this variable as function parameter or other connections to already known types may give you a hint about type of the variable. </p></li>
<li><p>If you see not constant offsets (such as offset in another variable) it can be array. Press asterisk (*) on the variable, IDA will suggest the length of the array, it does it relatively good.</p></li>
<li><p>If you see both of those patterns, it is possibly array of structures or more complicated case like array of structures in structure which is member of array and vice versa :) In this case try to find a smallest structure/array and start with it. </p></li>
</ol>
<p>At all, there are the following possibilities to affect the function stack in Hex-Rays:</p>
<ol>
<li>Changing variable type (press y on the variable, enter type) </li>
<li>Making variable an array (press * on it) </li>
<li>Creating new structure
type on a variable basis (Right click on the variable, corresponding
menu item)</li>
<li>Undefining a variable: Doubleclick on stack variable
will open stack function stack window. Pressing u on a variable will
undefine it. </li>
<li>Merging a variable: if you see that two different
variables are actually the same you can press = and tell to the
decompiler that they are really the same. AFAIR it works in IDA 6.5
and IDA6.5</li>
</ol>
<p>Don't forget to refresh the decompiled code view by pressing F5 after each change to see results.</p>
<p>That's all.</p>
<p>By the way, there is one point you are probably missing:
IDA and HexRays can make more than one variable on the same place in stack,
so v369, v370, v371, v372, v373 are using same place in the stack, but are different variables from decompiled code point of view.</p>
| 4117 | 2014-04-22T01:07:49.937 | <p>IDA PRO's Hex-Ray gives me these variables.</p>
<pre><code> void *v7; // esp@1
const char *v8; // ebx@1
PLAYER *v9; // ebp@1
int v10; // edi@5
PLAYER *v11; // edx@6
int v12; // ecx@9
int v13; // esi@17
int v14; // eax@33
const char v15; // al@36
const char *v16; // ebx@45
PLAYER *v17; // eax@50
int v18; // esi@51
const CHAR *v19; // ecx@54
int v20; // edx@56
unsigned int v21; // eax@61
signed int v22; // ebx@61
ARENA *v23; // eax@63
ARENA *v24; // edx@63
int v25; // ecx@63
int v26; // esi@64
signed int v27; // edi@65
int v28; // eax@66
ARENA *v29; // ecx@66
int v30; // esi@69
int v31; // edi@71
int v32; // esi@71
signed int v33; // ecx@74
int v34; // eax@75
const CHAR *v35; // edi@78
signed int v36; // ecx@78
signed int v37; // ecx@81
const void *v38; // esi@81
const CHAR *v39; // edi@81
unsigned int v40; // edx@81
signed int v41; // ecx@81
const char v42; // al@89
const char *v43; // ebx@89
const char v44; // al@90
const char v45; // al@96
const char *v46; // ebx@96
const char v47; // al@97
const char v48; // al@103
const char *v49; // ebx@103
const char v50; // al@104
int v51; // ebx@107
const char *v52; // edx@108
unsigned int v53; // kr48_4@108
unsigned int v54; // kr50_4@108
const char *v55; // edi@108
unsigned int v56; // kr58_4@110
const char *v57; // esi@110
const char *v58; // edi@110
int v59; // ecx@110
bool v60; // zf@110
int v61; // edi@121
int i; // esi@121
const char *v63; // eax@122
signed int v64; // esi@126
const char *v65; // ebx@134
PLAYER *v66; // eax@140
int v67; // edx@141
int v68; // ecx@141
PLAYER *v69; // eax@146
int v70; // edx@147
int v71; // ecx@147
const CHAR v72; // cl@153
int v73; // eax@153
const CHAR *j; // edx@153
char v75; // cl@156
const CHAR v76; // cl@157
int v77; // eax@157
const CHAR *k; // edx@157
char v79; // cl@160
const CHAR v80; // cl@161
int v81; // eax@161
const CHAR *l; // edx@161
char *v83; // eax@163
const CHAR v84; // al@171
int v85; // esi@171
const CHAR *m; // ecx@171
char v87; // al@174
const CHAR v88; // al@175
int v89; // esi@175
const CHAR *n; // ecx@175
const CHAR v91; // al@180
int v92; // esi@180
const CHAR *ii; // ecx@180
const char *v94; // ebx@189
const char v95; // al@200
const char *v96; // ebx@200
const char v97; // al@201
ARENA *v98; // eax@204
unsigned int v99; // kr68_4@208
int v100; // edi@208
PLAYER **v101; // ebx@209
int v102; // ebx@215
PLAYER *v103; // ecx@217
__int64 v104; // qax@218
int v105; // ecx@218
__int64 v106; // qax@218
int v107; // ST24_4@218
__int64 v108; // qax@218
PLAYER *v109; // esi@223
int v110; // ST24_4@224
int v111; // ST1C_4@224
int v112; // ST18_4@224
char *v113; // eax@224
ARENA *v114; // eax@226
signed int v115; // ecx@228
int v116; // eax@228
ENCRYPTION *v117; // ecx@230
int v118; // edi@230
int v119; // edx@233
int v120; // edi@233
unsigned __int64 v121; // st7@233
DWORD v122; // eax@235
int v123; // ecx@235
DWORD v124; // eax@237
unsigned __int64 v125; // st7@237
int v126; // ecx@237
int v127; // edx@239
DWORD v128; // edi@239
time_t v129; // ST20_8@242
signed __int64 v130; // qax@242
signed int v131; // edi@242
__int64 v132; // qax@243
__int64 v133; // ST20_8@243
signed int v134; // ecx@243
__int64 v135; // ST18_8@243
int v136; // ecx@247
PLAYER *v137; // eax@268
int v138; // ecx@269
const char v139; // al@276
const char *v140; // edi@276
const char v141; // al@277
const char v142; // al@279
__int16 v143; // ax@284
PLAYER *v144; // edi@289
ARENA *v145; // eax@292
int v146; // ecx@292
signed int v147; // edx@293
PLAYER *v148; // eax@294
ARENA *v149; // eax@296
int v150; // ecx@296
signed int v151; // edx@297
PLAYER *v152; // eax@298
PLAYER *v153; // eax@308
char *v154; // eax@309
const char v155; // al@312
const char *v156; // edi@312
const char v157; // al@313
ARENA *v158; // eax@318
unsigned int v159; // kr88_4@320
const char v160; // al@323
const char *v161; // edi@323
const char v162; // al@324
const char v163; // al@330
const char *v164; // edi@330
const char v165; // al@331
int v166; // eax@336
__int16 v167; // ax@342
PLAYER *v168; // esi@346
int v169; // edx@350
const char v170; // al@384
const char *v171; // edi@384
const char v172; // al@385
PLAYER *v173; // esi@392
int v174; // edx@397
PLAYER *v175; // esi@403
int v176; // edi@408
int v177; // eax@409
int v178; // esi@410
int v179; // eax@410
ARENA *v180; // ecx@413
int v181; // edi@415
ARENA **v182; // esi@416
PLAYER *v183; // ecx@423
int v184; // edi@427
PLAYER **v185; // esi@428
PLAYER *v186; // ecx@437
ARENA *v187; // ecx@443
FILE *v188; // eax@449
const char *v189; // edi@449
int v190; // ebx@451
int v191; // eax@451
FILE *v192; // eax@454
ARENA *v193; // eax@455
int v194; // esi@456
signed int v195; // edi@457
PLAYER *v196; // eax@458
const char *v197; // esi@461
int v198; // eax@461
signed int v199; // edi@463
int v200; // edx@471
time_t v201; // ST20_8@478
signed __int64 v202; // qax@478
signed int v203; // esi@478
signed int v204; // ecx@481
int v205; // edi@481
const char *v206; // esi@481
bool v207; // zf@481
const char *v208; // ebx@485
signed int v209; // ebx@490
char *v210; // eax@491
int v211; // ecx@491
int v212; // edi@494
signed int v213; // ecx@496
signed int v214; // ecx@499
const void *v215; // esi@499
const CHAR *v216; // edi@499
unsigned int v217; // edx@499
signed int v218; // ecx@499
const CHAR *v219; // edi@502
signed int v220; // ecx@502
signed int v221; // ecx@505
const void *v222; // esi@505
const CHAR *v223; // edi@505
unsigned int v224; // edx@505
signed int v225; // ecx@505
int v226; // ebx@516
signed int v227; // esi@516
ARENA *v228; // edx@520
int v229; // eax@520
int v230; // esi@520
int v231; // edi@520
int v232; // edx@520
int v233; // ecx@524
ARENA *v234; // eax@527
int v235; // edx@527
int v236; // ecx@527
__int16 v237; // ax@527
int v238; // eax@527
int v239; // ecx@527
int v240; // eax@527
int v241; // eax@540
ARENA *v242; // ecx@543
char *v243; // eax@543
char *v244; // edx@544
char *v245; // ebx@546
DWORD v246; // eax@546
ARENA *v247; // esi@547
char *v248; // esi@550
signed int v249; // ebp@554
int v250; // eax@557
int v251; // eax@562
signed int v252; // ecx@570
int v253; // edi@570
const char *v254; // esi@570
bool v255; // zf@570
int v256; // eax@575
const char *v257; // edx@578
ARENA *v258; // ecx@585
signed int v259; // ebx@586
PLAYER *v260; // ecx@587
signed int v261; // esi@587
__int64 v262; // qax@590
__int64 v263; // qax@591
int v264; // edi@602
signed int v265; // ecx@604
signed int v266; // ecx@607
const void *v267; // esi@607
const CHAR *v268; // edi@607
unsigned int v269; // edx@607
signed int v270; // ecx@607
const CHAR *v271; // edi@610
signed int v272; // ecx@610
signed int v273; // ecx@613
const void *v274; // esi@613
const CHAR *v275; // edi@613
unsigned int v276; // edx@613
signed int v277; // ecx@613
int v278; // edx@617
int v279; // ecx@621
int v280; // edx@638
char *v281; // ebx@638
unsigned int v282; // ebx@646
PLAYER *v283; // eax@646
signed int v284; // ecx@654
signed int v285; // ecx@658
signed int v286; // ecx@662
signed int v287; // ecx@671
int v288; // edi@671
const char *v289; // esi@671
bool v290; // zf@671
signed int v291; // ecx@675
int v292; // edi@675
const char *v293; // esi@675
bool v294; // zf@675
signed int v295; // ecx@682
int v296; // eax@683
const char v297; // al@688
const char *v298; // edi@688
const CHAR *jj; // ecx@688
const char v300; // al@691
const CHAR v301; // al@692
int v302; // edi@692
const CHAR *kk; // ecx@692
const char v304; // al@696
int *v305; // edi@704
int *v306; // esi@704
char v307; // al@707
int v308; // ecx@708
int v309; // edi@708
int v310; // esi@708
const char *v311; // eax@709
ARENA *v312; // edi@711
ARENA *v313; // ecx@718
int v314; // eax@718
int v315; // eax@723
__int64 v316; // qax@727
__int64 v317; // qax@728
__int64 v318; // qax@729
const char *v319; // ebx@730
int v320; // edi@737
char *v321; // ST1C_4@737
int v322; // esi@737
ARENA *v323; // eax@738
ARENA *v324; // eax@743
DWORD v325; // eax@748
int v326; // edx@749
PLAYER *v327; // esi@749
bool v328; // sf@749
ARENA **v329; // edi@750
ARENA *v330; // eax@751
int v331; // ebx@751
signed int v332; // esi@752
unsigned int v333; // krD8_4@774
ARENA **v334; // esi@775
int v335; // ebp@776
signed int v336; // edi@777
ARENA *v337; // edx@784
int v338; // esi@785
int v339; // ecx@785
const char *v340; // ecx@797
const char v341; // al@799
const CHAR *ll; // edx@799
const char v343; // al@803
const char v344; // al@804
const char *v345; // ecx@804
int v346; // eax@819
int v347; // eax@833
unsigned int v348; // krE8_4@836
int v349; // edi@836
PLAYER **v350; // ebp@837
char *v351; // [sp+4h] [bp-1608Ch]@668
const char *v352; // [sp+8h] [bp-16088h]@668
char *v353; // [sp+Ch] [bp-16084h]@52
char *v354; // [sp+Ch] [bp-16084h]@351
char *v355; // [sp+Ch] [bp-16084h]@598
char *v356; // [sp+Ch] [bp-16084h]@644
int v357; // [sp+Ch] [bp-16084h]@668
const char *v358; // [sp+10h] [bp-16080h]@52
const char *v359; // [sp+10h] [bp-16080h]@57
const char *v360; // [sp+10h] [bp-16080h]@218
const char *v361; // [sp+10h] [bp-16080h]@351
int v362; // [sp+10h] [bp-16080h]@525
const char *v363; // [sp+10h] [bp-16080h]@598
const char *v364; // [sp+10h] [bp-16080h]@644
int v365; // [sp+10h] [bp-16080h]@668
int v366; // [sp+14h] [bp-1607Ch]@52
char v367; // [sp+14h] [bp-1607Ch]@54
char v368; // [sp+14h] [bp-1607Ch]@218
int v369; // [sp+14h] [bp-1607Ch]@351
int v370; // [sp+14h] [bp-1607Ch]@525
int v371; // [sp+14h] [bp-1607Ch]@598
char *v372; // [sp+14h] [bp-1607Ch]@644
int v373; // [sp+14h] [bp-1607Ch]@668
time_t v374; // [sp+18h] [bp-16078h]@1
ARENA *a5[2]; // [sp+28h] [bp-16068h]@63
int v376; // [sp+30h] [bp-16060h]@69
int v377; // [sp+34h] [bp-1605Ch]@61
DWORD ExitCode; // [sp+38h] [bp-16058h]@233
char v379; // [sp+3Fh] [bp-16051h]@420
size_t Size; // [sp+40h] [bp-16050h]@69
int v381; // [sp+44h] [bp-1604Ch]@233
int v382; // [sp+48h] [bp-16048h]@69
int v383; // [sp+4Ch] [bp-16044h]@69
char v384; // [sp+50h] [bp-16040h]@527
__int16 v385; // [sp+51h] [bp-1603Fh]@527
int v386; // [sp+53h] [bp-1603Dh]@527
int v387; // [sp+57h] [bp-16039h]@527
__int16 v388; // [sp+5Bh] [bp-16035h]@527
__int16 v389; // [sp+5Dh] [bp-16033h]@527
int v390; // [sp+60h] [bp-16030h]@233
const CHAR KeyName; // [sp+64h] [bp-1602Ch]@157
const CHAR CommandLine; // [sp+84h] [bp-1600Ch]@52
char v393; // [sp+85h] [bp-1600Bh]@773
const CHAR Dest; // [sp+184h] [bp-15F0Ch]@69
__int16 v395; // [sp+185h] [bp-15F0Bh]@342
char v396; // [sp+187h] [bp-15F09h]@342
const CHAR Str1; // [sp+284h] [bp-15E0Ch]@153
const CHAR AppName; // [sp+2C4h] [bp-15DCCh]@76
int buf; // [sp+304h] [bp-15D8Ch]@45
char v400; // [sp+309h] [bp-15D87h]@208
char v401; // [sp+404h] [bp-15C8Ch]@255
char v402; // [sp+405h] [bp-15C8Bh]@255
char v403; // [sp+414h] [bp-15C7Ch]@255
CHAR StartupInfo[4]; // [sp+504h] [bp-15B8Ch]@163
int v405; // [sp+510h] [bp-15B80h]@309
char v406; // [sp+604h] [bp-15A8Ch]@741
char v407; // [sp+605h] [bp-15A8Bh]@741
char v408; // [sp+606h] [bp-15A8Ah]@741
__int16 v409; // [sp+607h] [bp-15A89h]@748
char v410; // [sp+609h] [bp-15A87h]@765
char v411; // [sp+804h] [bp-1588Ch]@638
char v412; // [sp+805h] [bp-1588Bh]@638
char v413; // [sp+2804h] [bp-1388Ch]@543
char Str; // [sp+2805h] [bp-1388Bh]@544
char v415; // [sp+2815h] [bp-1387Bh]@546
int v416; // [sp+16084h] [bp-Ch]@1
int (*v417)(); // [sp+16088h] [bp-8h]@1
int v418; // [sp+1608Ch] [bp-4h]@1
const char *Buf1b; // [sp+160A0h] [bp+10h]@485
const char *Buf1a; // [sp+160A0h] [bp+10h]@489
</code></pre>
<p>I was told on email by a very skilled reverser how to handle this, I don't know if he wants me to say his/her name so I'll not say anything.<br></p>
<p>But they said start doing repairs to arrays or structures starting at <code>[sp+####h]</code> and do calculations from that.</p>
<p>So the first part to start repairing is the since it begins with <code>[sp+#h]</code></p>
<pre><code>char *v351; // [sp+4h] [bp-1608Ch]@668
</code></pre>
<p>Now you go down to</p>
<pre><code> char *v372; // [sp+14h] [bp-1607Ch]@644
int v373; // [sp+14h] [bp-1607Ch]@668
time_t v374; // [sp+18h] [bp-16078h]@1
ARENA *a5[2]; // [sp+28h] [bp-16068h]@63
int v376; // [sp+30h] [bp-16060h]@69
</code></pre>
<p>So</p>
<pre><code> char *v372; // [sp+14h] [0]
int v373; // [sp+14h] 14-14 = 0 goes up [4]
time_t v374; // [sp+18h] [bp-16078h]@1 18-14 = 4 goes up [16]
ARENA *a5[2]; // [sp+28h] [bp-16068h]@63 28-18 = 16 goes up [8]
int v376; // [sp+30h] [bp-16060h]@69 30-28 = 8 goes up [4]
int v377; // [sp+34h] [bp-1605Ch]@61 34-30 = 4 goes up [ignored]
</code></pre>
<p>So <code>0</code> would mean char? yet it's a pointer to a char (4 bytes)?, Probably shouldn't touch those<br>
The <code>4</code> after would be int which seems right.<br>
The <code>16</code> after looks like <code>2 x 8 bytes</code>.<br>
Since time_t could be 4 bytes or 8 bytes.<br></p>
<p>I checked and time_t is defined as<br></p>
<pre><code>-00016080 var_16080 dd 6 dup(?) ; offset
</code></pre>
<p>So it thinks it's 4 bytes x 6 which would be 24 bytes? why does it think that? Yes I get that <code>var_16080</code> when I click on the <code>time_t</code></p>
<p>So this is where I get confused I think it's really 8 bytes and all I have to do is make it <code>time_t v374[2]</code></p>
<p>ARENA *a5[2]; looks right 2 pointers of 4 bytes. = 8 bytes and the int after looks right.</p>
<p>Can someone tell me how what to do in certain hard cases if I have to do it all manually I would do it.. </p>
<p>But if there is a way to automate this I'd also appreciate that if anyone can tell me of a plugin or a script to do that.</p>
<p>Here is how I got it down to is it right?</p>
<pre><code> int v369; // [sp+14h] [bp-1607Ch]@351
int v370; // [sp+14h] [bp-1607Ch]@525
int v371; // [sp+14h] [bp-1607Ch]@598
char *v372; // [sp+14h] [bp-1607Ch]@644
int v373; // [sp+14h] [bp-1607Ch]@668
char v374[16]; // [sp+18h] [bp-16078h]@1
ARENA *a5[2]; // [sp+28h] [bp-16068h]@63
int v376; // [sp+30h] [bp-16060h]@69
int v377; // [sp+34h] [bp-1605Ch]@61
char ExitCode[7]; // [sp+38h] [bp-16058h]@233
char v379; // [sp+3Fh] [bp-16051h]@420
size_t Size; // [sp+40h] [bp-16050h]@69
int v381; // [sp+44h] [bp-1604Ch]@233
int v382; // [sp+48h] [bp-16048h]@69
</code></pre>
<p>A bit lower you see this</p>
<pre><code> __int16 v385; // [sp+51h] [bp-1603Fh]@527 2
int v386; // [sp+53h] [bp-1603Dh]@527 53-51 = 2 up [4]
int v387; // [sp+57h] [bp-16039h]@527 57-53 = 4 up [4]
__int16 v388; // [sp+5Bh] [bp-16035h]@527 5B-57 = 4 up [2]
__int16 v389; // [sp+5Dh] [bp-16033h]@527 5D-5B = 2 up [3] ???
int v390; // [sp+60h] [bp-16030h]@233 60-5D = 3 up
</code></pre>
<p>Why is that one 3 bytes when it's a <code>__int16</code> or is it a <code>char[3]</code> ?</p>
<p>After I translated it to</p>
<pre><code> int v386; // [sp+53h] [bp-1603Dh]@527
int v387; // [sp+57h] [bp-16039h]@527
__int16 v388; // [sp+5Bh] [bp-16035h]@527
char v389[3]; // [sp+5Dh] [bp-16033h]@527
int v390; // [sp+60h] [bp-16030h]@233
</code></pre>
<p>Now it does something like this in code, so it must of been <code>__int16</code> after all. Maybe it's a <code>__int16</code> followed by a <code>char</code> after, but you can't do that in Hex-Rays afaik.</p>
<pre><code> *(_WORD *)v389 = v236;
</code></pre>
<p>Okay it's highly unpredictable and I don't think I can rely on the <code>[so+###h]</code>'s too much just in some cases.</p>
<p>I managed to get it down to this, but it has problems all over the place</p>
<p>Trimmed off the top stuff that didn't change</p>
<pre><code> char *v351; // [sp+4h] [bp-1608Ch]@668
const char *v352; // [sp+8h] [bp-16088h]@668
char *v353; // [sp+Ch] [bp-16084h]@52
char *v354; // [sp+Ch] [bp-16084h]@351
char *v355; // [sp+Ch] [bp-16084h]@598
char *v356; // [sp+Ch] [bp-16084h]@644
int v357; // [sp+Ch] [bp-16084h]@668
const char *v358; // [sp+10h] [bp-16080h]@52
char *v359; // [sp+10h] [bp-16080h]@57
char *v360; // [sp+10h] [bp-16080h]@218
const char *v361; // [sp+10h] [bp-16080h]@351
int v362; // [sp+10h] [bp-16080h]@525
const char *v363; // [sp+10h] [bp-16080h]@598
const char *v364; // [sp+10h] [bp-16080h]@644
int v365; // [sp+10h] [bp-16080h]@668
int v366; // [sp+14h] [bp-1607Ch]@52
char v367; // [sp+14h] [bp-1607Ch]@54
char v368; // [sp+14h] [bp-1607Ch]@218
int v369; // [sp+14h] [bp-1607Ch]@351
int v370; // [sp+14h] [bp-1607Ch]@525
int v371; // [sp+14h] [bp-1607Ch]@598
char *v372; // [sp+14h] [bp-1607Ch]@644
int v373; // [sp+14h] [bp-1607Ch]@668
char v374[16]; // [sp+18h] [bp-16078h]@1
ARENA *a5[2]; // [sp+28h] [bp-16068h]@63
int v376; // [sp+30h] [bp-16060h]@69
int v377; // [sp+34h] [bp-1605Ch]@61
char ExitCode[7]; // [sp+38h] [bp-16058h]@233
char v379; // [sp+3Fh] [bp-16051h]@420
size_t Size; // [sp+40h] [bp-16050h]@69
int v381; // [sp+44h] [bp-1604Ch]@233
int v382; // [sp+48h] [bp-16048h]@69
int v383; // [sp+4Ch] [bp-16044h]@69
char v384; // [sp+50h] [bp-16040h]@527
__int16 v385; // [sp+51h] [bp-1603Fh]@527
int v386; // [sp+53h] [bp-1603Dh]@527
int v387; // [sp+57h] [bp-16039h]@527
__int16 v388; // [sp+5Bh] [bp-16035h]@527
char v389[3]; // [sp+5Dh] [bp-16033h]@527
int v390; // [sp+60h] [bp-16030h]@233
char array1[32]; // [sp+64h] [bp-1602Ch]@157
char array2[256]; // [sp+84h] [bp-1600Ch]@52
char Dest[256]; // [sp+184h] [bp-15F0Ch]@69
char Str1[64]; // [sp+284h] [bp-15E0Ch]@153
char AppName[64]; // [sp+2C4h] [bp-15DCCh]@76
char buf[512]; // [sp+304h] [bp-15D8Ch]@45
char StartupInfo[256]; // [sp+504h] [bp-15B8Ch]@163
char v398[512]; // [sp+604h] [bp-15A8Ch]@741
char v399[8192]; // [sp+804h] [bp-1588Ch]@638
char v400[80000]; // [sp+2804h] [bp-1388Ch]@543
int v401; // [sp+16084h] [bp-Ch]@1
int (*v402)(); // [sp+16088h] [bp-8h]@1
int v403; // [sp+1608Ch] [bp-4h]@1
const char *Buf1b; // [sp+160A0h] [bp+10h]@485
const char *Buf1a; // [sp+160A0h] [bp+10h]@489
</code></pre>
<p>Seems all the array/buffers are only found at the bottom of all the variables all of them are buffers completely (maybe it's just the type of compiler / optimizer this program used). </p>
<p>I wonder what If it probably reuses all the buffers as just one big buffer?</p>
| IDA PRO repairing stack variables / local variables hex-rays with arrays and proper types / structures any scripts/plugins? | |ida|ida-plugin|local-variables|stack-variables| |
<p>Normally, the first parameter to <code>CallWindowProc</code> would be a function pointer. <code>CallWindowProc</code> treats the pointer to the string as a function pointer, and calls it. As long as the heap that the string exists in is executable, it will be executed as if it were machine code. The other four parameters passed to <code>CallWindowProc</code> are also passed to the function pointer, and can be used by the machine code however it's designed. In this case, it looks like the machine code your script is loading is a decryption subroutine. Try looking at the machine code in a disassembler.</p>
| 4125 | 2014-04-23T08:35:55.270 | <p>I recently came across a script that would read a file into a string, then do the equivalent of</p>
<pre><code>CallWindowProc((pointer to string), (pointer to encrypted string), (length of encrypted string), 0, 0)
</code></pre>
<p>in the scripting language to decrypt an encrypted string defined earlier in the file. What's going on here? How did the call to <code>CallWindowProc</code> decrypt the string?</p>
| What does this call to CallWindowProc do? | |windows|dynamic-linking| |
<p>I'm far from an authoritative source but I've personally never seen x-rays being used to identify anything but defects or bad joints under BGA chips. I don't think x-rays would be a good fit here because the die is extremely likely to appear like a solid blob with possibly the bonding wires visible.</p>
<p>The cheapest way I know of to accomplish what you want to do is by grinding, usually called polishing, the chip using a very very fine sandpaper or abrasive surface to remove the packaging and different metal layers. In your case you probably only want to remove the protective polymer. If you go this route you need to take extreme care when it comes to aligning the chip and the grinding surface so that your material removal isn't slanted.</p>
<p>The other, and similarly cheap, method is a chemical etch, also called wet etching. This is usually very dangerous though if you're not very careful and make damn sure that your ventilation is good. It usually involves nitric or sulphuric acid. I won't discuss it in detail here. </p>
<p>For an overview of different techniques please see <a href="http://siliconpr0n.org/wiki/doku.php?id=decap%3astart" rel="nofollow">siliconpr0n's article on decapsulation</a>. t4f has a decent article on low cost <a href="http://www.t4f.org/articles/ultra-low-cost-ic-decapsulation/" rel="nofollow">decapsulation using a chemical etch</a>. Siliconzoon has <a href="http://siliconzoo.org/tutorial.html" rel="nofollow">a nice tutorial</a> for the very basics of understanding what you're looking at once you've decapsulated, polished and imaged the metal layers of your ICs.</p>
<p>If you're not hell bent on doing it on your own there's a number of companies that offer decapsulation services for you. Usually this service doesn't have to be very expensive due to them using automated decapsulation machines, usually by so called jet etching. They usually send you the decapsulated sample for analysis. There's probably services out there willing to take high resultion, high magnification pictures of the various metal layers for you as well. My recommendation would be to send a sample to one of these firms, it will cost you less than 100 USD, you're very likely to get a working sample back and it also has the highest success to chemical burn ratio.</p>
| 4128 | 2014-04-23T13:35:16.920 | <p>I am on the hunt for several ICs by searching for the markings on them. But sometimes the markings are just too vague and I cannot really guarantee that the IC I found is the IC I am looking for. I know that some manufacturers place a logo or something equivalent on the die itself, and that it is possible to X-Ray the package in order to see the bare die.</p>
<p>My question is, is there any home-made (preferably cheap) alternative to the X-Ray method?</p>
| Any Home Made Alternatives to X-Ray ICs? | |integrated-circuit| |
<p>It's a binary search. I've renamed several of the variables, and in one case, introduced a new variable, because one of the local variables was used for one thing in the first half of the function and something else in the second half of the function.</p>
<p>The only tricky part is that once it finds an occurrence of the string to find, it iterates to find the <em>first</em> occurrence.</p>
<pre><code>#include <string.h>
typedef const char* MYTYPE;
typedef char* PTR_TYPE;
PTR_TYPE __cdecl SomeKindOfSortAlgorithm(MYTYPE elementToFind, PTR_TYPE array, unsigned int numElts, unsigned int eltSize, int (__cdecl *compare)(MYTYPE, PTR_TYPE), bool* pFound)
{
PTR_TYPE mid; // esi@1
int result; // eax@1
PTR_TYPE lower_bound; // ebp@2
PTR_TYPE upper_bound; // edi@2
mid = 0;
result = 0;
*pFound = false;
if ( !numElts )
return NULL;
lower_bound = array;
upper_bound = array + eltSize * (numElts - 1);
if ( array > upper_bound )
{
NOT_FOUND:
if ( result > 0 )
mid += eltSize;
return mid;
}
while ( 1 )
{
mid = lower_bound + eltSize * (upper_bound - lower_bound) / eltSize / 2;
result = compare(elementToFind, mid);
if ( result < 0 ) // elementToFind should go before mid
{
if ( mid == array )
goto NOT_FOUND;
upper_bound = mid - eltSize;
goto CHECK_LOOP_END;
}
if ( result <= 0 ) // elementToFind equals the element at mid
break;
// elementToFind should go after mid
lower_bound = mid + eltSize;
CHECK_LOOP_END:
if ( lower_bound > upper_bound )
goto NOT_FOUND;
}
PTR_TYPE pFirstOccurrance;
*pFound = true;
if ( mid == array )
{
AT_FIRST_ELEMENT:
pFirstOccurrance = array;
}
else
{
while ( 1 )
{
mid -= eltSize;
if ( compare(elementToFind, mid) ) // elementToFind != element at mid
break;
if ( mid == array )
goto AT_FIRST_ELEMENT;
}
pFirstOccurrance = mid + eltSize;
}
return pFirstOccurrance;
}
int __cdecl StrCmpiWrapper(MYTYPE element, PTR_TYPE arrayPointer)
{
return _strcmpi(element, *(MYTYPE*)arrayPointer);
}
int main(int argc, char* argv[])
{
MYTYPE lookFor = "def";
MYTYPE* pFirstOccurrance; // eax@1
bool found; // [sp+10h] [bp-4h]@1
MYTYPE data[3] = {
"abc",
"def",
"ghi"
};
pFirstOccurrance = (MYTYPE*)SomeKindOfSortAlgorithm(
lookFor,
(PTR_TYPE)data,
3,
sizeof(MYTYPE),
StrCmpiWrapper,
&found);
if ( !found && pFirstOccurrance )
{
//Allocate memory .. copy it and other stuff here.
}
return 0;
}
</code></pre>
| 4139 | 2014-04-25T04:21:01.057 | <p>I decompiled a application and found what seems like some kind of sorting algorithm, I was told it's not even a sorting algorithm, but a binary search on stackoverflow</p>
<p>I just don't know which one it is can someone let me know it's actual name?</p>
<p>whatever is passed into the strcmpi wrapper is in some area's divided by 2 who knows some crazy stuff.. I thought it was qsort (quicksort) since it's a standard library for C. But i'm not sure.</p>
<pre><code>int __cdecl SomeKindOfSortAlgorithm(int a1, int a2, int a3, signed int a4, int (__cdecl *a5)(unsigned int, unsigned int), int a6)
{
int v6; // esi@1
int result; // eax@1
int v8; // ebp@2
int v9; // edi@2
v6 = 0;
result = 0;
*(unsigned int *)a6 = 0;
if ( !a3 )
return result;
v8 = a2;
v9 = a2 + a4 * (a3 - 1);
if ( a2 > (unsigned int)v9 )
{
LABEL_9:
if ( result > 0 )
v6 += a4;
return v6;
}
while ( 1 )
{
v6 = v8 + a4 * (v9 - v8) / a4 / 2;
result = a5(a1, v8 + a4 * (v9 - v8) / a4 / 2);
if ( result < 0 )
{
if ( v6 == a2 )
goto LABEL_9;
v9 = v6 - a4;
goto LABEL_8;
}
if ( result <= 0 )
break;
v8 = v6 + a4;
LABEL_8:
if ( v8 > (unsigned int)v9 )
goto LABEL_9;
}
*(unsigned int *)a6 = 1;
if ( v6 == a2 )
{
LABEL_15:
result = a2;
}
else
{
while ( 1 )
{
v6 -= a4;
if ( a5(a1, v6) )
break;
if ( v6 == a2 )
goto LABEL_15;
}
result = v6 + a4;
}
return result;
}
</code></pre>
<p>Here is the compare function</p>
<pre><code>int __cdecl StrCmpiWrapper(const char *Str1, const char **a2)
{
return _strcmpi(Str1, *a2);
}
</code></pre>
<p>Here is how you use it.</p>
<pre><code> int ChatMsgBuffer;
int v4; // eax@1
int v5; // eax@5
int v8; // [sp+10h] [bp-4h]@1
v4 = SomeKindOfSortAlgorithm(
ChatMsgBuffer,
textFile->Pointer,
textFile->TotalElements,
4,
(int (__cdecl *)(unsigned int, unsigned int))StrCmpiWrapper,
(int)&v8);
if ( !v8 && v4 )
{
//Allocate memory .. copy it and other stuff here.
}
</code></pre>
<p>Here is how a real qsort looks when decompiled</p>
<pre><code>void __cdecl sub_4015D0(int a1, unsigned int a2, unsigned int a3, unsigned int a4, int (__cdecl *a5)(_DWORD, _DWORD, _DWORD), int a6)
{
unsigned int v6; // esi@2
int v7; // edi@9
unsigned int v8; // esi@32
int v9; // esi@38
unsigned int k; // edi@41
unsigned int v11; // edi@43
void *v12; // edi@52
int j; // [sp+Ch] [bp-20h]@52
unsigned int v14; // [sp+10h] [bp-1Ch]@16
int v15; // [sp+14h] [bp-18h]@11
int v16; // [sp+14h] [bp-18h]@16
unsigned int v17; // [sp+18h] [bp-14h]@9
int v18; // [sp+1Ch] [bp-10h]@2
unsigned int v19; // [sp+28h] [bp-4h]@2
unsigned int i; // [sp+38h] [bp+Ch]@38
while ( a3 )
{
if ( a2 <= 0x20 )
goto LABEL_37;
v19 = a1 + a4 * a2;
v6 = a1 + a4 * (a2 >> 1);
v18 = a4 + v6;
sub_401420(a1, a1 + a4 * (a2 >> 1), a1 + a4 * a2 - a4, a4, a5, a6);
while ( a1 < v6 && !a5(v6 - a4, v6, a6) )
v6 -= a4;
while ( v18 < v19 && !a5(v18, v6, a6) )
v18 += a4;
v7 = v18;
v17 = v6;
while ( 1 )
{
while ( 1 )
{
for ( ; v7 < v19; v7 += a4 )
{
v15 = a5(v6, v7, a6);
if ( v15 >= 0 )
{
if ( v15 > 0 )
break;
sub_401160(v18, v7, a4);
v18 += a4;
}
}
if ( a1 < v17 )
{
do
{
v14 = v17 - a4;
v16 = a5(v17 - a4, v6, a6);
if ( v16 >= 0 )
{
if ( v16 > 0 )
break;
v6 -= a4;
sub_401160(v6, v14, a4);
}
v17 -= a4;
}
while ( a1 < v14 );
}
if ( v17 == a1 )
break;
LABEL_27:
if ( v7 == v19 )
{
v17 -= a4;
v18 -= a4;
v6 -= a4;
if ( v17 == v6 )
sub_401160(v6, v18, a4);
else
sub_401220(v17, v18, v6, a4);
}
else
{
v17 -= a4;
sub_401160(v7, v17, a4);
v7 += a4;
}
}
if ( v7 == v19 )
break;
if ( v17 != a1 )
goto LABEL_27;
if ( v18 == v7 )
sub_401160(v7, v6, a4);
else
sub_401220(v7, v6, v18, a4);
v7 += a4;
v6 += a4;
v18 += a4;
}
a3 = (a3 >> 2) + (a3 >> 1);
v8 = (v6 - a1) / a4;
a2 = (v19 - v18) / a4;
if ( v8 > a2 )
{
sub_4015D0(v18, a2, a3, a4, a5, a6);
a2 = v8;
}
else
{
sub_4015D0(a1, v8, a3, a4, a5, a6);
a1 = v18;
}
}
if ( a2 <= 0x20 )
{
LABEL_37:
if ( a2 > 1 )
{
v9 = a1;
for ( i = a2 - 1; i; --i )
{
v9 += a4;
if ( a5(v9, a1, a6) >= 0 )
{
v12 = (void *)v9;
for ( j = v9; ; v12 = (void *)j )
{
j -= a4;
if ( a5(v9, j, a6) >= 0 )
break;
}
if ( v12 != (void *)v9 )
sub_401310(v12, v9, a4);
}
else
{
sub_401310((void *)a1, v9, a4);
}
}
}
return;
}
for ( k = a2 >> 1; k; sub_401500(a1, k, a2, a4, a5, a6) )
--k;
v11 = a1 + a4 * a2;
while ( a2 > 1 )
{
v11 -= a4;
sub_401160(a1, v11, a4);
--a2;
sub_401500(a1, 0, a2, a4, a5, a6);
}
}
int __cdecl sub_401420(int a1, int a2, int a3, unsigned int a4, int a5, int a6)
{
unsigned int v6; // edi@2
int result; // eax@2
if ( 40 * a4 >= a3 - a1 )
{
result = sub_4013B0(a1, a2, a3, a4, a5, a6);
}
else
{
v6 = a4 * (((a3 - a1) / a4 >> 3) + 1);
sub_4013B0(a1, v6 + a1, a1 + 2 * v6, a4, a5, a6);
sub_4013B0(a2 - v6, a2, a2 + v6, a4, a5, a6);
sub_4013B0(a3 - 2 * v6, a3 - v6, a3, a4, a5, a6);
result = sub_4013B0(a1 + v6, a2, a3 - v6, a4, a5, a6);
}
return result;
}
int __cdecl sub_401500(int a1, unsigned int a2, unsigned int a3, int a4, int (__cdecl *a5)(_DWORD, _DWORD, _DWORD), int a6)
{
int v6; // ebx@1
int v7; // esi@1
int i; // edi@1
int result; // eax@6
unsigned int v10; // ebx@7
unsigned int v11; // edi@7
int v12; // [sp+Ch] [bp-4h]@1
v12 = a2;
v6 = 2 * a2 + 2;
v7 = a1 + a4 * a2;
for ( i = a1 + a4 * (2 * a2 + 2); v6 <= a3; i = a1 + a4 * v6 )
{
if ( v6 == a3 || a5(i, i - a4, a6) < 0 )
{
--v6;
i -= a4;
}
sub_401160(v7, i, a4);
a2 = v6;
v7 = i;
v6 = 2 * v6 + 2;
}
result = v12;
if ( v12 < a2 )
{
do
{
v10 = (a2 - 1) >> 1;
v11 = a1 + a4 * ((a2 - 1) >> 1);
result = a5(v7, a1 + a4 * ((a2 - 1) >> 1), a6);
if ( result <= 0 )
break;
sub_401160(v11, v7, a4);
a2 = (a2 - 1) >> 1;
v7 = v11;
result = v12;
}
while ( v12 < v10 );
}
return result;
}
</code></pre>
| C What kind of sorting algorithm is this? | |ida|decompilation|c| |
<p>It doesn't seem to be on the IDA snapshot. You probably identified the right part of the program flow to set the initial <code>key</code> array. and assumed that the left part should have something to do with the <code>iv</code>, but that's not true. <code>iv</code> is 16 bytes, the left part of the IDA disassembly defines 24 bytes, which is the same length as <code>key</code>. Also, the very top of the image shows there's an if condition that branches either to setting up the key or setting up the left part, so these branches exclude each other.</p>
<p>After the key initialization on the right part, the MD5 calculation is performed, then there's the call to <code>dword ptr eax+2AH</code> where <code>eax</code> is set up to <code>[ebp+0]</code>, and <code>ebp</code> itself being the top item on the stack. The parameters are put on the stack - <code>ebp</code> on top, then <code>edx=esp+40</code>, then <code>edx=esp+60</code>. <code>esp+60</code> is setup earlier to be the location of the result of the call that preludes <code>MD5_Init</code>, and obviously the string being MD5'ed <code>(MD5_Update(x, esp+60, strlen(esp+60))</code>. This pattern - <code>ebp</code> being a pointer to data and functions, with the first parameter of the functions being <code>ebp</code> - hints at <code>ebp</code> being a pointer to a C++ class.</p>
<p>Overall, the right part sets up the key, calls a class method, does the MD5 stuff, and calls another class method. Comparing that to the python script, it seems like the first class method performs the python "read from file", the second the XOR-ing. The CVP.cipher call comes later, not shown on the IDA snapshot, so you can't see how iv is set up.</p>
<p>The left part of the IDA snapshot is something else, maybe code for decrypting old versions of the database. The top of the IDA snapshot says "perform the right part if something is equal to 5"; this could be a version number, with lower versions branching to the left; however, the IDA disassemble doesn't show the conditions for the left side branch, so this is guesswork.</p>
| 4142 | 2014-04-20T21:57:10.790 | <p>I was going through <a href="http://www.securitybydefault.com/2014/03/descifrando-msgstoredbcrypt5-la-nueva.html" rel="nofollow noreferrer">DECIPHERING MSGSTORE.DB.CRYPT5, THE NEW DATABASE WHATSAPP</a>
& <a href="http://bas.bosschert.nl/steal-whatsapp-update/" rel="nofollow noreferrer">Steal WhatsApp update</a>.</p>
<p>The author of the first link published a <a href="https://github.com/aramosf/pwncrypt5/blob/master/pwncrypt5.py" rel="nofollow noreferrer">decryptor</a> which sets the iv for the <code>aes_192_cbc</code> to </p>
<pre><code>iv = bytearray([0x1E,0x39,0xF3,0x69,0xE9,0xD,0xB3,0x3A,0xA7,0x3B,0x44,0x2B,0xBB,0xB6,0xB0,0xB9])
</code></pre>
<p>Here is the full decryptor:</p>
<pre><code>#!/usr/bin/python
"""
48bits presents:
8===============================================D~~~
WhatsApp msgstore crypt5 decryptor by grbnz0 and nullsub
8===============================================D~~~
"""
import sys
import hashlib
import StringIO
from M2Crypto import EVP
key = bytearray([141, 75, 21, 92, 201, 255, 129, 229, 203, 246, 250, 120, 25, 54, 106, 62, 198, 33, 166, 86, 65, 108, 215, 147])
iv = bytearray([0x1E,0x39,0xF3,0x69,0xE9,0xD,0xB3,0x3A,0xA7,0x3B,0x44,0x2B,0xBB,0xB6,0xB0,0xB9])
def decrypt(db,acc):
fh = file(db,'rb')
edb = fh.read()
fh.close()
m = hashlib.md5()
m.update(acc)
md5 = bytearray(m.digest())
for i in xrange(24): key[i] ^= md5[i&0xF]
cipher = EVP.Cipher('aes_192_cbc', key=key, iv=iv, op=0)
sys.stdout.write(cipher.update(edb))
sys.stdout.write(cipher.final())
if __name__ == '__main__':
if len(sys.argv) != 3:
print 'usage %s <db> <accountname> > decrypted.db' % sys.argv[0]
else:
decrypt(sys.argv[1],sys.argv[2])
</code></pre>
<p>The only piece I miss is where did the IV came from ? I don't see it on the ida snapshot:</p>
<p><img src="https://i.stack.imgur.com/43t7L.png" alt="enter image description here"></p>
| How the author found the initialization vector for the AES CBC whatsapp CRYPT5 db uses? | |cryptography| |
<p>This is quite tricky and necessitates a LOT of patience. I'll assume here that you're trying to find the <code>main</code> function as it is defined in C and not as the entry point of your program. It's very hard to find what you're looking for by scanning the code with your eyes & brain. But here's a way. What you can do is first check the header of the binary file you're trying to disassemble. Below you'll find the output of <code>readelf -h</code> on a random file. If the file isn't damaged (on purpose or not) you'll be able to find the <em>Entry point address</em>. </p>
<pre><code> Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x400440
Start of program headers: 64 (bytes into file)
Start of section headers: 4680 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 8
Size of section headers: 64 (bytes)
Number of section headers: 35
Section header string table index: 32
</code></pre>
<p>This address usually points to the location of the first chunk of code which will be executed at run time (<code>_start</code> function) and which will handle the <code>main</code> function parameters (or command line arguments) before calling the <code>main</code> function. Another technique would be to run your program under a debugger (<strong>GDB</strong> for instance) and go step by step.</p>
<p>I have to warn you though, if you're dealing with ELF binaries, things could turn out to be more complicated as they contain <code>ctor</code> and <code>dtor</code> tables which hold pointers to functions that are executed before and after the <code>main</code> function. You have also some undocumented weirdness going on when dealing with statically linked binaries. And of course, other programs can make do without a <code>main</code> function and call whatever they wish.</p>
| 4146 | 2014-04-25T21:37:10.020 | <p>Given a binary and only using a tool like <code>ndisasm</code>, how can I find <code>main()</code>?
I don't want to use smart tools like IDA Pro because I'm doing this exercise to learn.</p>
| How to find main() in binary? | |disassembly| |
<p>I see you're mixing up sections with functions.</p>
<p>What you have provided in your question are functions necessary to an ELF binary to execute. For example, the <code>_start</code> function is usually the <em>entry point</em> of a binary and it will probably call the <code>main</code> function at some point. You can get the address of the entry of a binary using <code>readelf -h</code> on the binary file you have.</p>
<p>About the output, though your program is "empty" it was still compiled & linked successfully - for that it is not erroneous - into an executable ELF. This document provides everything you need to know about how an ELF binary is structured & how to manipulate it : <a href="http://www.skyfree.org/linux/references/ELF_Format.pdf">ELF Format</a> (PDF).</p>
<p>Now if you want to retrieve section information in a binary file the <code>readelf</code> function can again help with that, you just have to call it with the <code>-S</code> and the target binary file (<code>readelf -S prog</code>).</p>
<p>Since you're just starting to learn, I recommend you checking the binutils (<code>readelf</code>, <code>objdump</code>, ...) and their related documentation and start playing with simple programs before moving to crackmes and more advanced or obfuscated binary files.</p>
| 4149 | 2014-04-26T00:21:24.140 | <p>I want to learn reverse engineering so I was starting to try compiling simple (to start with) C programs and then reading the disassembly.</p>
<p>The following file</p>
<pre><code>int main(void) {
return 0;
}
</code></pre>
<p>compiled with <code>gcc</code> then disassembled with <code>objdump -d</code> ends up creating 172 lines of output. I don't understand why there is so much output.</p>
<p>What is the meaning of the different sections:</p>
<pre><code>0000000000400370 <_init>:
0000000000400390 <__libc_start_main@plt-0x10>:
00000000004003a0 <__libc_start_main@plt>:
00000000004003b0 <__gmon_start__@plt>:
00000000004003c0 <_start>:
00000000004003f0 <deregister_tm_clones>:
0000000000400420 <register_tm_clones>:
0000000000400460 <__do_global_dtors_aux>:
0000000000400480 <frame_dummy>:
00000000004004ad <main>:
00000000004004c0 <__libc_csu_init>:
0000000000400530 <__libc_csu_fini>:
0000000000400534 <_fini>:
</code></pre>
<p>Of course I have been reading about the calling convention and opcodes so I can see how the section corresponds to the C code.</p>
| What are the sections of a x86 linux binary? | |assembly|x86|objdump| |
<p>The answer to your question is fairly easy. You can either use the <code>nm</code> command with the <code>-D</code> switch (or <code>--dynamic</code>), or use <code>objdump</code> with the <code>-T</code> switch. Both commands will output the dynamic symbol table entries and the libraries they originate from.</p>
| 4150 | 2014-04-26T00:54:07.627 | <p>I used the <code>file</code> command in Linux to get information about a binary file. I am also looking for the addresses that these calls are located at. I think I can get this information from GDB or <code>objdump</code> but I am not very good with Linux commands and programs yet so any help is much appreciated. The output from the file command is below:</p>
<pre><code>ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x6d232dd468b2344847a4b9c81eb064ffe257d5d0, stripped
</code></pre>
<p>Then using the <code>strings</code> command I got this output (I see several C function calls but which are external ?):</p>
<pre><code>/lib/ld-linux.so.2
-#mH4
__gmon_start__
libc.so.6
_IO_stdin_used
exit
strncmp
strncpy
puts
printf
malloc
atoi
__libc_start_main
GLIBC_2.0
PTRh
QVh>
UWVS
[^_]
testing
strncmp: %s;
atoi
Complete
;*2$"
</code></pre>
| How do I view external function calls in dynamically linked ELF binary in linux? | |gdb|static-analysis|dynamic-analysis|dynamic-linking|objdump| |
<p>It's not a memset at all, it's just a memcpy. Copying dword by dword is more efficient than byte by byte, since 32 bits of data bus can be used per copy (at least if the data is word aligned). So what the compiler does is:</p>
<ul>
<li>divide the number of bytes by 4, by shifting cx right by 2 bits</li>
<li>copy the appropriate number of 4-byte-dwords, using movsd</li>
<li>calulate the remainder of the number of bytes (by AND-ing cx with 3)</li>
<li>copy these bytes one-by-one, using movsb</li>
</ul>
<p>What adds to the confusion is that the compiler already prepares the arguments for the next function call while still doing the memcpy - the <code>push 0</code>, <code>add eax, 4</code> and <code>push eax</code> belong to the send call after the <code>jmp</code>.</p>
| 4189 | 2014-04-28T11:45:59.620 | <p>Trying to understand a function thats responsible for sending out a packet.
I don't understand could it be a integer array or something? or some inline function that's not properly getting rendered in Hex-Rays</p>
<p>I understand the else statement sends a 4 byte packet which contains a timestamp of the GetTickCount API.</p>
<p>The if statement should send the packet that comes in the <code>a2</code> is the pointer to characters with a3 being the size for all the characters.</p>
<p>Usage is similar to this</p>
<pre><code>char buffer[448];
memset(buffer, 0, sizeof(buffer));
//blah blah packet stuff here
strncpy(&buffer[90], "blah blah blah", 250u);
buffer[339] = 0;
//then the call below.
// 91+250+91 = 432, yet memset is 448, 16 extra probably stack padding.
test(*v28, buffer, strlen(&buffer[90]) + 91);
</code></pre>
<p><p></p>
<p>Here is the original code decompiled from Hex-Rays.</p>
<pre><code>void __thiscall test(void *this, const void *a2, unsigned int a3)
{
void *v3; // ebx@1
char *v4; // eax@3
int v5; // [sp-8h] [bp-418h]@3
int v6; // [sp-4h] [bp-414h]@3
char v7[4]; // [sp+Ch] [bp-404h]@4
char buf[1024]; // [sp+10h] [bp-400h]@3
v3 = this;
if ( a2 && (signed int)a3 > 0 )
{
*(_DWORD *)buf = 0;
memcpy(&buf[4], a2, 4 * (a3 >> 2));
v6 = 0;
v5 = a3 + 4;
v4 = buf;
memcpy(&buf[4 * (a3 >> 2) + 4], (char *)a2 + 4 * (a3 >> 2), a3 & 3);// Looks like Copy by DWORDs, not by Bytes.
}
else
{
v6 = 0;
*(_DWORD *)v7 = GetTickCount() / 0xA;
v5 = 4;
v4 = v7;
}
send(*(_DWORD *)v3, v4, v5, v6);
}
</code></pre>
<p>Here I fixed it up a little by hand, still don't understand it.</p>
<pre><code>void __thiscall test(void *this, const void *a2, unsigned int a3)
{
void *v3; // ebx@1
char *v4; // eax@3
int v5; // [sp-8h] [bp-418h]@3
int v6; // [sp-4h] [bp-414h]@3
char v7[4]; // [sp+Ch] [bp-404h]@4
char buf[1024]; // [sp+10h] [bp-400h]@3
v3 = this;
if ( a2 && (signed int)a3 > 0 )
{
*(_DWORD *)buf = 0;
//Might be a swap of the 5th offset DWORD to end of the packet?
//Or maybe it fills in the packet offsetted by the first 4 bytes?
memcpy(&buf[4], a2, 4 * (a3 / 4)); // Looks like Copy by DWORDs, not by Bytes.
v6 = 0;
v5 = a3 + 4;
v4 = buf;
//Might be a swap of the end of the packet to the 5th offset DWORD?
//Looks like some kind of footer to above memcpy function like to finish what the first function couldn't do?
memcpy(&buf[4 * (a3 / 4) + 4], (char *)a2 + 4 * (a3 / 4), a3 & 3);// Looks like Copy by DWORDs, not by Bytes.
}
else
{
v6 = 0;
*(_DWORD *)v7 = GetTickCount() / 0xA;
v5 = 4;
v4 = v7;
}
send(*(_DWORD *)v3, v4, v5, v6);
}
</code></pre>
<p>Okay I gave it some more time could this be the correct?</p>
<pre><code>void __thiscall test(void *this, const void *a2, unsigned int a3)
{
void *v3; // ebx@1
char *v4; // eax@3
int v5; // [sp-8h] [bp-418h]@3
int v6; // [sp-4h] [bp-414h]@3
char v7[4]; // [sp+Ch] [bp-404h]@4
char buf[1024]; // [sp+10h] [bp-400h]@3
v3 = this;
if ( a2 && (signed int)a3 > 0 )
{
*(_DWORD *)buf = 0;
memmove(&buf[4],a2,a3 - 4);
v6 = 0;
v5 = a3 + 4;
v4 = buf;
}
else
{
v6 = 0;
*(_DWORD *)v7 = GetTickCount() / 0xA;
v5 = 4;
v4 = v7;
}
send(*(_DWORD *)v3, v4, v5, v6);
}
</code></pre>
<p>Assembly below</p>
<pre><code>.text:00408750 ; =============== S U B R O U T I N E =======================================
.text:00408750
.text:00408750
.text:00408750 ; void __thiscall test(void *this, const void *a2, unsigned int a3)
.text:00408750 test proc near
.text:00408750 ; CODE XREF: ServerMainLoop+5DDp
.text:00408750 ; ServerMainLoop+64Dp
.text:00408750
.text:00408750 var_404 = byte ptr -404h
.text:00408750 buf = byte ptr -400h
.text:00408750 a2 = dword ptr 4
.text:00408750 a3 = dword ptr 8
.text:00408750
.text:00408750 sub esp, 404h
.text:00408756 push ebx
.text:00408757 push esi
.text:00408758 mov esi, [esp+40Ch+a2]
.text:0040875F push edi
.text:00408760 test esi, esi
.text:00408762 mov ebx, ecx
.text:00408764 jz short loc_408799
.text:00408766 mov eax, [esp+410h+a3]
.text:0040876D test eax, eax
.text:0040876F jle short loc_408799
.text:00408771 mov ecx, eax
.text:00408773 lea edi, [esp+410h+buf+4]
.text:00408777 mov edx, ecx
.text:00408779 mov dword ptr [esp+410h+buf], 0
.text:00408781 shr ecx, 2
.text:00408784 rep movsd
.text:00408786 mov ecx, edx
.text:00408788 push 0
.text:0040878A and ecx, 3
.text:0040878D add eax, 4
.text:00408790 push eax
.text:00408791 lea eax, [esp+418h+buf]
.text:00408795 rep movsb
.text:00408797 jmp short loc_4087B7
.text:00408799 ; ---------------------------------------------------------------------------
.text:00408799
.text:00408799 loc_408799: ; CODE XREF: test+14j
.text:00408799 ; test+1Fj
.text:00408799 call ds:GetTickCount
.text:0040879F mov edx, eax
.text:004087A1 mov eax, 0CCCCCCCDh
.text:004087A6 mul edx
.text:004087A8 shr edx, 3
.text:004087AB push 0 ; flags
.text:004087AD mov dword ptr [esp+414h+var_404], edx
.text:004087B1 push 4 ; len
.text:004087B3 lea eax, [esp+418h+var_404]
.text:004087B7
.text:004087B7 loc_4087B7: ; CODE XREF: test+47j
.text:004087B7 mov ecx, [ebx]
.text:004087B9 push eax ; buf
.text:004087BA push ecx ; s
.text:004087BB call send
.text:004087C0 pop edi
.text:004087C1 pop esi
.text:004087C2 pop ebx
.text:004087C3 add esp, 404h
.text:004087C9 retn 8
.text:004087C9 test endp
.text:004087C9
.text:004087C9 ; ---------------------------------------------------------------------------
</code></pre>
| Can someone tell me what this memcpy or maybe it's a memset? looks like a memset, rep movsd and rep movsb This is a packet sending function | |ida|decompilation| |
<p>A call to <code>malloc</code> returns the address of the allocated memory in the <code>eax</code> register on <code>x86</code>, <code>rax</code> on <code>x86_64</code>, and <code>r0</code> on <code>ARM</code>. Nothing is pushed on the stack. Check the line following the call to <code>malloc</code> & you'll understand !
You should check the calling conventions of your platform too. </p>
<p>Suppose you have this in your code :</p>
<pre><code> int *p = malloc(sizeof(int) * 4);
</code></pre>
<p>If you translate this line into assembly you'll get what follows : </p>
<pre><code> call 80483d0 <malloc@plt>
mov %eax,0x18(%esp)
</code></pre>
<p>The <code>malloc</code> call returns in <code>eax</code> the address of allocated memory block. In the C code, this address is assigned to <code>p</code> which is a local variable located on the stack. And that's what the following assembly does : <code>mov %eax,0x18(%esp)</code>. </p>
| 4192 | 2014-04-28T15:25:38.817 | <p>Here is my <code>objdump -d</code> snippet with the malloc call and surrounding code. This binary is stripped. </p>
<pre><code>dbgLab.bin: file format elf32-i386
080483d0 <malloc@plt>:
80483d0: ff 25 04 a0 04 08 jmp *0x804a004
80483d6: 68 08 00 00 00 push $0x8
80483db: e9 d0 ff ff ff jmp 80483b0 <printf@plt-0x10>
Disassembly of section .text:
804853e: 55 push %ebp
804853f: 89 e5 mov %esp,%ebp
8048541: 83 e4 f0 and $0xfffffff0,%esp
8048544: 83 ec 20 sub $0x20,%esp
8048547: c6 44 24 1f cf movb $0xcf,0x1f(%esp)
804854c: c7 04 24 0d 00 00 00 movl $0xd,(%esp)
8048553: e8 78 fe ff ff call 80483d0 <malloc@plt>
8048558: 89 44 24 18 mov %eax,0x18(%esp)
804855c: 83 7d 08 02 cmpl $0x2,0x8(%ebp)
8048560: 7f 0c jg 804856e <strncmp@plt+0x12e>
8048562: c7 04 24 ff ff ff ff movl $0xffffffff,(%esp)
8048569: e8 92 fe ff ff call 8048400 <exit@plt>
804856e: 8b 45 0c mov 0xc(%ebp),%eax
8048571: 83 c0 04 add $0x4,%eax
8048574: 8b 00 mov (%eax),%eax
8048576: c7 44 24 08 0d 00 00 movl $0xd,0x8(%esp)
</code></pre>
| At which address on the stack or in memory is eax stored after the malloc call? | |ida|decompilation|debuggers|gdb|objdump| |
<p>In general, this concept is referred to as <em>register preservation</em> or <em>register volatility</em>.</p>
<p>From <a href="http://en.wikipedia.org/wiki/X86_calling_conventions#Register_preservation">http://en.wikipedia.org/wiki/X86_calling_conventions#Register_preservation</a> --</p>
<blockquote>
<p>According to the Intel ABI to which the vast majority of compilers
conform, the <code>EAX</code>, <code>EDX</code>, and <code>ECX</code> are to be free for use within a
procedure or function, and need not be preserved.</p>
</blockquote>
<p>In other words, an API function such as <code>IsDebuggerPresent()</code> might modify <code>EAX</code>, <code>EDX</code>, and/or <code>ECX</code>, but it won't modify <code>EBX</code>, <code>ESP</code>, or <code>EBP</code>.</p>
| 4196 | 2014-04-29T00:58:56.597 | <p>How can I figure out which general-purpose registers are modified by a function call. I am programming a Win32 Assembly program that calls IsDebuggerPresent(). According to MSDN, it will return a boolean value of nonzero is a debugger is present. How would I find out which register is modified without having to assemble and link the program to test it.</p>
| registers set by function | |windows|debugging|anti-debugging| |
<p>cgdb is a curses based gdb interface that may be worth looking at: </p>
<p><a href="https://cgdb.github.io/" rel="nofollow">https://cgdb.github.io/</a></p>
| 4203 | 2014-04-29T20:42:00.310 | <p>I am looking for something that is like <code>gdb</code> but maybe with a GUI that can show all registers current values ans the current values of memory and things on the stack? Freeware is preferred. I have a free version of IDA and anything that comes with Kali Linux. It is an ELF file that is stripped.</p>
| Tools to show the registers and memory locations in use during execution? | |ida|disassembly|decompilation|binary-analysis|gdb| |
<p>The variable names that IDA is generating aren't "fake"; they are exactly the same as they would be labeled had the function been <code>ebp</code>-based. The problem you describe is only really an issue when debugging since that's the only time you can inspect the values pointed to. I'm not aware of any built-in way to get IDA to display what you want. When you hover over an operand, IDA just takes the current value of the register and adds the offset. If <code>esp</code> has changed, then it will show you the wrong address, which is what you're seeing in your example.</p>
<p>Since IDA does know the correct stack offset, one way to do what you're asking is to write an IDC script that checks <code>eip</code> and adjusts for the difference in <code>esp</code> values before calculating the final target address. The IDC function that gets the stack offset at a given address is <a href="https://www.hex-rays.com/products/ida/support/idadoc/351.shtml" rel="nofollow"><code>GetSpd()</code></a>. The algorithm would go something like this:</p>
<ol>
<li>Calculate the target address of the operand you are interested in (i.e. esp+5Eh+var_1 -> 0x10000000)</li>
<li>Get the stack pointer delta (SPD) of the line containing the operand you're interested in (i.e. 5Eh)</li>
<li>Get the SPD of <code>eip</code> (i.e. 6Eh)</li>
<li>Calculate the difference of the two SPDs and add (or subtract) that amount from the target address you calculated in step 1 (i.e. 0x10000000 + (6Eh-5Eh) = 0x10000010)</li>
<li>Go to your calculated address ([0x10000010] contains the variable you are after)</li>
</ol>
| 4213 | 2014-04-30T11:32:13.813 | <p>I have a problem just like in the question. Modern compilers don't use <code>ebp</code> to handle local variables and arguments, they just calculate and add hard=coded offsets to <code>esp</code>. Example: </p>
<pre><code>sub esp, 5Eh
...
mov [esp+5Eh+var_1], 123h
mov [esp+5Eh+var_2], 456h
call some_func ; var_1 and var_2 point to actual addresses
cmp eax, 0 ; esp changed (stdcall), var_1 and var_2 point to wrong addresses
...
; creation of a "fake" variable example:
; var_3 = -8h
; var_4 = -12h
mov [esp+5Eh+var_3], 78h ; var_3 at: esp + 5Eh -8h
pop eax ; esp = esp + 4
mov [esp+5Eh+var_4], 89h ; var_4 at: esp + 4 + 5eh -12h = esp + 5Eh -8h = var_3
; desireable fix:
mov [esp+62h+var_3], 89h
</code></pre>
<p>This results in a lot of overhead: IDA creates "fake" local variables (i.e. several names for one and the same address), you can't freely check variables whenever you want to, you have to create additional comments, etc. So I was wondering is there any way to fix that ?</p>
<p>PS. I'm using IDA Pro Free. Tell me if it is possible only in IDA Pro (full version). </p>
| Is there a way to adjust local variables when a function doesn't utilize ebp? | |ida| |
<p>I've been doing the same/similar stuff recently, and while you can get it to work, it's not that easy.</p>
<p>It helps if you have a rooted android device, a terminal emulator to run gdbserver, and, if possible, a terminal emulator that can open more than one window, so you kan kill the gdbserver from the second window if it hangs up. Also, a real keyboard on the android device helps, as it's much easier to repeat commands when you have real cursor keys.</p>
<p>You don't need to connect your Droid to your mac using an USB cable. I started gdbserver like this:</p>
<pre><code>gdbserver --attach 0.0.0.0:8765 2338
</code></pre>
<p>(where 2338 is the pid of my target application), then used</p>
<pre><code>$ arm-none-eabi-gdb
target remote 192.168.178.103:8765
</code></pre>
<p>to connect. 192.168.178.103 is the IP address of my android device.</p>
<p>I didn't want to install the complete NDK on my machine, so i downloaded the ARM GDB from <a href="https://launchpad.net/gcc-arm-embedded/+download" rel="nofollow">https://launchpad.net/gcc-arm-embedded/+download</a>, they have windows, linux and mac versions. The NDK GDB should probably work as well, of course.</p>
<p>You can add the "IDA Address" of the function to the Map address. For example, in my library:</p>
<ul>
<li>The two functions i was interested in had "IDA addresses" BA584 and BA99C.</li>
<li>The address in /proc/maps said 688D0000 as base address of the executable segment.</li>
<li>The real function addresses were 6898A584/6898A99C, which is base address + "IDA address" in both cases.</li>
</ul>
<p>There are two things to consider when you check you really found the correct address:</p>
<ul>
<li><p>You can't use the gdb disassemble command as it relies on functions and symbols. Use the x/i command to disassemble. For example, in my case, x/20i 0x6898A584 to see the 20 instructions at this address.</p></li>
<li><p>The arm has two different modes, Thumb mode and ARM mode. If gdb has no symbols, it might choose the wrong one when disassembling, making your GDB disassembly not resemble the IDA disassembly at all. </p></li>
</ul>
<p>Use <code>set arm force-mode arm</code> and <code>set arm force-mode thumb</code> to try both. For example, this is what IDA told me about my BA584 function:</p>
<pre><code>.text:000BA584
.text:000BA584 sub_BA584 ; CODE XREF: sub_BF1C4+3Ap
...
.text:000BA584 PUSH.W {R4-R11,LR}
.text:000BA588 MOV R6, R0
.text:000BA58A SUB SP, SP, #0x1C
.text:000BA58C LDR R0, [R0,#0x18]
.text:000BA58E MOV R4, R1
.text:000BA590 STR R2, [SP,#0x40+var_34]
</code></pre>
<p>this is what gdb made of it first:</p>
<pre><code>(gdb) x/20i 0x6890F584
0x6890f584: svcmi 0x00f0e92d
0x6890f588: addlt r4, r7, r6, lsl #12
0x6890f58c: strmi r6, [r12], -r0, lsl #19
0x6890f590: stmdacs r0, {r0, r1, r9, r12, pc}
0x6890f594: bichi pc, r10, r0
0x6890f598: mlapl r12, r6, r8, pc ; <UNPREDICTABLE>
0x6890f59c: ; <UNDEFINED> instruction: 0xf0402d00
</code></pre>
<p>then</p>
<pre><code>(gdb) set arm force-mode thumb
(gdb) x/20i 0x6890F584
0x6890f584: stmdb sp!, {r4, r5, r6, r7, r8, r9, r10, r11, lr}
0x6890f588: mov r6, r0
0x6890f58a: sub sp, #28
0x6890f58c: ldr r0, [r0, #24]
0x6890f58e: mov r4, r1
0x6890f590: str r2, [sp, #12]
</code></pre>
<p>So don't give up if the disassembly looks weird, try forcing thumb / arm mode.</p>
<p>My application crashed various times when i was debugging it, which made gdbserver lose sync with gdb, resulting in lots of "Ignoring packet error" messages from gdb. Killing gdbserver (thus the 2nd window), restarting the application, and reconnecting gdb seemed to be the only remedy in these cases. When you restart the application, make sure you re-check /proc/..../maps for the .so base address, it stays the same most of the time but changes sometimes, especially when there's some time between invocations. You'll notice my address (0x6890F584) in the above example does not match the one i told you earlier, this is because in the example, the map base address had changed to 68855000.</p>
| 4216 | 2014-04-30T16:20:48.623 | <p>I am a noob to the reversing world. I have a Android Java application which uses a shared library via JNI and I would like to set a breakpoint in the shared library. The shared library is stripped so as I far as I understand I can set breakpoints by address only. I have put the shared object through IDA and have a couple of positions I would like to set breakpoints but I am unsure on how I can calculate this or even if this is possible. I am using gdbserver on Android attaching to the process and connecting from Mac OSX using gdb in the NDK. I have been trying to calculate the memory address by taking what /proc/'pid'/smap gives me and calculating an offset from the IDA assembly I have. Is this the right direction? Is this even possible?</p>
| Set breakpoint on shared library | |ida|gdb|android|arm| |
<p>The <code>gdb</code> terms (and commands) are <code>step</code> and <code>next</code> and the difference is that <code>step</code> continues to run until it changes line of source code, while <code>next</code> doesn't trace into a subroutine, but rather skips over it. The <code>stepi</code> and <code>nexti</code> commands are similar but operate at the machine instruction level rather than source code level. Read more in <a href="https://sourceware.org/gdb/current/onlinedocs/gdb/Continuing-and-Stepping.html#Continuing-and-Stepping" rel="nofollow noreferrer">The Fine Manual</a>.</p>
<p>Here's an example that may help clarify. Let's say you have a simple project with three simple files:</p>
<h2>main.c</h2>
<pre><code>#include "squareit.h"
#include <stdio.h>
int main(void) {
int x = 5;
printf("%d squared is %d\n", x, squareit(x));
}
</code></pre>
<h2>squareit.h</h2>
<pre><code>#ifndef SQUAREIT_H
#define SQUAREIT_H
// return the square of an integer
int squareit(int x);
#endif // SQUAREIT_H
</code></pre>
<h2>squareit.c</h2>
<pre><code>#include "squareit.h"
int squareit(int x) {
return x*x;
}
</code></pre>
<p>We compile the program with debugging flags enabled (<code>gcc -g main.c squareit.c -o simple</code>) and then run <code>gdb simple</code>. If we are sitting on the <code>printf</code> line and execute <code>step</code>, we will find ourselves in the <code>squareit()</code> function. If instead at that same point we execute <code>next</code>, the <code>squareit()</code> function and the <code>printf()</code> functions will both execute and we will find ourselves on the last line of <code>main()</code>.</p>
<p>The session follows:</p>
<pre><code>(gdb) b main
Breakpoint 1 at 0x40113d: file ./main.c, line 5.
(gdb) run
Starting program: ./simple
Breakpoint 1, main () at ./main.c:5
5 int x = 5;
(gdb) step
6 printf("%d squared is %d\n", x, squareit(x));
(gdb) s
squareit (x=5) at ./squareit.c:4
4 return x*x;
(gdb) s
5 }
(gdb) s
5 squared is 25
main () at ./main.c:7
7 }
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: ./simple
Breakpoint 1, main () at ./main.c:5
5 int x = 5;
(gdb) s
6 printf("%d squared is %d\n", x, squareit(x));
(gdb) next
5 squared is 25
7 }
</code></pre>
<p>The <code>gdb</code> commands used here are <code>b</code> to set a breakpoint, <code>s</code> which is an alias of <code>step</code>, <code>next</code> (has an alias of <code>n</code>) and <code>run</code> (has an alias of <code>r</code>).</p>
| 4219 | 2014-04-30T18:22:47.713 | <p>I am using EDB and stepping through the program but I do not even know what the difference in behavior is when doing this. I feel like this is something I should know if I ever have hope of reverse engineering this program.</p>
| What is the difference between step into and step over when debugging? | |ida|disassembly|debuggers|gdb|debugging| |
<p>As you expected, it's doing a very simple XOR. The equivalent code looks something like this:</p>
<pre><code>int mystery(char *buff, int bufsize, int nonce1, int nonce2)
{
int result = 0;
// ch is the next byte (character) in the buffer
char ch = 0;
int count = bufsize;
char *ptr = buff;
int x;
for (x = nonce1; count; --count)
{
// this bit of trickery just replaces the low 8 bits
// of x with the low 8 bits of (x+ch) neglecting carry, if any
x = (x & ~0xff) | ((x+ch) & 0xff);
// XOR the buffer with the calculated x value
*ptr ^= x;
// read in the next character into ch
ch = *ptr++;
// obfuscate by adding nonce2
x += nonce2;
// if x = 0x12345678, this would make it 0x34567812
// for 32-bit ints. Just a rotate left of 8 bits.
x = (x<<8) | ((x >>((sizeof(int)-1)*8) ) & 0xff);
}
result = x;
// return the last calculated x which may be used to chain all
// of the blocks together. That is, the return value x is
// probably passed as nonce1 to encode the next block.
return result;
}
</code></pre>
| 4226 | 2014-04-27T20:16:34.257 | <p>I have been battling this infection I got that encrypts my files in 512 byte chunks with a friend. We have managed to find the Decryption function we think in IDA (the code is <em>heavily</em> obfuscated) from a user who paid for the decrypter. Below is the C dump of the encryption function:</p>
<pre><code>int __stdcall sub_40C78E(int a1, int a2, int a3, int a4)
{
int result;
char v5;
int v6;
int v7;
int v8;
v7 = a1;
v6 = a2;
v5 = 0;
result = 0;
if ( a2 )
{
v8 = a3;
do
{
LOBYTE(v8) = v5 + v8;
*(_BYTE *)v7 ^= v8;
v5 = *(_BYTE *)v7++;
v8 = __ROL__(a4 + v8, 8);
--v6;
}
while ( v6 );
result = v8;
}
return result;
}
</code></pre>
<p>My friend tried to simplify it or make sense of it and this is what he came up with:</p>
<pre><code>int __stdcall sub_40C78E((_BYTE *)buffer, int nonce1, int nonce2)
{
char v5;
int n;
int v8;
n = 0x400; // It is a little bit confusing, because the length of block is 0x200 (rest of buffer is filled by 0).
// Only first 0x200 bytes are saved to a file for block CT0A.
v5 = 0;
v8 = nonce1;
do
{
LOBYTE(v8) = v5 + LOBYTE(v8);
*buffer ^= LOBYTE(v8);
v5 = *buffer;
buffer++;
v8 = __ROL__(nonce2 + v8, 8);
--n;
}
while ( n );
return v8;
}
</code></pre>
<p>Where nonce1 and nonce2 is suppose to represent some kind of key.</p>
<p>What we have found with this infection is that if you XOR the first byte of the cipher text with the plain text, you get a key byte you can use to get the first byte of every file back. Which makes sense with this function because the first time in the loop the key is added to 0, which means it is simply the key. But then this guy used some type of weird CFB type xor encryption where it uses the previous xor'ed byte with the key next.</p>
<p>I just don't quite understand the function and I was hoping somewhere here could perhaps simplify it more for me or explain it. I also know C#, and VB if anyone would know how to explain the function in these languages.</p>
| IDA Pro C dump for Decryption function | |c|ida| |
<p>The <code>.bss</code> <em>block started by symbol</em> (also called Uninitialized data segment - <code>bss</code> is an old assembly instruction on an ancient IBM chip) section is supposed to contain <code>global variables</code> and <code>static variables</code> uninitialized or initialized to 0 or NULL. The <code>.bss</code> section is usually non existing until your program starts executing, this is why you can't retrieve its content statically. It is fairly important to note that this section helps reduce the program's size & makes it quicker to load. </p>
| 4230 | 2014-05-01T21:50:42.797 | <p>Test is on Linux 32bit.</p>
<p>I use this command to get the context of <code>.text</code> <code>.rodata</code> and <code>.data</code> section:</p>
<pre><code>objdump -s -j .text elf_binary
objdump -s -j .rodata elf_binary
objdump -s -j .data elf_binary
</code></pre>
<p>But basically when I tried to use this to get the content of <code>.bss</code> section, I always get the error shown below:</p>
<pre><code>objdump -s -j .bss elf_binary
objdump: section '.bss' mentioned in a -j option, but not found in any input file
</code></pre>
<p>Basically how can I get the content of <code>.bss</code> section from ELF binary?</p>
| Why I can not directly get the content of `.bss` section? | |disassembly|binary-analysis|elf| |
<p>Check out <a href="https://bitbucket.org/haypo/hachoir/wiki/hachoir-subfile" rel="nofollow">hachoir-subfile</a>. Example from their repo. </p>
<pre><code>$ hachoir-subfile chiens.PPS
[+] Start search (828.5 KB)
[+] Found file at 0: Microsoft Office document
[+] Found file at 537 size=28449 (27.8 KB): JPEG picture: 433x300 pixels
[+] Found file at 29011 size=34761 (33.9 KB): JPEG picture: 433x300 pixels
[+] Found file at 63797 size=40326 (39.4 KB): JPEG picture: 433x300 pixels
[+] Found file at 104148 size=30641 (29.9 KB): JPEG picture: 433x300 pixels
[+] Found file at 134814 size=22782 (22.2 KB): JPEG picture: 384x325 pixels
[+] Found file at 157621 size=24744 (24.2 KB): JPEG picture: 443x313 pixels
[+] Found file at 182390 size=27241 (26.6 KB): JPEG picture: 443x290 pixels
[+] Found file at 209656 size=27407 (26.8 KB): JPEG picture: 443x336 pixels
[+] Found file at 237088 size=30088 (29.4 KB): JPEG picture: 388x336 pixels
[+] Found file at 267201 size=30239 (29.5 KB): JPEG picture: 366x336 pixels
[+] Found file at 297465 size=81634 (79.7 KB): JPEG picture: 630x472 pixels
[+] Found file at 379124 size=36142 (35.3 KB): JPEG picture: 599x432 pixels
[+] Found file at 415291 size=28801 (28.1 KB): JPEG picture: 443x303 pixels
[+] Found file at 444117 size=28283 (27.6 KB): JPEG picture: 433x300 pixels
[+] Found file at 472425 size=95913 (93.7 KB): PNG picture: 433x431x8
[+] Found file at 568363 size=219252 (214.1 KB): PNG picture: 532x390x8
[+] Found file at 811308 size=20644 (20.2 KB): Microsoft Windows Metafile (WMF) picture
</code></pre>
<p><a href="https://bitbucket.org/snippets/Alexander_Hanel/EXKzg/extractsubfile" rel="nofollow">ExtractSubFile</a> can be used if you need to carve out the files. </p>
| 4238 | 2014-05-02T18:05:36.500 | <p>I have got a file which contains other files. I know where the subfiles start (<code>header</code>) but how do I know where the subfiles end ?</p>
<p>Edit: Files are like: <em>sound files (.wav)</em> and <em>images (.bmp, png, jpeg)</em></p>
<p>Example:</p>
<pre><code>file1
file2
data
file3
</code></pre>
<p>How do I know where <code>file2</code> starts and <code>file2</code> ends ?</p>
| Finding end of file | |binary-analysis|file-format| |
<p>thanks @yaspr for his excellent answer.</p>
<p>So in this post I was asking that </p>
<p><strong>How to re-use <code>.rodata</code> <code>.data</code> and <code>.bss</code> sections' contents?</strong></p>
<p>Here is my answer:</p>
<p>you can quickly go this way once you dumped all the content of these sections, I use a hello world code to demostrate it:</p>
<pre><code>.section .text
.globl main
main:
push %ebp
mov %esp,%ebp
and $0xfffffff0,%esp
sub $0x20,%esp
fldl S_80484f0 // lift the conceret address into symbol
fstpl 0x18(%esp)
fldl 0x18(%esp)
fstpl 0x4(%esp)
movl $S_80484e0,(%esp) // lift the conceret address into symbol
call printf
leave
ret
nop
nop
nop
.section .rodata
S_80484e0:
.long 0x6c6c6568
.long 0x6f77206f
.long 0x20642c72
.long 0x000a6625
S_80484f0:
.long 0x00000000
.long 0x40240000
</code></pre>
<p>To @yaspr about AT&T vs. Intel style:</p>
<p>It is obviously that Intel style is much more clear that AT&T:)</p>
<p>However, in the current project I am struggling on, I find it might not be very easy to use <code>nasm</code> inside a bunch of GNU tools(<code>objdump</code> <code>objcopy</code> <code>ld</code> and others...) </p>
<p>IMHO, Even though <code>objdump</code> can disassemble in <code>Intel</code> style, I still encounter a bunch of problems/unclear issues, especially after comparing the disassemble results between <code>objdump</code> and <code>IDAPro</code></p>
| 4240 | 2014-05-02T18:47:25.400 | <p>OK, so basically I want to <strong>re-use</strong> the content from dumped <code>.rodata</code> <code>data</code> and <code>bss</code> section from ELF on Linux 32bit.</p>
<p>The dump command:</p>
<pre><code>objdump -s -j .text elf_binary
objdump -s -j .rodata elf_binary
objdump -s -j .data elf_binary
</code></pre>
<p>and for the <code>.bss</code> section, I am preparing to re-use a bunch of <code>00000000</code> which has the same size of the <code>bss</code> section.</p>
<p>It works fine when I re-use it in <code>nasm</code> assembler in this way.</p>
<pre><code>.section rodata
S_label1: db 0x01
db 0x02
....
.section data
db 0x01
S_label2: db 0x02
....
.section bss
S_label3: db 0x00
db 0x00
....
nasm -f elf test.s
</code></pre>
<p>But basically my question is that:</p>
<p>how to re-use these dumped sections in <code>gas</code> asssembler?</p>
<p>Basically <code>gas</code> has a different assemble style, and apparently the data sections representations are different...</p>
<p>I tried for several times and I still can not find the solution..</p>
<p>Did I clearly demonstrate my question..? Could anyone give me some help?</p>
<p>=======update==========</p>
<p>So basically I want to re-use the <code>rodata</code> <code>data</code> and <code>bss</code> sections dumped from another binary.</p>
<p>For example, here is the content of <code>rodata</code> section</p>
<pre><code>03000000 01000200 0a0a556e 736f7274
65642061 72726179 2069733a 20200020
25642000 0a0a536f 72746564 20617272
61792069 733a2020 00
</code></pre>
<p>and I can re-use it in this way:</p>
<pre><code>label1:
db 0x03
db 0x00
db 0x00
db 0x00
label2:
db 0x01
db 0x00
db 0x02
db 0x00
.....
mov eax, label1 // of course I will guarantee that I use it correctly
....
</code></pre>
<p>Basically in <code>nasm</code>, I can easily re-use them in the above way, but my question is that , how can I re-use it in <code>gas</code> (or directly use <code>gcc</code>) in a similiar way?</p>
<p>Is it possible?</p>
| How to directly re-use the dumped content of `.rodata`, `.data` and `.bss` section? | |disassembly|assembly|nasm| |
<p><sub>(I deal more with code generated by Visual C++, so some parts of this answer are just educated guesses.)</sub></p>
<blockquote>
<p>1.Why these 3 addresses of variables are not consecutive when allocated?</p>
</blockquote>
<p>Variable alignment and even <em>presence</em> on the stack is up to the compiler and can differ based on the specific compiler version you use, the optimization options being used, and other factors.</p>
<p>The 3 variables in your example are not all "real" variables. <code>var_4</code> is the variable corresponding to your <code>x</code>, whereas <code>var_1C</code> and <code>var_20</code> are simply results of GCC's approach to passing arguments to deeper function calls. When you write <code>scanf("%d", &x);</code>, GCC knows that it will need to pass two 4-byte variables to that function on the stack, so it pre-emptively reserves enough room for them upon entering the function. This way, it doesn't need to <code>push</code> anything onto the stack (which might be problematic if there's no stack space left...), it just needs to <code>mov</code> the arguments into that preallocated space.</p>
<p>That doesn't explain why there's a gap between the two allocations, though. GCC also prefers to align stack allocations to 16 bytes<sup>1</sup>, and <sub>this is where I am guessing</sub> it seems that the sizes needed for "real local variables" and "space reserved for deeper function arguments" are aligned independently before being summed into the final value of "reserved stack space".</p>
<p><sup>1</sup>You can control this alignment by using <a href="http://gcc.gnu.org/onlinedocs/gcc-3.2/gcc/i386-and-x86-64-Options.html" rel="nofollow noreferrer"><code>-mpreferred-stack-boundary=num</code></a>.</p>
<blockquote>
<p>2.If not, when could I speculate the number of variables generated from stack by watching the clause like <code>add esp, N</code> which is often at the end of a routine? Is it related with calling convention?</p>
</blockquote>
<p>As you can see in this example, that instruction does not always get generated. Its counterpart, <code>sub esp, N</code>, is a much better indicator. From that you can make educated guesses about the amount/sizes of local variables.</p>
<p>Calling convention is not related to a function's local variables, it controls the way the arguments are passed <em>into</em> the function and whose responsibility it is to clean them up afterwards.</p>
<blockquote>
<p>3.In this example, why compiler does not generated the <code>add esp, 20h</code> with it?</p>
</blockquote>
<p>The function in your example begins with <code>push ebp; mov ebp, esp</code>, which saves the original values of both <code>ebp</code> and <code>esp</code>. The <code>leave</code> instruction at the end does the reverse - it restores the saved values of <code>esp</code> and <code>ebp</code>, so there's no need to calculate anything.</p>
<p>The saved <code>ebp</code> is also known as a <a href="https://stackoverflow.com/questions/579262/what-is-the-purpose-of-the-frame-pointer"><em>frame pointer</em></a>. It is possible to instruct the compiler <em>not</em> to generate it, in which case the original value of <code>esp</code> needs to be restored using that calculation you mentioned.</p>
| 4250 | 2014-05-03T16:52:21.290 | <p>The code below is generated by gcc from a simple scanf program.
My question is that </p>
<ol>
<li>Why these 3 addresses of variables are not consecutive when allocated?</li>
<li>If not, when could I speculate the number of variables generated from stack by watching the
clause like <code>add esp, N</code> which is often at the end of a routine? Is it related with calling convention?</li>
<li>In this example, why compiler does not generated the <code>add esp, 20h</code> with it?</li>
</ol>
<p>C code </p>
<pre><code>#include <stdio.h>
int main() {
int x;
printf ("Enter X:\n");
scanf ("%d", &x);
printf ("You entered %d...\n", x);
return 0;
};
</code></pre>
<p>asm</p>
<pre><code>main proc near
var_20 = dword ptr -20h
var_1C = dword ptr -1Ch
var_4 = dword ptr -4
push ebp
mov ebp, esp
and esp, 0FFFFFFF0h
sub esp, 20h
mov [esp+20h+var_20], offset aEnterX ; "Enter X:"
call _puts
mov eax, offset aD ; "%d"
lea edx, [esp+20h+var_4]
mov [esp+20h+var_1C], edx
mov [esp+20h+var_20], eax
call ___isoc99_scanf
mov edx, [esp+20h+var_4]
mov eax, off set aYouEnteredD___ ; "You entered %d...\n"
mov [esp+20h+var_1C], edx
mov [esp+20h+var_20], eax
call _printf
mov eax, 0
leave
retn
main endp
</code></pre>
| Why addresses of variable on stack are not consecutive? | |disassembly|assembly|x86|c| |
<p>If this is a class competition, and it's filed under Reverse Engineering, the chances are the tutor has compiled this package themselves and has used techniques you've learnt in previous lessons to store the password somewhere within the executable.</p>
<p>Look back on the techniques you've already learnt, and try those.</p>
<p>Judging by the question I'll assume that you've not covered advanced Reverse Engineering techniques so the first place to start would be to view all the strings in the executable - provided no encryption is used, again I'm assuming not.</p>
<p>If you're using Windows you can use Sys Internals Strings (<a href="http://technet.microsoft.com/en-gb/sysinternals/bb897439.aspx" rel="nofollow">http://technet.microsoft.com/en-gb/sysinternals/bb897439.aspx</a>). IDA Pro also allows you to do this.</p>
<p>Use a PE viewer to check whether it's a valid zip file created with WinZip, WinRar etc. It could have been created with a different program that contains a vulnerability to extract/crack the password.</p>
<p>Failing that, and not knowing the level of your experience or class the only option left is to brute force.</p>
| 4251 | 2014-05-03T17:23:49.993 | <p>Is it possible to reverse engineer ZIP file with password, and get the password or the data that is containing. I'm wondering because there is challenge in hacking lab which is to extract file from ZIP with passwd protection, and it's category is reverse engineering but when I look at the HEX dump I only see the file name.</p>
| Reverse engineering zip file | |disassembly| |
<p>Basically the best way to start doing this is to compile your own EXE (with the same compiler as the Themida protected file when possible) and try to merge the sections by yourself. I crafted an example for you (source code: <a href="http://codepad.org/RqNiH3Ly" rel="nofollow">http://codepad.org/RqNiH3Ly</a>, download RAR (merged + directly compiled): <a href="https://mega.co.nz/#!aoAUALBJ!6riSM4VmT43Ywf_jxQAY73EsVXyjEAAhJ1rOSGaYdeI" rel="nofollow">https://mega.co.nz/#!aoAUALBJ!6riSM4VmT43Ywf_jxQAY73EsVXyjEAAhJ1rOSGaYdeI</a>, just some executable I was working with at the moment, compiled with VS10).</p>
<p>The base of the .data section can basically be found by searching for references to every address aligned to 0x1000 in the code (VS10 uses data pointers in order, so just scroll up to the base of the first section and look for a pointer). For example:</p>
<blockquote>
<p>01251000 /$ 81EC 0C010000 SUB ESP,10C<br>
01251006 |. A1 00302501 MOV EAX,DWORD PTR DS:[1253000] ; pointer to .data<br>
0125100B |. 33C4 XOR EAX,ESP</p>
</blockquote>
<p>In my case (the EXE also has relocations) the ImageBase was 0x1250000, so the RVA of the .data section would be 0x1253000 - 0x1250000 = 0x3000</p>
<p>The .rdata section is just the RVA aligned up to 0x1000 from the actual end of the code (you can learn this from the original file). In this case:</p>
<blockquote>
<p>012518E2 $- FF25 60202501 JMP DWORD PTR DS:[<&MSVCR100._except_han><br>
012518E8 $- FF25 64202501 JMP DWORD PTR DS:[<&MSVCR100._invoke_wat><br>
012518EE $- FF25 68202501 JMP DWORD PTR DS:[<&MSVCR100.<em>controlfp</em>> ; end<br>
012518F4 00 DB 00<br>
012518F5 00 DB 00</p>
</blockquote>
<p>0x12518EE - 0x1250000 = 0x18EE, rounded up 0x2000, so the original RVA of the .rdata section is 0x2000.</p>
<p>Similar calculations can be done for the .reloc section (search for the binary mask "3? 3? 3? 3? 3?" will get you pretty close on x86).</p>
<p>Just learn to know the compiler structure and use it to recover something similar to the original (maybe Themida just appends the raw section data and changes all data pointers etc). Hope this helps a little :)</p>
| 4257 | 2014-05-04T02:01:04.953 | <p>Visual C++ produces binaries with <code>.code</code>, <code>.rdata</code>, and <code>.data</code> sections (in that order). Themida merges all three into a nameless section, which is detrimental to analysis. In particular, I want to run the <a href="http://www.openrce.org/downloads/details/253/IDA_Extra_Pass" rel="nofollow">Extra Pass</a> plugin for IDA on a dump from memory of a Themida'd executable (imports not recovered), but it needs the real bounds of the .code section or it will aggressively convert a lot of actual data into code.</p>
<p>How could I go about recovering the base of the <code>.rdata</code> and <code>.data</code> sections?</p>
| Recovering original PE sections after Themida merges them | |windows|pe| |
<p>In fact, you should first run a '<em>function</em>' analysis of the program. To better understand this type <code>a?</code>:</p>
<pre><code>[0x00404890]> a?
Usage: a[?adfFghoprsx]
a8 [hexpairs] ; analyze bytes
aa ; analyze all (fcns + bbs)
ad ; analyze data trampoline (wip)
ad [from] [to] ; analyze data pointers to (from-to)
ae [expr] ; analyze opcode eval expression (see ao)
af[bcsl?+-*] ; analyze Functions
aF ; same as above, but using graph.depth=1
ag[?acgdlf] ; output Graphviz code
ah[?lba-] ; analysis hints (force opcode size, ...)
ao[e?] [len] ; analyze Opcodes (or emulate it)
ap ; find and analyze function preludes
ar[?ld-*] ; manage refs/xrefs
as [num] ; analyze syscall using dbg.reg
at[trd+-*?] [.] ; analyze execution Traces
ax[-cCd] [f] [t] ; manage code/call/data xrefs
Examples:
f ts @ `S*~text:0[3]`; f t @ section..text
f ds @ `S*~data:0[3]`; f d @ section..data
.ad t t+ts @ d:ds
</code></pre>
<p>And, more precisely with an <code>af?</code>:</p>
<pre><code>[0x00404890]> af?
Usage: af[?+-l*]
af @ [addr] ; Analyze functions (start at addr)
af+ addr size name [type] [diff] ; Add function
af- [addr] ; Clean all function analysis data (or function at addr)
afb 16 ; set current function as thumb
afbb fcnaddr addr size name [type] [diff] ; Add bb to function @ fcnaddr
afl[*] [fcn name] ; List functions (addr, size, bbs, name)
afi [fcn name] ; Show function(s) information (verbose afl)
afr name [addr] ; Rename name for function at address (change flag too)
afs [addr] [fcnsign] ; Get/set function signature at current address
af[aAv][?] [arg] ; Manipulate args, fastargs and variables in function
afc @ [addr] ; Calculate the Cyclomatic Complexity (starting at addr)
af* ; Output radare commands
</code></pre>
<p>Then, start a '<em>functions</em>' analysis beginning at <code>main</code>:</p>
<pre><code>[0x00404890]> af@main
</code></pre>
<p>Then, you can run a recursive traversal disassembly:</p>
<pre><code>[0x00404890]> pdr@main
/ (fcn) fcn.004028c0 7460
| ;-- main:
| 0x004028c0 4157 push r15
| 0x004028c2 4156 push r14
| 0x004028c4 4155 push r13
| 0x004028c6 4154 push r12
| 0x004028c8 55 push rbp
| 0x004028c9 4889f5 mov rbp, rsi
| 0x004028cc 53 push rbx
| 0x004028cd 89fb mov ebx, edi
| 0x004028cf 4881ec88030. sub rsp, 0x388
| 0x004028d6 488b3e mov rdi, [rsi]
| 0x004028d9 64488b04252. mov rax, [fs:0x28]
| 0x004028e2 48898424780. mov [rsp+0x378], rax
| 0x004028ea 31c0 xor eax, eax
| 0x004028ec e8afad0000 call 0x40d6a0 ; (fcn.0040d69f)
| fcn.0040d69f(unk, unk, unk, unk, unk, unk)
| 0x004028f1 be19694100 mov esi, 0x416919
| 0x004028f6 bf06000000 mov edi, 0x6
| 0x004028fb e810feffff call sym.imp.setlocale
... clip ...
--
| 0x00402980 83e801 sub eax, 0x1
| 0x00402983 7405 jz fcn.004038a8
-[true]-> 0x0040298a
-[false]-> 0x00402985
--
</code></pre>
<p>You can also start radare2 with the <code>-A</code> option:</p>
<blockquote>
<p>-A : run 'aaa' command before prompt or patch to analyze all referenced code</p>
</blockquote>
<p>See also <a href="http://radare.today/posts/analysis-by-default/" rel="nofollow">http://radare.today/posts/analysis-by-default/</a></p>
| 4260 | 2014-05-04T16:57:40.147 | <p>I am trying the recursive traversal disassembler of the <code>radare2</code> tool. But, I cannot use it properly.</p>
<p>First, according to the <code>radare2</code> manual, we can use recursive traversal disassembler by using the <code>pdr</code>:</p>
<pre><code>[0x00404890]> pd?
Usage: pd[f|i|l] [len] @ [addr]
pda : disassemble all possible opcodes (byte per byte)
pdj : disassemble to json
pdb : disassemble basic block
pdr : recursive disassemble across the function graph
pdf : disassemble function
pdi : like 'pi', with offset and bytes
pdl : show instruction sizes
</code></pre>
<p>But I always get this error message:</p>
<pre><code>Cannot find function at 0x004028c0
</code></pre>
<p>Here is a full session of <code>radare2</code> on the <code>ls</code> command with, first, a linear sweep disassembly and, then, an attempt of recursive traversal disassembly:</p>
<pre><code>$> radare2 /bin/ls
syntax error: error in error handling
syntax error: error in error handling
[0x00404890]> pd@main
;-- main:
0x004028c0 4157 push r15
0x004028c2 4156 push r14
0x004028c4 4155 push r13
0x004028c6 4154 push r12
0x004028c8 55 push rbp
0x004028c9 4889f5 mov rbp, rsi
0x004028cc 53 push rbx
0x004028cd 89fb mov ebx, edi
0x004028cf 4881ec88030. sub rsp, 0x388
...
0x00402dff 8b0567772100 mov eax, [rip+0x217767] ; 0x0040a56c
0x00402e05 488b0d64772. mov rcx, [rip+0x217764] ; 0x0040a570
0x00402e0c 83f801 cmp eax, 0x1
0x00402e0f 0f84de0d0000 jz 0x403bf3
0x00402e15 83f802 cmp eax, 0x2
0x00402e18 be0f384100 mov esi, 0x41380f
0x00402e1d b80e384100 mov eax, str.vdir
0x00402e22 480f45f0 cmovnz rsi, rax
0x00402e26 488b3de3772. mov rdi, [rip+0x2177e3] ; 0x0040a610
0x00402e2d 48c70424000. mov qword [rsp], 0x0
0x00402e35 41b9bd384100 mov r9d, str.DavidMacKenzie
0x00402e3b 41b8cd384100 mov r8d, str.RichardM.Stallman
[0x00404890]> pdr@main
Cannot find function at 0x004028c0
</code></pre>
<p>In fact, I strongly suppose that I am missing a step here. It seems that we should first build the call graph of the program, but I didn't manage to find how to do it (I obviously have missed some documentation somewhere, sorry for that!). </p>
<p>So, if somebody can give me a hint about it, I would be pleased !</p>
| Recursive traversal disassembling with Radare2? | |disassembly|radare2| |
<p>The extra <code>s</code> character added to the ARM instruction mean that the <strong>APSR</strong> (Application Processor Status Register) will be updated depending on the outcome of the instruction.</p>
<p>The status register (APSR) contain four flags <code>N</code>, <code>Z</code>, <code>C</code> and <code>V</code> which means the following:</p>
<ul>
<li><code>N == 0</code>: The result is greater or equal to 0, which is considered positive, and so the <code>N</code> (negative) bit is set to 0.</li>
<li><code>Z == 1</code>: The result is 0, so the <code>Z</code> (zero) bit is set to 1.</li>
<li><code>C == 1</code>: We lost some data because the result did not fit into 32 bits, so the processor indicates this by setting <code>C</code> (carry) to 1.</li>
<li><code>V = 0</code>: From a two's complement signed-arithmetic viewpoint, 0xffffffff really means -1, so the operation we did was really (-1) + 1 = 0. That operation clearly does not overflow, so <code>V</code> (overflow) is set to 0.</li>
</ul>
<p>More information about the <em>condition flags</em> in the ARM architecture can be found <a href="http://community.arm.com/groups/processors/blog/2010/07/16/condition-codes-1-condition-flags-and-codes">here</a>.</p>
| 4261 | 2014-05-04T17:08:09.753 | <p>I am starting to look a bit more precisely at ARM assembler and I looked up some dumps from <code>objdump</code>. I saw a lot of instruction (<code>add</code> is not the only one) with an extra <code>s</code> at the end (<code>adds</code>, <code>subs</code>, ...).</p>
<p>I looked a bit to the ARM documentation and it seems to mean something significant, but I can't figure out exactly what (the documentation I found about it seemed extremely obscure to me).</p>
<p>Has somebody some insight on what is the meaning of this extra <code>s</code> added at the end of some ARM instructions ?</p>
| Difference between 'add' and 'adds' in ARM assembler? | |arm|gas| |
<p>IDA Pro has this mechanism called <a href="http://www.hexblog.com/?p=112" rel="nofollow">AppCall</a> which basically allows you to set up the parameters of a function and call it directly. </p>
<blockquote>
<p>Briefly, Appcall is a mechanism used to call functions inside the debugged program from the debugger or your script as if it were a built-in function. If you’ve used GDB (call command), VS (Immediate window), or Borland C++ Builder then you’re already familiar with such functionality.</p>
</blockquote>
<p>Now , this might not be directly applicable to your situation, bit I guess creating a small wrapper program and using appcall on it could make things easier. </p>
| 4271 | 2014-05-05T13:29:32.563 | <p>I'm "middle-new" in re and I have to debug a dll's function. How can I tell to IDA : start function XXX with parameters A, B and C ?
Is there a solution or a plugin doing that ? I've tried doing that with rundll32.exe but unsucessfully.</p>
<p>I have no experience on ollydbg this is why I ask for a IDA solution</p>
<p>Thanks for helping !</p>
| DLL - Dynamic Analysis on IDA | |ida|dll|dynamic-analysis| |
<p>The <a href="https://code.google.com/p/mynav/" rel="nofollow noreferrer">MyNav</a> plugin will show you calls from a function recursively, as shown below:
<img src="https://i.stack.imgur.com/0z5Nh.png" alt="enter image description here"></p>
| 4276 | 2014-05-05T20:20:21.223 | <p>I want to generate all xrefs from function just to check what api it uses in its call-tree. I know I can generate call tree graph by right clicking on function name and "Xrefs from", but I would like to have this functions listed just as text or something like that, so I could read it easily.</p>
<p>Reading it from WinGraph is pretty hard, and I have trouble with this nasty colors..
I mean, how is it readable? It is really hard to read white text on cyan background. And I dont see any way to configure it. My eyes just cant stand looking at it.</p>
<p>So how can I get this xrefs in some friendly format? I am sure it is possible..
I am using IDA 6.1 </p>
<p>Thanks in advance</p>
| IDA Xrefs from - how to? | |ida|tools|ida-plugin| |
<p>testing memory address can be done with with </p>
<pre><code>.if ( poi(@R32) operator CONST ) { commands }
</code></pre>
<p>but i think the intent the question is not to break on memory access failure </p>
<p>if that is the case wrap your conditional command with a <code>.catch {} ; command to execute on exception</code> this will let the execution flow without breaking</p>
<p>a sample code </p>
<pre><code>#include <stdio.h>
int main (void)
{
__asm
{
xor eax , eax
increase:
inc eax
cmp eax , 0ffffffffh
jne increase
}
printf("we reached here\n");
__asm
{
loopfever:
jmp loopfever
}
return 0;
}
</code></pre>
<p>disassembly of main</p>
<pre><code>0:001> uf 401000
image00400000+0x1000:
00401000 55 push ebp
00401001 8bec mov ebp,esp
00401003 33c0 xor eax,eax
image00400000+0x1005:
00401005 40 inc eax
00401006 83f8ff cmp eax,0FFFFFFFFh
00401009 75fa jne image00400000+0x1005 (00401005)
image00400000+0x100b:
0040100b 6840814000 push offset image00400000+0x8140 (00408140)
00401010 e809000000 call image00400000+0x101e (0040101e)
00401015 83c404 add esp,4
image00400000+0x1018:
00401018 ebfe jmp image00400000+0x1018 (00401018)
</code></pre>
<p>a conditinal break point on 401006 dereferencing eax (will throw exception on almost 3 gb of address space ) wrapped in a </p>
<pre><code>.catch {} ; gc
</code></pre>
<p>bp </p>
<pre><code>0:001> .bpcmds
bp0 0x00401006 " .catch { .printf \"%x\n\" , poi( @eax ) ; gc }; ? @eax ;gc ";
</code></pre>
<p>here is an output </p>
<pre><code>Memory access error at ') ; gc '
Evaluate expression: 65529 = 0000fff9
Memory access error at ') ; gc '
Evaluate expression: 65530 = 0000fffa
Memory access error at ') ; gc '
Evaluate expression: 65531 = 0000fffb
Memory access error at ') ; gc '
Evaluate expression: 65532 = 0000fffc
Memory access error at ') ; gc '
Evaluate expression: 65533 = 0000fffd
Memory access error at ') ; gc '
Evaluate expression: 65534 = 0000fffe
Memory access error at ') ; gc '
Evaluate expression: 65535 = 0000ffff
380039 44003800 440038 44004400 440044 4b004400 4b0044 3d004b00 3d004b 45003d00 45003d 3a004500 3a0045 5c003a00 5c003a 39005c00 39005c 38003900 380039 44003800 440038 44004400
Environment starts at 0x10000 compare
0:000> s -su 10000 10100
00010000 "98DDK=E:\98DDK"
0001001e "=::=::\"
0001002e "=C:=C:\Documents and Settings\Ad"
7c90120e cc int 3
0:001> ~0s
eax=000100c4 ebx=7ffdf000 ecx=00000001 edx=0040c5f0 esi=00000000 edi=009af6ee
eip=00401006 esp=0013ff78 ebp=0013ff78 iopl=0 nv up ei pl nz na po cy
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000303
image00400000+0x1006:
00401006 83f8ff cmp eax,0FFFFFFFFh
</code></pre>
| 4282 | 2014-05-07T07:27:45.913 | <p>Following break point fails because of bad memory address in some situation and cause break in execution:</p>
<pre><code>bp 0x12345678 ".printf \"PID: %d, unkVar: %d\\n\", ..., poi(poi(ecx+1c)+8)+c;g"
</code></pre>
<p>Is there any way to test memory address before dereferencing in Windbg?</p>
| Prevent Windbg Log Breakpoint Fail by Memory Check | |windbg| |
<p>This is just the ugly AT&T syntax. In Intel syntax it's:</p>
<pre><code>jmp dword ptr [eax*4+0x80509e4]
</code></pre>
<p>And yes, it's most likely a jump table.</p>
<p>You can switch <code>objdump</code> to Intel syntax by adding <code>-M intel</code> to the command line.</p>
| 4285 | 2014-05-07T19:51:17.127 | <p>So basically I am using <code>objdump</code> to disassemble a binary from GNU Coreutils, on 32-bit Linux x86.</p>
<p>In the disassembled code, I found one "<em>broken</em>" instruction like this:</p>
<pre><code> 804b4db: ff 24 85 e4 09 05 08 jmp *0x80509e4(,%eax,4)
</code></pre>
<p>It seems like a <strong>disassemble error</strong>?</p>
<p>And, by digging into the section info, I figure out that <code>0x80509e4</code> inside the <code>.rodata</code> section.</p>
<p>So does it mean that <code>0x80509e4</code> is a <strong>jump table</strong>?</p>
| How to deal with this "error" instructions generated by objdump? | |disassembly|elf|objdump| |
<p>It seems to me that the problem is a bit worse than you describe, and there's no easy way to get around it.</p>
<p>First, @sealed seems to be right with the function being a class method, and your compiler passing the class pointer in <code>ecx</code>, because that's the only way for</p>
<pre><code>.text:008F28B5 lea ecx, [esi+eax*4+4]
.text:008F28B9 call ReadCfgEntry
</code></pre>
<p>to make sense - other parameters are pushed on the stack, and the values of eax, edx and ebx seem not to be very meaningful, so the compiler doesn't use some kind of parameters-in-registers fastcall abi.</p>
<p>Now, let me reorder the statements from the loop start to the call a bit (without changing the meaning, just to make a bit clearer what happens). The compiler spread the "increment the count by one" instructions between the others, probably to take advantage of pipelining within the processor:</p>
<pre><code>-- get the current "number of config entries" count, and abort the loop if it exceeds 500
.text:008F2890 mov eax, [esi+79954h]
.text:008F2896 cmp eax, 1F4h
.text:008F289B jge loc_8F29DF
-- increment the count by one
.text:008F28A6 lea ecx, [eax+1]
.text:008F28AE mov [esi+79954h], ecx
-- eax = (( count << 5 - count ) * 8 ) + count = count*249
.text:008F28A1 mov edx, eax
.text:008F28A3 shl edx, 5
.text:008F28A9 sub edx, eax
.text:008F28AB lea eax, [eax+edx*8]
-- push edi, which is a parameter to this function, on the stack;
-- pass this = esi+4*eax+4 == esi+996*count+4 in ecx. Remember esi was set to
-- `this`=`ecx` at the start of the current method and hasn't been changed.
.text:008F28B4 push edi
.text:008F28B5 lea ecx, [esi+eax*4+4]
.text:008F28B9 call ReadCfgEntry
</code></pre>
<p>This seems like ReadCfgEntry is a class method as well, which gets its <code>this</code> pointer in cx. From the way the array index is calculated, i'd assume the original C++ class looked like this:</p>
<pre><code>class Configuration {
int whatever;
ConfigurationEntry entries[500];
....
}
</code></pre>
<p>with ConfigurationEntry being a 996 byte class.
Now, the bad news is: These two class members need 4+(500*996) bytes. This is equal to 498004, or 0x79954. So your entry count is directly behind the entries at 0x79954, with another variable at 0x79958:</p>
<pre><code>class Configuration {
int whatever;
ConfigurationEntry entries[500];
int entryCount;
int somethingelse;
... ??? ...
}
</code></pre>
<p>Now, if the entries had been a pointer and allocated with <code>new</code>, it would have been easier to just modify the size parameter in that new from 500 to 1000. But in your case, you'd have to modify the new method of the Configuration class, and you'd have to modify ALL references to variables "behind" the configuration entries as well. You already mentioned that for the count variable at offset 0x79954, and the next variable at offset 0x79958, but there might be more of them that don't get referenced in your reader function, so you'll have a hard time finding them all.</p>
<hr>
<p>I was just about to post this, when i saw your edit to the question.</p>
<p>As you realized in your edit, you need to change all accesses to structure components behind the array of entries that you want to increase. You need to do this in your class methods (which you can find easily as you have the vtables), but also in the rest of your program (as you don't know which of the original class variables where public and might be accessed from outside the class itself). <em>Unfortunately, you can't really automate that, because, for every occurence of, say 0x79954, you'd have to check if it's an index into your class, or something else.</em></p>
<p>Since i don't know which compiler was used to write your program, i can't tell much about how it stores the function vtable. With a bit of luck, the first entry (the one i called <code>whatever</code> earlier) is the vtable pointer. You can check this if you run the program with a debugger, and check if the <code>whatever</code> variable points to the vtable when it reaches the ReadConfigFile method. If it does, this is good, because we don't have to care about overwriting the vtable when extending the structure.</p>
<p>Then, there must be some allocator function for your classes. Since you seem to have a class called DATABASE_TABLE, and a derived one called CFG_DB, the second one is probably larger. Try finding the initializing method of this second, larger class. It should call <code>new</code> or a similar memory-allocator, with a size that fits the structure size (so it's probably somewhere between 79960h and 79a00h), and it should move the vtable pointer into the newly allocated memory, so you might be able to find it checking for cross-references of the vtable. Or, use a debugger and set a breakpoint on ReadConfigFile and check the stack for what called it, chances should be good you find a function that allocates the class instance first, then calls its ReadConfigFile member function.</p>
<p>After you find where the class is instantiated, i'd probably try not to increase the array size from 500 to 1000, but, instead, just allocate a larger array behind the structure. For example, if your current function allocates 79a00h bytes, add the 996000d bytes to it that 1000 entries need, resulting in 16cca0h bytes. Then, change the</p>
<pre><code>lea ecx, [esi+eax*4+4]
</code></pre>
<p>in front of ReadCfgEntry to</p>
<pre><code>lea ecx, [esi+eax*4+16cca0h]
</code></pre>
<p>That way, you've created another array <em>behind</em> the current structure instead of extending the current one. <em>Which means none of your structure offsets, except the config items themselves, have changed</em>.</p>
<p>Speaking in C++, we just changed the class to</p>
<pre><code>class Configuration {
int whatever;
ConfigurationEntry entries[500];
int entryCount;
int somethingelse;
... ??? ...
ConfigurationEntry newEntries[1000];
}
</code></pre>
<p>and in the next step we have to re-write all accessed to <code>entries</code> to use <code>newentries</code>.</p>
<p>Check your member functions for accesses to the original array, and replace them as well. The most simple way to do this is probably</p>
<ul>
<li>start the program with a debugger</li>
<li>set a breakpoint on the function you identified previously that allocates the structure</li>
<li>after the structure is allocated, set a hardware breakpoint on (structure address + 4)</li>
<li>continue the program, and whenever you hit the hardware breakpoints, you found a match</li>
</ul>
<p>Since the hardware breakpoint is on entries[0], chances are good that every access to an entry hits the 0-th of it at some point.</p>
<p>Also, since ReadCfgEntry is probably a class method, chances are good that there is a loop somewhere that just allocates one class instance for each entry - something like</p>
<pre><code>for (i=0; i<500; i++) {
entries[i]=new ConfigurationEntry()
}
</code></pre>
<p>Your hardware breakpoint should catch this loop quite quickly. Patch the executable to change the 500 to 1000, and the entries[i] calculation to your new array. After that, your new array will get initialized, but the old one will hold nothing but NULL pointers. Which means, you might get invalid memory acceses through those NULL pointers in the future, which help identifying accessed to your original array (that you can patch) as well.</p>
<hr>
<h2>Edit - Edit after reading OP's 2nd answer</h2>
<p>Dead end? Not at all, you gathered and posted very valuable information.</p>
<p>First, your pseudo code of the CFG_DB constructor</p>
<pre><code>void __thiscall sub_8EEFC0(void *this)
{
void *v1 = this; // esi@1
*(_DWORD *)this = &DATABASE_TABLE<CFG_ENTRY_500_unsigned_int>::_vftable_;
</code></pre>
<p>confirms that the 4 bytes at the beginning of the class structure are actually a pointer to the virtual function table of the class.</p>
<p>Second, your snippet</p>
<pre><code>.text:008EE639 push ebp
.text:008EE63A lea ecx, [esp+50h+var_2C]
.text:008EE63E push ecx
.text:008EE63F mov ecx, ebx
.text:008EE637 mov eax, [ebx]
.text:008EE641 call dword ptr [eax+14h]
.text:008EE644 ; ---------------------------------------------------------------------------
.text:008EE644 test al, al ; HERE IS THE STACK REFERENCE
</code></pre>
<p>fits very well. (Again, i reordered the assembly instructions in a way that doesn't change what they do, but makes it clearer to understand them). Remember your vtable that had a function offset, a nullsub, 3 more function offsets, and <code>ReadCfgFile</code> as its entries? Since each of these has 4 bytes, the offset of the ReadCfgFile function pointer in the vtable is 20, or 14h. In that code snippet, ebx is a class pointer; <code>mov eax, [ebx]</code> gets the vtable pointer from the class pointer, and <code>call dword ptr [eax+14h]</code> calls the function at that offset, namely <code>ReadCfgFile</code>. Before that, it initializes the <code>this</code> register (ecx) to ebx, and pushes 2 parameters on the stack. This seems to be a very standard call to a class method.</p>
<p>Next, your constructor ends in</p>
<pre><code>unknown_libname_2673((char *)v1 + 4, 0x3E4u, 500, sub_8EEA00);
</code></pre>
<p>with the first parameter (v1+4) being the address of the ConfigurationEntries array within the Configuration class (i'm keeping my old invented variable/class names), the second parameter (0x3e4 == 996) the size of each array entry; the third (500) the number of entries, and a callback function. I'm almost sure this is the constructor function for the individual ConfigurationEntries. Which means this is the function i said you need to find, and which should be changed to</p>
<pre><code>unknown_libname_2673((char *)v1 + XXXXX, 0x3E4u, 1000, sub_8EEA00);
</code></pre>
<p>with XXXXX being the offset of the newEntries array once we allocate space for it.</p>
<p>Next, reconsidering a part of the code snippet before the indirect call to ReadConfigFile,</p>
<pre><code>.text:008EE62A mov edx, [ebx]
.text:008EE62C mov ecx, ebx
.text:008EE62E mov [ebx+7A938h], ebp
.text:008EE634 call dword ptr [edx+4]
</code></pre>
<p>we see that there is a move to <code>ebx+7A938h</code>, with ebx being the class pointer, so there seems to be another class member at this offset. This is quite a lot of memory <em>after</em> the offset of the element count (79954h) - so the structure has a lot more components. Good thing you're not trying to shift them all. The constructor function, which accesses <code>this+502036</code>, or <code>this+0x7a914</code>, would have been another hint at that. (It also accesses <code>this+124503</code>, but with <code>this</code> being a dword pointer, this means 498012 bytes, which is still less than 502036).</p>
<p>Next, you found out the address 0190BD08, which is a very good thing. Along with the XREFs, and the data definition,</p>
<pre><code>.data:0190BD08 unk_190BD08 db ?
</code></pre>
<p>this means:</p>
<p>The class structure at that address is NOT allocated dynamically, and it is NOT initialized to anything, instead, it is an uninitialized global variable. In C++, it would have probably been a</p>
<pre><code>static Configuration myConfig;
</code></pre>
<p>with all the xrefs you're seeing being a reference to myConfig. As the assembly instructions at those places are</p>
<pre><code>mov ecx, offset unk_190BD08
</code></pre>
<p>i'm almost sure the calls to a class member function are one or 2 instructions after each of these. Congractulations, you've just found a way to catch many of the instances where the configuration gets accessed. To verify this, unk_190BD08 being a global variable for the configuration, you could run the program with a breakpoint on the constructor function (sub_8EEFC0), i bet it's called only once, and 190BD08 is the parameter to it.</p>
<p>The bad thing about the configuration class instance being a static variable, not a new()-instantiated one, is that we can't just increase the size in the new() call. Instead, we'll have to move it to a part of the address space where it doesn't disturb anything else - at the end of the current uninitialized data segment. Find the very last entry in your data segment, then choose a nice address behind that, and rewrite all the xrefs to unk_190BD08 to that new address. Then, run the program using ollydbg, place a hardware breakpoint on 190BD08 just in case, and check that the functions you know to be class member functions, and the initialized, all get the new address instead of 190BD08 as their <code>ecx</code> (this) parameter. When you've finished, you're ready to implement the 2nd part, change the accesses to <code>this+4</code> to <code>this+XXXX</code> with <code>XXXX</code> being the class size.</p>
<p>The fact that we're missing a new() call for the Configuration variable means we can't use its parameter to get the class size. But we already know the class size is at least 7A938h, so the static variable occupies the address space from 0x190BD08 to at least 0x1986640. With a bit of luck, your .data segment has that unk_190BD08 label, with the next label being at an address a bit after 0x1986640, so the next label is some other variable, and the difference between both labels the size of the Configuration instance.</p>
<p>There's one thing left to do - when you move the configuration variable behind everything else, you'll also have to increase the size of that data segment in the PE file. Unfortunately, i'm not that skilled in the windows PE file format, so i'd have to do a lot of research how to do this correctly myself, so maybe you're lucky and someone else, who has more experience in this than i have, can help you with this.</p>
<hr>
<h2>Edit - Edit to answer to comment</h2>
<ul>
<li>how can I calculate how big is the CFG_DB class in order to append something at the end of it?</li>
</ul>
<p>The original C++ program will have had something like</p>
<pre><code>int somevariable;
int someothervariable;
CFG_DB globalConfig;
int athirdvariable;
</code></pre>
<p>(int as an example, the data type could be anything)
and the compiler should place all of these varuables into the uninitialized part of the .data segment. Any access to one of the variables will make IDA create a label at that memory address, with an XREF to where it's accessed. So, if your .data dump looks like this</p>
<pre><code>.data:0190BD08 unk_190BD08 db ? <-- along with some xrefs
.data:0190BD09 db ? <-- this repeats a lot of times
.data:01986670 unk_1986670 db ? <-- along with some xrefs
</code></pre>
<p>and you know that 0190BD08 is your <code>globalConfig</code>, then 01986670 is <code>athirdvariable</code>, and the size of globalConfig is 0x01986670 - 0x0190BD08 = 0x7a968 = 502120 bytes.</p>
<p>This is not 100% foolproof, because if anything accesses globalConfig.somemember, with somemember having a structure offset of, say, 500000 (=0x7a120), then IDA will generate a label and an XREF at 0x190BD08+0x7a120=0x1985E28 ass well. But with a bit of luck, the "rest" of the program will just use the globalConfig variable as an argument to a member function, where references are indirect, so they won't be used by IDA to create labels.</p>
<ul>
<li>what will happen if other classes/methods/etc are interacting with this modified class?</li>
</ul>
<p>If they access anything but the 500 individual config entries, nothing at all, because these haven't changed their offset. The thing that might be dangerous is when they access the entries, because those accesses should be rewritten to newEntries. You have to find out where they are (if you're lucky, that's just in the member functions) and patch the code at that point. The hardware breakpoint on the (old) entries[0] address (which is 0x190BD0C in your case, structure start + 4) should help with this, because anything that's likely to access any entry, is likely to access entry[0] as well. So, if you hit the hardware breakpoint, on, say, <code>mov eax, [ebx+4]</code>, then you know <code>ebx+4</code> is accessing the old address, and should be rewritten to <code>mov eax, [ebx+XXXXX]</code> to use the new array.</p>
<p>Unfortunately, you can't create a hardware breakpoint that covers the whole structure to catch any access. That's where the null pointer exceptions kick in. If, after the change, your program throws a NPE where it normally doesn't, it's probably because 'something' accessed the old array, which contains nothing but NULLs now, instead of the new one. Catching the NPE in the debugger, and checking the disassembly/call stack that lead to it, should give you an idea where the NULL pointer was read from the old array, so you know which instruction to change to point to the new one.</p>
| 4287 | 2014-05-08T00:59:14.813 | <p>I'm not new to StackExchange, but I'm quite new to reverse engineering, so please be patient with me! :P</p>
<p>At present I'm dealing with an executable that I would like to modify a little bit, for personal use; the source code of the application is not available so I can only modify it or die trying. I'm using both IDA Pro 6.1 and OllyDBG 2.0 as tools.</p>
<p>To be exact, I would just like to increase the amount of CFG_ENTRY that the application can read from <code>500</code> to <code>1000</code> in the method <code>ReadCfgFile</code> which, apparently, has a static memory region preallocated at compile-time:</p>
<pre><code>.text:008F2860 ReadCfgFile proc near ; DATA XREF: .rdata:0137A1ECo
.text:008F2860 var_20 = dword ptr -20h
.text:008F2860 var_1C = dword ptr -1Ch
.text:008F2860 var_18 = dword ptr -18h
.text:008F2860 var_C = dword ptr -0Ch
.text:008F2860 var_4 = dword ptr -4
.text:008F2860 arg_0 = dword ptr 4
.text:008F2860 arg_4 = byte ptr 8
.text:008F2860
.text:008F2860 push 0FFFFFFFFh
.text:008F2862 mov eax, large fs:0
.text:008F2868 push offset sub_1288B18
.text:008F286D push eax
.text:008F286E mov large fs:0, esp
.text:008F2875 sub esp, 14h
.text:008F2878 push ebx
.text:008F2879 push ebp
.text:008F287A push esi
.text:008F287B push edi
.text:008F287C mov edi, [esp+30h+arg_0]
.text:008F2880 mov eax, [edi+10h]
.text:008F2883 cmp eax, [edi+8]
.text:008F2886 mov esi, ecx
.text:008F2888 jnb loc_8F2930
.text:008F288E mov edi, edi
.text:008F2890
.text:008F2890 loc_8F2890:
.text:008F2890 mov eax, [esi+79954h]
.text:008F2896 cmp eax, 3E8h
.text:008F289B jge loc_8F29DF
.text:008F28A1 mov edx, eax
.text:008F28A3 shl edx, 5
.text:008F28A6 lea ecx, [eax+1]
.text:008F28A9 sub edx, eax
.text:008F28AB lea eax, [eax+edx*8]
.text:008F28AE mov [esi+79954h], ecx
.text:008F28B4 push edi
.text:008F28B5 lea ecx, [esi+eax*4+4]
.text:008F28B9 call ReadEDURecord
.text:008F28BE test al, al
.text:008F28C0 jz loc_8F2947
.text:008F28C6 mov eax, [esi+79954h]
.text:008F28CC mov ecx, eax
.text:008F28CE shl ecx, 5
.text:008F28D1 sub ecx, eax
.text:008F28D3 lea edx, [eax+ecx*8]
.text:008F28D6 mov ebp, [esi+edx*4-3E0h]
.text:008F28DD lea eax, [esp+30h+arg_0]
.text:008F28E1 lea ebx, [esi+79958h]
.text:008F28E7 push eax
.text:008F28E8 mov ecx, ebx
.text:008F28EA mov [esp+34h+arg_0], ebp
.text:008F28EE call sub_437DF0
.text:008F28F3 test eax, eax
.text:008F28F5 jz short loc_8F2902
.text:008F28F7 cmp [esp+30h+arg_4], 0
.text:008F28FC jz loc_8F2994
.text:008F2902
.text:008F2902 loc_8F2902:
.text:008F2902 mov ecx, [esi+79954h]
.text:008F2908 sub ecx, 1
.text:008F290B lea edx, [esp+30h+arg_0]
.text:008F290F push edx
.text:008F2910 lea eax, [esp+34h+var_20]
.text:008F2914 mov [esp+34h+arg_0], ecx
.text:008F2918 push eax
.text:008F2919 mov ecx, ebx
.text:008F291B mov [esp+38h+var_20], ebp
.text:008F291F call sub_437890
.text:008F2924 mov ecx, [edi+10h]
.text:008F2927 cmp ecx, [edi+8]
.text:008F292A jb loc_8F2890
.text:008F2930
.text:008F2930 loc_8F2930:
.text:008F2930 pop edi
.text:008F2931 pop esi
.text:008F2932 pop ebp
.text:008F2933 mov al, 1
.text:008F2935 pop ebx
.text:008F2936 mov ecx, [esp+20h+var_C]
.text:008F293A mov large fs:0, ecx
.text:008F2941 add esp, 20h
.text:008F2944 retn 8
.text:008F2947
.text:008F2947 loc_8F2947:
.text:008F2947 push 0
.text:008F2949 call sub_D386E0
.text:008F294E add esp, 4
.text:008F2951 mov ecx, edi
.text:008F2953 mov esi, eax
.text:008F2955 call sub_D4D270
.text:008F295A push eax ; ArgList
.text:008F295B push offset aErrMsg_1 ; Error Message
.text:008F2960 call sub_D386E0
.text:008F2965 add esp, 8
.text:008F2968 call sub_D388C0
.text:008F296D lea edx, [esp+30h+var_20]
.text:008F2971 push edx
.text:008F2972 lea ecx, [esp+34h+var_18]
.text:008F2976 mov [esp+34h+var_20], eax
.text:008F297A mov [esp+34h+var_1C], 640h
.text:008F2982 call sub_403D60
.text:008F2987 mov [esp+30h+var_4], 0
.text:008F298F jmp loc_8F2A27
.text:008F2994
.text:008F2994 loc_8F2994:
.text:008F2994 push 0
.text:008F2996 call sub_D386E0
.text:008F299B add esp, 4
.text:008F299E mov ecx, edi
.text:008F29A0 mov esi, eax
.text:008F29A2 call sub_D4D270
.text:008F29A7 push eax
.text:008F29A8 push ebp ; ArgList
.text:008F29A9 push offset aErrMsg_2 ; Error Message
.text:008F29AE call sub_D386E0
.text:008F29B3 add esp, 0Ch
.text:008F29B6 call sub_D388C0
.text:008F29BB mov [esp+30h+var_20], eax
.text:008F29BF lea eax, [esp+30h+var_20]
.text:008F29C3 push eax
.text:008F29C4 lea ecx, [esp+34h+var_18]
.text:008F29C8 mov [esp+34h+var_1C], 640h
.text:008F29D0 call sub_403D60
.text:008F29D5 mov [esp+30h+var_4], 1
.text:008F29DD jmp short loc_8F2A27
.text:008F29DF
.text:008F29DF loc_8F29DF:
.text:008F29DF push 0
.text:008F29E1 call sub_D386E0
.text:008F29E6 add esp, 4
.text:008F29E9 mov ecx, edi
.text:008F29EB mov esi, eax
.text:008F29ED call sub_D4D270
.text:008F29F2 push eax ; ArgList
.text:008F29F3 push offset aErrMsg_0 ; Error Message
.text:008F29F8 call sub_D386E0
.text:008F29FD add esp, 8
.text:008F2A00 call sub_D388C0
.text:008F2A05 lea ecx, [esp+30h+var_20]
.text:008F2A09 push ecx
.text:008F2A0A lea ecx, [esp+34h+var_18]
.text:008F2A0E mov [esp+34h+var_20], eax
.text:008F2A12 mov [esp+34h+var_1C], 640h
.text:008F2A1A call sub_403D60
.text:008F2A1F mov [esp+30h+var_4], 2
.text:008F2A27
.text:008F2A27 loc_8F2A27:
.text:008F2A27 mov eax, [esp+30h+var_18]
.text:008F2A2B test eax, eax
.text:008F2A2D jz short loc_8F2A3A
.text:008F2A2F push esi
.text:008F2A30 push eax
.text:008F2A31 call ds:??$?6U?$char_traits@D@std@@@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@PBD@Z ; std::operator<<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,char const *)
.text:008F2A37 add esp, 8
.text:008F2A3A
.text:008F2A3A loc_8F2A3A:
.text:008F2A3A lea ecx, [esp+30h+var_18]
.text:008F2A3E mov [esp+30h+var_4], 0FFFFFFFFh
.text:008F2A46 call sub_403DF0
.text:008F2A4B mov ecx, [esp+30h+var_C]
.text:008F2A4F pop edi
.text:008F2A50 pop esi
.text:008F2A51 pop ebp
.text:008F2A52 xor al, al
.text:008F2A54 pop ebx
.text:008F2A55 mov large fs:0, ecx
.text:008F2A5C add esp, 20h
.text:008F2A5F retn 8
.text:008F2A5F ReadCfgFile endp
</code></pre>
<p><strong>[EDIT 1 - All what I should have known since the beginning!]</strong></p>
<p>After following the suggestions of the answer of @sealed..., I used a class inspector to detect the Virtual Function Table and I found the full class descriptor. Well... in fact there are two classes referring to my target method <code>ReadCfgFile</code> and no direct calls to it in the whole executable:</p>
<pre><code>.rdata:0137A1D4 ; class DATABASE_TABLE<CFG_ENTRY,500,unsigned int> [SI] O: 0, A: 0
.rdata:0137A1D4 dd offset ??_R4?$DATABASE_TABLE@UCFG_ENTRY@@$0BPE@I@@6B@ ; RTTI Complete Object Locator
.rdata:0137A1D8 ; const DATABASE_TABLE<struct CFG_ENTRY,500,unsigned int> VF Table
.rdata:0137A1D8 ??_7?$DATABASE_TABLE@UCFG_ENTRY@@$0BPE@I@@6B@ dd offset sub_8EF0F0 ; DATA XREF: sub_8EEFC0+1Do
.rdata:0137A1DC dd offset nullsub_648
.rdata:0137A1E0 dd offset sub_8EAB30
.rdata:0137A1E4 dd offset sub_8EF060
.rdata:0137A1E8 dd offset sub_8EE500
.rdata:0137A1EC dd offset ReadCfgFile
.rdata:0137A1F0 ; class CFG_DB: DATABASE_TABLE<CFG_ENTRY,500,unsigned int> [SI] O: 0, A: 0
.rdata:0137A1F0 dd offset ??_R4CFG_DB@@6B@ ; RTTI Complete Object Locator
.rdata:0137A1F4 ; const CFG_DB VFTable
.rdata:0137A1F4 ??_7UNIT_DB@@6B@ dd offset sub_8EF2B0 ; DATA XREF: sub_8EF290+8o
.rdata:0137A1F8 dd offset nullsub_648
.rdata:0137A1FC dd offset sub_8EAB30
.rdata:0137A200 dd offset sub_8EF060
.rdata:0137A204 dd offset sub_8EE8B0
.rdata:0137A208 dd offset ReadCfgFile
.rdata:0137A20C dd offset sub_8EE5D0
</code></pre>
<p><strong>[EDIT 2 - The adventure continues! Yay!]</strong></p>
<p>After reading the answer of @Guntram Blohm, I investigated some more in order to collect and analyze the data he suggested. The first thing I did is to analyze the executable with PEiD and here is the information I got from it:</p>
<pre><code>Compiler: Microsoft Visual C++ 7.0 Method2 [Debug]
Entropy: 6.24 (Not Packed)
Linker Info: 7.10
</code></pre>
<p>When I set a breakpoint on my <code>ReadCfgFile</code> method, here is what I get from OllyDBG stack:</p>
<pre><code>CPU Stack
Address Value ASCII Comments
0018B2C0 [008EE644 D�. ; RETURN to myapp.008EE644
</code></pre>
<p>And <code>008EE644</code> is a small part of the following method that, for what I can understand, looks for the configuration file and starts the routine for reading it, but without an explicit call to <code>ReadCfgFile</code> (offset highlighted):</p>
<pre><code>.text:008EE5D0 sub_8EE5D0 proc near ; CODE XREF: sub_411B20+2CBp
.text:008EE5D0 var_41 = byte ptr -41h
.text:008EE5D0 var_40 = dword ptr -40h
.text:008EE5D0 var_3C = dword ptr -3Ch
.text:008EE5D0 var_38 = byte ptr -38h
.text:008EE5D0 var_34 = dword ptr -34h
.text:008EE5D0 var_30 = dword ptr -30h
.text:008EE5D0 var_2C = byte ptr -2Ch
.text:008EE5D0 var_C = dword ptr -0Ch
.text:008EE5D0 var_4 = dword ptr -4
.text:008EE5D0
.text:008EE5D0 push 0FFFFFFFFh
.text:008EE5D2 push offset SEH_8EE5D0
.text:008EE5D7 mov eax, large fs:0
.text:008EE5DD push eax
.text:008EE5DE mov large fs:0, esp
.text:008EE5E5 sub esp, 38h
.text:008EE5E8 push ebx
.text:008EE5E9 push ebp
.text:008EE5EA mov ebx, ecx
.text:008EE5EC push offset aCfgFile ; "application.cfg"
.text:008EE5F1 mov [esp+50h+var_30], ebx
.text:008EE5F5 call sub_41BD00
.text:008EE5FA add esp, 4
.text:008EE5FD push eax
.text:008EE5FE lea ecx, [esp+50h+var_38]
.text:008EE602 call sub_F018E0
.text:008EE607 xor ebp, ebp
.text:008EE609 push ebp
.text:008EE60A lea eax, [esp+50h+var_38]
.text:008EE60E push eax
.text:008EE60F lea ecx, [esp+54h+var_2C]
.text:008EE613 mov [esp+54h+var_4], ebp
.text:008EE617 call sub_D50170
.text:008EE61C lea ecx, [esp+4Ch+var_38] ; void *
.text:008EE620 mov byte ptr [esp+4Ch+var_4], 2
.text:008EE625 call sub_EFFE30
.text:008EE62A mov edx, [ebx]
.text:008EE62C mov ecx, ebx
.text:008EE62E mov [ebx+7A938h], ebp
.text:008EE634 call dword ptr [edx+4]
.text:008EE637 mov eax, [ebx]
.text:008EE639 push ebp
.text:008EE63A lea ecx, [esp+50h+var_2C]
.text:008EE63E push ecx
.text:008EE63F mov ecx, ebx
.text:008EE641 call dword ptr [eax+14h]
.text:008EE644 ; ---------------------------------------------------------------------------
.text:008EE644 test al, al ; HERE IS THE STACK REFERENCE
.text:008EE644 ; ---------------------------------------------------------------------------
.text:008EE646 jnz short loc_8EE66C
.text:008EE648 lea ecx, [esp+4Ch+var_2C]
.text:008EE64C mov [esp+4Ch+var_4], 0FFFFFFFFh
.text:008EE654 call sub_D4BB30
.text:008EE659 pop ebp
.text:008EE65A xor al, al
.text:008EE65C pop ebx
.text:008EE65D mov ecx, [esp+44h+var_C]
.text:008EE661 mov large fs:0, ecx
.text:008EE668 add esp, 44h
.text:008EE66B retn
.text:008EE66C
.text:008EE66C loc_8EE66C:
.text:008EE66C xor edx, edx
.text:008EE66E or eax, 0FFFFFFFFh
.text:008EE671 mov dword_1986644, edx
.text:008EE677 mov dword_1986650, eax
.text:008EE67C push esi
.text:008EE67D mov dword_1986648, edx
.text:008EE683 mov dword_1986654, eax
.text:008EE688 push edi
.text:008EE689 mov dword_198664C, edx
.text:008EE68F mov dword_1986658, eax
.text:008EE694 xor edi, edi
.text:008EE696 cmp [ebx+79954h], ebp
.text:008EE69C jle loc_8EE727
.text:008EE6A2 lea esi, [ebx+40h]
.text:008EE6A5 jmp short loc_8EE6B0
.text:008EE6A7 align 10h
.text:008EE6B0
.text:008EE6B0 loc_8EE6B0:
.text:008EE6B0 mov eax, [esi]
.text:008EE6B2 mov cx, [esi+92h]
.text:008EE6B9 lea eax, ds:1986644h[eax*2]
.text:008EE6C0 mov ax, [eax]
.text:008EE6C3 cmp ax, cx
.text:008EE6C6 jnb short loc_8EE6CA
.text:008EE6C8 mov eax, ecx
.text:008EE6CA
.text:008EE6CA loc_8EE6CA:
.text:008EE6CA mov ecx, [esi]
.text:008EE6CC mov word ptr dword_1986644[ecx*2], ax
.text:008EE6D4 mov eax, [esi]
.text:008EE6D6 mov cx, [esi+92h]
.text:008EE6DD lea eax, ds:1986650h[eax*2]
.text:008EE6E4 mov ax, [eax]
.text:008EE6E7 cmp ax, cx
.text:008EE6EA jb short loc_8EE6EE
.text:008EE6EC mov eax, ecx
.text:008EE6EE
.text:008EE6EE loc_8EE6EE:
.text:008EE6EE mov edx, [esi]
.text:008EE6F0 lea ecx, [esi-3Ch]
.text:008EE6F3 mov word ptr dword_1986650[edx*2], ax
.text:008EE6FB call sub_8ED600
.text:008EE700 push eax
.text:008EE701 mov eax, [ebx+7A938h]
.text:008EE707 push eax
.text:008EE708 call sub_F1E550
.text:008EE70D add edi, 1
.text:008EE710 add esp, 8
.text:008EE713 mov [ebx+7A938h], eax
.text:008EE719 add esi, 3E4h
.text:008EE71F cmp edi, [ebx+79954h]
.text:008EE725 jl short loc_8EE6B0
.text:008EE727
.text:008EE727 loc_8EE727:
.text:008EE727 xor esi, esi
.text:008EE729 cmp dword_1667290, ebp
.text:008EE72F mov [esp+54h+var_3C], esi
.text:008EE733 jbe loc_8EE840
.text:008EE739 lea esp, [esp+0]
.text:008EE740
.text:008EE740 loc_8EE740:
.text:008EE740 cmp [ebx+79954h], ebp
.text:008EE746 mov [esp+54h+var_41], 0
.text:008EE74B mov [esp+54h+var_40], ebp
.text:008EE74F mov [esp+54h+var_34], ebp
.text:008EE753 jle loc_8EE7D9
.text:008EE759 mov ebp, 1
.text:008EE75E mov ecx, esi
.text:008EE760 shl ebp, cl
.text:008EE762 lea edi, [ebx+3B0h]
.text:008EE768
.text:008EE768 loc_8EE768:
.text:008EE768 cmp [esp+54h+var_41], 0
.text:008EE76D jnz short loc_8EE77F
.text:008EE76F test [edi-2Ch], ebp
.text:008EE772 jz short loc_8EE77F
.text:008EE774 test byte ptr [edi+3], 20h
.text:008EE778 jz short loc_8EE77F
.text:008EE77A mov [esp+54h+var_41], 1
.text:008EE77F
.text:008EE77F loc_8EE77F:
.text:008EE77F xor esi, esi
.text:008EE781 xor eax, eax
.text:008EE783
.text:008EE783 loc_8EE783:
.text:008EE783 mov ecx, [edi-24h]
.text:008EE786 test [eax+ecx], ebp
.text:008EE789 jz short loc_8EE7AF
.text:008EE78B cmp eax, 10h
.text:008EE78E jnb loc_8EE89B
.text:008EE794 mov ecx, esi
.text:008EE796 shr ecx, 5
.text:008EE799 lea edx, [esp+ecx*4+54h+var_40]
.text:008EE79D mov ecx, esi
.text:008EE79F and ecx, 1Fh
.text:008EE7A2 mov ebx, 1
.text:008EE7A7 shl ebx, cl
.text:008EE7A9 or [edx], ebx
.text:008EE7AB mov ebx, [esp+54h+var_30]
.text:008EE7AF
.text:008EE7AF loc_8EE7AF:
.text:008EE7AF add eax, 4
.text:008EE7B2 add esi, 1
.text:008EE7B5 cmp eax, 10h
.text:008EE7B8 jb short loc_8EE783
.text:008EE7BA mov eax, [esp+54h+var_34]
.text:008EE7BE add eax, 1
.text:008EE7C1 add edi, 3E4h
.text:008EE7C7 cmp eax, [ebx+79954h]
.text:008EE7CD mov [esp+54h+var_34], eax
.text:008EE7D1 jl short loc_8EE768
.text:008EE7D3 mov esi, [esp+54h+var_3C]
.text:008EE7D7 xor ebp, ebp
.text:008EE7D9
.text:008EE7D9 loc_8EE7D9:
.text:008EE7D9 push esi
.text:008EE7DA call sub_8D1490
.text:008EE7DF mov edi, eax
.text:008EE7E1 add esp, 4
.text:008EE7E4 cmp byte ptr [edi+0BCh], 0
.text:008EE7EB jz short loc_8EE82D
.text:008EE7ED xor esi, esi
.text:008EE7EF cmp esi, 4
.text:008EE7F2 jnb loc_8EE8A4
.text:008EE7F8
.text:008EE7F8 loc_8EE7F8:
.text:008EE7F8 mov ecx, esi
.text:008EE7FA and ecx, 1Fh
.text:008EE7FD mov edx, 1
.text:008EE802 shl edx, cl
.text:008EE804 mov ecx, esi
.text:008EE806 shr ecx, 5
.text:008EE809 add ecx, ecx
.text:008EE80B add ecx, ecx
.text:008EE80D test [esp+ecx+54h+var_40], edx
.text:008EE811 setnz al
.text:008EE814 test al, al
.text:008EE816 jnz short loc_8EE821
.text:008EE818 not edx
.text:008EE81A and [ecx+edi+0C0h], edx
.text:008EE821
.text:008EE821 loc_8EE821:
.text:008EE821 add esi, 1
.text:008EE824 cmp esi, 4
.text:008EE827 jb short loc_8EE7F8
.text:008EE829 mov esi, [esp+54h+var_3C]
.text:008EE82D
.text:008EE82D loc_8EE82D:
.text:008EE82D add esi, 1
.text:008EE830 cmp esi, dword_1667290
.text:008EE836 mov [esp+54h+var_3C], esi
.text:008EE83A jb loc_8EE740
.text:008EE840
.text:008EE840 loc_8EE840:
.text:008EE840 xor esi, esi
.text:008EE842 cmp [ebx+79954h], ebp
.text:008EE848 jle short loc_8EE875
.text:008EE84A lea edi, [ebx+108h]
.text:008EE850
.text:008EE850 loc_8EE850:
.text:008EE850 mov eax, [edi]
.text:008EE852 mov ecx, dword_16E9DC8
.text:008EE858 push eax ; Str2
.text:008EE859 add ecx, 84h
.text:008EE85F call sub_10E86C0
.text:008EE864 add esi, 1
.text:008EE867 add edi, 3E4h
.text:008EE86D cmp esi, [ebx+79954h]
.text:008EE873 jl short loc_8EE850
.text:008EE875
.text:008EE875 loc_8EE875:
.text:008EE875 lea ecx, [esp+54h+var_2C]
.text:008EE879 mov [esp+54h+var_4], 0FFFFFFFFh
.text:008EE881 call sub_D4BB30
.text:008EE886 mov ecx, [esp+54h+var_C]
.text:008EE88A pop edi
.text:008EE88B pop esi
.text:008EE88C pop ebp
.text:008EE88D mov al, 1
.text:008EE88F pop ebx
.text:008EE890 mov large fs:0, ecx
.text:008EE897 add esp, 44h
.text:008EE89A retn
.text:008EE89B
.text:008EE89B loc_8EE89B:
.text:008EE89B lea ecx, [esp+54h+var_40]
.text:008EE89F jmp sub_8D0FE0
.text:008EE8A4
.text:008EE8A4 loc_8EE8A4:
.text:008EE8A4 lea ecx, [esp+54h+var_40]
.text:008EE8A8 jmp sub_8D0FE0
.text:008EE8A8 sub_8EE5D0 endp
</code></pre>
<p>The, I digged a little bit more finding out the <code>CFG_DB</code> contructor, which looks like this (pseudo-code from IDA Pro):</p>
<pre><code>void __thiscall sub_8EEFC0(void *this)
{
void *v1 = this; // esi@1
*(_DWORD *)this = &DATABASE_TABLE<CFG_ENTRY_500_unsigned_int>::_vftable_;
sub_8EE500((int)this);
sub_8EC030((char *)v1 + 502036);
if ( *((_DWORD *)v1 + 124503) )
operator delete__(*((void **)v1 + 124503));
*((_DWORD *)v1 + 124503) = 0;
unknown_libname_2673((char *)v1 + 4, 0x3E4u, 500, sub_8EEA00);
}
</code></pre>
<p>So it looks like the "array" of <code>CFG_ENTRY</code> is being instantiated calling a method belonging to another library linked to the executable.
Finally, I put a breakpoint at the beginning of my <code>ReadCfgFile</code> method in order to see if pointers passed to it can be of any help:</p>
<pre><code>.text:008F287A push esi ==> esi = 00400000
[...] [...]
.text:008F2886 mov esi, ecx ==> ecx = myapp.0190BD08
</code></pre>
<p>And following the address <code>0190BD08</code> I just stumbled upon this:</p>
<pre><code>.data:0190BD08 unk_190BD08 db ? ; ; DATA XREF: sub_40FFF0:loc_410049o
.data:0190BD08 ; sub_40FFF0:loc_410053o ...
.text:00410049 loc_410049: ; DATA XREF: .rdata:01484034o
.text:00410049 ; .rdata:0148489Co ...
.text:00410049 mov ecx, offset unk_190BD08
.text:00410053
.text:00410053 loc_410053: ; DATA XREF: .rdata:01484078o
.text:00410053 ; .rdata:01484C3Co ...
.text:00410053 mov ecx, offset unk_190BD08
</code></pre>
<p>It looks like a dead end to me...</p>
| How to deal with Static Memory Allocation while modifying an executable? | |assembly|decompilation|debugging|memory|struct| |
<p>what does broken mean </p>
<p>the instruction in either this post or in your earlier post do not seem to be broken </p>
<p>if you are confused with AT&T syntax then you can ask objdump to disassemble in intel syntax</p>
<p>a better explanation of broken can get a better answer </p>
<pre><code>root@box:/home/dsl/gcctests/test# cat testjt.c
void naked (void)
{
asm( ".globl _naked\n");
asm( "_naked:\n");
asm( "jmp *0x80509e4(,%eax,4)");
asm( "lea 0x0(,%ebp,8),%eax");
asm( "ret");
}
int main (void)
{
naked();
return 0;
}
root@box:/home/dsl/gcctests/test# objdump -t ./testjt | grep _naked
08048357 g .text 00000000 _naked
root@box:/home/dsl/gcctests/test# objdump -d --disassembler-option=intel ./testjt | grep -A 2 _naked
08048357 <_naked>:
8048357: ff 24 85 e4 09 05 08 jmp DWORD PTR [eax*4+134547940]
804835e: 8d 04 ed 00 00 00 00 lea eax,[ebp*8]
root@box:/home/dsl/gcctests/test#
</code></pre>
| 4288 | 2014-05-08T02:40:49.520 | <p>So basically I am using <code>objdump</code> to disassemble a binary from `GNU Coreutils", on 32 bit Linux x86.</p>
<p>In the disassembled code, I found one "broken" instruction like this:</p>
<pre><code> 804bb49: 8d 04 ed 00 00 00 00 lea 0x0(,%ebp,8),%eax
</code></pre>
<p>I asked a related question about a very similar instruction <a href="https://reverseengineering.stackexchange.com/questions/4285/how-to-deal-with-this-error-instructions-generated-by-objdump">here</a>, in that question, I think it should be a jump table related instruction, but how about this one ? I am quite confused..</p>
<p>Could anyone give me some help?</p>
| How to deal with this “lea” instructions generated by objdump? | |disassembly|assembly|objdump| |
<p>that is normal for virtual pc to say debugee not connected as it has not<br>
connectedto the target yet it has just established a transport</p>
<p>press <code>ctrl+break</code> once to connect and press <code>g</code> to resume running.</p>
<p>As Jason suggested get the <code>free vmware player</code> and configure <code>virtualkd</code> for a much<br>
faster debugging com port is too slow for many things especially conditinal log breaks<br>
will stall the debugger for minutes together.</p>
| 4298 | 2014-05-09T00:32:46.323 | <p>I am trying to connect to a Windows XP Professional Virtual Machine running under Microsoft Virtual PC for debugging purposes.
I was following the <a href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff538143%28v=vs.85%29.aspx" rel="nofollow">MSDN kernel mode debugging article</a>, however bcdedit command was not being recognized on the guest machine so I added the following entry in the boot.ini file under C:\ drive</p>
<pre><code>multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="KD" /fastdetect /debug /debugport=2 /baudrate=115200
</code></pre>
<p>I configured the com2 port to a named pipe (\\.\pipe\pipe2)</p>
<p>I enter the following command at the command prompt and turned on the virtual machine</p>
<pre><code>windbg -k com:pipe,port=\\.\pipe\Pipe2,resets=0,reconnect
</code></pre>
<p>and I get the following output</p>
<pre><code>Microsoft (R) Windows Debugger Version 6.12.0002.633 X86
Copyright (c) Microsoft Corporation. All rights reserved.
Waiting for pipe \\.\pipe\pipe2
Waiting to reconnect...
Connected to Windows XP 2600 x86 compatible target at (Fri May 9 05:34:23.920 2014 (UTC + 5:30)), ptr64 FALSE
Kernel Debugger connection established.
Symbol search path is: C:\Windows\Symbols;srv*C:\Windows\Symbols*http://msdl.microsoft.com/download/symbols
Executable search path is:
Windows XP Kernel Version 2600 UP Free x86 compatible
Built by: 2600.xpsp_sp3_qfe.100216-1510
Machine Name:
Kernel base = 0x804d7000 PsLoadedModuleList = 0x8055b1c0
System Uptime: not available
56: ERROR: UMRxReadDWORDFromTheRegistry/ZwQueryValueKey: NtStatus = c0000034
ERROR: DavReadRegistryValues/RegQueryValueExW(4). WStatus = 127
ERROR: DavReadRegistryValues/RegQueryValueExW(5). WStatus = 127
ERROR: DavReadRegistryValues/RegQueryValueExW(6). WStatus = 127
</code></pre>
<p>The status bar at the bottom says 'Debugee not connected'</p>
<p>Am I missing something?</p>
| Debugging Virtual Machine using Windbg | |debuggers|debugging|windbg|virtual-machines| |
<p>I'm afraid you're not too clear when you talk about "reusing" some part of a program. What exactly do you want to do? Load the original program into a debugger, then call just one single function of it? Extract a ranged of addresses and turn that into a new program? Or extract one function from the original program, possibly together with everything the function depends on, then embed that function into a larger program?</p>
<p>You're wrong in one thing: the .bss section is NOT initialized at all at program start (at least not to anything but a load of <code>\0</code> bytes). The initialization of stuff like <code>stdout</code> is done in runtime before main() starts. Stuff that gets initialized with constant data goes into the .data segment.</p>
<p>(On a side note: One of the reasons why the stdout FILE structure isn't put into .data and initialized as constants is most runtimes on unix-like systems will check if their file descriptor goes to a terminal or something else, and turn on/off buffering depending on that check).</p>
<p>When you extract a part of the code, you'll have to check each reference to .data AND .bss (and to .text - the code - as well if the extracted part depends on any libraries etc.). For each of those references, you'll have to decide what to do with them - share them with the new program, keep them for the functions you extracted, whatever.</p>
<p>If you're lucky, and have an unstripped executable, you can use <code>nm</code> to find out which symbol is where; if you're not that lucky, you'll have to disassemble/decompile everything, understand it, rewrite it into new source, and compile it together with whatever you want to embed it in.</p>
| 4306 | 2014-05-10T16:24:59.647 | <p>So basically I am trying to re-use some assembly code/data dumped by <code>objdump</code> from 32 bit ELF binary on Linux.</p>
<p>So basically, in the disassembled binary, I found some symbol referring to <code>.bss</code> section like this:</p>
<pre><code> 80486b7: mov 0x804b264,%eax <- 0x804b264 is an addr in .bss
80486bc: movl $0x0,0x4(%esp)
80486c3:
80486c4: mov %eax,(%esp)
80486c7: call 804876c <sum>
</code></pre>
<p>By digging into the original source code, I find out that <code>0x804b264</code> is used for <code>stdin</code> in <code>.bss</code> section.</p>
<p>IMHO, there are basically two situations on <code>.bss</code> section's data:</p>
<ol>
<li><p>uninitialized data, will be uninitialized in the source code</p></li>
<li><p>some global/system related data (such as <strong>stdin</strong> in the above situation)</p></li>
</ol>
<p>So when trying to re-use <code>.bss</code> section's data, I have tried this way:</p>
<pre><code>.section .bss
S_0x804B260 : .byte 0x00
.byte 0x00
.byte 0x00
.byte 0x00
S_0x804B264 : .byte 0x00 <- I lift addr into symbol!
.byte 0x00
.byte 0x00
.byte 0x00
.byte 0x00
</code></pre>
<p>and in the <code>.text</code> section, I will also lift corresponding addr into symbol.</p>
<p><strong>same situation works fine for <code>.rodata</code> and <code>.data</code> section</strong></p>
<p>But the problem is that, there are some global variables (such as <strong>stdin</strong> <strong>stdout</strong>), locating in <code>.bss</code> section, while are not initialized by <strong>user defined code</strong>, which means that in my re-used asm code, this symbol variable will always be <strong>zero</strong>!</p>
<p>I tried to manually substitute symbols corresponding to <strong>stdin</strong>, and it seems that it works fine.</p>
<p>But the problem is that:</p>
<ol>
<li><p>How can I identify which symbol in the <code>.bss</code> section is some variables that initialized by system? such as <strong>stdin</strong>, <em>stdout*</em> and others?</p></li>
<li><p>If it is not possible, then is there anyway that I can enforce system to use memory in the <code>.bss</code> section where I want it to use?</p></li>
</ol>
<p>Am I clear? Could anyone give me some help?</p>
| How to reuse symbol/data defined in .bss section? | |disassembly|assembly|elf|reassembly| |
<p>The fast answer is that in Java, the <em>fully qualified name of the class</em> is the <em>actual</em> name of the class concatenated with its package. To the classloader, <code>com.foo.Annamalai</code> is the name it will load at runtime. This is also, subsequently why you can't have two classes of the same name <em>in the same package.</em> </p>
<p>This will be a (semi) contrived instance, but lets pretend your app has two different logging frameworks, and you need to support both. </p>
<pre><code>import java.util.logging.Logger;
import org.apache.log4j.Logger;
</code></pre>
<p>If you try this, your compiler should barf, complaining that you have two classes with the same name. Well, this version will work:</p>
<pre><code>org.apache.log4j.Logger logger4j = org.apache.log4j.Logger.getLogger(DeleteMe.class);
java.util.logging.Logger loggingLogger = java.util.logging.Logger.getLogger(DeleteMe.class.getName());
</code></pre>
<p>Because of this package/class naming convention, it is trivial to extract the file structure of the application for reverse-engineering purposes, because in java the package name is specified to correlate to a file structure. </p>
<p>In a comment you asked about NOT using a package, well, if you did that then you would NEVER be able to use two classes of the same name--the compiler would never allow it. </p>
<p>The above example would turn into this:</p>
<pre><code>Logger logger4j = Logger.getLogger(DeleteMe.class.getName());
Logger loggingLogger = Logger.getLogger(DeleteMe.class.getName());
</code></pre>
<p>Which <code>Logger</code> is the compiler going to pick if you have two on the classpath without package names? </p>
| 4315 | 2014-05-12T13:51:20.780 | <p>I have been involved in disassembling Android apps using <a href="https://code.google.com/p/smali/" rel="noreferrer">baksmali</a> and dexpler. Whenever I disassemble an app, I find the packages and package hierarchy (that would have been available in development scenario) intact. For instance, when I disassemble a task manager app (MD5: 3377f8527479ab4e72bf9fa5eec62abe), I get the package hierarchy as shown in the figure below.<img src="https://i.stack.imgur.com/Zrhkg.png" alt="enter image description here">. </p>
<p>In this context, I have the following questions,</p>
<ol>
<li>Will this hierarchy of Java packages be preserved and retained while building apps. When we develop apps, of course, we use many packages and we put them in a hierarchy while we build the app. But, all the code written by developers will be converted into a single classes.dex file. Will the packages and package hierarchy information be retained in this classes.dex file or any other part of the apk file (like META-INF)? </li>
<li>If this information is not available from .dex, where does this come from? How does every disassembling/ decompiling tool manages to extract this package hierarchy?</li>
<li>Where to find documentation/ literature relevant to packages and package hierarchy in the app building process? I tried skimming thro' Android documentation in vain.</li>
</ol>
| How do Android reverse engineering tools extract packages/ package hierarchy present in Android apps? | |disassembly|decompilation|static-analysis|android| |
<p>According to <a href="http://www.swag.uwaterloo.ca/acd/docs/ItaniumC++ABI.htm#mangling">documentation</a> the first one is base object destructor and the second one is deleting destructor. </p>
<pre><code>Constructors and destructors are simply special cases of <unqualified-name>, where the final <unqualified-name> of a nested name is replaced by one of the following:
<ctor-dtor-name> ::= C1 # complete object constructor
::= C2 # base object constructor
::= C3 # complete object allocating constructor
::= D0 # deleting destructor
::= D1 # complete object destructor
::= D2 # base object destructor
</code></pre>
<p>According to <a href="http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041d/IHI0041D_cppabi.pdf">ARM IHI 0041D</a> document the difference between these destructors is as follows:</p>
<pre><code>This ABI requires C1 and C2 constructors to return this (instead of being void functions) so that a C3 constructor
can tail call the C1 constructor and the C1 constructor can tail call C2.
Similarly, we require D2 and D1 to return this so that D0 need not save and restore this and D1 can tail call D2 (if
there are no virtual bases). D0 is still a void function.
</code></pre>
| 4323 | 2014-05-13T07:50:41.487 | <p>The beginning of the virtual function table (VFT, also virtual method table, VMT) disasembled by IDA goes as:</p>
<pre><code> _ZTV13QSystemLocale DCD 0, _ZTI13QSystemLocale, _ZN13QSystemLocaleD2Ev+1, _ZN13QSystemLocaleD0Ev+1
</code></pre>
<p>and <code>c++filt</code> decodes it as</p>
<pre><code> vtable for QSystemLocale DCD 0, typeinfo for QSystemLocale, QSystemLocale::~QSystemLocale()+1, QSystemLocale::~QSystemLocale()+1
</code></pre>
<p>Here we see <code>_ZN13QSystemLocaleD2Ev</code> and <code>_ZN13QSystemLocaleD0Ev</code>, both transformed by <code>c++filt</code> to <code>QSystemLocale::~QSystemLocale()</code>.</p>
<p>(+1 is normal, the bit selects the right instruction set on ARM).</p>
<p>The Qt source declares:</p>
<pre><code>virtual ~QSystemLocale();
</code></pre>
<p><strong><em>Why there are two virtual destructors?</em></strong></p>
<p>(I work with ARM, Android NDK (gcc/g++), C++, Qt).</p>
| Why two virtual destructors? | |c++|android|arm|virtual-functions| |
<p>Have you considered using Microsoft's code to do it? Reverse the loader just enough to find the pointer that you're looking for once the image is loaded, stop it from executing any actual code beyond parsing the PE. The offset to your desired pointer in ntdll or wherever will be version-specific but you'll get a super-accurate parser.</p>
| 4324 | 2014-05-13T08:41:27.717 | <p>I write a Portable Executable (PE) library that also provides finding the starting offset of the overlay (appended data to the PE that is not mapped into memory).</p>
<p>My algorithm finding the overlay offset looks like this so far:</p>
<pre><code>public long getOverlayOffset() throws IOException {
if (offset == null) {
SectionTable table = data.getSectionTable();
offset = 0L;
for (SectionTableEntry section : table.getSectionEntries()) {
long pointerToRaw = section.get(POINTER_TO_RAW_DATA);
long sizeOfRaw = section.get(SIZE_OF_RAW_DATA);
long virtSize = section.get(VIRTUAL_SIZE);
//see https://code.google.com/p/corkami/wiki/PE#section_table: "if bigger than virtual size, then virtual size is taken. "
//and: "a section can have a null VirtualSize: in this case, only the SizeOfRawData is taken into consideration. "
if(virtSize != 0 && sizeOfRaw > virtSize) {
sizeOfRaw = virtSize;
}
long endPoint = pointerToRaw + sizeOfRaw;
if (offset < endPoint) {
offset = endPoint;
}
}
}
if(offset > file.length()) {
offset = file.length();
}
return offset;
}
</code></pre>
<p>I used <a href="https://code.google.com/p/corkami/wiki/PE#section_table" rel="noreferrer">corkami</a> as a source to get to know some of the odds in calculating the overlay offset. I do not only want it to be robust, but also accurate.
Did I miss something? What else do I have to put into consideration?</p>
<p>Note: There was a similar question here: <a href="https://reverseengineering.stackexchange.com/questions/2014/how-can-one-extract-the-appended-data-of-a-portable-executable">How can one extract the appended data of a Portable Executable?</a>
But it doesn't cover a reliable algorithm so far. As I understand it, using a tool suffices in that question.</p>
| Reliable algorithm to extract overlay of a PE | |pe| |
<p>Looks like those are LOCAL definitions, perhaps defined in two places to allow slightly different versions to be used. They're not global functions, which would be constrained to only appear (or be defined) once.</p>
<p>Perhaps defined in several files, statically, so they're only used (or scoped to) in that particular file.</p>
<p>I don't see those functions in the output of readelf -s, or objdump on my system's installed 'pr'. I imagine you've compiled the coreutils yourself?</p>
<p>Since you're playing with the source code of 'pr', you could grep for bkm_scale and bkm_scale_by_power to see where they're used and defined.</p>
<p>Keep exploring! Keep learning!</p>
| 4336 | 2014-05-15T03:23:48.707 | <p>So basically when I am using <code>objdump</code> and <code>readelf</code> to disassemble some GNU Coreutils, I find a very weird situation like this: </p>
<pre><code>readelf -s pr | grep bkm_scale
....
176: 08050620 118 FUNC LOCAL DEFAULT 13 bkm_scale
177: 080506a0 39 FUNC LOCAL DEFAULT 13 bkm_scale_by_power
181: 08050a60 173 FUNC LOCAL DEFAULT 13 bkm_scale
182: 08050b10 50 FUNC LOCAL DEFAULT 13 bkm_scale_by_power
</code></pre>
<p>and when I disassemble <code>pr</code> using <code>objdump</code>, I do see two definition of function <code>bkm_scale</code> and <code>bkm_scale_by_power</code>, and their disassembled asm code are different.</p>
<p>So does it indicate some disassemble error? If not, then why there are two <code>FUNC</code> symbols have the same name?</p>
| conflict function name using objdump to disassemble | |disassembly|objdump|reassembly| |
<p>I solved my problem with IDC files and VM snapshot =)
I'm on IDA 6.4 and the snapshot function doesn't work in live debugging.
My solution is to take a snapshot of my virtual machine (on VMware workstation) to have everytime the same kernel state. When I wanna reload my work, I begin to load the virtual machine snapshot then I attach IDA to my virtual machine with windbg and finally I can load my IDC file using the script file option.</p>
<p>Thank you very much for you answer, it really was helpful !</p>
| 4337 | 2014-05-15T07:51:30.640 | <p>I'm working with IDA pro on a kernel mode function (VMware + windbg) and I'm a annoying because I can't save the workstation state. IDA crash after 3 hours when I "quit and save memory status".
Is there a plugin or anything else that could make me able to save my work (variables rename, commentaries and others) ?
Thanks for reading (and answers =)</p>
| IDA - save work on kernel mode debugging | |ida|dynamic-analysis|kernel-mode| |