c - 如何使用 Fiddle 和 Windows API (dll) 在 Ruby 中运行 shellcode

标签 c ruby windows winapi fiddle

我正在尝试将 ruby​​ Fiddle 标准库用于 Windows API 以运行 shellcode

代码的思路是

  1. 拥有可执行任何操作的原始十六进制 shellcode(例如 MessageBoxA)
  2. 调用kernel32.dll
  3. 为这个shellcode分配一 block 内存。 虚拟分配
  4. 为这个shellcode创建一个缓冲区
  5. 将 shellcode 移至该分配区。 RtlMoveMemory
  6. 创建一个新线程来执行该 shellcode。 创建线程
  7. 等待执行/线程结束。 WaitForSingleObject

代码如下:

require 'fiddle'
require 'fiddle/import'
require 'fiddle/types'

shellcode = # MessageBoxA
"\x31\xd2\xb2\x30\x64\x8b\x12\x8b\x52\x0c\x8b\x52\x1c\x8b\x42" +  
"\x08\x8b\x72\x20\x8b\x12\x80\x7e\x0c\x33\x75\xf2\x89\xc7\x03" + 
"\x78\x3c\x8b\x57\x78\x01\xc2\x8b\x7a\x20\x01\xc7\x31\xed\x8b" + 
"\x34\xaf\x01\xc6\x45\x81\x3e\x46\x61\x74\x61\x75\xf2\x81\x7e" + 
"\x08\x45\x78\x69\x74\x75\xe9\x8b\x7a\x24\x01\xc7\x66\x8b\x2c" + 
"\x6f\x8b\x7a\x1c\x01\xc7\x8b\x7c\xaf\xfc\x01\xc7\x68\x79\x74" + 
"\x65\x01\x68\x6b\x65\x6e\x42\x68\x20\x42\x72\x6f\x89\xe1\xfe" + 
"\x49\x0b\x31\xc0\x51\x50\xff\xd7"

include Fiddle 
kernel32 = Fiddle.dlopen('kernel32')

puts "[-] VirtualAlloc"
ptr = Function.new(kernel32['VirtualAlloc'], [4,4,4,4], 4).call(0, (shellcode.size), 0x3000, 0x40)
Function.new(kernel32['VirtualProtect'], [4,4,4,4], 4).call(ptr, shellcode.size, 0, 0)

puts "[-] Create buffer"
buf = Fiddle::Pointer[shellcode]

puts "[-] RtlMoveMemory"
Function.new(kernel32['RtlMoveMemory'], [4, 4, 4], 4).call(ptr, buf, shellcode.size)

puts "[-] CreateThread"
# thread = Function.new(kernel32['CreateThread'], [4, 4, 4, 4, 4, -4], 4).call(0, 0, ptr, 0, 0, 0)
thread = Function.new(kernel32['CreateThread'], [4,4,4,4,4,4], 4).call(Fiddle::NULL, 0, ptr, 0, 0, 0)

pp Function.new(kernel32['WaitForSingleObject'], [4,4], 4).call(thread, -1)

问题是 MessageBoxA 永远不会执行,当我尝试使用绑定(bind) shell 之类的东西时,TCP 连接在我连接时成功启动,但我无法执行命令,一旦我发送任何东西两次(如按两次回车)它就会结束。

我检查了缓冲区 buf 大小和内容 buf.sizebuf.to_str,它是准确的。

我是不是漏掉了什么?

谢谢!

注意:我不想直接调用 MessageBoxA API,我需要从 shellcode 中执行它。

最佳答案

好吧,我认为这段代码工作得很好

require 'fiddle'
require 'fiddle/import'
require 'fiddle/types'

# MessageBoxA
shellcode =
    "\xd9\xeb\x9b\xd9\x74\x24\xf4\x31\xd2\xb2\x77\x31\xc9\x64" +
    "\x8b\x71\x30\x8b\x76\x0c\x8b\x76\x1c\x8b\x46\x08\x8b\x7e" +
    "\x20\x8b\x36\x38\x4f\x18\x75\xf3\x59\x01\xd1\xff\xe1\x60" +
    "\x8b\x6c\x24\x24\x8b\x45\x3c\x8b\x54\x28\x78\x01\xea\x8b" +
    "\x4a\x18\x8b\x5a\x20\x01\xeb\xe3\x34\x49\x8b\x34\x8b\x01" +
    "\xee\x31\xff\x31\xc0\xfc\xac\x84\xc0\x74\x07\xc1\xcf\x0d" +
    "\x01\xc7\xeb\xf4\x3b\x7c\x24\x28\x75\xe1\x8b\x5a\x24\x01" +
    "\xeb\x66\x8b\x0c\x4b\x8b\x5a\x1c\x01\xeb\x8b\x04\x8b\x01" +
    "\xe8\x89\x44\x24\x1c\x61\xc3\xb2\x08\x29\xd4\x89\xe5\x89" +
    "\xc2\x68\x8e\x4e\x0e\xec\x52\xe8\x9f\xff\xff\xff\x89\x45" +
    "\x04\xbb\x7e\xd8\xe2\x73\x87\x1c\x24\x52\xe8\x8e\xff\xff" +
    "\xff\x89\x45\x08\x68\x6c\x6c\x20\x41\x68\x33\x32\x2e\x64" +
    "\x68\x75\x73\x65\x72\x30\xdb\x88\x5c\x24\x0a\x89\xe6\x56" +
    "\xff\x55\x04\x89\xc2\x50\xbb\xa8\xa2\x4d\xbc\x87\x1c\x24" +
    "\x52\xe8\x5f\xff\xff\xff\x68\x6f\x78\x58\x20\x68\x61\x67" +
    "\x65\x42\x68\x4d\x65\x73\x73\x31\xdb\x88\x5c\x24\x0a\x89" +
    "\xe3\x68\x52\x49\x58\x20\x68\x47\x53\x41\x42\x68\x40\x4b" +
    "\x49\x4e\x31\xc9\x88\x4c\x24\x0a\x89\xe1\x31\xd2\x52\x53" +
    "\x51\x52\xff\xd0\x31\xc0\x50\xff\x55\x08"


include Fiddle

kernel32 = Fiddle.dlopen('kernel32')

puts "[-] VirtualAlloc"
ptr = Function.new(kernel32['VirtualAlloc'], [4,4,4,4], 4).call(0, (shellcode.size), 0x3000, 0x40)
Function.new(kernel32['VirtualProtect'], [4,4,4,4], 4).call(ptr, shellcode.size, 0, 0)

puts "[-] Create buffer"
buf = Fiddle::Pointer[shellcode]

puts "[-] RtlMoveMemory"
Function.new(kernel32['RtlMoveMemory'], [4, 4, 4], 4).call(ptr, buf, shellcode.size)

puts "[-] CreateThread"
# thread = Function.new(kernel32['CreateThread'], [4,4,4,4,4,4], 4).call(Fiddle::NULL, 0, ptr, 0, 0, 0) # Works
thread = Function.new(kernel32['CreateThread'], [4,4,4,4,4,4], 4).call(0, 0, ptr, 0, 0, 0)              # Works
Function.new(kernel32['WaitForSingleObject'], [4,4], 4).call(thread, -1)

注意:Shellcode 应该与操作系统架构兼容。在我们的例子中,这个 MessageBoxA 是 32 位兼容的。

引用

HTH 有一天有人

关于c - 如何使用 Fiddle 和 Windows API (dll) 在 Ruby 中运行 shellcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51505870/

相关文章:

c - 自制内核链接器全局变量和内联字符串无法访问

ruby - HABTM 链接表未在可安装引擎中采用 isolate_namespace 值

C++/Win32 - 如何迭代特定进程的线程列表并将起始地址解析为模块?

ruby - 独立 Ruby 中的事件记录

javascript - 从我的 Controller 返回不同于 200 的代码以触发 ajax 错误是否被认为是好的做法?

.net - Windows 服务如何以编程方式自行重启?

windows - 用于在 Facebook 墙上发布的 SVN Post-commit Hook

c - 使用 ptrace 停止或杀死进程中的线程?

c - 如何在管道的文件描述符上的线程 block 中进行 read() ?

python - Colab 中的 Cython : "cdef struct Rectangle" (line 9) identified as syntax error