CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/external/source/byakugan/msfpattern.cpp
Views: 11766
1
#define DEBUG 0
2
3
#include <stdlib.h>
4
#include <memory.h>
5
6
#if DEBUG
7
#include <stdio.h>
8
#endif
9
10
void msf_pattern_create(int length, char *patternStr) {
11
char upper = 'A';
12
char lower = 'a';
13
char num = '0';
14
char c;
15
unsigned int i = 0;
16
17
while (i != length) {
18
switch (i%3) {
19
case 0:
20
c = upper;
21
break;
22
case 1:
23
c = lower;
24
break;
25
case 2:
26
c = num;
27
if (num++ == '9') {
28
num = '0';
29
if (lower++ == 'z') {
30
lower = 'a';
31
if (upper++ == 'Z') {
32
upper = 'A';
33
}
34
}
35
}
36
break;
37
}
38
patternStr[i] = c;
39
i++;
40
}
41
}
42
43
int msf_pattern_offset(int length, unsigned int needle) {
44
char *patternStr;
45
unsigned int *haystack;
46
47
if (length < 4)
48
return (-1);
49
50
patternStr = (char *) malloc(length+1);
51
if (patternStr == NULL) {
52
return (-1);
53
}
54
memset(patternStr, 0x00, length+1);
55
56
msf_pattern_create(length, patternStr);
57
58
length -= 4;
59
while (length >= 0) {
60
haystack = (unsigned int *) &patternStr[length];
61
//printf("Haystack: 0x%08x\n", *haystack);
62
if (needle == *haystack)
63
break;
64
length--;
65
}
66
67
free(patternStr);
68
return (length);
69
}
70
71
#if DEBUG
72
int main() {
73
char pattern[256];
74
char findme[] = "0Aa1";
75
76
memset(pattern, 0x00, 256);
77
msf_pattern_create(255, pattern);
78
79
printf("Pattern: %s\n", pattern);
80
printf("%s @ %d\n", findme, msf_pattern_offset(255, findme));
81
return (0);
82
}
83
#endif
84
85