forked from DaxxTrias/Process.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDwordPattern.cs
More file actions
68 lines (59 loc) · 2.24 KB
/
DwordPattern.cs
File metadata and controls
68 lines (59 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace ProcessNET.Patterns
{
public class DwordPattern : IMemoryPattern
{
private readonly byte[] _bytes;
private readonly string _mask;
public readonly string PatternText;
public int SearchStartOffset { get; set; }
public MemoryPatternType PatternType { get; }
public PatternScannerAlgorithm Algorithm { get; }
public DwordPattern(string dwordPattern, int startOffset = 0, PatternScannerAlgorithm algorithm = PatternScannerAlgorithm.Naive)
{
this.PatternType = MemoryPatternType.Function;
this.Algorithm = algorithm;
this.SearchStartOffset = startOffset;
PatternText = dwordPattern;
_bytes = GetBytesFromDwordPattern(dwordPattern);
_mask = GetMaskFromDwordPattern(dwordPattern);
}
public DwordPattern(byte[] pattern, int startOffset = 0, PatternScannerAlgorithm algorithm = PatternScannerAlgorithm.Naive)
{
this.PatternType = MemoryPatternType.Function;
this.Algorithm = algorithm;
this.SearchStartOffset = startOffset;
PatternText = string.Join(" ", pattern.Select(o => o.ToString("X2")));
_bytes = new byte[pattern.Length];
for (int i = 0; i < _bytes.Length; i++)
_bytes[i] = pattern[i];
_mask = new string(Enumerable.Repeat<char>('x', _bytes.Length).ToArray());
}
public IList<byte> GetBytes()
{
return _bytes;
}
public string GetMask()
{
return _mask;
}
private static string GetMaskFromDwordPattern(string pattern)
{
var mask = pattern.Split(' ').Select(s => s.Contains('?') ? "?" : "x");
return string.Concat(mask);
}
private static byte[] GetBytesFromDwordPattern(string pattern)
{
return
pattern.Split(' ')
.Select(s => s.Contains('?') ? (byte) 0 : byte.Parse(s, NumberStyles.HexNumber))
.ToArray();
}
public override string ToString()
{
return PatternText;
}
}
}