[+] 16进制数据通配符查找

This commit is contained in:
huiyadanli 2019-12-24 00:16:06 +08:00
parent a363d32e72
commit fa4b15d7d2
2 changed files with 112 additions and 6 deletions

View File

@ -0,0 +1,105 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace RevokeMsgPatcher.Assistant.Matcher
{
/// <summary>
/// 对16进制数据进行模糊查找
/// 通配符的方式
/// </summary>
public class FuzzyMatcher
{
public const byte wildcard = 0x3F; // 通配符
/// <summary>
/// 模糊匹配所有
/// </summary>
/// <param name="content">被查找对象</param>
/// <param name="pattern">查找串</param>
/// <returns></returns>
public static int[] MatchAll(byte[] content, byte[] pattern)
{
byte[] head = GetHead(pattern);
int[] indexs = BoyerMooreMatcher.MatchAll(content, head);
// 头串和查找串相同则直接返回,不同则继续判断是否符合查询串
if (head.Length == pattern.Length)
{
return indexs;
}
else
{
List<int> res = new List<int>();
foreach (int index in indexs)
{
if (IsEqual(content, index, pattern))
{
res.Add(index);
}
}
if (res.Count > 0)
{
return res.ToArray();
}
else
{
return null;
}
}
}
/// <summary>
/// 获取头串
/// </summary>
/// <param name="whole">完整查找串</param>
/// <returns></returns>
private static byte[] GetHead(byte[] whole)
{
int len = whole.Length;
for (int i = 0; i < whole.Length; i++)
{
if (whole[i] == wildcard)
{
len = i;
break;
}
}
if (len == 0)
{
throw new Exception("不正确的通配符位置!");
}
return whole.Take(len).ToArray();
}
/// <summary>
/// 确认整个查找串是否匹配
/// </summary>
/// <param name="content">被查找对象</param>
/// <param name="start">头串匹配位置</param>
/// <param name="whole">完整查找串</param>
/// <returns></returns>
private static bool IsEqual(byte[] content, int start, byte[] whole)
{
int i = 0;
for (i = 0; i < whole.Length; i++)
{
if (whole[i] == wildcard)
{
continue;
}
if (content[start + i] != whole[i])
{
break;
}
}
if (i == whole.Length)
{
return true;
}
else
{
return false;
}
}
}
}

View File

@ -69,6 +69,7 @@
</Compile>
<Compile Include="JsonData.cs" />
<Compile Include="Matcher\BoyerMooreMatcher.cs" />
<Compile Include="Matcher\FuzzyMatcher.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="FormAssisant.resx">
@ -97,12 +98,6 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\RevokeMsgPatcher\RevokeMsgPatcher.csproj">
<Project>{977bf781-ced8-4389-9404-0fa08fdf21df}</Project>
<Name>RevokeMsgPatcher</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
@ -110,5 +105,11 @@
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\RevokeMsgPatcher\RevokeMsgPatcher.csproj">
<Project>{977bf781-ced8-4389-9404-0fa08fdf21df}</Project>
<Name>RevokeMsgPatcher</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>