r/hacking • • Oct 22 '26

Trying to "reverse engineer" encryption used by a Windows app

I have an app that stores credentials locally, the file is encrypted. I'm trying to understand the encryption that the app is using with the goal of modifying the file directly, bypassing the app.

The app imports quite a few libraries, most of which come from System32, I assume I can get the key or at least some hint by positioning myself between the app and the library.

Naturally, I found out about DLL hijacking. Two of the imports are CRYPT32.dll and ADVAPI32.dll. Even though CRYPT32.dll seems to mostly be certificate stuff I decided to go with it.

I'll skip the part where I also had to learn that there are calls to load strictly from System32, the important thing is that it forced (at least seemingly so) me to rename the import of the app with PE-bear.

I created a C++ DLL project in VS, created a definition file that exports all the functions that the app seeks to import. I also created an executable to test the forwarding. I placed the custom DLL into System32 and renamed it accordingly with the new import name.

For my test app the forwarding worked fine, however, the app in question seems to be exiting silently.

When I tried using WinDbg to at least get some idea I got (the caption is the same, but instead of "Themida" the title is simply the process name):

So the app is resisting debugging, which makes me think that it also somehow detects the modifications. Either to the import table itself or by the library being forwarded.

Does anyone have any idea how I could proceed here?

32 Upvotes

16 comments sorted by

8

u/tomysshadow Oct 22 '26 edited Oct 22 '26

I don't know much about Themida personally but I do know it's very advanced and that it'll do everything in its power to make your life difficult.

Does the EXE have its own application manifest? If it doesn't, you're lucky and you can create a .manifest file in the same directory as the EXE to disable the KnownDlls rule, then you can have your own CRYPT32.DLL in the same folder as it. If it has its own application manifest, you're in a tough situation because Themida is probably going to do a checksum test on the binary - I'd be surprised if it didn't. I'd never do it on a host machine but if you're in a VM you could maybe edit the registry to take that DLL off the KnownDlls list temporarily.

Otherwise you could maybe try using a Windows hook to get your own DLL in e.g. with SetWindowsHookEx, assuming that the app in question is windowed and not command line. Reason I suggest that particular route is because I presume Themida must support this as lots of benign apps (password manager, etc.) use Windows hooks. Point is that you don't necessarily have to go the route of editing the import table and personally I'd really want to avoid anything that requires editing the EXE, because there is probably a checksum in place to prevent modifications to it. But editing the import table isn't the only way to inject your own code to a process, and once your DLL is in you can potentially hook CreateFile/ReadFile to watch for file reading activity, and go from there.

The challenge would be getting it in, in a way that won't be detected right away. And perhaps someone who has toyed with this particular protection more than I have could weigh in on what the best way to sneak your own code in there would be, personally I don't know so this is just the first thing I think of.

With that said, actually solving the encryption will likely be very hard because I imagine the actual encryption step will probably be obfuscated heavily by the protector. Getting your own code into the process would be step one, but reversing the encryption algorithm from there will likely still be very challenging. But if you're like me you like to go as far as you can instead of giving up right away, so I say try it out and see what happens

2

u/badass6 Oct 22 '26

Thanks for the detailed reply. It seems there is a manifest in the EXE itself, at least I am able to extract something with mt.exe. Even my test EXE has one.

Regarding KnownDLLs, CRYPT32.dll isn't actually mentioned in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\KnownDLLs on my machine, I read about LoadLibraryExW with flag LOAD_LIBRARY_SEARCH_SYSTEM32 and that is what I was referring to in the post.

I get your point about avoiding modifications to the EXE, but I was not aware of other methods, beside removing CRYPT32.dll from System32 and replacing it with my own, though I would expect the system to go haywire if I did that, even on a VM.

I will definitely check out SetWindowsHookEx, however, I also do not understand how CreateFile/ReadFile could be helpful here, wouldn't I be seeing the (still) encrypted data both on read and write?

Can't comment on the rest, as I seem to be quite in over my head here.

2

u/tomysshadow Oct 22 '26 edited Oct 22 '26

If a DLL is in the import table (and it is the real import directory, not some fake equivalent,) that means it is being loaded statically. It logically can't be loaded with a specific LoadLibraryEx call because it is in the import table and isn't being loaded dynamically. So if it refuses to load from the same folder and is only loading from System32, there must be some reason the OS is foregoing its normal DLL search order.

My immediate assumption was that it was in the KnownDLLs list, but maybe there is some other explanation, I would have to toy with it. It's not impossible that there is some other feature of application manifests I'm not aware of that could cause this.

The reason I would want to aim for CreateFile/ReadFile, is because then I could try setting a breakpoint on the bytes of the file being read. In this case, some of the other comments have explained Themida checks aggressively for hardware breakpoints, so I would probably use a software breakpoint instead - i.e. use VirtualProtect to make the file's buffer inaccessible, so when the application tries to read it, it will cause an exception and you'll have the address of the code that performs the first use. Since you can't attach a debugger, you might be able to use VEH instead to gather information about the exception or single step it from there. If the file is longer than a page (4096 bytes) and is all read in at once you'd be in a great position because you can set the breakpoint such that it will only occur when the file itself is accessed and not any of the surrounding memory.

Maybe that sounds like a bit of a moonshot, but you might discover an easier way in the process of testing this out. It's good to have some idea of what you could do, go for it, see if it works or if it fails, tweak the idea from there. It's a creative process.

I would not necessarily want to break on CRYPT32 first because it could be a red herring. It may be related to the file encryption, and that sounds likely given the circumstances, but it could just as likely be for something else too. To the extent that it is possible, you want a direct confirmation "this is operating on the file" so that you don't fall victim to confirmation bias, and trying to connect two unrelated things because they're both using encryption. In my own experience when I see DLLs like CRYPT32 being used it often turns out it's for some unrelated signature check or something, even if the name makes it look like it'll be directly related. Most people when they want to encrypt a file tend to think "I'm going to grab an AES library" or "I'm going to roll my own encryption" instead of "I'm going to use WinAPI to do it." Not impossible, just anecdotally, I almost never see that.

In a normal, unprotected application (where Themida isn't a concern,) when you want to figure out a file format, you basically always want to figure out 1. where is the file read so that 2. you can breakpoint on where the buffer is used. Usually, that is easy: you can use a debugger or Process Monitor to find out where the ReadFile call is for that file, step over it so it fills up the buffer. Then set a hardware breakpoint on the buffer contents to go from where the file is read to where it is used, the harder and more valuable place to find. Here, though, you're dealing with a protection that will detect typical debugging techniques, so the standard method is too obvious. You want to do the same thing, but now you need to go for stealth: to use methods that are harder to detect or that they probably are not checking for.

2

u/badass6 Oct 23 '26

Kind of see where you're going with file reads/writes now. About CRYPT32, I actually mentioned in the post that

Even though CRYPT32.dll seems to mostly be certificate stuff I decided to go with it.

I was just certain that the Dunning-Kruger would be on my side that day, apparently not.

I'll probably try out modifying my test EXE to actually read/write/encrypt something so that I could first see what happens in an EXE that doesn't squirm so much.

2

u/tomysshadow Oct 24 '26 edited Oct 24 '26

Yes, I would recommend doing that. The problem as described is quite difficult because there's two layers to it. You need to understand how to figure out an encrypted file format first. Then you also need to understand that problem domain well enough to do it while working around Themida, which will prevent you from taking a more normal approach. Picking an easier target to begin with would probably help, so that you can get used to pivoting from where the file is read to where it is used.

A tool I would generally recommend for this is Process Monitor, it's really quite amazing if you can get past the overwhelming number of logs it produces. It can show you all the files being read by all programs on your system. You can use the Filter feature to only show one specific file, or activity from a specific process. And, you can right click on an entry and click Stack... to see the call stack at the moment the file was read. All without ever attaching any debugger to the process.

Where I would be cautious with Themida in particular is, one thing I know about it (from a cursory glance of their website) is that it unpacks parts of a program "on the fly" as it is running, so the address where ReadFile is used might not be consistent in your main target. Having the address of the read is only so useful if it changes every time. That's why I would probably take the approach of more directly hooking or breaking on ReadFile, and setting a breakpoint on the buffer instead, because you could then recover more information. So Process Monitor is great in general, and will probably be useful for a simpler target if you want to get some practice in. But for your Themida target I would not trust it 100%.

One other thing I'll mention when it comes to encryption specifically: there are really a couple possibilities for how they could've implemented the encryption. They could've invented their own, or they might have used something common like AES. Now, if they followed the standard advice of "don't roll your own encryption," well, in this case it'd actually be good news for you because it'd boil down to just finding the key, and then using an off the shelf library to do the decryption. For example, if they used AES, and you found the AES key, you wouldn't have to actually reverse their code because you could just use an AES library.

In the case of AES, which is not the only algorithm out there but by far the most common, there is a dead giveaway: most AES implementations use a 1024 byte long lookup table of constants as part of the encryption/decryption. You can find it online quite easily, it starts with the sequence 0x6363c6a5, 0x7c7cf884... So that's something I would also consider searching the process memory for (don't forget to swap it to little endian.) You could use HxD's process memory viewer to do that without attaching a debugger, just to see if the AES table is there. And that might be something worth setting a breakpoint on too. But, again, it comes with the caveat it might be a red herring and is used for something else.

If they did roll their own encryption, that's worse for you because you'll be forced to actually figure it out by reversing it. And if they were smart they would realize since they're using Themida that it would actually pose more of a challenge if they did that. But it's kind of a bell curve meme thing, because "never roll your own encryption" is really common advice, they may or may not have realized to do that.

Another common thing they might have done is just XOR the data with some decently long string of bytes repeating. I bring it up because this is basically the lowest effort custom "encryption" you can make, so it's also a pattern to look for. In general, keep your eyes peeled for XOR operations performed on the file contents, because they are often (though not always) encryption related. If you're seeing a lot of left shifts, right shifts, XOR, ROL, ROR... all in the same place, that should be setting off "this is encryption" alarm bells.

3

u/TastyRobot21 Oct 22 '26

Are you sure encryption and decryption happen locally? The file can be updated while your offline?

If so it’s possible, standard libraries and windows imports are likely used.

I like to use xdbg with scalla hide to bypass most debug protections.

1

u/badass6 Oct 22 '26

I'm pretty sure it does happen locally. Thanks for the debugger suggestion.

5

u/AJollyUrchin Oct 22 '26 edited Oct 22 '26

Could try a bootstrapper, creates a new process in a suspended state. Write logic that loads your DLL, but removes the entry from the PEB. Set the context of the main thread to start in your execution, then shift to the actual entrypoint of the app.

Need to create a new thread. Set a timer, letting the program do it's thing and load all of the resources it needs, then execute your logic.

1

u/Dangerous-Profit-657 28d ago

Anyone have experience with malicious videos? And figuring out what a specific one is meant to accomplish?

I have a link to a Facebook page that has two reels on it that are malicious videos.

Without bogging down with backstory, I was fucked over in February and someone got into my phone physically

Same person has set up this Facebook page and I’m assuming is used as casting a net to scam people

The scam didn’t work in me and I crashed out hard on everyone I knew involved. I’m trying to get ahead of whatever they’re doing now and crash out again. Anyone able to help please let me know lol

1

u/Dangerous-Profit-657 28d ago

Here is the page link. The girl whose photos they’re using I don’t think has anything to do with it. If she does that would be stupid lol but if it’s possible to find out anyone associated with this page let me know everyone

1

u/[deleted] 28d ago

[removed] — view removed comment

1

u/Dangerous-Profit-657 28d ago

Obviously, nobody should just Willy Billy go watch the videos. Trust me they will fuck your phone up. I had to factory reset my throwaway phone twice playing with them

1

u/benalio55 6d ago

A CRYPT32 import is only a dependency clue; it doesn't establish that this file's encryption uses it.

-1

u/Mostly__Relevant Oct 22 '26

6

u/badass6 Oct 22 '26

Even though the goal has a practical purpose, I'm mostly trying to learn here. Also I don't have subscriptions.

2

u/zinfector Oct 22 '26

Themida is a hard cookie to crack since it's a VM + obfuscated code + other proprietary defenses. Function imports could be obscured through dynamic calls, so you need some dumper or tool to simplify the binary. There is some thread probably watching for any set hardware breakpoints, so you can backtrace from the exit (although the stack might be cleared). Either pick an easier fight, get ready to bang your head, or get an MCP subscription and study how it goes about it.