吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1930|回复: 3
收起左侧

[C&C++ 转载] 简单的读写内存 小例子

[复制链接]
古月不傲 发表于 2019-12-23 11:11
本帖最后由 古月不傲 于 2019-12-23 11:33 编辑

[C] 纯文本查看 复制代码
#pragma once
//用户层
#include <Windows.h>
#include <winioctl.h>
#include <iostream>

#define READ_WRITE_MEMERY CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_OUT_DIRECT, FILE_ANY_ACCESS)

typedef struct _DATA
{
        HANDLE hProcessId;
        PVOID64 lpVirtualAddress;
}DATA, *PDATA;

.code
GetOwnerPid proc
        sub rsp, 28h
        mov rax, gs:[30h]
        mov rax, qword ptr [rax+40h]
        add rsp, 28h
        ret
GetOwnerPid endp

end

#include "UserTest.h"

extern "C" HANDLE WINAPI GetOwnerPid();

int main(void)
{
        HANDLE hDevice = NULL;
        DATA data = { 0 };
        DWORD dwRealBytes = 0;
        DWORD64 a = 5;
        LPVOID lp = &a;
        HANDLE dwProcessId = GetOwnerPid();
        data.hProcessId = dwProcessId;
        data.lpVirtualAddress = lp;

        hDevice = CreateFile(L"\\\\.\\ReadWriteMemerySymbol", GENERIC_READ | GENERIC_WRITE, 0, NULL,
                OPEN_EXISTING,
                FILE_ATTRIBUTE_NORMAL ,
                NULL);
        if (hDevice == INVALID_HANDLE_VALUE)
        {
                printf("CreateFile %d\n", GetLastError());
                system("pause");
                return -1;
        }
        if (!DeviceIoControl(hDevice, READ_WRITE_MEMERY, &data, sizeof(data), NULL, 0, &dwRealBytes, NULL))
        {
                printf("DeviceIoControl\n");
                system("pause");
                return -2;
        }
        printf("a被改变:%lld\n", a);

        system("pause");
        return 0;
}

#pragma once
//内核层
#include <ntifs.h>
#include <ntddk.h>

#define READ_WRITE_MEMERY CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_OUT_DIRECT, FILE_ANY_ACCESS)

//创建自定义设备扩展
typedef struct _DEVICE_EXTENTION
{
        PDEVICE_OBJECT pDeviceObject;
        UNICODE_STRING uszDeviceName;
        UNICODE_STRING uszSymbolName;
}DEVICE_EXTENTION, *PDEVICE_EXTENTION;

typedef struct _DATA
{
        HANDLE hProcessId;
        PVOID64 lpVirtualAddress;
}DATA, *PDATA;

//驱动入口
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING puszRegPathName);
//驱动卸载
VOID MyDriverUnload(PDRIVER_OBJECT pDriverObject);
//创建设备对象 为了交互数据
NTSTATUS CreateDevice(PDRIVER_OBJECT pDriverObject);
//普通回调
NTSTATUS DispatchGeneral(PDEVICE_OBJECT pDeviceObject, PIRP pIrp);
//控制回调
NTSTATUS DispatchContrl(PDEVICE_OBJECT pDeviceObject, PIRP pIrp);
//读取进程
VOID ReadMemory(HANDLE ProcessId, PVOID64 pVirtualAddress);

#include "PlantsVsZombies.h"

//读取进程
VOID ReadMemory(HANDLE ProcessId, PVOID64 pVirtualAddress)
{
        PEPROCESS pEprocess = NULL;
        NTSTATUS ntStatus = STATUS_SUCCESS;
        KAPC_STATE state = { 0 };
        ntStatus = PsLookupProcessByProcessId(ProcessId, &pEprocess);
        if (!NT_SUCCESS(ntStatus))
        {
                ntStatus = STATUS_INVALID_PARAMETER;
                return;
        }
        KeStackAttachProcess(pEprocess, &state);
        ULONG64 uRead = 0;
        ULONG64 uWirte = 10;
        PMDL pMdl = IoAllocateMdl(pVirtualAddress, 8, 0, 0, NULL);
        if (pMdl == NULL)
        {
                return;
        }
        MmBuildMdlForNonPagedPool(pMdl);
        PVOID lpMapVirtualAddress = NULL;
        __try
        {
                ProbeForRead(pVirtualAddress, 8, 8);
                RtlCopyMemory(&uRead, pVirtualAddress, 8);
                KdPrint(("uRead:%lld\n", uRead));
                lpMapVirtualAddress = MmMapLockedPages(pMdl, KernelMode);
                RtlCopyMemory(lpMapVirtualAddress, &uWirte, 8);
                KdPrint(("pVirtualAddress %lld\n", *(DWORD64 *)pVirtualAddress));
        }
        __except (EXCEPTION_EXECUTE_HANDLER)
        {
                KdPrint(("异常\n"));
        }
        MmUnmapLockedPages(lpMapVirtualAddress, pMdl);
        KeUnstackDetachProcess(&state);
        ObDereferenceObject(pEprocess);
}

//驱动入口
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING puszRegPathName)
{
        NTSTATUS ntStatus = STATUS_SUCCESS;
        HANDLE hProcessId = NULL;
        pDriverObject->DriverUnload = MyDriverUnload;
        CreateDevice(pDriverObject);
        for (ULONG64 uCount = 0; uCount < IRP_MJ_MAXIMUM_FUNCTION; uCount++)
        {
                pDriverObject->MajorFunction[uCount] = DispatchGeneral;
        }
        pDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DispatchContrl;
        return ntStatus;
}

//驱动卸载
VOID MyDriverUnload(PDRIVER_OBJECT pDriverObject)
{
        PDEVICE_EXTENTION pDeviceExtention = NULL;
        PDEVICE_OBJECT pFirstDeviceObject = NULL;
        pFirstDeviceObject = pDriverObject->DeviceObject;
        ASSERT(pFirstDeviceObject != NULL);
        pDeviceExtention = (PDEVICE_EXTENTION)pFirstDeviceObject->DeviceExtension;
        IoDeleteSymbolicLink(&pDeviceExtention->uszSymbolName);
        IoDeleteDevice(pDeviceExtention->pDeviceObject);
}

//创建设备对象 为了交互数据
NTSTATUS CreateDevice(PDRIVER_OBJECT pDriverObject)
{
        NTSTATUS ntStatus = STATUS_SUCCESS;
        UNICODE_STRING uszDeviceName = RTL_CONSTANT_STRING(L"\\Device\\ReadWriteMemery");
        UNICODE_STRING uszSymbolName = RTL_CONSTANT_STRING(L"\\??\\ReadWriteMemerySymbol");
        PDEVICE_OBJECT pDeviceObject = NULL;
        PDEVICE_EXTENTION pDeviceExtention = NULL;

        ntStatus = IoCreateDevice(pDriverObject, sizeof(DEVICE_EXTENTION), &uszDeviceName, 
                FILE_DEVICE_UNKNOWN, 0, TRUE, &pDeviceObject);
        if (!NT_SUCCESS(ntStatus))
        {
                KdPrint(("IoCreateDevice 错误:%x\n", ntStatus));
                return ntStatus;
        }
        ntStatus = IoCreateSymbolicLink(&uszSymbolName, &uszDeviceName);
        if (!NT_SUCCESS(ntStatus))
        {
                KdPrint(("IoCreateSymbolicLink 错误:%x\n", ntStatus));
                return ntStatus;
        }
        pDeviceExtention = (PDEVICE_EXTENTION)(pDeviceObject->DeviceExtension);
        pDeviceExtention->pDeviceObject = pDeviceObject;
        pDeviceExtention->uszDeviceName = uszDeviceName;
        pDeviceExtention->uszSymbolName = uszSymbolName;
        pDeviceObject->Flags |= DO_DIRECT_IO;

        return ntStatus;
}

//普通回调
NTSTATUS DispatchGeneral(PDEVICE_OBJECT pDeviceObject, PIRP pIrp)
{
        NTSTATUS ntStatus = STATUS_SUCCESS;
        pIrp->IoStatus.Information = 0;
        pIrp->IoStatus.Status = ntStatus;

        IoCompleteRequest(pIrp, IO_NO_INCREMENT);
        return ntStatus;
}

//控制回调
NTSTATUS DispatchContrl(PDEVICE_OBJECT pDeviceObject, PIRP pIrp)
{
        PIO_STACK_LOCATION pIoStack = NULL;
        NTSTATUS ntStatus = STATUS_SUCCESS;
        PDATA pData = NULL;
        pIoStack = IoGetCurrentIrpStackLocation(pIrp);
        ULONG uInLen = pIoStack->Parameters.DeviceIoControl.InputBufferLength;
        ULONG uCode = pIoStack->Parameters.DeviceIoControl.IoControlCode;
        switch (uCode)
        {
        case READ_WRITE_MEMERY:
        {
                PDATA pData = (PDATA)pIrp->AssociatedIrp.SystemBuffer;
                KdPrint(("%lld\n", pData->hProcessId));
                KdPrint(("%lld\n", *((DWORD64 *)pData->lpVirtualAddress)));
                ReadMemory(pData->hProcessId, pData->lpVirtualAddress);
                break;
        }
        default:
        {
                break;
        }
        }
        pIrp->IoStatus.Information = 0;
        pIrp->IoStatus.Status = ntStatus;
        IoCompleteRequest(pIrp, IO_NO_INCREMENT);
        return ntStatus;
}


免费评分

参与人数 1吾爱币 +3 热心值 +1 收起 理由
苏紫方璇 + 3 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

头像被屏蔽
mokson 发表于 2019-12-23 11:44
提示: 作者被禁止或删除 内容自动屏蔽
黄河大鲤鱼 发表于 2019-12-23 11:31
diyikuai 发表于 2019-12-23 12:33
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-16 19:50

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表