主题
从内存注入DLL - InjectFromBuffer
函数简介
从内存缓冲区直接注入DLL到指定窗口进程,无需落地文件,隐蔽性最强。(部分模式文件会落盘)
接口名称
InjectFromBufferDLL调用
c
int32_t InjectFromBuffer(int64_t instance, int64_t hwnd, int64_t bufferAddr, int32_t bufferSize, int32_t type, int32_t bypassGuard);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
| hwnd | 长整数型 | 目标窗口句柄, 可以通过SetMemoryHwndAsProcessId接口设置成传入进程ID。 |
| bufferAddr | 长整数型 | DLL数据在内存中的起始地址。 |
| bufferSize | 整数型 | DLL数据的大小(字节)。 |
| type | 整数型 | 注入类型:1 标准注入(CreateRemoteThread),2 驱动注入模式1,3 驱动注入模式2,4 驱动注入模式3。 |
| bypassGuard | 整数型 | 是否绕过保护:0 不绕过,1 尝试绕过常见反注入保护。 |
示例
以下示例演示 读入 DLL 到内存 → 取得首地址 → 注入目标进程 的完整流程。
SDK 调用
cpp
#include "OLAPlugServer.h"
#include <fstream>
#include <vector>
OLAPlugServer ola;
long targetHwnd = /* 目标窗口句柄 */;
const char* dllPath = "inject.dll";
std::ifstream ifs(dllPath, std::ios::binary | std::ios::ate);
if (!ifs) return;
long dllSize = (long)ifs.tellg();
ifs.seekg(0);
std::vector<unsigned char> dllBuf(dllSize);
ifs.read((char*)dllBuf.data(), dllSize);
int ret = ola.InjectFromBuffer(targetHwnd, (long)dllBuf.data(), dllSize, 1, 0);
// 注入完成后可释放 dllBufcsharp
using System.IO;
using System.Runtime.InteropServices;
using OLAPlug;
var ola = new OLAPlugServer();
long targetHwnd = /* 目标窗口句柄 */;
byte[] dllBytes = File.ReadAllBytes(@"inject.dll");
GCHandle pin = GCHandle.Alloc(dllBytes, GCHandleType.Pinned);
try {
int ret = ola.InjectFromBuffer(targetHwnd,
pin.AddrOfPinnedObject().ToInt64(), dllBytes.Length, 1, 0);
} finally { pin.Free(); }python
from OLAPlugServer import OLAPlugServer
import ctypes
ola = OLAPlugServer()
target_hwnd = 0 # 目标窗口句柄
with open("inject.dll", "rb") as f:
dll_bytes = f.read()
buf = ctypes.create_string_buffer(dll_bytes, len(dll_bytes))
ret = ola.InjectFromBuffer(target_hwnd, ctypes.addressof(buf), len(dll_bytes), 1, 0)java
import com.olaplug.OLAPlugServer;
import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import java.nio.file.Files;
import java.nio.file.Paths;
OLAPlugServer ola = new OLAPlugServer();
long targetHwnd = 0; // 目标窗口句柄
byte[] dllBytes = Files.readAllBytes(Paths.get("inject.dll"));
Memory mem = new Memory(dllBytes.length);
mem.write(0, dllBytes, 0, dllBytes.length);
int ret = ola.InjectFromBuffer(targetHwnd, Pointer.nativeValue(mem), dllBytes.length, 1, 0);cpp
var ola = com("OlaPlug.OlaSoft")
var targetHwnd = /* 目标窗口句柄 */
var dllSize = ola.GetFileSize("inject.dll")
var dllPtr = ola.ReadBytesFromFile("inject.dll", 0, 0)
if(dllPtr && dllSize > 0) {
var ret = ola.InjectFromBuffer(targetHwnd, dllPtr, dllSize, 1, 0)
ola.FreeMemoryPtr(dllPtr) // 注入完成后可释放
}vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
targetHwnd = 0 ' 目标窗口句柄
dllSize = ola.GetFileSize("inject.dll")
dllPtr = ola.ReadBytesFromFile("inject.dll", 0, 0)
If dllPtr <> 0 And dllSize > 0 Then
ret = ola.InjectFromBuffer(targetHwnd, dllPtr, dllSize, 1, 0)
ola.FreeMemoryPtr(dllPtr)
End Iftext
.局部变量 ola, OLAPlug
.局部变量 dllData, 字节集
.局部变量 targetHwnd, 长整数型
.局部变量 ret, 整数型
ola.创建 ()
targetHwnd = 0 ' 目标窗口句柄
' ① 读入 DLL 到字节集
dllData = 读入文件 (“inject.dll”, )
.如果真 (取字节集长度 (dllData) > 0)
' ② 取变量数据地址 → bufferAddr;取字节集长度 → bufferSize
ret = ola.InjectFromBuffer (
targetHwnd, 取变量数据地址 (dllData), 取字节集长度 (dllData), 1, 0)
.如果真结束
' 注入完成前 dllData 须保持有效;注入成功后即可释放aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var targetHwnd = 0; // 目标窗口句柄
var dllData = io.load("inject.dll");
var ret = ola.InjectFromBuffer(targetHwnd, dllData, #dllData, 1, 0);text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
变量 dllData <类型 = 字节集类>
长整数 targetHwnd = 0 ' 目标窗口句柄
' ① 读入 DLL
dllData = 读入文件("inject.dll", )
如果真 (取字节集长度(dllData) > 0)
{
' ② 取字节集指针 → bufferAddr
整数 ret = ola.InjectFromBuffer(
targetHwnd, 取字节集指针(dllData), 取字节集长度(dllData), 1, 0)
}
' 注入完成前 dllData 须保持有效cpp
#include "OLAPlugServer.h"
#include <fstream>
#include <vector>
OLAPlugServer ola;
int64_t targetHwnd = 0;
std::ifstream ifs("inject.dll", std::ios::binary | std::ios::ate);
long dllSize = (long)ifs.tellg();
ifs.seekg(0);
std::vector<unsigned char> dllBuf(dllSize);
ifs.read((char*)dllBuf.data(), dllSize);
int32_t ret = ola.InjectFromBuffer(targetHwnd, (long)dllBuf.data(), dllSize, 1, 0);原生 DLL 调用
cpp
#include <fstream>
#include <vector>
long instance = CreateCOLAPlugInterFace();
long targetHwnd = 0;
std::ifstream ifs("inject.dll", std::ios::binary | std::ios::ate);
auto dllSize = (int)ifs.tellg();
ifs.seekg(0);
std::vector<unsigned char> buf(dllSize);
ifs.read((char*)buf.data(), dllSize);
InjectFromBuffer(instance, targetHwnd, (long)buf.data(), dllSize, 1, 0);csharp
using System.IO;
using System.Runtime.InteropServices;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int InjectFromBuffer(long ola, int hwnd, int bufferAddr, int bufferSize, int type, int bypassGuard);
long instance = CreateCOLAPlugInterFace();
long targetHwnd = 0;
byte[] dllBytes = File.ReadAllBytes(@"inject.dll");
GCHandle pin = GCHandle.Alloc(dllBytes, GCHandleType.Pinned);
try {
InjectFromBuffer(instance, (int)targetHwnd,
(int)pin.AddrOfPinnedObject().ToInt64(), dllBytes.Length, 1, 0);
} finally { pin.Free(); }python
from ctypes import CDLL, c_int, c_int64, create_string_buffer
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
target_hwnd = 0
with open("inject.dll", "rb") as f:
dll_bytes = f.read()
buf = create_string_buffer(dll_bytes, len(dll_bytes))
ola.InjectFromBuffer(instance, target_hwnd, id(buf.raw), len(dll_bytes), 1, 0)返回值
| 返回值 | 说明 |
|---|---|
1 | 成功。 |
0 | 失败。 |
注意事项
| 项目 | 说明 |
|---|---|
| 字节流 → 地址 | 易语言:取变量数据地址(字节集) + 取字节集长度(字节集);火山:取字节集指针(字节集) + 取字节集长度(字节集)。 |
| ReadBytesFromFile | 返回指针可直接作为 bufferAddr;注入完成后须 FreeMemoryPtr 释放。 |
| DLL 完整性 | DLL 数据必须完整且有效,bufferSize 须与实际文件大小完全一致。 |
| 内存生命周期 | 注入完成前 bufferAddr 指向的内存须保持有效;注入成功后即可释放。 |
| 位数匹配 | 32 位进程只能注入 32 位 DLL,64 位进程只能注入 64 位 DLL。 |
| 注入类型 | 推荐手动映射注入(type=3)以获得最佳兼容性;标准注入(type=1)部分场景可能无法从内存加载。 |
| 权限 | 注入系统进程或受保护进程需要管理员权限。 |
