<epigraph>
  <attribution>Marcus Terentius Varro</attribution>
  <para>
    The longest part of the journey is said to be the passing of the gate.
  </para>
</epigraph>

<para>
  After emotions cooled down a bit we can examine the infected executable
  and compare it with <link linkend="bashful_glance">the original</link>.
</para>

<formalpara>
  <title>Command</title>
  <para>
    <programlisting><inlinegraphic format="linespecific"
      fileref="../src/entry_point/readelf.sh"
    ></programlisting>
  </para>
</formalpara>

<formalpara>
  <title>Output</title>
  <para>
    <programlisting><inlinegraphic format="linespecific"
      fileref="../out/entry_point/readelf"
    ></programlisting>
  </para>
</formalpara>

<para>
  File size and code segment have grown as expected.
  Data segment and <literal>DYNAMIC</literal> segment moved accordingly:
</para>
<blockquote>
  <para>
    infected.file_size - sh.file_size =
    &calc_infone_size; - &calc_sh_size; =
    &calc_infone_size_delta_D; =
    &calc_infone_size_delta_X;
  </para>
  <para>
    infected.<literal>LOAD</literal>[1].<literal>Filesiz</literal> -
    sh.<literal>LOAD</literal>[1].<literal>Filesiz</literal> =
    &calc_infone_LOAD1_filesiz_X; -
    &calc_sh_LOAD1_filesiz_X; =
    &calc_infone_LOAD1_filesiz_delta_X;
  </para>
  <para>
    infected.<literal>LOAD</literal>[2].<literal>Offset</literal> -
    sh.<literal>LOAD</literal>[2].<literal>Offset</literal> =
    &calc_infone_LOAD2_offset_X; -
    &calc_sh_LOAD2_offset_X; =
    &calc_infone_LOAD2_offset_delta_X;
  </para>
  <para>
    infected.<literal>DYNAMIC</literal>.<literal>Offset</literal> -
    sh.<literal>DYNAMIC</literal>.<literal>Offset</literal> =
    &calc_infone_DYNAMIC1_offset_X; -
    &calc_sh_DYNAMIC1_offset_X; =
    &calc_infone_DYNAMIC1_offset_delta_X;
  </para>
</blockquote>

<sect2>
  <title>First scan</title>

  <para>
    Let's give the
    <link linkend="turn_the_pages">heuristic scanner</link> a try.
  </para>

  <formalpara>
    <title>Command</title>
    <para>
      <programlisting><inlinegraphic format="linespecific"
	fileref="../src/entry_point/scan.sh"
      ></programlisting>
    </para>
  </formalpara>
  <formalpara>
    <title>Output</title>
    <para>
      <programlisting><inlinegraphic format="linespecific"
	fileref="../out/entry_point/scan"
      ></programlisting>
    </para>
  </formalpara>

  <para>
    As predicted. This is like playing chess against oneself, and losing.
    Can't do much about it, though.
    I'll fix something else in revenge.
  </para>

</sect2>
<sect2 id="second_scan">
  <title>Second scan</title>

  <para>
    The value of <literal>Entry point</literal> changed dramatically.
    In the original it is in the first part of the file:
  </para>
  <blockquote>
    <para>
      entry_point_ofs =
      &calc_sh_entry_point_X; - &calc_base_addr; = 
      &calc_sh_entry_point_start_ofs_X; =
      &calc_sh_entry_point_start_ofs_D; bytes.
    </para>
  </blockquote>

  <para>
    The infected copy moved that to exactly &calc_sh_LOAD1_align_D;
    bytes from the end of the code segment.
  </para>

  <blockquote>
    <para>
      entry_point_ofs =
      &calc_infone_entry_point_X; - &calc_base_addr; = 
      &calc_infone_entry_point_start_ofs_X; =
      &calc_infone_entry_point_start_ofs_D; bytes.
    </para>
    <para>
      end_of_LOAD1 =
      &calc_infone_LOAD1_virtaddr_X; +
      &calc_infone_LOAD1_filesiz_X; =
      &calc_infone_LOAD1_end_X;
    </para>
    <para>
      entry_point_distance_to_end =
      &calc_infone_LOAD1_end_X; - 
      &calc_infone_entry_point_X; =
      &calc_infone_entry_point_dist_to_end_X; =
      &calc_infone_entry_point_dist_to_end_D;
    </para>
  </blockquote>

  <para>
    This is another easy vulnerability to scanners.
    By restructuring <link linkend="Target_infection">our code</link>
    we can make that number even smaller. But for a real cure we
    need stronger voodoo.
  </para>

</sect2>
<sect2 id="patch_me_if_you_can">
  <title>Patch me if you can</title>

  <para>
    If we chose to leave <literal>entry_point</literal> as it is,
    we have to patch something else. One approach is to disassemble
    the code, starting at <literal>entry_point</literal>, find the first
    <literal>call</literal> (or <literal>jmp</literal>) and abuse it.
    This requires way too much intelligence for a virus, though.
  </para>
  <para>
    But then we are operating in a homogeneous environment,
    having one compiler and one C run-time library for all.
    The startup code should be the same for every executable.
  </para>

  <formalpara>
    <title>Command</title>
    <para>
      <programlisting><inlinegraphic format="linespecific"
	fileref="../src/entry_point/gdb.sh"
      ></programlisting>
    </para>
  </formalpara>
  <formalpara>
    <title>Output</title>
    <para>
      <programlisting><inlinegraphic format="linespecific"
	fileref="../out/entry_point/gdb"
      ></programlisting>
    </para>
  </formalpara>

</sect2>
<sect2 id="disassemble_it_again">
  <title>Disassemble it again, Sam</title>

  <para>
    Of course we have to implement a check whether the code at the
    entry address really looks like above output. Just in case the
    target is already infected (by a superior virus).
    To implement a comparison we only need offset and size,
    not actual opcodes. But I will feel better after I have them
    straight in front of me. And <command>ndisasm</command>
    starts counting with zero, which requires less brain activity.
  </para>

  <formalpara>
    <title>Command</title>
    <para>
      <programlisting><inlinegraphic format="linespecific"
	fileref="../src/entry_point/ndisasm.sh"
      ></programlisting>
    </para>
  </formalpara>
  <formalpara>
    <title>Output</title>
    <para>
      <programlisting><inlinegraphic format="linespecific"
	fileref="../out/entry_point/ndisasm"
      ></programlisting>
    </para>
  </formalpara>

</sect2>
<sect2 id="patch_entry_addr_two">
  <title>patchEntryAddr 2.0</title>

  <para>
    There is one remaining issue. <varname>Elf32_Ehdr::e_entry</varname>
    is an absolute address, as is the value popped off the stack by
    <literal>ret</literal>. The operand of <literal>call</literal> and
    <literal>jmp</literal> is encoded relative to the location of the
    following instruction, however. This is described in the documentation
    of <ulink url="&http_nasm_site;">nasm</ulink>:
  </para>
  <blockquote>
    <para>
      <literallayout
        >CALL imm                      ; E8 rw/rd             [8086]</literallayout>

      [&hellip;] 

      The codes <literal>rb</literal>, <literal>rw</literal> and
      <literal>rd</literal> indicate that one of the operands to the
      instruction is an immediate value, and that the difference between
      this value and the address of the end of the instruction is to be
      encoded as a byte, word or doubleword respectively.
      Where the form <literal>rw/rd</literal> appears, it indicates that
      either <literal>rw</literal> or <literal>rd</literal> should be
      used according to whether assembly is being performed in
      <literal>BITS 16</literal> or <literal>BITS 32</literal>
      state respectively.
    </para>
  </blockquote>

  <formalpara>
    <title>Source - patchEntryAddr</title>
    <para>
      <programlisting><inlinegraphic format="linespecific"
        fileref="../src/one_step_closer/patch_entry_addr/two.inc"
      ></programlisting>
    </para>
  </formalpara>
  <formalpara>
    <title>Output - build</title>
    <para>
      <programlisting><inlinegraphic format="linespecific"
        fileref="../out/one_step_closer/two/cc"
      ></programlisting>
    </para>
  </formalpara>
  <formalpara>
    <title>Output - test script</title>
    <para>
      <programlisting><inlinegraphic format="linespecific"
        fileref="../out/one_step_closer/two-test"
      ></programlisting>
    </para>
  </formalpara>

</sect2>
<sect2>
  <title>Second verse, same as the first</title>

  <para>
    Output looks nice, but we had that
    <link linkend="off_we_go">already</link>.
    What has increased code size and complexity gained us?
  </para>

  <formalpara>
    <title>Source</title>
    <para>
      <programlisting><inlinegraphic format="linespecific"
        fileref="../src/entry_point/entry_point.sh"
      ></programlisting>
    </para>
  </formalpara>
  <formalpara>
    <title>Output</title>
    <para>
      <programlisting><inlinegraphic format="linespecific"
        fileref="../out/entry_point/entry_point"
      ></programlisting>
    </para>
  </formalpara>

  <para>
    OK. One vulnerability of the infection is not visible to
    <command>readelf</command> anymore. But does that really help?
    It's still trivial to write a heuristic scanner for it.
    All it takes is to verify the operand of <literal>call</literal>
    shown in the disassembly listing.
  </para>

  <formalpara>
    <title>Output</title>
    <para>
      <programlisting><inlinegraphic format="linespecific"
        fileref="../out/entry_point/ndisasm_two"
      ></programlisting>
    </para>
  </formalpara>

  <para>
    <link linkend="disassemble_it_again">Original value</link> is 
    &calc_sh_ndisasm_call_addr_X;, which resolves into a shared library.
    The new value is local to the executable and easy to spot:
    &calc_inftwo_ndisasm_call_addr_X;. So what's the point?
  </para>

</sect2>
<sect2>
  <title>Use the Source, Luke</title>

  <para>
    <command>gdb</command>
    <link linkend="patch_me_if_you_can">revealed us</link> the name
    of the function whose <literal>call</literal> we abused:
    <function>&calc_sh_gdb_call0_name;</function>.
    I can't help thinking that it is part of glibc, but don't be hasty.
  </para>

  <formalpara>
    <title>Command</title>
    <para>
      <programlisting><inlinegraphic format="linespecific"
        fileref="../src/entry_point/ldd.sh"
      ></programlisting>
    </para>
  </formalpara>
  <formalpara>
    <title>Output</title>
    <para>
      <programlisting><inlinegraphic format="linespecific"
        fileref="../out/entry_point/ldd"
      ></programlisting>
    </para>
  </formalpara>

  <para>
    Now that we have a filename we can search the function in the library.
  </para>

  <formalpara>
    <title>Command</title>
    <para>
      <programlisting><inlinegraphic format="linespecific"
        fileref="../src/entry_point/nm.sh"
      ></programlisting>
    </para>
  </formalpara>
  <formalpara>
    <title>Output</title>
    <para>
      <programlisting><inlinegraphic format="linespecific"
        fileref="../out/entry_point/nm"
      ></programlisting>
    </para>
  </formalpara>

  <para>
    First class service. We even got a line number from
    <command>nm</command>.
  </para>

  <formalpara>
    <title>Command</title>
    <para>
      <programlisting><inlinegraphic format="linespecific"
        fileref="../src/entry_point/get_libc_start_main.sh"
      ></programlisting>
    </para>
  </formalpara>

  <formalpara>
    <title>Output</title>
    <para>
      <programlisting><inlinegraphic format="linespecific"
        fileref="../src/entry_point/&calc_sh_gdb_call0_name;"
      ></programlisting>
    </para>
  </formalpara>

  <para>
    If you have a procedure with 10 parameters,
    you probably missed some (according to an old saying).
  </para>
  <para>
    Let's see what this declaration tells about the
     <link linkend="disassemble_it_again">disassembled code</link>.
     For one thing, arguments are pushed in reverse order on the stack.
     This is the traditional way of the C.
     It allows easy implementation of functions like 
     <citerefentry>
      <refentrytitle>printf</refentrytitle>
      <manvolnum>3</manvolnum>
    </citerefentry>
    that take an arbitrary number of arguments.
    Actual values for arguments:
    <parameter>main</parameter> =
    <literal>&calc_sh_ndisasm_dword2_X;</literal>,
    <parameter>init</parameter> =
    <literal>&calc_sh_ndisasm_dword1_X;</literal>,
    <parameter>fini</parameter> =
    <literal>&calc_sh_ndisasm_dword0_X;</literal>.
  </para>
  <para>
    The case of <parameter>rtld_fini</parameter> needs more documentation.
    <ulink url="&http_linuxassembly_startup;"
      >&http_linuxassembly_startup;</ulink>
    makes excellent reading. We might use it someday. For the moment
    a reference to a comment in <filename>/usr/include/asm/elf.h</filename>
    is enough.
  </para>
  <programlisting
>/* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program starts %edx
   contains a pointer to a function which might be registered using `atexit'.
   This provides a mean for the dynamic linker to call DT_FINI functions for
   shared libraries that have been loaded before the code runs.

   A value of 0 tells we have no such handler.</programlisting>
  <para>
    Anyway, even without looking at the complete source of
    <function>&calc_sh_gdb_call0_name;</function> I would guess that
    each of these function pointers is invoked at some time.
    Efforts are concentrated on <parameter>main</parameter>.
  </para>

</sect2>
<sect2>
  <title>patchEntryAddr 3.0</title>

  <formalpara>
    <title>Source - patchEntryAddr</title>
    <para>
      <programlisting><inlinegraphic format="linespecific"
        fileref="../src/one_step_closer/patch_entry_addr/three.inc"
      ></programlisting>
    </para>
  </formalpara>
  <formalpara>
    <title>Output - test script</title>
    <para>
      <programlisting><inlinegraphic format="linespecific"
        fileref="../out/one_step_closer/three-test"
      ></programlisting>
    </para>
  </formalpara>

</sect2>
<sect2>
  <title>Two is company, three is an orgy</title>

  <para>
    We see the same nice output <link linkend="off_we_go">again</link>
    and <link linkend="patch_entry_addr_two">again</link>.
    So what's different this time?
  </para>

  <formalpara>
    <title>Output</title>
    <para>
      <programlisting><inlinegraphic format="linespecific"
        fileref="../out/entry_point/ndisasm_three"
      ></programlisting>
    </para>
  </formalpara>

  <para>
    The difference to the
    <link linkend="disassemble_it_again">original</link>
    is less obvious. Both values of <parameter>main</parameter> are local
    to the executable. But <link linkend="second_scan">again</link> the
    modified value is exactly &calc_sh_LOAD1_align_D; bytes from the end
    of the code segment.
  </para>
  <para>
    It seems that we achieved little. But the concept of studying
    source code to find patch points looks promising. I declare this
    battle a draw.
<!-- this kinda sucks
    It might not change the outcome of the war.
    Still it gives hope to fight on.
-->
  </para>
</sect2>
