A closer look at “NetSupport”(Rat) top 2 layers

This post is based on the blog post by FireEye located Here.

I was given a private .saz to look at by someone else that gave me the entire infection chain.

In this post I will only be doing 2 levels of decoding..

While looking at this saz file, FireEye published the post above. This gave me a better idea of what to expect as I was pulling these files apart.

What I will be showing here is a more lower level on how the script is decoding the first 2 levels. After that it requires that the server  for the second level that it calls out to, to still be online and available to get the next level to download.

This sample is after it has already fingerprinted the system and sent the encoded data to the server.

At the end of the FireEye post there is a list of several hashes of files. We will be using “229f69a0b21740c91bfd0174e63cc0b7be3a7f5f9d6819cc3bdf3cf117ada9ff”  which can be found on Hybrid Analysis Here , Virus Total Here , (I re-uploaded to) VirusBay Here in case those that want to follow along only have an account there.

Now that we have our sample lets take a look.

Level-1Ugly

That is pretty dense so lets make a copy and then make it “Pretty”

Level-1-Pretty

That is a little better but it is still highly obfuscated. It appears to have a hex string at the bottom that gets reassembled and is using a crazy method of string replacement / rebuilding. The clean won’t get output if you just walk thru the code in the debugger. So we have to figure out what each section decodes to and then do the string replacements by hand for our first script..

If we take a closer look at what this script is doing we can see the first part here is just putting the first few letters back together then is has a “Choice” in the parentheses.A

For some reason which I don’t fully understand yet it will always choose the last char in that array. So the next value to tack on is “i”. Now this would be a pain to go thru by hand so what else can we do ?

If we look close, they are in brackets and we can decode each section using some simple html code.

First part looks like this.

C

Second part of the line.

D

So now it goes from the long crazy string to this decoded version.

E

So lets use this method and decode the rest of the lines and see what we have.

G-b

When using the above decoding method to extract the decoded string be sure to mind the matching pairs for the square brackets. If you add any extra code including the string like highlighted in the green you may end up with other results.

Also note that this would most probably not run after we do the replacements but does help to understand what it is doing.

Lets take a closer look at the decoding function. By the time we get here the hex string at the bottom has already been reassembled and converted to a byte array and assigned to the the array variable of “ kijuqmoha”

DecodeFunc

So what is this thing actually doing ?

It starts with the last char of the key then

Xor’s the byte array with that value. Next it adds 1 more char to the left then Xor’s the bytes from the last output by these to Chars.

It will continue on until is uses all of the chars for the key.

I wrote a tool to help visualize the output. This came about from a conversation between Lee Holmes and myself.  He has a post Here

XorSample

In this tool I started with the full key then reduced to the last char. The decode starts with 1 char and increases to the full key. That is the way I made this decoder.

In this test we can see we can start with the full key or the last char as long as we add/remove the same direction.

ToolTest-2 

Next I wrote another tool to test the same idea but reducing to first char instead of the last.

As you can imagine, changing sides using the same data and key will give different output.

LeftVsRight 

Back to decoding the first level hex values.

We input the cleaned up hex string and the key of “c~J=e~~kk” we get get back this.

Level-2-Decode

We can also step thru the code in the web browser debugger until it outputs the decoded value in a anonymous function then extracting it from there.

If we look at the bottom of the code after we pretty it up we see this.

SecondLevelPretty

This second level will use the same decoding function to decode the hex array at the bottom but with a twist. It will input the original function code as a string and the current running code as a string and a “Seed Key” as a decimal array thru the function “encKey” to to generate a new Key to finally decode the hex array at the bottom.

If we look at this part of the code we see if is sending the 2 pieces of code into the key builder .

GetVals

One other thing we notice here is in the caller function it is tacking on something else to the Caller code.

If we step thru the code in IE debugger we end up with this “MozillacallerFunctionText” getting tacked onto the original code that gets run.

After messing with this for a few days and not getting anywhere I started to wonder if this was supposed to fail with the catch block.

If we look at this comment  in the code below it appears as if this code is supposed to be run by Windows script host rather than the browser scripting engine.

WshComment

I developed this tool to try and generate the key needed for the decoding tool.

Stepping thru the code in the browser debugger can give us an idea of what needs to be run thru the function to output the proper decoding key.

Our first try using the added string will produce this.

Second-Key-1

Testing that key  in the decoder failed to properly decode the hex string at the end.

Back to the drawing board on what to input. After failing several more times I started to look for a way to brute force the key. If I understand the math correctly it would be 256 to the 10th power possible combinations to find a key. That is way to many to try them all.

The way this is set up you can not do a normal plain text search to find the Xor Key.

I wrote another tool to test brute forcing a 10 Char key using a know plaintext search which worked very well on a normal 1 level Xored data.

Do to the nature of the way the function works in this case the key would be thoroughly diffused  and you can not look for a string the same way as a normal 1 level Xor.

The decoding is tied to the way the key gets cut up and used in the rounds. So even if you had a know plain text and know encoded text I have not found a way to run it backwards to get the key.(yet)

So that leaves us with figuring out what has to go into the key builder.

This code is similar to a hashing function in that if you change 1 item it can drastically change the output.

After lots of trial and error and dumping what gets output by the debugger we end up with this for the original calling input.

keyBuild-1 

And for the current running code we use this.

KeyBuild-2

You may notice that both inputs do not have the hex arrays in the strings.

We also need the Seed Key  “var key = [181, 205, 244, 42, 206, 126, 77, 88, 31, 20];”

We are finally able to generate a proper key and use that to decode to the next level.

There is nothing like seeing the proper output after so many hours of researching.

KeyBuild-Decoded

Here is what it looks like pretty.

level-2.decoded

If we take a closer look at the top there are 2 values that are encoded and use the function “geturl” to decode.

GetUrl

All this function does is go thru the string and if the index mod 2 = 1 then that char gets saved to the output.  After that it is a simple reverse string to get the final string.

DecodeStr

In the original saz file I was looking at, the early scripts were using this same function for decoding some values.

Since the server was down for this URL that was as far as I could go on decoding the levels and have not gotten anymore recent samples to try and go further.

That’s it for this one. I hope you learned something like I did.

If I left any questions unanswered catch me on Twitter @Ledtech3

Thanks for reading.

Advertisements
Posted in Malware, security | Tagged , | Leave a comment

PowerShell encoding used for Emotet Downloader

I first ran across the SecureString usage in this twitter thread where  @Anyrun_app is talking about a version of “Fake Net” to get all of the C2’s here https://twitter.com/anyrun_app/status/966227622899351552

There are a few methods listed in this thread by different people to run the PowerShell malware to get the output but as usual I like to dig deeper to see how it works.

This link below was actually the first thread I had seen, with “PO3T @PO3T1985”. All of it does not show in in the first link.

https://twitter.com/PO3T1985/status/966954308020441088

We also see the thread here from @JAMESWT_MHT of a long thread of malware documents used with Emotet.

https://twitter.com/JAMESWT_MHT/status/972077102542524416

So lets dig in and figure out how the encryption works.

Lets take a look and see what the original PowerShell script I seen looks like.

If we look on the anyrun_app site at the extracted PowerShell script we see this.

OnAnyrun

And if we copy it to Notepad++ we see this.

FullScript

After a little more digging we find out that this was most likely generated using @danielhbohannon Invoke-Obfuscation Out-SecureStringCommand.ps1 found here https://github.com/danielbohannon/Invoke-Obfuscation/blob/master/Out-SecureStringCommand.ps1

So how exactly does this thing work ?

1

The first part is just standard setup script encoded using Invoke-Obfuscation to run this next part that we are really interested in at this point.

2

This is “Marshaling” managed to  unmanaged code to decode the encrypted data (that is in the base 64 string) into memory as a “SecureString” then run the decrypted code.

We can get more info here on SecureString.  https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/convertto-securestring?view=powershell-6

The last part is where it gets piped into ConvertTo-SecureString with a key value.

4

The 2 dots between the two integers represents that it is an array from 224 down to 201.

You will also see it go the other way from low to high and the length depends on the key length chosen.

That will be the numbers used for the key to decrypt the data.

Lets Base64 Decode using Unicode encoding this string to bytes and see what we have.

B64Utf16

If we look close we can see a possible delimiter of the char “|”  a possible base 64 string and what looks like a Unicode Hex encoded string with a null byte after each value. The first part of that looks like garbage.

Lets take a closer look at the beginning of this base64 string.

Nums

All of the samples I looked at had the same hex value at the beginning but what is it?

After some more searching I stumbled onto this page.

This is found in the source for PowerShell itself.

ExportHeader

We now see that it is a header to indicate that this data was a Secure string exported to an encrypted string.

So what does our bytes look like after we ignore the header bytes and base 64 decode the remainder of the string ?

B64-2

Now that is looking a bit different. We now have a #2 , a base64 string and hex bytes.

If we go to here we can now see the format of how this data works.

So we now split to Version | IV | Encrypted Data . We only need the IV and data.

The IV is the middle base 64 string that we will decode to byte() in Unicode format which in this case will give us 16 bytes of data for the IV. While testing decoding to UTF-8 encoding did not produce the correct bytes for the IV.

The next thing we need is the key.

So I built a tool to speed up building the array.

KeyBuilder

From the example from the builder we can see you can also supply the key as a full array.

ExampleKeyArray

We now have all of the pieces to the puzzle and just need to decrypt the data.

It says it is AES but does not say which Version / Flavor to use .

I had a Rijndael Managed version handy so I used that using PKCS7 padding mode to decrypt with.

Decrypt

So now we end up with these decrypted bytes.

DecryptedBytesUtf16

Although it is readable it is still in UTF-16/Unicode format. So I need to convert the bytes to UTF-8.

ConvertEncoding

And we finally end up with these UTF-8 bytes.

DecryptedBytesUtf-8

A little better view.

FinalScript

As we can see here it splits the string of Url’s using the Char “?” and attempts to download and run a file from each one.

The “NSB” appears to be a random number value used for a file name that gets run.

Going back and looking at the PowerShell source code we find in “SecureStringCommands” the function called “ProcessRecord()” on how to unpack and decrypt this Base 64 string.

ProcessRecord

It is nice to find this at all. I keep forgetting the the PowerShell source code is available online now.

I also found the plain version of the AES class https://msdn.microsoft.com/en-us/library/system.security.cryptography.aes(v=vs.110).aspx

The one without the different Modes (CBC,EBC, etc..) It looks pretty much the same code I used.

Well that it for this one I hope you found the discovery as interesting as I did how how the internals worked.

Posted in Malware, PowerShell, Programming | Tagged , , | Leave a comment

Hidden .Net Resources “Are Your Tools Finding Them” ?

This file was found thru Twitter https://twitter.com/0x7fff9/status/936301229612961792 and https://beta.virusbay.io/sample/browse/106366f1fe0f39232bc86be49ecbad4a

This sample appears to be a test piece of Ransomware written in dot Net with 2 binary resources that do not show up in normal tools. No obfuscation was used to “Hide” the resources. I was able to build my own test program that would respond the same way.

So really, they are not necessarily hidden, it’s just that some tools don’t see them.

I have run across this problem before but don’t see it very often when viewing the decompiled dot Net files.

If we look at the recourses in few different tools we don’t see this data.

Lets start with PEStudio , I had reported this problem a couple of times and have gotten no response about it.

Here we see PEStudio next to ILSpy

ResourcesNotShown

It can be found here https://www.winitor.com/binaries.html

Next we have MITEC  EXE Explorer

ExeExplorer

It can be found here http://www.mitec.cz/exe.html

Finally we have Resource Hacker

ResourceHacker

It can be found here. http://www.angusj.com/resourcehacker/ (I assume this is the same one everyone else uses)

The only thing I can think of is they are not enumerating every type of resource or because the data has no file extension associated with it is not being seen.

So lets save the resources data to a file. We end up with a file called “gray.resources”

Opening it up in a hex editor we  see this.

ResFile

This would not be easy to extract without more information, so after some trial and error I got a tool to extract the binary data to .bin files.

enumResc

This simple tool will enumerate all of the resources, list the names and type then extract just the data of  “Type Byte[]” to the folder with the name and a .bin extension.

As you can see this one only had 2 items. I have not tested it yet on other files.

Lets take a look at the first one.

ghf

And the Second

ghf-2

These look to be encrypted/encoded so lets go back to ILSpy and see how.

IlSpy-1

The highlighted area is where it tells us what to do with the extracted resources.

At the top we see the key used for decryption and test4 at the bottom is AES CBC mode.
It also has a hard coded Salt value.

Since I didn’t have a tool for this type yet I just borrowed the code from them and after fixing 1 bug that may have been introduced in the decompilation  I got it working.

AES-1

Notice anything in the out put ? 0x4D5A(MZ)

So we now have an extracted and decrypted binary.

Here is the second one.

AES-2

They just used the AES function to hide the registry “Run Path”.

What we end up with is the program that does the actual “Bad Stuff” including encrypting various files using a folder/ file extension list.

cryptor-b

As of Last night that Bitcoin value did not exist.

Towards the bottom we have a “Random” password generator. The problem is it does not appear to save it anywhere to use for decryption later that I have found so far.

PasswordGen

They are also nice enough to delete the shadow copies too.

Program

I also looked up the GUID and you can find it used in several places.

I have no Idea if this thing will even run and do what the code suggest it will do.

The point to this whole post was not to show the malware, but to highlight the problem of mainstream tools not being able to find or show these types of resources.

There could be more than meets the eye that is not being shown as it was here.

That is for this one. Go check your tools.

Posted in Malware, Programming, security | Tagged , | 1 Comment

Peeling away the layers of a word document macro

The sample used in this one was first brought to my attention from the blog post by @HerbieZimmerman  and the blog post is here. https://www.herbiez.com/?p=1028 and the link to the doc file is here https://www.hybrid-analysis.com/sample/0de3f4380b642e59d0cde5570ed13bfc727000b94a034ce10e1f87bfac3fac79?environmentId=100

This one peaked my interest because it had 3 obfuscated scripts plus a “ThisDocument”  script instead of just 2 that I have normally been seeing .

There are several ways to extract the script(s) but I have been trying Open Office and it has worked fairly well for this type.

Here is what we see when we open the script that contains the “AutoOpen” sub.

autoopen-b

When opening up the script in Open Office they are default disabled. The “Rem” is commenting out those lines so the code can not run. But it can also make it harder to read so you can use Notpad++ or your favorite editor and do a  find replace of “REM “ (include the space) with “” (Nothing).

We see the “Sub AutoOpen()” which will cause this script to get fired when the document opens.

As the notes show the “Shell” will call the function listed with the parameter of “0” Zero or hidden. You can read about it here . https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/shell-function

If you look just under the green box you can see where the function starts.

If we keep scrolling down we see where the value of the function gets set.

FuncArray

We see here that there are several strings that get tacked onto each other including the third item of “Chr(34)” AKA the double quote.

These strings are the different function names that gets called to build the final string that gets added in at that position.

Lets get the fourth item and take a closer look.

fourthItem-b

Here we see the fourth item corresponds to a function above and calls a “Mid” function on a string above that.

So this takes the string in “APwTzEijQ” and extracts a substring from it starting at character 19 and takes 71 characters and then returns the string.

But as you can see there are way to many of these to do by hand so either extract and echo the result of building the string(s) or build a new tool.

Tool-1

So using this if we cleaned out almost everything we don’t need we end up with this.

FourthitemOnly

The output is the value that will get tacked on in this position.

After some trial and error I added a new section to my tool to tell me which functions were not present in the current script.

If we notice from the screenshot above and here
”XZGDjkTXd = iXjTfDtwt + MfwkTAlmO + Chr(34) +”
those are the first 2 values that get tacked on to this new script.

Those will be found in the other 2 scripts.

The first value

Script1st

FirstDecoded

Second

Script2nd

SecondDecoded

Now we have to piece this back together in the correct order.

FullSecondLevel

This is the full reassembled script. This is still hard to read so lets format it in a way we can read it better.

One thing you may notice is all of the  ‘+’  and + in this part of the script. We will have to remove those before we start the string replacements.

When the script is run they will automatically be taken care of but we have to remove them for the method we are using here.

FullFormated

Lets take a closer look at the top of this.

Top

If you look closely at the set variables the word “powershell” is broken up into different variables and reassembled in the last “set’.
The ComSpec at the end will decode to “IEX”

ComSpec We want Chars 4,26,25 which builds “IEX”

On the bottom section this is where we are peeling away more layers in the form of string replacements.

The trick is to do it in the correct order.

Bottom

The Highlighted area is the inner most section and the final section left.

If you can zoom on this and the shot above you will notice the way that the “ ( ) “ are highlighted in red. This tells us where the section will begin and end.

A normal nested function will start evaluating from the most inner section working out.

Here we start at the most outward section and do the replacements working towards the middle.

As we find in my last article the “-cReplace” means it is a case sensitive replacement meaning you only replace the exact string, which could be in the middle of another string as long as it is the exact string. the plain “-Replace” is not case sensitive so will find the string if it is upper, lower or even random mixed case.

Here you can also note that the “[Char]” is in the proper case as I have already done a string replace for the mixed case versions so let use a new tool to find out what all of the char codes evaluate to.

CharRepl

So here we have 3 items to replace.
“-cREplACe  (tiQ),$-REPLacE’3Gj’,’-REPLacE(PBw),|)“   If you have trouble seeing, the 3GJ actually gets replaced by the single quote.

After doing the replacements we move in another level.

Level-2

The highlighted area is the new string section we will be doing the string replacements with.

Just below are the strings that we will be working with.

Here is an issue we can run into while de-obfuscating by hand.

issue-b

In the earlier replacement we replaced some characters with the single quote now it stops us from doing a proper replacement using notepad++ so we have to remove the dual single quotes to do the final string replacement. We will have to keep an eye on this problem with each layer or it will not get some of the characters replaced properly.

ThirdLevel

We can see we had to remove/ replace the dual single quotes here also.

And finally we have.

FinalLayer

And a more cleaned up formatted version.

Final-Formatted 

This defiantly would not run as is but you can see what it is doing now.

The part from $franc to the catch statement is the final decoded section.The rest in the screenshot is left for reference and also artifacts left after decoding the other levels.

We finally see that we have 5 different URL’s that it will attempt to download a file from and put it in the “Public” folder with a random Numeric name from 1 to 3453245 plus a “.exe” and attempt to run it.

If it has an error it will write the error to the console window even though it was launched as hidden.

One last important note. Always use a “working” copy so if you mess something up you don’t have to go back and re-extract the the scripts. You could just make a new copy and start over.

So why would we want to decode the script in this manner rather than just running it in a sandbox? It could have possibly had some sandbox checking routine and just exit if it finds what it does not want to run with.

If the routine did not loop thru all of the sites in an attempt to download the file then you may miss some of the possible download sites.

To get a better understanding of “How” the obfuscation works, it’s strengths and it weaknesses.

Finally, another excuse to build more tools and hone my programing skills.

Well that’s it for this one I hope we all learned a few things.

Posted in Malware, PowerShell, security, VBScript | Tagged , , | Leave a comment

De-obfuscating a PowerShell Script Obfuscated by Invoke-Obfuscation

Here I will be trying to deep dive on how the obfuscation works and what is required to de-obfuscate it.

This sample comes from @James_inthe_box posted here https://twitter.com/James_inthe_box/status/928644055054946305 on November 9th 2017.

Here is the link to the “pastebin” of the script if you want to follow along. https://pastebin.com/P5AK7div

When we first look at it we see this.

FullScript

That is the full script. The first thing you may notice here is that it is in 3 separate parts so we will tackle 1 part at a time.

If we look at the top of Section-1 we see it is being set as a variable and there is a long array of binary strings.

Sect-1-Top

And the bottom.

Sect-1-Bottom

This is fairly straight forward. It will take each binary string in the array and convert it to an Integer 16 and then to a string/Char.  Lets take a closer look at that little piece on the end.

Sect-1-End

So what does this last part do? It calls “$ShellID” to get the string but only uses the Characters in positions 1 and 13. So lets do that.

ShellID

So we call $shellid  and it returns a string as seen here and then we convert that to an indexed list to see better what letters are used.

So in the end this first level gets binary decoded then piped to IEX after we concatenate them together. IEX is short for the cmdlet “Invoke-Expression”.

This would be to much to do by hand so you could just extract the the binary array and do an echo on it to see what it decodes to. But in usual fashion I like to make drag and drop programs to do it with so I’m not having to “run” anything if possible.

Sect-1-Level-2

I thought I had messed up my decoder at first but lets take a closer look.

section-1-level-2-top-b

Here they are using the function where the values were disassembled to a “random” array so we have to rebuild the string from the array.

So another new tool. (Still a work in progress and a few bugs yet)

Sect-1-Level-3 

Now we have most all of the arrays replaced with the strings. the next step is to do the Char conversions that we will be doing the string replacements with. In the screenshot you may see something like this “-F  [ChaR]92)”  So we have to convert that char code to a actual Character in this case 92 = “\”.

We also have to remove the “ ‘+’ “ from the string which tells us to concatenate the string in order to do our string replacement  from strings like “ (‘+'((B’+’5d@*B5d+B5d ”.

You you will also note that there are some “-c” in with the replace function and thanks to @danielhbohannon  I found out the it stands for Case Sensitive. So we only replace the string that exactly matches.

-creplace (case-sensitive)
-ireplace (case-insensitive)
-replace (same as -ireplace)

We can also see at the bottom of the script section that there are more string replacements.

Sect-1-FinalReplacements

After doing all of the replacements and some cleaning up, we get this as the final output.

Sect-1-Level-3-Decoded

Here we can see an array of partial URL’s and some other variables that get set up.

In section 2 we see this.

Section-2-Top

At the top, again it is using  the Shell ID to spell out “IEX” and also we have an array of Binary values.

section-2-bottom-b

In this case it is using Random characters as split Char’s rather than “,”. This is still fairly easy to overcome. Looking at the way this is implemented I way say there would be only a handful of characters that could not be used for the split  chars.

After decoding the first level we see this.

Section-2-Level-2

Here again we have the mixed up arrays and at the bottom we have several string replacements.

Sect-2-Lev-2-Replace

Once we rebuild the array strings and do the replacements and clean up a few things we end up with this.

Sect-2-Level-3-Decoded

As you can see, there is still some random string cases used here which makes it more difficult to read, but still readable.

In the final section we see this.

Sect-3

This is similar to decode as section 2 but it uses different separator characters.

sect-3-sep-b

After decoding.

Section-3-Level2

Once they got to the third level they did not add extra obfuscation like the first 2 sections.

Below we see that they are checking for tool names. I would assume no average person would have those on their system so it must be checking for a sandbox or researcher.

Sect-3-IsDbg

After viewing all 3 sections I do not believe that it is possible to automate decoding the way I have done here. There are just to many option variables to be able to account for in a script or a program. Each layer would have to be viewed and peeled away as you go until the original code is revealed.

What I have done here in a windows programs could also be done in PowerShell or python script passing in each section to decode. The only other way would be to possibly echo it out after it decoded itself which could get dangerous depending on what it would end up doing. In the case above it could get caught and end or exit without revealing what it does.

Most likely I still have a few minor mistakes in my decoded versions that would not allow them to run, but should be clear enough to tell what it is doing now.

That’s it for this one. I hope this shows it is possible to decode even this type of encoding with some patience and persistence. (And a few new tools to help)

Posted in Malware, security | Tagged , | Leave a comment

Not A DerbyCon Talk part #1

This will be the first in a series (1 of x ) that I was hoping to stuff into a 30 minuet talk at DerbyCon 2017.

In hindsight it would be better suited as an informal training session where questions could have been asked.

Those that have read my post or whom I have decoded something for may know my passion for decoding/decrypting exploit kits started while Angler EK was still “Top Dog”.

That is where I’m going to start.

Our sample is at http://www.malware-traffic-analysis.net/2015/12/21/index.html we will be using sample #2 from this download because it has the redirect page.

In the screenshot from link above you can see the code to the gate / redirect to the exploit kit. This is what we will start with here. This is packet 35.

After extracting the code from the infected page this is the full script. You will have to zoom to get a better look.

FullCode

Don’t worry we will be taking a closer look at each section.

If we look at the top section of the code we see this.

TopSection

Here we see an encoded section of text that will get decoded after going thru the rest of the sections getting decoded.

Next we have this large section of  “var” and these will be built for later replacements in the functions below.

Vars-1

Looking at the green highlighted variables  we can first see that it is declared with “var’ then it is tacking on each element as it get evaluated.

vars-2B

As we can see here we start out our first letters as “fu”.

The first few times I did this I did it all by hand, all of the math and all of the hex to char code conversions. After that was when I started developing tools to deal with this obfuscation.

Vars-3

In this screenshot what I chose to do, still being new to this, was to save the “var’s” to a file and import them into the “Get Vars” program. What this will do is search thru the lines of vars and get a unique list of variable names. I first select a variable name from the dropdown list and the click the “Get Var Val”  button to get the  associated variables that get added together.

Do to the complexity at the time I chose to split the decode function out to another program. So in the “Script Decode 2” we can see what those variables evaluate to. After doing several of these I had verified we didn’t need to decode this section every time so I did not put this all into 1 program.

So from here we continue on down the line and do all of the decoding for each variable name and if you see the counter next to the names there are 59 unique variable names. So this will still take some time to do all of the decoding and replacements.

After doing all of the reassembling we end up with a variable list like this.

DecodedVars

The next step is to do the variable replacements in the functions below.

Script-1

After doing the replacements It is still not real clear what it is doing.

DecodeFun-1

It will first check the browser being used  and pass a parameter of “2” if it is IE.

If it is IE then from the “div” above, it will get the data to decode and a decoding Key.

todecode-B

DecodedFinal

And formatted a little better.

DecodeFinalFormatted

In order to build this tool I did have to step thru the code several times in the IE debugger to fully understand how it worked.

Looking at several samples even though some  math parameters in the final decode function changed they always work out to the same end value after they were evaluated so I could build a static decoder now with just the Key value and the encoded string.

And here is what my final decode function looked like in VB dot Net.

VBCode

This is still somewhat complicated. The “IeIdx” = 2 here if you want to do the math.

The one thing about these “Kits / Builders” is even though the variables may change, the underlying decoded function stayed the same. This particular encoding has not been used since some time before Angler EK disappeared.

As complicated as this is I would have only have been able to show it quickly and what it looked like decoded in a 30 minuet talk.

The next Post was going to be the landing page but I’ve already written on that so will just add the link to it here so as not to repeat what I’ve done already.

https://pcsxcetrasupport3.wordpress.com/2016/06/11/decoding-angler-exploit-kit/

There is a link in this post to the previous one on Angler also.

I’ll Probably do a more recent Magnitude EK version for the next one. They have obfuscated the decoding a bit more than my last post here. https://pcsxcetrasupport3.wordpress.com/2017/04/24/a-look-at-the-magnitude-exploit-kit-encoding/

That’s it for now I hope this helps.

Posted in Malware, security | Tagged , | Leave a comment

Extracting and decoding malicious macros

The sample used here is from the video from  Karsten Hahn @struppigel .

If you have not seen any of them before I would highly recommend checking them out.

The video can be found here https://youtu.be/SCJVW1E8dFA

The Sample can Be found here at Hybird-Analysis if you want to follow along.

In the Video he shows a base 64 string that he extracts from the document using a Hex Editor like this.

PSFromHex

After watching the video and locating the file I wanted to take a deeper look at it seeing how my methods would work at decoding this file.

I recently decided to try and  use “Apache OpenOffice”  to do the macro extraction from the documents and it has worked.

When We first open the document we see this.

Warning

At least the default is to pop up a warning. After clicking “OK” we see.

Document-1

That is actually a picture on the document not a popup window.

When we locate the macro we see this.

OpendMacro

Closer look in Notepad++

Macro-1

That is kind of difficult to sort thru so lets clear out everything that does not appear to do anything. We end up with this.

CleanedMacro

That sure did cut a lot of junk out of the way.

So it looks like it is getting the value of the documents comments property.

Comments

As you can see here the comments section is where the Base64 string gets extracted from.

And the full Base64 string.

FullB64

Most of the “Red Team Tools” use UTF-16 encoding. So we will try Base64 decoding using UTF-16 Encoding.

After decoding I get this.

Bas64DecodeTool

A closer look.

B64Decoded

So what is this thing doing ? If we get a closer look at the end we se this.

end

Normally what I see is hex separated some type of fill in delimiter/ fill-in chars that gets removed.

Here we have Decimal char codes using several different split chars  to turn the Char codes into normal characters . This is the first time I’ve seen this done so another new tool to quickly overcome this type of encoding.

NewDecodeTool

All this new tool does is a string replacement for the various split chars used and replaces them all with just one, in this case is a comma. Once you have a common split character then you can splint on that char and loop thru and decode the the char code and return the string output.

We could have done a string replace 1 char at a time and ended up with something like this.

DelimReplace

After that we still have to decode the Decimal Char Codes.

Here is a closer look at the formatted script that was decoded.

FormatedScript

This script will reach out to the sites to download the file and save it with a random name.

I was not able to download the file directly today after extracting the URL’s.

So we know it was a downloader but we don’t know what it would have downloaded if it had succeeded.

This had peeked my interest because of the multiple split chars used in between char codes and the fact that they did not use the Macro to store the base 64 string but the Documents Comment section to hide it.

That’s it for this one I hope you learned something as well as I did.

Posted in Malware, security | Tagged , , | Leave a comment