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.

Metasploit updates to msfencode and exe template

HD Moore and the team at Metasploits are constantly updating the framework.    The programs, scripts and approaches I document In my  SANS paper on the Effectiveness of Antivirus in Detecting Metasploit Payloads have changed significantly.     If you haven't read my paper you may find it interesting.  Its here

In the document I showed how an attacker can create standalone executable payloads of any of the available payloads in the framework.  I showed how to you can use msfencode to alter the payload to avoid detection by antivirus.   One difficulty at the time was that msfencode didn't make an executable.   That all changed on 9-26!  HDM make the some changes to both the template that is used by msfpayload and msfencode (among other things).  It now much easier to avoid antivirus.  Now msfencode will create an EXE!   It doesn't show up in the options when you do msfencode -h but it works! So the following:

./msfpayload windows/meterpreter/bind_tcp R | ./msfencode -t exe

will encode the standalone meterpreter with the default encoder Shikata_ga_nai.   It works great!!  REMEMBER: msfencode wants machine language code as input (RAW output from msfpayload)   If you tell msfpayload to generate an EXE then pipe that to msfencode, msfencode will encode the Win32 PE headers and you end up with binary that will not run.  Give msfencode C source code and it will produce encoded C source code.  But that source code won't run and better than the unecoded one.    msfencode needs RAW input.  msfencode will also generate RAW output, so you should  be able to chain multiple payload encodes.  This works great too!

# ./msfpayload windows/shell_bind_tcp R | ./msfencode -e x86/fnstenv_mov -t raw | ./msfencode -t exe > doubleencoded.exe 
[*] x86/fnstenv_mov succeeded, final size 342
[*] x86/shikata_ga_nai succeeded, final size 369

UPDATE:  I have been unable to reproduce this result again.    Encoding binaries a second time has resulted in corruption. I'm not sure what I did wrong last night.   I probably tested my single encoded binary thinking it was my double encoded.  

Things have change quite a bit since february.   A straight payload with no encoding is detected by 3 antivirus products, Avast, AVG and GData.   But none of them detect it as a metasploit payload.  Instead they detect a generic "dropper".  These are NOT the same antivirus products that detected payloads back in february.  Those two products (Kasperski and Webgateway) don't detect anything now.  Seems we are relying on dumb luck    

Unencoded payload is detected by 3 antivirus products, Avast, AVG and GData 
Single encoded (shikata_ga_nia) is detected by 3 antivirus products, Avast, AVG and GData
Double encoded (fnstenv_mov + shikata_ga_nia) is detected by 1 Antivirus product, AVG

Additionally HD changed the template that is used.   When msfpayload and msfencode create an executable they rather elegantly do a merge of the payload text with the binary /data/template.exe.   HD change the template to make it more difficult for antivirus to detect the payloads.  It now stores the payloads in the .rdata section rather than the .data section and employes some techniques to avoid detection.   

Lets pretend for a minute that antivirus was able to detect the payloads BEFORE these changes.   That task just got a whole lot harder for the antivirus vendors. did some some similar work on metasploit payloads in September of this year.  Check out his stuff here.  

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

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.

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

Three Free Python apps to improve your defenses and incident response

I did a SANS Webcast on a couple of tools I developed to use automation to enhance your network defenses and incident response capability.    If you missed it you can check it out here:


An issue was reported where is some conditions SRUM_DUMP would stop processing and print the following error to the screen. UnboundLocalError: local variable 'sid_str' referenced before assignment The issue was that sometimes the SRUM database had entries in it that were all zeros. OrderedDict([('IdType', 3), ('IdIndex', 38127), ('IdBlob', '0000000000000000')]) I've released an update that handles the anomoly althought I do not understand the circomstances of why Windows would record all zero's for as the user SID. The issue was fixed and new versions of both SRUM DUMP and SRUM DUMP CSV were released.