January 28, 2022 in Malware Analysis, Sandboxing
This is just a simple proof of concept that can be extended to build a full-blown Delphi API Monitor.
Delphi lives in its own API ecosystem. Reversing Delphi applications requires us to use a dedicated tool/decompiler (e.g. IDR), flirt signatures, and most of this work relies on DCU32INT decompiler. When I built my sandbox and wanted to add Delphi support I created some mini-signatures for some of the more crucial Delphi APIs and anytime Delphi app would be analyzed, I’d look for code patterns, patch them with my API hook, and then observe the results (I described it here).
With the invention of new reversing tools we have an opportunity to re-visit this topic to rapidly produce a prototype of a Delphi API monitor that will be fast, robust and will cover most angles.
Before we begin, couple of points first:
With that, we just need to find an application for testing, and write our first handler.
The old Resource Hacker is written in Delphi. Using IDA we can quickly identify one of its comparison functions PStrCmp at address 0x004029E0 (RVA=29E0):
The example handler showing the calls to this API with parameters can look like this:
{
onEnter(log, args, state) {
eax_len = this.context.eax.readS8();
edx_len = this.context.edx.readS8();
eax_str = this.context.eax.add(1).readUtf8String(eax_len);
edx_str = this.context.edx.add(1).readUtf8String(edx_len);
console.log(this.context.eip + ":" + eax_str+" "+edx_str);
},
onLeave(log, retval, state) {
}
}
Now if we launch rsold.exe under frida-trace:
frida-trace c:\test\rsold.exe c:\windows\notepad.exe -a rsold.exe!2A64
which will tell frida-tools to load old Resource Hacker (rsold.exe) and make it open resources of c:\windows\notepad.exe, and add API hook for PStrCmp (RVA=29E0 –> handlers\rsold.exe\sub_2a64.js), we get result like this:
Now that we know what we can do with it, there are at least 2 different avenues we can follow:
What are interesting APIs to handle?
Could start with strings — these are often great to understand the inner workings of programs:
File operations are of interest as well f.ex.: