31 Aug

Let’s say something good about Google Chrome

In my previous post I criticized Google’s decision to disable NPAPI plugin technology. I still think it was a bad decision. But today let’s talk about a change that should be an improvement for virtually all users.

Chrome will begin pausing many Flash ads by default to improve performance for users. This change is scheduled to start rolling out on September 1, 2015.

Source: https://plus.google.com/+GoogleAds/posts/2PmwKinJ7nj

Say what? Is Google going against ads? 8-) Well, not really. HTML5 ads are apparently OK. But those obnoxious Flash-based ads will become click-to-play.

The setting in question is located in Settings->Advanced->Content Settings->Plugins:
It has been present in Chrome for several months already. So, I’m guessing that Google will be only pushing out some configuration change, or change the default value for new installations. Who knows, as Google is not giving us any details at this point..

Google’s ad detection algorithm might need some improvements and there might be some other side-effects but overall I think it’s a great change! Good job Google, you made my day better! :)

21 Aug

Dancing pigs – or how I won my fight with Google Chrome updates

I think removing NPAPI support from Google Chrome was a really stupid decision from Google. Sure, Java and some other plugins were buggy and vulnerable. But there is a huge group of users that need to have NPAPI for perfectly legit reasons. Certain banks use NPAPI plugins for 2-factor authentication. Certain countries have made their digital government and signatures based on NPAPI plugins. And the list goes on.

I have my reasons too. If I have to run older version of Chrome for that, I will do so – and no amount of nagging will change my mind.

That’s a well known fact in security circles, named “dancing pigs“:

If J. Random Websurfer clicks on a button that promises dancing pigs on his computer monitor, and instead gets a hortatory message describing the potential dangers of the applet — he’s going to choose dancing pigs over computer security any day

Unfortunately pointy-haired managers at Google fail to understand this simple truth. Or they just don’t give a crap.

Hello, I am AutoUpdate, I just broke your computer

Imagine my reaction one day when my NPAPI plugin suddenly stopped working. It just wouldn’t load. It turned out that Google Chrome was silently updated by Google Update. It broke my plugin in the process and – officially – there is no way of going back.

What do you think I did next?

That’s right – I disabled Google Update from services, patched GoogleUpdate.exe to terminate immediately and restored previous version of Google Chrome from the backup. Dancing pigs, remember?

Your Google Chrome is out-of-date

It worked well for few months. But this week, Chrome started nagging me again.
Quick Google search lead me to this answer: you need to disable Chrome updates using Google’s administrative templates.

Let’s ignore the fact that the described approach works only for XP (for Windows 7 you need to use ADMX templates which you need to copy manually to %systemroot%\PolicyDefinitions) and now there are like 4 places related to Google Chrome updates in the policies.

So, I set the policies and it seemed to work. For a day.

Your Google Chrome is still out-of-date

Imagine my joy the next day when I saw yet-another-nagscreen. Like this:

No, I don’t need that update. Really!

I can close the nag, but 10 minutes later it will pop up again. And it looks like the only way to get rid of the nag is to patch chrome.dll. I really didn’t want to do that but dumb decisions by Google managers are forcing my hand here.

Reversing Google Chrome

Since Chrome is more or less open-source, you can easily find the nagware message:

From here, we can find which dialog is responsible for the nag:

From there we can find NOTIFICATION_OUTDATED_INSTALL which comes from UpgradeDetector. And finally we arrive at CheckForUpgrade() procedure:

This is what I want to patch! But how?

You could load Chrome DLL in IDA and try to find the offending call on your own. But I’m willing to bet that it will take you hours, if not days. Well, PDB symbols to the rescue!

Symbols for Chrome are stored at https://chromium-browser-symsrv.commondatastorage.googleapis.com and you will need to add that path to your _NT_SYMBOL_PATH. Something like this:

_NT_SYMBOL_PATH is a very complex beast, you can do all sorts of things with it. If you want a more detailed explanation how it works, I suggest that you read Symbols the Microsoft Way.

After that, you can load chrome.dll in IDA, wait until IDA downloads 850MB of symbols, and drink a coffee or two while IDA is analyzing the file. After that it’s all walk in the park. This is the place:

And one retn instruction makes my day so much better..

Final words

Unfortunately for me, this world is changing. You are no more the sole owner of your devices, all the big corporations want to make all the decisions for you.

Luckily for me, it is still possible to achieve a lot using a disassembler and debugger. And reverse engineering for interoperability purposes is completely legal in EU. :)

Have fun!

08 Jul

Fun with encrypted VBScript

In the post on malicious LNK file analysis, I mentioned that malicious LNK file contained encrypted VBScript. Of course I wanted to check the script, so I started Googling for tools to decrypt VBScript files.

Existing tools

There is some research about the used encryption algorithm and few tools as well. The most useful to me seemed article explaining the algorithm behind script encoder. Its author also has released several versions of his scrdec tool which is supposed to decrypt VBE files.

But, as it usually happens, publicly available tools tend to blow up in the most unexpected ways on non-standard scripts.

I’m gonna fix that!

Cause of the problems

All publicly available tools assume that the encrypted file will be using ANSI encoding. But it doesn’t have to! :) All the operations in vbscript.dll use wide chars and wide strings.

For example, here’s a simple encrypted VBScript that is saved as a “unicode” file: https://www.mediafire.com/?cd98w73v12fpdq7. This one you can actually open in notepad, save as ANSI and then decode using scrdec.exe.

But how about this one? https://www.mediafire.com/?xdj6dfxsrcgbdr1. You can open it in text editor and save as ANSI – but after that it will not work anymore and script decoders will fail as well. How about that? ;)

Writing my own script decoder

Taking into account these problems, I decided to write my own decoder in C#. C# seemed to be a logical choice as .NET uses wide strings internally and it has StreamReader class that can take care of ANSI/Unicode/UTF8 string encoding automatically – so that I can focus on the actual decryption algorithm instead.

Well, almost.

.NET Framework contains Base64 decoding functions. Unfortunately, VBE files use a nonstandard approach to Base64. So, I had to implement simple Base64 decoder from scratch, taking into account all the weirdness of the VBE files. But that’s a very simple thing to do.

First version of decoder took me few hours to make. And it worked nicely! :) But I still wasn’t satisfied because vbscript.dll disassembly showed me quite a few “weird” jumps. They were not taken in my examples but I was sure I could make up some test files that would force these jumps to be taken.

Oh boy, I rather wish I hadn’t done that..

Bug-features of VBScript.dll

It turns out that not only you can use wide chars, you can do some other really hairy stuff:

  • you can mix encrypted code blocks with plain VBS in one file;
  • and you can put several encrypted code blocks in one file;
  • if encrypted code block length cannot be decoded or is larger than the filesize, this block will be considered to be plain VBS;
  • if encrypted code block checksum cannot be decoded or doesn’t match the calculated one, this block will be considered to be plain VBS;

Having fun already?

It took me a few more hours to get my script decoder right. Now it should work almost the same way Microsoft’s vbscript.dll works. But I wonder how many AV engines got this implementation right? We shall test and see!

Testing AV engines

OK, OK, I didn’t really test AV engines. I just used VirusTotal to scan the files. This approach is really flawed (for many reasons that deserve a separate blog post) but it gives you some estimate how good or bad the situation really is.

I googled for some really common and easily detected VBScript malware. In few minutes I found this great example named aiasfacoafiasksf.vbs.

Let’s see what VirusTotal says for the original file:

File name: aiasfacoafiasksf.vbs
Detection ratio: 38 / 56
Analysis date: 2015-06-30 12:10:06 UTC

OK, that’s pretty decent. Should I try encrypting it using Microsoft’s screnc.exe?

Hmm, VT results are 24/55. Looks like some antiviruses don’t even know that VBScript can be encrypted..

Let’s see what happens when we start pushing the limits:

  • Converting VBE to unicode we get: 19/55. Wide strings? Who needs that, the entire world speaks English;
  • VBE file consisting of multiple blocks: 12/55. I know, for loop is hard to implement;
  • Mixing plain text and encrypted VBE blocks: 10/55. Nah, that lonely jmp will never be taken;
  • Mixing plain/encrypted blocks and using unicode: 7/55. I feel really protected now;

Please note, I haven’t modified a single byte in the plain-text representation of the malicious script. All I did is change the way the script is encoded.

Let’s see what happens when I add some comments to the malicious script. Again, these are cosmetic changes, I am not changing a single variable name or functionality of the script. Any AV engine that normalizes input data before scanning should still detect malware.

  • Plain VBS with extra comments: 14/55. Apparently they don’t normalize input data;
  • VBE with extra comments: 10/55. That’s kinda expected;
  • Extra comment containing VBE start marker: 10/55. Suddenly getting “BehavesLike.JS.Autorun.mx” from McAfee. Excuse me? Are you using PRNG to generate those detections?

OK, so adding comments to VBScript is not that bad. But what happens when we put it all together?

Add extra comments + one VBE start marker in comment + one encrypted VBE block + use unicode: 3/53.


What can I say? Apparently some AV companies love to talk about their script engine features – but they don’t implement these features properly. It would be extremely easy to detect most of my test files as malformed VBScript – as there is no way to generate such files using standard tools. But they don’t do even that. Considering all this misery, I will not release any code that could be used to create super-duper-awesome-FUD VBScript encoder – even though it takes less than an hour to make one.

Note – it took me few weeks to get this post from a working draft to a published version. I’m sure that current VirusTotal results will be different, as most AV companies have had chance to process script files and make signatures for them. ;) Also, any decent AV company should be able to get those samples from VirusTotal if they wish to fix their VBScript processing.

Have fun and keep it safe!

Useful links

Windows Script Encoder v1.0
My VBScript decoder: https://www.mediafire.com/?9pqowl05um4jums
I am not posting the source code but this is a non-obfuscated application made in C#. So feel free to use any decompiler you like..
Set of non-malicious VBE files demonstrating some edge cases of what you can do: https://www.mediafire.com/?bu3u62t858dn4id
Article in klaphek.nl, explaining the algorithm behind script encoder
scrdec.exe v1.8
scrdec.c v1.8
vbs_dec.pas by Andreas Marx

29 Jun

Linking OMF files with Delphi

Continuing the discussion about Delphi compiler and the object files.

Here is the OMF file template I made for 010 Editor: https://www.mediafire.com/?bkpbkjvgen7ubz1

Please note, it is not a full-featured implementation of OMF specification. I only implemented all OMF file records that are processed by Delphi 2007 compiler. So, next time you have a cryptic compiler error while trying to link OMF file in Delphi, you can take a look into your OBJ file and make an educated guess what’s causing the problem.

TL;DR version

In 95+% of cases you will encounter OBJ file that has unsupported segment name in SEGDEF record. And it’s a simple fix, too – you just need to use objconv.exe by Agner Fog and use -nr option to rename the offending segment. Something like this:

Next possible issue is exceeding the number of EXTDEF or LNAMES records – this can happen if you’re trying to convert a really large DLL file into OBJ file.

Finally, your OBJ file might contain some record type which is not supported by Delphi compiler at all. I’m not aware of a simple way to fix it, I would try using 010Editor and OMF template to remove the entire record.

If your problem is not caused from any of the above issues, please feel free to drop me a note – I’ll be happy to look into it.

Known limitations of Delphi compiler

This is a list of limitations I was able to compile and/or confirm. Some of them come from Embarcadero official notes and the rest I obtained by analyzing dcc32.exe.

SEGDEF (98H, 99H)

  • Not more than 10 segments – if number of segments exceeds 10, buffer overrun will probably happen.
  • Segments must be 32bits. Will cause “E2215 16-Bit segment encountered in object file ‘%s'”
  • Segment name must be one of (case insensitive):
    • Code segments: “CODE”, “CSEG”, “_TEXT”
    • Constant data segments: “CONST”, “_DATA”
    • Read-write data segments: “DATA”, “DSEG”, “_BSS”

    Segment with any other name will be ignored.


Not more than 50 local names in LNAMES records – will cause “E2045 Bad object file format: ‘%s'” error.


Not more than 255 external symbols – will cause “E2045 Bad object file format: ‘%s'”
Certain EXTDEF records can also cause “E2068 Illegal reference to symbol ‘%s’ in object file ‘%s'” and “E2045 Bad object file format: ‘%s'”

PUBDEF (90H, 91H)

Can cause “E2045 Bad object file format: ‘%s'” and “F2084 Internal Error: %s%d”


Embarcadero says that “LEDATA and LIDATA records must be in offset order” – I am not really sure what that means. Can cause “E2045 Bad object file format: ‘%s'”


Embarcadero says that “LEDATA and LIDATA records must be in offset order” – I am not really sure what that means. Can cause “E2045 Bad object file format: ‘%s'”


This type of record is unsupported, will cause immediate error “E2103 16-Bit fixup encountered in object file ‘%s'”


Embarcadero documentation says:

  • No THREAD subrecords are supported in FIXU32 records
  • Only segment and self relative fixups
  • Target of a fixup must be a segment, a group or an EXTDEF

Again I’m not sure what they mean. But there are lots of checks that can cause “E2045 Bad object file format: ‘%s'”


Accepted by compiler, but no real checks are performed.

LINNUM (94H, 95H)

Accepted by compiler, but no real checks are performed.


Accepted by compiler, but no real checks are performed.


Ignored by compiler.

That’s the end of the list. Any other entry type will cause immediate error “E2045 Bad object file format: ‘%s'” :)

Useful links

My OMF file template for 010Editor: https://www.mediafire.com/?bkpbkjvgen7ubz1
OMF file format specification.
The Borland Developer’s Technical Guide
Objconv.exe by Agner Fog
Manual for objconv.exe

19 Jun

Weirdness of C# compiler

I’ve been quite busy lately. I made OMF file template I promised few weeks ago, found a remote code execution vulnerability in One Big Company’s product and spent some time breaking keygenme by li0nsar3c00l. I’ll make a blog post about most of these findings sooner or later.

But today I want to show you something that made me go WTF..

I needed to see how the loop “while true do nothing” looks like in IL. Since C# compiler and .NET JIT compiler are quite smart and optimize code that’s certainly an eternal loop, I needed to get creative:

Nothing fancy, but C# & JIT compiler don’t track param values, so they both generate proper code..

Well, I thought it’s a proper code, until I looked at generated MSIL:

WTF WTF WTF? Can anyone explain to me why there are 2 ceq instructions?

I could understand extra nop and stloc/ldloc instructions, but that ceq completely blows my mind.. And it’s the same for .NET 2.0 and 4.5 compilers.

12 Jun

I won’t show you

if you come to me with a question, i won’t give you an answer.

if you ask for my advice, i won’t give you a recommendation.

at best i’ll give you a pointer, a suggestion. perhaps a direction, a map. you’ll then start walking, creating your own route, getting lost and finding your way again. you’ll see other people’s footsteps often, some old and some fresh, and sometimes you’ll walk open territories no one has visited before. you’ll meet Eureka and dance together and laugh in joy, you’ll cry with Despair, which is not a bad thing.

if you want a lesson, i won’t give you instruction.

if you want guidance, but i won’t show you.

i want you to have fun too. i won’t spoil your journey.

or maybe i just have no idea and this is my fancy excuse to slip away from the situation?

Thank you, el trastero, couldn’t have said it any better.

09 Jun

Excuse the mess #2

I’m in the middle of updating the blog to the latest version of WordPress. It’s not a really straightforward process as the new emoji support in WordPress 4.2 causes RSS feeds to stop validating. And there are lots of other tiny but ugly issues with it.

So, please excuse the mess, I’m doing my best to iron-out all the remaining wrinkles. ;) And please let me know if you notice anything wrong with the site, comments or RSS.

Update: looks like all issues are resolved. Enjoy!

08 Jun

Analyzing malicious LNK file

Last week I noticed a month-old post from F-Secure titled “Janicab Hides Behind Undocumented LNK Functionality” in my RSS Reader. I had starred it but never had time to read and analyze it thoroughly. Post title and their statement caught my attention:

But the most interesting part is the use of an undocumented method for hiding the command line argument string from Windows Explorer.

What is this undocumented functionality you’re talking about? Tell me more! Unfortunately, they didn’t provide any technical details just some screenshots.

I’m gonna fix that (and have some fun in process) :)

Initial findings and available tools (are crap)

Armed with information from FSecure’s post, I started Googling. In a few minutes I found VirusTotal scan and a couple of searches later the LNK file itself. Now what?

I’m not a LNK file expert and I don’t have magic tools for analyzing them. So, I did what everyone else would – Googled again. There are 3 tools that can be found easily:

All of these tools showed the same information as VirusTotal scan – absolutely nothing useful. See for yourself in screenshots:

Marked in blue is the final structure of LNK file, as per 010Editor Template. COMMAND_LINE_ARGUMENTS are empty. And it looks like malware authors somehow managed to put the real command-line “/c copy *.jpg.lnk…” outside the defined structures, right? How’s that possible?

So, I got an official Shell Link (.LNK) Binary File Format specification by Microsoft and started reading.

Fixing 010Editor template

LNK file consists of sequence of structures of variable size. There’s always size of the structure, followed by data. Right after it there’s a next size and next data. Size, data, size, data.. It’s very easy to process, just look at the size, read the appropriate number of bytes, process them. Repeat until done. How hard is that?

Yet, Mr. Stevens (proudly calling himself Microsoft MVP Consumer Security, CISSP, GSSP-C, MCSD .NET, MCSE/Security, MCITP Windows Server 2008, RHCT, CCNP Security, OSWP) managed to mess up his template.


His implementation of LinkInfo structure looks like this:

He just reads all the fields one after another and expects that the length of all the fields will be equal to the size of structure (LinkInfoSize). Sure, it might work on “normal” LNK files but we’re dealing with malware and intentionally modified files here. ;)

After fixing that and few more bugs in the template, I was able to see the proper structure of this LNK file:
Command-line passed to cmd.exe is exactly where it should be, so why is Windows Explorer having problems with it?

Modifications in LNK file

Well, let’s look at the file in more details.

Looks like someone took a hex editor and just filled with zeroes every possible field in most of ItemID structures.

Same in LinkInfo, lots of zeroes.

And in EnvironmentDataBlock too..

Hmm, this is interesting, 0xA0000000 is not a defined ExtraData structure ID according to MS spec.

Well, there are lots of changes but which one(-s) are causing the Explorer cockup F-Secure is mentioning? There are two ways to find it – debugging Windows Explorer shell, or making a test LNK file and filling it with zeroes until Explorer breaks. First one would be fun, but second one will be definitely faster. ;)

After few attempts, I found the culprit: it’s the modified EnvironmentDataBlock.

So, now you know. Adding empty EnvironmentDataBlock to any shortcut and setting ShellLinkHeader.LinkFlags.HasExpString = 1 will cause Windows Explorer to hide your command-line (but not exe name) from the user. :twisted:

Command line magic and steganography

I wanted to finish my analysis but some things were still bugging me.
What are these data after the end-of-file marker? And why is this LNK file 500+KB in size?

The answer is simple – it’s using steganography to hide malicious VBScript inside.

Let’s look at the commands passed to cmd.exe, I’ll just prettify them a bit. To do it properly, you need to know about command prompt escape characters, like “^” (at first I didn’t and FSecure got it wrong in their blogpost, too! ;) ). These are the actual commands:

First few lines are copying our LNK file to TEMP folder. Remember, FSecure analysis says that LNK file was originally called fotomama.jpg.lnk. ;)
dir command is printing full filename of our evil file into another file named “o”.
echo lines are generating file .bat.
And finally .bat is executed.

So, what is being written into .bat?

First 2 lines just copy our evil file to z9. I really don’t know why file copy is done via environment variables, maybe it’s some sort of anti-antivirus trick to hide the malware origin.
findstr treats z9 as text file and copies all “lines” of text that contain “#@~” into a file 1.vbe. If you Google a bit, you’ll learn that encrypted VBScript files have extension .vbe, consist of one long string and start with magic bytes “#@~^””.
Then a script gets executed, LNK file deleted and stupid user ends up being pwned. :)

Now, the bytes after end-of-file marker in LNK file start to make sense. 0x0A is new-line that’s necessary for findstr, and then comes encrypted VBScript. Nice example of steganography, lots of black command-prompt scripting magic, I really like it! ;)

Decoding VBE file and analyzing it – that’s a matter of another blog post. Yes, it’s really on my todo list.

Fixed LNK Template

I spent few more evenings working on the LNK template. It was just bugging me, to have something so useful, yet so terribly broken. So, I’m happy to present a fully reworked version that should be much more stable and able to deal with malicious LNK files in much better fashion.

Download link: https://www.mediafire.com/?zvrlmjy9v9m3ed3

Final thoughts

This was a fun evening project. I learned a lot about 010Editor Binary Templates, learned something new about LNK files and got a refresher course in CMD scripting and steganography. Writing this post took much much more time than the actual research, though.

If you want to have some fun too, you can use my 010Editor Template to edit your LNK files. Or make a simple tool in C#. Or create a LNK obfuscation service webpage in PHP. Possibilities are endless! :)

Obviously I won’t give out link to an actual malware (if Google is your friend, you can find it anyway) but here’s a demo LNK file I put together just for fun: http://www.mediafire.com/?gmphyn0mkmmyuag

It’s harmless, trust me! :D

Useful links

FSecure post that started my adventure
Original LNK Template
Official LNK specification by Microsoft
Overview of LNK file data that are useful for computer forensics