吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1250|回复: 33
收起左侧

[求助] 各位老师,网上找到一篇C++源码,编译出现如下错误,是啥原因呢?

[复制链接]
jtwc 发表于 2022-4-22 09:10
本帖最后由 jtwc 于 2022-4-22 09:12 编辑

各位老师,网上找到一篇C++源码,编译出现如下错误,是啥原因呢?如何修改呢?谢谢了
[C++] 纯文本查看 复制代码
#include<Windows.h>
#include<iostream>
#include<vector>
#include<time.h>
  
using namespace std;
  
#define BLOCKMAXSIZE 409600//每次读取内存的最大大小
BYTE* MemoryData;//每次将读取的内存读入这里
short Next[260];
  
//特征码转字节集
WORD GetTzmArray(char* Tzm, WORD* TzmArray)
{
    int len = 0;
    WORD TzmLength = strlen(Tzm) / 3 + 1;
  
    for (int i = 0; i < strlen(Tzm); )//将十六进制特征码转为十进制
    {
        char num[2];
        num[0] = Tzm[i++];
        num[1] = Tzm[i++];
        i++;
        if (num[0] != '?' && num[1] != '?')
        {
            int sum = 0;
            WORD a[2];
            for (int i = 0; i < 2; i++)
            {
                if (num[i] >= '0' && num[i] <= '9')
                {
                    a[i] = num[i] - '0';
                }
                else if (num[i] >= 'a' && num[i] <= 'z')
                {
                    a[i] = num[i] - 87;
                }
                else if (num[i] >= 'A' && num[i] <= 'Z')
                {
                    a[i] = num[i] - 55;
                }
  
            }
            sum = a[0] * 16 + a[1];
            TzmArray[len++] = sum;
        }
        else
        {
            TzmArray[len++] = 256;
        }
    }
    return TzmLength;
}
  
//获取Next数组
void GetNext(short* next, WORD* Tzm, WORD TzmLength)
{
    //特征码(字节集)的每个字节的范围在0-255(0-FF)之间,256用来表示问号,到260是为了防止越界
    for (int i = 0; i < 260; i++)
        next[i] = -1;
    for (int i = 0; i < TzmLength; i++)
        next[Tzm[i]] = i;
}
  
//搜索一块内存
void SearchMemoryBlock(HANDLE hProcess, WORD* Tzm, WORD TzmLength, unsigned __int64 StartAddress, unsigned long size, vector<unsigned __int64>& ResultArray)
{
    if (!ReadProcessMemory(hProcess, (LPCVOID)StartAddress, MemoryData, size, NULL))
    {
        return;
    }
  
    for (int i = 0, j, k; i < size;)
    {
        j = i; k = 0;
  
        for (; k < TzmLength && j < size && (Tzm[k] == MemoryData[j] || Tzm[k] == 256); k++, j++);
  
        if (k == TzmLength)
        {
            ResultArray.push_back(StartAddress + i);
        }
  
        if ((i + TzmLength) >= size)
        {
            return;
        }
  
        int num = Next[MemoryData[i + TzmLength]];
        if (num == -1)
            i += (TzmLength - Next[256]);//如果特征码有问号,就从问号处开始匹配,如果没有就i+=-1
        else
            i += (TzmLength - num);
    }
}
  
//搜索整个程序
int SearchMemory(HANDLE hProcess, char* Tzm, unsigned __int64 StartAddress, unsigned __int64 EndAddress, int InitSize, vector<unsigned __int64>& ResultArray)
{
    int i = 0;
    unsigned long BlockSize;
    MEMORY_BASIC_INFORMATION mbi;
  
    WORD TzmLength = strlen(Tzm) / 3 + 1;
    WORD* TzmArray = new WORD[TzmLength];
  
    GetTzmArray(Tzm, TzmArray);
    GetNext(Next, TzmArray, TzmLength);
  
    //初始化结果数组
    ResultArray.clear();
    ResultArray.reserve(InitSize);
  
    while (VirtualQueryEx(hProcess, (LPCVOID)StartAddress, &mbi, sizeof(mbi)) != 0)
    {
        //获取可读可写和可读可写可执行的内存块
        if (mbi.Protect == PAGE_READWRITE || mbi.Protect == PAGE_EXECUTE_READWRITE)
        {
            i = 0;
            BlockSize = mbi.RegionSize;
            //搜索这块内存
            while (BlockSize >= BLOCKMAXSIZE)
            {
                SearchMemoryBlock(hProcess, TzmArray, TzmLength, StartAddress + (BLOCKMAXSIZE * i), BLOCKMAXSIZE, ResultArray);
                BlockSize -= BLOCKMAXSIZE; i++;
            }
            SearchMemoryBlock(hProcess, TzmArray, TzmLength, StartAddress + (BLOCKMAXSIZE * i), BlockSize, ResultArray);
  
        }
        StartAddress += mbi.RegionSize;
  
        if (EndAddress != 0 && StartAddress > EndAddress)
        {
            return ResultArray.size();
        }
    }
    free(TzmArray);
    return ResultArray.size();
}
  
int main()
{
    //初始化MemoryData大小
    MemoryData = new BYTE[BLOCKMAXSIZE];
  
    DWORD pid=0;
    vector<unsigned __int64> ResultArray;
     
    cout << "请输入进程ID:" << endl;
    cin >> pid;
  
    //通过进程ID获取进程句柄
    HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
  
    int start = clock();
    SearchMemory(hProcess, (char*)"FF 3F FF ?? FF F2", 0x410000, 0xFFFFFFFF, 30, ResultArray);
    int end = clock();
  
    cout << "用时:" << end-start << "毫秒"<<endl;
    cout << "搜索到" << ResultArray.size() << "个结果" << endl;
  
    for (vector<unsigned __int64>::iterator it = ResultArray.begin(); it != ResultArray.end(); it++)
    {
        printf("%x\n", *it);
    }
  
    return 0;
}
1.png
1.png

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

HULANG-BTB 发表于 2022-4-22 09:23
兄弟,写入错误无非就是数组越界。调试检查一下你Tzm数组里面的值是不是都小于next数组的大小就得了
JuncoJet 发表于 2022-4-22 09:23
mac52pojie 发表于 2022-4-22 09:33
本帖最后由 mac52pojie 于 2022-4-22 09:35 编辑

num[0] = Tzm[i++];
        num[1] = Tzm[i++];

这两句就应该报错了吧,这里已经数组越界了。

你截图的地方应该也是数组越界,你调试的时候可以看到下标应该大于260了。
 楼主| jtwc 发表于 2022-4-22 09:40
mac52pojie 发表于 2022-4-22 09:33
num[0] = Tzm;
        num[1] = Tzm;

老师,经过调试这个地方还真没有问题?
 楼主| jtwc 发表于 2022-4-22 09:44
JuncoJet 发表于 2022-4-22 09:23
Tzm 数据有问题       导致Tzm或者next越界

老师,如何修改呢?
★〓★〓★ 发表于 2022-4-22 09:45
VS2022 windows sdk 10.0.20348 直接编译运行成功,没有错误。
 楼主| jtwc 发表于 2022-4-22 09:48
★〓★〓★ 发表于 2022-4-22 09:45
VS2022 windows sdk 10.0.20348 直接编译运行成功,没有错误。

老师,多试几次,
★〓★〓★ 发表于 2022-4-22 09:48
jtwc 发表于 2022-4-22 09:48
老师,多试几次,

试了很多次了,没有崩溃,只不过搜不到数据而已,结果都是0
HULANG-BTB 发表于 2022-4-22 09:50
WORD TzmLength = strlen(Tzm) / 3 + 1;  --->  WORD TzmLength = (strlen(Tzm) + 1) / 3;
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

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

GMT+8, 2024-11-25 12:53

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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