首页
社区
课程
招聘
[原创]C/C++ 文件特征码定位搜索方法
发表于: 2022-7-21 14:27 9384

[原创]C/C++ 文件特征码定位搜索方法

2022-7-21 14:27
9384

同文在吾爱也分享了一份,祝各位大佬生活每天开心,代码有问题请指出。

#include <iostream>
#include <string>
#include <array>
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <immintrin.h>
 
namespace test
{
    using s8  = int8_t;
    using s16 = int16_t;
    using s32 = int32_t;
    using s64 = int64_t;
 
    using u8  = uint8_t;
    using u16 = uint16_t;
    using u32 = uint32_t;
    using u64 = uint64_t;
 
    using s96 = intptr_t;
    using u96 = uintptr_t;
 
    static unsigned char hex[] = { 0xE9, 0x3B, 0x9A, 0x0A, 0x00, 0xE9, 0x96, 0xBC, 0x05, 0x00, 0xE9, 0xA1, 0x56, 0x07, 0x00, 0xE9, 0x2C, 0x3A, 0x05, 0x00, 0xE9, 0xB7, 0x57, 0x07, 0x00, 0xE9, 0x72, 0x77, 0x04, 0x00, 0xE9, 0xDD, 0xAD, 0x08, 0x00, 0xE9, 0xF8, 0xDF, 0x06, 0x00, 0xE9, 0xE3, 0xBB, 0x01, 0x00, 0xE9, 0x4E, 0xDC, 0x03, 0x00, 0xE9, 0x49, 0xD6, 0x03, 0x00, 0xE9, 0xE4, 0x3F, 0x03, 0x00, 0xE9, 0x8F, 0x7C, 0x01, 0x00, 0xE9, 0xFA, 0xCD, 0x08, 0x00, 0xE9, 0x05, 0xEA, 0x05, 0x00, 0xE9, 0xA0, 0xB3, 0x03, 0x00, 0xE9, 0x9B, 0xEC, 0x04, 0x00, 0xE9, 0x16, 0x2E, 0x05, 0x00, 0xE9, 0xA1, 0x33, 0x0C, 0x00, 0xE9, 0x1C, 0xB0, 0x0C, 0x00, 0xE9, 0xE7, 0x08, 0x04, 0x00, 0xE9, 0xB2, 0xC4, 0x05, 0x00, 0xE9, 0xED, 0x66, 0x02, 0x00, 0xE9, 0x48, 0xCC, 0x09, 0x00, 0xE9, 0xC3, 0xB8, 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 0x02, 0x00, 0xE9, 0x9E, 0xD5, 0x03, 0x00, 0xE9, 0x29, 0x55, 0x07, 0x00, 0xE9, 0xE4, 0xE1, 0x07, 0x00, 0xE9, 0x0F, 0xDD, 0x07, 0x00, 0xE9, 0x6A, 0xDE, 0x07, 0x00, 0xE9, 0xF5, 0x73, 0x0D, 0x00, 0xE9, 0xF0, 0xC6, 0x04, 0x00, 0xE9, 0xFB, 0xD4, 0x0A, 0x00, 0xE9, 0x26, 0xE9, 0x02, 0x00, 0xE9, 0x41, 0x30, 0x05, 0x00 };
 
    static constexpr char charhexset[] = "0123456789ABCDEFabcdef";
 
    static bool StringReplace(std::string& source, const std::string& delimiters, const std::string& dispose = "", const std::size_t offset = 1)
    {
        if (source.empty())
        {
            return false;
        }
 
        if (delimiters.empty())
        {
            return true;
        }
 
        for (std::string::size_type pos = source.find(delimiters); pos != std::string::npos; pos = source.find(delimiters))
        {
            if (source.replace(pos, offset, dispose).size())
            {
                continue;
            }
            return false;
        }
 
        return true;
    }
 
    static bool Hexadec2xdigit(const std::string& data, std::string& buffer, std::size_t offset)
    {
        if (data.empty())
        {
            return false;
        }
 
        for (std::size_t i = 0; i < data.size(); i += offset)
        {
            if (isxdigit(data[i]))
            {
                buffer += static_cast<char>(std::stoul(data.substr(i, offset), nullptr, 16));
            }
            else
            {
                buffer += 'X';
            }
        }
 
        return true;
    }
 
 
    static bool Hexadec2xdigitEx(const std::string& data, std::string& buffer, u32 checkxdigit = 0, u32 transform = 1)
    {
        if (data.empty())
        {
            return false;
        }
 
        std::string masks(data);
        {
            // 去掉 0x 去掉 空格
            if (StringReplace(masks, "0x") && StringReplace(masks, " "))
            {
                // 大小写转换
                if (masks.end() == (1 == transform ? std::transform(masks.begin(), masks.end(), masks.begin(), [] (unsigned char ch) { return toupper(ch); }) : std::transform(masks.begin(), masks.end(), masks.begin(), [] (unsigned char ch) { return tolower(ch); })))
                {
                    // 检查是否是完整的十六进制数
                    if (checkxdigit)
                    {
                        if (std::string::npos != masks.find_first_not_of(charhexset))
                        {
                            return false;
                        }
                    }
 
                    return Hexadec2xdigit(static_cast<const std::string&>(masks), buffer, 2);
                }
            }
        }
 
        return false;
    }
 
    static bool SearchPattern(const u8* pos, const std::string& chars)
    {
        for (std::string::const_iterator xdigit = chars.cbegin(); xdigit != chars.cend(); ++xdigit, ++pos)
        {
            if (*pos != static_cast<const u8>(*xdigit) &&           // no match
                'X' != static_cast<const u8>(*xdigit))              // filter out arbitrary characters
            {
                return false;
            }
            else
            {
                // hit character
                continue;
            }
        }
 
        return true;
    }
 
    static bool SearchPattern(const u8* start,                      // start address
                              const u8* end,                        // end address
                              const std::string& keyword,           // characteristic code
                              std::size_t index,                    // take out the serial number
                              std::size_t offset,                   // pointer offset
                              void*& address)                       // return to address
    {
        if (keyword.empty())
        {
            return true;
        }
 
        if (start != end && end > start && static_cast<std::size_t>(end - start) > keyword.size())
        {
            std::string chars;
            {
                if (Hexadec2xdigitEx(keyword, chars, 0))
                {
                    for (auto [pos, i] = std::make_tuple(start, static_cast<std::size_t>(0)); pos <= end; ++pos)
                    {
                        if (SearchPattern(pos, chars))
                        {
                            if (++i != index)
                            {
                                continue;
                            }
                            else
                            {
                                address = reinterpret_cast<void*>(const_cast<u8*>(reinterpret_cast<const u8*>(pos)) + offset);
                            }
 
                            return false;
                        }
                    }
                }
            }
        }
 
        return true;
    }
 
    template <typename Type = void*>
    static bool SearchPattern(const u96 start,                      // start address
                              const u96 end,                        // end address
                              const std::string& keyword,           // characteristic code
                              std::size_t index,                    // take out the serial number
                              std::size_t offset,                   // pointer offset
                              Type& address)                        // number of characteristic multiples
    {
        return SearchPattern(reinterpret_cast<const u8*>(start), reinterpret_cast<const u8*>(end), keyword, index, offset, reinterpret_cast<void*&>(address));
    }
 
    static bool SearchPatternEx(const u8* start,                    // start address
                                const u8* end,                      // end address
                                const std::string& chars,           // characteristic code
                                std::size_t index,                  // take out the serial number
                                std::size_t offset,                 // pointer offset
                                void*& address,                     // return to address
                                std::size_t count)                  // number of characteristic multiples
    {
        for (auto [masks, i, n, pos] = std::make_tuple(std::array<std::size_t, 32>{}, static_cast<std::size_t>(0), static_cast<std::size_t>(0), start); i < count; ++i)
        {
            for (std::size_t j = 0; j < 16 && n < chars.size(); ++j, ++n)
            {
                if ('X' != reinterpret_cast<const s8*>(chars.c_str() + i * 16)[j])
                {
                    masks[i] |= static_cast<std::size_t>(1) << j;
                }
            }
 
            for (auto [store, j] = std::make_tuple(_mm_loadu_si128(reinterpret_cast<const __m128i *>(chars.c_str() + i * 16)), static_cast<std::size_t>(0)); pos <= end; _mm_prefetch(reinterpret_cast<const char*>(++pos + 64), _MM_HINT_NTA))
            {
                if (static_cast<std::size_t>(static_cast<std::size_t>(_mm_movemask_epi8(_mm_cmpeq_epi8(_mm_loadu_si128(reinterpret_cast<const __m128i *>(pos + i * 16)), store))) & masks[i]) == masks[i])
                {
                    if (n < chars.size())
                    {
                        break;
                    }
 
                    if (++j != index)
                    {
                        continue;
                    }
                    else
                    {
                        address = reinterpret_cast<void*>(const_cast<u8*>(reinterpret_cast<const u8*>(pos)) + offset);
                    }
 
                    return true;
                }
            }
        }
 
        return false;
    }
 
    static bool SearchPatternEx(const u8* start,                    // start address
                                const u8* end,                      // end address
                                const std::string& keyword,         // characteristic code
                                std::size_t index,                  // take out the serial number
                                std::size_t offset,                 // pointer offset
                                void*& address)                     // return to address
    {
        if (keyword.empty())
        {
            return false;
        }
 
        if (start != end && end > start && static_cast<std::size_t>(end - start) > keyword.size())
        {
            std::string chars;
            {
                if (Hexadec2xdigitEx(keyword, chars, 0))
                {
                    return SearchPatternEx(start, end, chars, index, offset, address, static_cast<std::size_t>((((chars.size()) + (16 - 1)) & ~(16 - 1)) >> 4));
                }
            }
        }
 
        return false;
    }
 
    template <typename Type = void*>
    static bool SearchPatternEx(u96 start,                          // start address
                                u96 end,                            // end address
                                const std::string& keyword,         // characteristic code
                                std::size_t index,                  // take out the serial number
                                std::size_t offset,                 // pointer offset
                                Type& address)                      // return to address
    {
        return SearchPatternEx(reinterpret_cast<const u8*>(start), reinterpret_cast<const u8*>(end), keyword, index, offset, reinterpret_cast<void*&>(address));
    }
}
 
 
int main()
{
    std::string string_hex = "E9 C3 8B ?? ?? E9 ?? 01 0A 00 E9 ?? 4A 02 ?? E9 ?? 22 ?? ?? ?? FF 75 04 00 E9 2A EB 07 ?? E9 ?? 30 ?? 00 ?? ?? 50";
 
    void* address = 0;
 
    test::SearchPatternEx(test::hex, test::hex + sizeof(test::hex), string_hex, 1, 0, address);
 
    printf("%p\n", address);
 
    return 0;
}
#include <iostream>
#include <string>
#include <array>
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <immintrin.h>
 
namespace test
{
    using s8  = int8_t;
    using s16 = int16_t;
    using s32 = int32_t;
    using s64 = int64_t;
 
    using u8  = uint8_t;
    using u16 = uint16_t;
    using u32 = uint32_t;
    using u64 = uint64_t;
 
    using s96 = intptr_t;
    using u96 = uintptr_t;
 
    static unsigned char hex[] = { 0xE9, 0x3B, 0x9A, 0x0A, 0x00, 0xE9, 0x96, 0xBC, 0x05, 0x00, 0xE9, 0xA1, 0x56, 0x07, 0x00, 0xE9, 0x2C, 0x3A, 0x05, 0x00, 0xE9, 0xB7, 0x57, 0x07, 0x00, 0xE9, 0x72, 0x77, 0x04, 0x00, 0xE9, 0xDD, 0xAD, 0x08, 0x00, 0xE9, 0xF8, 0xDF, 0x06, 0x00, 0xE9, 0xE3, 0xBB, 0x01, 0x00, 0xE9, 0x4E, 0xDC, 0x03, 0x00, 0xE9, 0x49, 0xD6, 0x03, 0x00, 0xE9, 0xE4, 0x3F, 0x03, 0x00, 0xE9, 0x8F, 0x7C, 0x01, 0x00, 0xE9, 0xFA, 0xCD, 0x08, 0x00, 0xE9, 0x05, 0xEA, 0x05, 0x00, 0xE9, 0xA0, 0xB3, 0x03, 0x00, 0xE9, 0x9B, 0xEC, 0x04, 0x00, 0xE9, 0x16, 0x2E, 0x05, 0x00, 0xE9, 0xA1, 0x33, 0x0C, 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 0x00, 0xE9, 0x3C, 0x9A, 0x02, 0x00, 0xE9, 0x27, 0x54, 0x07, 0x00, 0xE9, 0xA2, 0xE5, 0x07, 0x00, 0xE9, 0x0D, 0xE7, 0x0C, 0x00, 0xE9, 0x28, 0x2A, 0x08, 0x00, 0xE9, 0x33, 0x5A, 0x02, 0x00, 0xE9, 0x9E, 0xD5, 0x03, 0x00, 0xE9, 0x29, 0x55, 0x07, 0x00, 0xE9, 0xE4, 0xE1, 0x07, 0x00, 0xE9, 0x0F, 0xDD, 0x07, 0x00, 0xE9, 0x6A, 0xDE, 0x07, 0x00, 0xE9, 0xF5, 0x73, 0x0D, 0x00, 0xE9, 0xF0, 0xC6, 0x04, 0x00, 0xE9, 0xFB, 0xD4, 0x0A, 0x00, 0xE9, 0x26, 0xE9, 0x02, 0x00, 0xE9, 0x41, 0x30, 0x05, 0x00 };
 
    static constexpr char charhexset[] = "0123456789ABCDEFabcdef";
 
    static bool StringReplace(std::string& source, const std::string& delimiters, const std::string& dispose = "", const std::size_t offset = 1)
    {
        if (source.empty())
        {
            return false;
        }
 
        if (delimiters.empty())
        {
            return true;
        }
 
        for (std::string::size_type pos = source.find(delimiters); pos != std::string::npos; pos = source.find(delimiters))
        {
            if (source.replace(pos, offset, dispose).size())
            {
                continue;
            }
            return false;
        }
 
        return true;
    }
 
    static bool Hexadec2xdigit(const std::string& data, std::string& buffer, std::size_t offset)
    {
        if (data.empty())
        {
            return false;
        }
 
        for (std::size_t i = 0; i < data.size(); i += offset)
        {
            if (isxdigit(data[i]))
            {
                buffer += static_cast<char>(std::stoul(data.substr(i, offset), nullptr, 16));
            }
            else
            {
                buffer += 'X';
            }
        }
 
        return true;
    }
 
 
    static bool Hexadec2xdigitEx(const std::string& data, std::string& buffer, u32 checkxdigit = 0, u32 transform = 1)
    {
        if (data.empty())
        {
            return false;
        }
 
        std::string masks(data);
        {
            // 去掉 0x 去掉 空格
            if (StringReplace(masks, "0x") && StringReplace(masks, " "))
            {
                // 大小写转换
                if (masks.end() == (1 == transform ? std::transform(masks.begin(), masks.end(), masks.begin(), [] (unsigned char ch) { return toupper(ch); }) : std::transform(masks.begin(), masks.end(), masks.begin(), [] (unsigned char ch) { return tolower(ch); })))
                {
                    // 检查是否是完整的十六进制数
                    if (checkxdigit)
                    {
                        if (std::string::npos != masks.find_first_not_of(charhexset))
                        {
                            return false;
                        }
                    }
 
                    return Hexadec2xdigit(static_cast<const std::string&>(masks), buffer, 2);
                }
            }
        }
 
        return false;
    }
 
    static bool SearchPattern(const u8* pos, const std::string& chars)
    {
        for (std::string::const_iterator xdigit = chars.cbegin(); xdigit != chars.cend(); ++xdigit, ++pos)
        {
            if (*pos != static_cast<const u8>(*xdigit) &&           // no match
                'X' != static_cast<const u8>(*xdigit))              // filter out arbitrary characters
            {
                return false;
            }
            else
            {
                // hit character
                continue;
            }
        }
 
        return true;
    }
 
    static bool SearchPattern(const u8* start,                      // start address
                              const u8* end,                        // end address
                              const std::string& keyword,           // characteristic code
                              std::size_t index,                    // take out the serial number
                              std::size_t offset,                   // pointer offset
                              void*& address)                       // return to address
    {
        if (keyword.empty())
        {
            return true;
        }

[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!

最后于 2022-7-21 14:35 被intlinfo编辑 ,原因:
收藏
免费 3
支持
分享
最新回复 (4)
雪    币: 1025
活跃值: (633)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
感谢分享
2022-7-21 17:05
0
雪    币: 33
活跃值: (113)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
这是干嘛的?
2022-7-26 16:22
0
雪    币: 212
活跃值: (217)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
萨格拉斯 这是干嘛的?
内存、文件特征码搜索定位算法
2022-8-11 10:52
0
雪    币: 2239
活跃值: (1109)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
mark
2022-8-17 13:04
0
游客
登录 | 注册 方可回帖
返回
//