Skip to main content

Welcome to Mark Baggett - In Depth Defense

I am the course Author of SANS SEC573 Automating Information Security with Python. Check back frequently for updated tools and articles related to course material.

msfencoding tips and SANS CDI presentation

UPDATE 1-21-2009:  HD Moore delivered this patched on Christmas Eve.   I don't want to start any rumors, but has anyone ever seen HD Moore and Santa Claus in the room at the same time?   Google certainly seems to indicate some type of relationship..Hmm

Original Post:
On Dec 15th I am giving a presentation at SANS CDI on my whitepaper on the Effectiveness of Antivirus detecting Metasploit payloads. Metasploit changes CONSTANTLY and I want to be sure my presentation is up to date. So I've been spending some time updating my reasearch. Here is what I learned.

First, when I wrote my paper, msfencode wouldn't produce an EXE. In my paper I described three techniques for creating an EXE. Since then, metasploit added the ability to create an EXE, but it still has a few kinks. First, msfencode doesn't actually encode the payload. Today it just changes the base address and adds a 0x0A to the end of the payload. I've reported the bug to the development team today. Given that the guys on that team seem to exhale highly functional code I suspect it will be fixed long before anyone reads my blog. I suggest you wait for their fix, but here is what I found.

msfencode has this line where it sets the encoded payload to the variable "RAW"..

# Encode it up
raw = enc.encode(buf, badchars)

Then when it creates its payload it does this call...

exe = Rex::Text.to_win32pe(buf, "")

But "BUF" is the unencrypted payload. Yep. It does nothing. Every EXE you've encoded since the update on Sept 26th (when the EXE encoding option was introduced) hasn't been encoded. "But the MD5 hash changed", you say? Yep. The to_win32pe method of the TEXT object used by msfpayload and msfencode also changes the base memory load address of the binary randomly. So it changes the EXE by a couple of bytes. While waiting on the real fix from the metasploit team you can use one of the three methods describe in my paper or you can make this change...

exe = Rex::Text.to_win32pe(raw, "")

And guess what... msfencode encodes now! BUT the payloads still don't work. If you encode a payload it doesn't run. So we take a look at our new binary in OLLYDBG and see that when the new exe reaches the XOR function to decrypt the payload it generates a Memory Access Violation. I suspect this was the result of the fact that 3.2 moved the actual payload the the .rdata section of the executable. So I reverted the EXE template to the one that came with the 3.1 version. The template that is used for the payload is located in the /data/template/template.exe If you revert to the TEMPLATE.EXE from 3.1 then everything works great. You can encode your payloads (Remember msfencode requires RAW input, see my paper for details) like this...

./msfpayload windows/shell_bind_tcp R | ./msfencode -t exe -o ~/winbindencoded.exe

Hungry for more? Lets have some real fun and double encode it!

./msfpayload windows/shell_bind_tcp R | ./msfencode -e x86/countdown -t raw | ./msfencode -t exe -o ~/winbinddoubleencode.exe

This encrypts the payload once with the countdown xor engine and then wraps that in a shikata_na_gia encoding. Double encoding. Cool! BUT perhaps not very helpful in avoiding antivirus. Encoding something twice will likely just result in the avoidance of the outer encoding algorithm. Oh well.

Here are some numbers from submitting them to
Bindshell = 3/37
Bindshell + countdown = 6/37
Bindshell + Shikata_na_gia encoding = Detected by 6/37
Bindshell + countdown encoding + Shikata_na_gia encoding = Detected by 6/37

I'll talk about this some more at my SANS CDI talk.

Popular posts from this blog

Awesome Keyboard Tricks - Clevo/Sager Backlight control from Powershell

I'm back on Windows.   After 8 years on a Macintosh I just couldn't go another day with ONLY 16GB of RAM.   I priced it out and for the cost of a top of the line MacBook I could get a tricked out PC with 32GB of ram and 2.5 TB or hard drive space (1.5 of it being SSD).   So I made the switch.  To get a top performing laptop I ended up buying a gaming machine from   The model is Sager NP9752 ( Clevo P750ZM ).    I have to say I like it quite a bit.    One of the features I was curious about was the "Programmable backlit keyboard".   With it you can set your keyboard backlight to various colors and light movement patterns.    Now, when I hear "programmable" I think APIs.   I was a little disappointed to find out there weren't any documented APIs that I could use to control the keyboard.    Your only choice is to use their built in tool to configure the lights on the keyboard.   That stinks.  I want to be able to change key colors automatically

New tool

I read an article on Fireeye's website the other day where they used Machine Learning to eliminate a lot of the noise that comes out of tools like strings.  It's pretty interesting and looks like it would save me some time when looking through malware. I wondered how effective scores would be in helping to eliminate the noise.  45 minutes and 29 lines of Python code later I have something that looks like it works.  Check out Before here is the output of strings on a piece of malware: student@573:~/freq$ strings -n 6 malware.exe | head -n 20 !This program cannot be run in DOS mode. e!Rich `.rdata .pdata @.gfids @.rsrc @.reloc \$0u"H L$ SVWH K SVWH |$ H;_ <bt%<xt!<Zt |$ AVH l$ VWAV L$ SUVWH UVWATAUAVAWH 0A_A^A]A\_^] UVWATAUAVAWH @A_A^A]A\_^] After the useful stings quickly bubble to t

Security Onion getting the most from and Domain States

My talk at Security Onion conference has been posted and is available for viewing here.

SRUM-DUMP and SRUM_DUMP_CSV Ported to Python 3

SRUM_DUMP and SRUM_DUMP_CSV have been ported to Python3 and are available for download from the PYTHON3 branch of my github page. In moving to Python3 I also updated the modules that I depend upon to parse and create XLSX files and access the ESE database that contains the SRUM data.  I hope that this will fix the issue that some users have experienced with SRUDB.dat files that create very large spreadsheets.  If it does not please let me know and continue to use SRUM_DUMP_CSV.EXE to avoid the XLSX problem. In moving to Python3 you will find the process to be faster. If you would like to run the tools from source instructions for doing so are in the README on the github page.

Reverse Pivots with Metasploit - How NOT to make the lightbulb

In a penetration test your target is PII kept on a corporate file server which I will call Victim2. You are outside the firewall but have gained access to an internal host, Victim1, when a user opened your word document with an embeeded Meterpreter payload . The stager embedded in the word document made a REVERSE_TCP connection to your machine which uploaded metsrv.dll to the victim. The machine you have access to (Victim1) has unfiltered access to your target (Victim2). Victim2 is vulnerable to ms08_067_netapi. Victim2 however, has NO access to the internet at all. Were it not for the strict egress firewall rules controlling Victim2 you could have used the ROUTE command to pivot your attack through your meterpreter session on Victim1 to Victim2, and have Victim2 send you a shell directly like this... Your IP = Victim1 = Victim2 = Background session 1? [y/N] y msf exploit(ms08_067_netapi) > route add 1 msf exploit(m