text
stringlengths
0
1.99k
the day: it's just good software development practice.
Let's get back to our questions. The other questions, while having the
same action item, have a more complicated answer. We need to test and
analyze the infected executables to verify and debug infections. So we
need to enumerate what we need to test based on our design intent.
Because we're dealing with the TLS directory, we are dealing primarily
with *virtual addresses*, as opposed to RVA and offsets. Virtual addresses
tend to imply the need for relocations within the binary. This is
absolutely something we need to deal with as an executable infector-- with
the ubiquity of Address Space Layout Randomization (aka /DYNAMICBASE),
we would be stupid to not consider modifying the relocation directory of
a target executable in the case of infection.
Thus, we have four states of configuration to test infection against:
+ no tls directory present, no relocation directory present
+ no tls directory present, relocation directory present
+ tls directory present, no relocation directory present
+ tls directory present, relocation directory present
In addition to this, we need to consider targeting the 32-bit architecture
as well, creating a total of 8 binary configurations to test against! This
brings the total code projects in our virus project to 12. With a good
build system, we can construct all these test binaries rather easily:
$ cd infectables
$ mkdir build32 build64
$ cd build32
$ cmake ../ -A Win32
$ cd ../build64
$ cmake ../ -A x64
The build scripts can basically follow a folder hierarchy and build
multiple projects contained within, which is what's happening here.
We now have two configured build environments-- one for 32-bit and one
for 64-bit.
$ cd build32
$ cmake --build ./ --config Release
$ cd ../build64
$ cmake --build ./ --config Release
This will place all the binaries in the Release directory within the
build environment. They can then be targeted for infection by our
infector executable for testing purposes. Like the compiler at the
command line, we can configure various switches to define CMake headers.
We can configure our infector to be aware of a directory containing our
infectable executables:
$ mkdir build
$ cd build
$ cmake -DBROODSAC_DEBUG=ON -DBROODSAC_INFECTABLES="infectables" \
-A x64 ../
This command effectively builds Broodsac in debug mode. Rather than
targeting the user's home directory, it will instead target the infectable
directory, where our test programs are currently built. By running
Broodsac in this state, we can easily verify the state of infection and
its corresponding payload. And this is of utmost importance-- the demons
that hit the hardest, lurk the lowest. Robust testing will help to
eradicate them.
--[ 5. Dealing with Development Hazards
The result of the virus you see here is a labor of love, of many hours
spent debugging, testing, verifying, fixing and refactoring. But when
you see the final product, what you don't see are those little steps
along the way that ultimately built the product before you. So it's hard
to appreciate the struggle, the fight that comes with software
development. It is, for the most part, an individual journey that
everyone who writes code wanders on.
It is very easy to laugh at a good, terrible bug. It amazes us when stupid
bugs seem to have a persistent lifetime, just waiting to be discovered by
the next lucky actor. But bugs are part of the life-cycle of software,
whether exploitable or not, and as you cannot escape the gravitational
pull of software development as a virus writer, you may as well embrace
its best practices.
This section focuses on two pivotal points of critical failure in the
process of developing this virus: a point when an initial payload idea
failed at the last minute, and the point when antivirus seemed to start
detecting our infections.
--[ 5.1 The Original Design Fails
Do you remember when I said the TLS directory would come back to bite us
in the ass? How did having a robust build system help us with that?
Originally, our payload was a very simple, sensible program: import
GetFileAttributes, URLDownloadToFileA and ShellExecuteA. This would
essentially be all we needed to download our sheep and run it on the
target system. To help explain the chaos we mitigated, let's break down
the steps we need to generate and test our final payload, the infector:
1. compile the C infection
2. test the C infection
3. translate to assembly on 32-bit and 64-bit architectures