<div dir="ltr"><div>Thanks for the replies.</div><div><br></div><div>My Goal is to get &#39;the mybin.exe&#39;s execution trace log&#39; like the PIN tool trace log.</div><div><br></div><div>ex) 0x804826 mov eax, ebx   (eax = 0x10101010 , ebx = 0x20202020)</div><div>    0x804828 mov ebx, ecx   (eax = 0x20202020 , ebx = 0x20202020, ecx = 0x30303030)</div><div>    ...</div><div><br></div><div><br></div><div>First, I need the mybin.exe&#39;s execution flow(opcode).</div><div><br></div><div>1) when I tried to get opcode from env-&gt;eipv(PANDA_CB_INSN_EXEC), it seems to be translated by the PANDA.</div><div>2) So, I tried to get opcode from pc (PANDA_CB_INSN_TRANSLATE) as told @Igor.</div><div>but it&#39;s results are same.</div><div><br></div><div>I made a plugin to test with following code.</div><div><br></div><div>// only pid == target_pid</div><div>if (get_pid(env,eproc) == 0x73c){</div><div>                unsigned char buf[20] = {0,};</div><div>                if (types == 1){ </div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>// if called from PANDA_CB_INSN_TRANSLATE</div><div>                        fprintf(plugin_log, &quot;[T] &quot;);</div><div>                        panda_virtual_memory_rw(env, pc, buf, 20, 0);</div><div>                }else{ </div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>// if called from PANDA_CB_INSN_EXEC</div><div>                        fprintf(plugin_log, &quot;[E] &quot;);</div><div>                        panda_virtual_memory_rw(env, env-&gt;eip, buf, 20, 0);</div><div>                }</div><div><br></div><div>                //calc instruction length</div><div>                int inst_len = 0;</div><div>                inst_len = InstructionLength((BYTE*) buf);</div><div>                panda_disas(plugin_log, buf, inst_len);</div><div>                if(inst_len &gt; 0){</div><div>                        fprintf(plugin_log, &quot;opcode = 0x%02x&quot;, buf[0]);</div><div>                        if(inst_len &gt; 1){</div><div>                                int k;</div><div>                                for(k = 1; k &lt; inst_len; k++){</div><div>                                        fprintf(plugin_log, &quot;%02x&quot;, buf[k]);</div><div>                                }</div><div>                        }</div><div>                        fprintf(plugin_log, &quot;\n&quot;);</div><div>                }</div><div>                fprintf(plugin_log, &quot;PC = &quot; TARGET_FMT_lx &quot; EIP = &quot; TARGET_FMT_lx &quot; EAX = &quot; TARGET_FMT_lx &quot;... \n&quot;, pc, env-&gt;eip, env-&gt;regs[R_EAX]...);</div><div><br></div><div><br></div><div><br></div><div>and result is</div><div><br></div><div>[T] 0x7f9383cc0640:  mov    0xa8(%rsi),%eax</div><div>opcode = 0x8b86a8000000</div><div>PC = 77d075f3 EIP = 77d075f3 EAX = 005fc8b8 EDI = 00000001 EBP = 0012fea8 ESP = 0012fe7c</div><div>[T] 0x7f9383cc0640:  cmp    %ebx,%eax</div><div>opcode = 0x3bc3</div><div>PC = 77d075f9 EIP = 77d075f3 EAX = 005fc8b8 EDI = 00000001 EBP = 0012fea8 ESP = 0012fe7c</div><div>[T] 0x7f9383cc0640:  je     0x7f9383cc06a0</div><div>opcode = 0x745e</div><div>PC = 77d075fb EIP = 77d075f3 EAX = 005fc8b8 EDI = 00000001 EBP = 0012fea8 ESP = 0012fe7c</div><div>[E] 0x7f9383cc0670:  mov    0xa8(%rsi),%eax</div><div>opcode = 0x8b86a8000000</div><div>PC = 77d075f3 EIP = 77d075f3 EAX = 005fc8b8 EDI = 00000001 EBP = 0012fea8 ESP = 0012fe7c</div><div>[E] 0x7f9383cc0670:  cmp    %ebx,%eax</div><div>opcode = 0x3bc3</div><div>PC = 77d075f9 EIP = 77d075f9 EAX = 00144d6c EDI = 00000001 EBP = 0012fea8 ESP = 0012fe7c</div><div>[E] 0x7f9383cc0670:  je     0x7f9383cc06d0</div><div>opcode = 0x745e</div><div>PC = 77d075fb EIP = 77d075fb EAX = 00144d6c EDI = 00000001 EBP = 0012fea8 ESP = 0012fe7c</div><div><br></div><div>As you can see, PANDA_CB_INSN_TRANSLATE&#39; opcode and PANDA_CB_INSN_EXEC&#39;s opcode is same.</div><div><br></div><div>I think the result is binary execution flow on guest machine, so I compare the result with original binary code(mybin.exe).</div><div>but it seems to be not matched.</div><div>I don&#39;t know why it is not matched. Is somthing wrong? How can i get the original execution flow?</div></div><div class="gmail_extra"><br><div class="gmail_quote">2015-08-18 2:03 GMT+09:00 Brendan Dolan-Gavitt <span dir="ltr">&lt;<a href="mailto:brendandg@gatech.edu" target="_blank">brendandg@gatech.edu</a>&gt;</span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">&quot;pc&quot; and env-&gt;eip can be different! QEMU typically only updates<br>
env-&gt;eip every basic block. The insn_exec callback will provide the<br>
precise program counter value as its argument though (it stores it at<br>
translation time so it can be passed in).<br>
<br>
Manolis is right that this won&#39;t give you the original binary back.<br>
One thing you can do is take a memory snapshot during replay and then<br>
use Volatility to extract the binary image from memory. This will<br>
preserve the headers, data sections, etc. However, depending on the<br>
amount of RAM available, some pages might be swapped out.<br>
<br>
If what you&#39;re looking to do is just disassemble something, you can<br>
use the recently added panda_disas function:<br>
<br>
void panda_disas(FILE *out, void *code, unsigned long size)<br>
<br>
Alternatively, if you want to have some machine-parseable description<br>
of the disassembled instruction, you can use distorm; an example of<br>
that can be found in the callstack_instr plugin.<br>
<span class="HOEnZb"><font color="#888888"><br>
-Brendan<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
On Mon, Aug 17, 2015 at 9:08 AM, Manolis Stamatogiannakis<br>
&lt;<a href="mailto:mstamat@gmail.com">mstamat@gmail.com</a>&gt; wrote:<br>
&gt; Igor, are you sure that the &quot;pc&quot; argument and &quot;env-&gt;eip&quot; will contain<br>
&gt; different arguments? I&#39;d guess that &quot;pc&quot; is provided as convenience so that<br>
&gt; you can avoid architecture-specific #ifdef macros in your plugin code<br>
&gt; (&quot;env-&gt;eip&quot; is x86 specific).<br>
&gt;<br>
&gt; InGap, could you elaborate on what you attempt to achieve?<br>
&gt;<br>
&gt; Reconstructing mybin.exe from an execution trace is a non-trivial task. Even<br>
&gt; in the (unlikely) case you have full coverage of mybin.exe in the execution<br>
&gt; trace (i.e. every instruction in mybin.exe was executed at least once), the<br>
&gt; order of the instructions as executed still may be different than the order<br>
&gt; they appear in the binary. Moreover, executables are not plain instruction<br>
&gt; dumps. They contain a lot of structured information (see<br>
&gt; <a href="https://en.wikipedia.org/wiki/Portable_Executable" rel="noreferrer" target="_blank">https://en.wikipedia.org/wiki/Portable_Executable</a>) that you will not be able<br>
&gt; to recapture just by observing the execution.<br>
&gt;<br>
&gt; M.<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; 2015-08-17 8:33 GMT+02:00 Igor R &lt;<a href="mailto:boost.lists@gmail.com">boost.lists@gmail.com</a>&gt;:<br>
&gt;&gt;<br>
&gt;&gt; &gt; I trying to get the &quot;mybin.exe&#39;&#39; &#39;s original assembly code(opcode) in<br>
&gt;&gt; &gt; the PANDA plugin.<br>
&gt;&gt; &gt; (for tracing binary&#39;s opcode, registers, memory ..)<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Host OS : ubuntu x64<br>
&gt;&gt; &gt; Guest OS : windows xp x86<br>
&gt;&gt; &gt; Test binary : mybin.exe<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; I got the opcode using panda_virtual_memory_rw function at<br>
&gt;&gt; &gt; PANDA_CB_INSN_TRANSLATE.<br>
&gt;&gt; &gt; ex) panda_virtual_memory_rw(env, env-&gt;eip, buf, 20, 0);<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; but, It is not same as original assembly code(&#39;mybin.exe&#39;).<br>
&gt;&gt; &gt; It seems to be translated by the PANDA.<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; Quoting from the documentation:<br>
&gt;&gt; &lt;&lt;<br>
&gt;&gt; insn_translate: called before the translation of each instruction<br>
&gt;&gt;<br>
&gt;&gt; Callback ID: PANDA_CB_INSN_TRANSLATE<br>
&gt;&gt;<br>
&gt;&gt; Arguments:<br>
&gt;&gt;<br>
&gt;&gt; CPUState *env: the current CPU state<br>
&gt;&gt; target_ulong pc: the guest PC we are about to translate<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; So, if you need the opcode of the instruction being translated, you<br>
&gt;&gt; should read the memory from &quot;pc&quot; address (rather than env-&gt;ip).<br>
&gt;&gt; _______________________________________________<br>
&gt;&gt; panda-users mailing list<br>
&gt;&gt; <a href="mailto:panda-users@mit.edu">panda-users@mit.edu</a><br>
&gt;&gt; <a href="http://mailman.mit.edu/mailman/listinfo/panda-users" rel="noreferrer" target="_blank">http://mailman.mit.edu/mailman/listinfo/panda-users</a><br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; panda-users mailing list<br>
&gt; <a href="mailto:panda-users@mit.edu">panda-users@mit.edu</a><br>
&gt; <a href="http://mailman.mit.edu/mailman/listinfo/panda-users" rel="noreferrer" target="_blank">http://mailman.mit.edu/mailman/listinfo/panda-users</a><br>
&gt;<br>
_______________________________________________<br>
panda-users mailing list<br>
<a href="mailto:panda-users@mit.edu">panda-users@mit.edu</a><br>
<a href="http://mailman.mit.edu/mailman/listinfo/panda-users" rel="noreferrer" target="_blank">http://mailman.mit.edu/mailman/listinfo/panda-users</a><br>
</div></div></blockquote></div><br></div>