⚡Megatron LLM Hub
←Back to search
Website

Crystal Palace 新增系统调用篡改与 PIC 代码头定位原语

提供用户态 syscall 篡改手法,可用于规避 EDR 的调用监控。
工具绕过syscall红队
I’ve got another Crystal Palace and Tradecraft Garden update. This release expands on LSH Delish and adds a syscall tampering project to the Tradecraft Garden. We also have a new linker intrinsic to find the beginning of PIC/PICO executable code. And, I’ve introduced a new way to define content headers for data transformed and appended to programs. Syscall Tampering with LSH A few releases ago , I added an option to set a Language-specific (exception) handler (LSH) in Crystal Palace. This was a target of opportunity from when I implemented .pdata/.xdata (stack unwinding meta-info) in the June release . I noticed I could dump a pointer into that data and designate, function by function, a catch-all exception handler that has powers similar to a Vectored Exception Handler. It’s a cool primitive and has potential to demonstrate some VEH-dependent tradecraft without a VEH. One thing we haven’t had, was a good example demonstrating the LSH in context. That’s where Dr. Call comes in. Dr. Call is a syscall tampering project, following the concept rad9800 demonstrated a few years ago. The idea behind syscall tampering is we call a syscall-destined function with fake arguments, potentially fooling userland hooks, and right before a syscall—we switch in the real arguments. The idea builds on program instrumentation CPU features (e.g., Hardware breakpoints, CPU Trap flag) which give us the ability to trace and edit context for our program from within itself. Most POCs using these instrumentation tools rely on Vectored Exception Handlers to catch the single step exceptions. TCG’s Dr. Call uses the CPU Trap flag to step through our program up to a syscall instruction. But, instead of a VEH, it uses a LSH setup with Crystal Palace. It’s a concise example and demonstrates the LSH feature well. __text_begin() This release adds the __text_begin intrinsic to get position 0 of your PIC or PICO object code. This is a replacement for using the go() [or other entry function] for this value. The intrinsic is defined in tcg.h: char * __text_begin(); I’ve also updated the TCG examples to use this intrinsic. Several examples free() the base PIC and this is helpful for that. Decoupling the program’s entry point from the de facto “tell me where the program begins” symbol also means tradecraft can take over the PIC entry point safely in more situations. Stack Cutting’s setup label is an example of this pattern. Content Headers One of my personal C programming rules is to avoid manual pointer arithmetic and size calculations as much as possible. It’s a recipe for crashes, headaches, and code that’s harder to read and write. Tradecraft Garden has several examples of size calculation gymnastics and these are tied to the prepsum and preplen commands. I intended these commands as a way to decorate a byte blob with header information, which is helpful. But they get messy. Let’s take these three .spec commands as an example: push $DLL prepsum preplen Here’s a struct based on that: 1 2 3 4 5 struct { DWORD length; DWORD adler32sum; char DLL[0]; } This looks straight forward enough, right? But, let’s say we want to calculate the adler32 checksum of our DLL content to validate it. What’s the length of the DLL? Why, it’s length minus 4b to account for the adler32sum prepended before we calculated the length of the byte[] blob on the stack. Yeah, that doesn’t work. The recently added encode and mask make this headache worse. Undoing different transforms requires sometimes knowing the pre-transform length of the data, a checksum over the data is nice, etc. This release introduces prepend , which prepends a header to an argument on the stack. The header argument is a comma-separated list of field values to build into the header. prepend "length, adler32" encode and mask accept an optional header argument too. These commands can calculate fields over the input (pre-transform) and output (post-transform) content. encode "lznt1" "length, in_length" mask "aes-ctr" $KEY "length, iv16" No header argument or an empty string means no header. The header fields are: Value description size total Total length of header+data 4 in_length Length of input 4 in_adler32 Adler32 sum of input 4 in_crc32 CRC32 sum of input 4 length Length of data 4 adler32 Adler32 sum of data 4 crc32 CRC32 sum of data 4 iv16 AES-CTR initialization vector 16 $VAR Contents of $VAR var The header is calculated in one pass over the input and output data for the command. And, the header maps cleanly to a C struct where the values mean what you expect them to mean. There are some breaking changes though. mask "aes-ctr" and encode "lznt1" no longer generate default headers. They apply the transform only and expect you to specify the header you want. aes-ctr requires iv16 in its header (the operation isn’t reversible without it) and mask will throw an error if this field is missing. preplen and prepsum are deprecated too. This doesn’t mean removed. They will go away eventually. Right now, they function as expected, but also print a warning encouraging you to migrate to their prepend equivalents. Migration Notes mask "aes-ctr" and encode "lznt1" no longer prepend default headers to their output. To migrate existing .spec files, use the following commands to generate the same output as before: 1 2 3 4 5 # prepend the 16b initialization vector to any AES encrypted data mask "aes-ctr" $KEY "iv16" # prepend the input data (original, decompressed data) length to our lznt1 data encode "lznt1" "in_length" 2. prepsum and preplen are now deprecated in favor of prepend . Here are their new equivalents: 1 2 prepend "length" prepend "adler32" Closing Thoughts To close each release, I go after a few low-hanging fruit tasks to fill things out. This time, I polished the examples in the Tradecraft Garden. And, I really wanted a project to demonstrate the LSH too. So, I implemented Dr. Call. Dr. Call came together smoothly. But, when I tested other situations, I found myself down a corner case rabbit hole. One of my tests mysteriously crashed with exception code 4 (EXCEPTION_SINGLE_STEP). The LSH wasn’t triggering somewhere it should have. I had working and not working assembly dumps, narrowed down to one function. I used my phone-an-AI lifeline and it came through. I learned that Win32’s unwinder requires an instruction between a call and the epilogue in functions with a LSH attached. It’s bitten others too. I inserted a targeted NOP with ised to prove one instruction made that difference. I then wrote a check to flag this situation as an error. Case closed. The above was one of three deep dive WTF issues I surfaced and tackled this development cycle. I call these inconvenient happy accidents. I’m happy each issue showed up in a reproducible way. It’s inconvenient when it’s midnight and I hoped to ship the next day. For me, I got a healthy reminder that bin2bin remains a hellscape minefield . Enjoy the update. For a full list of what’s new, check out the release notes .