This repository has been archived by the owner on Sep 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
sha1.cpp
154 lines (128 loc) · 4.1 KB
/
sha1.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#define _CRT_SECURE_NO_WARNINGS
#include "procfilter/procfilter.h"
#include <array>
#include <map>
#include <fstream>
#include <string>
#include <algorithm>
#include <cctype>
#include <set>
using std::string;
using std::wstring;
using std::ifstream;
using std::set;
typedef std::basic_string<BYTE> Hash;
static CRITICAL_SECTION g_CriticalSection;
static WCHAR g_WhitelistFile[MAX_PATH+1];
static WCHAR g_BlacklistFile[MAX_PATH+1];
static set<Hash> g_WhitelistHashes;
static set<Hash> g_BlacklistHashes;
static std::map<Hash,wstring> g_WhitelistAdditions;
#define MODE_BUILD_WHITELIST 0
#define MODE_CHECK_WHITELIST 1
static int g_RunningMode = MODE_BUILD_WHITELIST;
static bool g_bBlockFilesNotWhitelisted = false;
static
bool
LoadHashfile(PROCFILTER_EVENT *e, set<Hash> &c, const WCHAR *lpszFileName)
{
using std::getline;
bool rv = false;
ifstream infile(lpszFileName);
if (infile.fail()) {
using std::ofstream;
ofstream outfile(lpszFileName, ofstream::ate);
if (!outfile) return false;
outfile.close();
infile = ifstream(lpszFileName);
if (infile.fail()) return false;
}
string line;
while (getline(infile, line)) {
BYTE baRawDigest[20];
auto space_begin = std::remove_if(line.begin(), line.end(), [](char c){ return std::isspace(c); });
line.erase(space_begin, line.end());
if (line.length() >= 40) {
bool bSuccess = true;
for (size_t i = 0; i < 20; ++i) {
int value = 0;
if (sscanf(&line.c_str()[i*2], "%2x", &value) == 1) {
baRawDigest[i] = value & 0xFF;
} else {
bSuccess = false;
}
}
if (bSuccess) {
c.insert(Hash(baRawDigest));
}
}
}
return true;
}
static
void
SaveWhitelist(PROCFILTER_EVENT *e, const WCHAR *lpszFileName)
{
FILE *f = _wfopen(lpszFileName, L"a");
if (f) {
for (auto &v : g_WhitelistAdditions) {
char szDigest[41] = { '\0' };
for (size_t i = 0; i < 20; ++i) {
static const char *hex = "0123456789ABCDEF";
BYTE b = v.first[i];
szDigest[i*2] = hex[b >> 4];
szDigest[i*2+1] = hex[b & 0x0F];
}
fprintf(f, "%hs # %ls\n", szDigest, v.second.c_str());
}
fclose(f);
} else {
e->LogFmt("Unable to open \"%ls\" for writing", lpszFileName);
}
}
static
bool
Sha1InSet(PROCFILTER_EVENT *e, const set<Hash> &c, const Hash &hash)
{
return c.find(hash) != c.end();
}
DWORD
ProcFilterEvent(PROCFILTER_EVENT *e)
{
DWORD dwResultFlags = PROCFILTER_RESULT_NONE;
if (e->dwEventId == PROCFILTER_EVENT_INIT) {
e->RegisterPlugin(PROCFILTER_VERSION, L"Sha1", 0, 0, false,
PROCFILTER_EVENT_PROCESS_CREATE, PROCFILTER_EVENT_NONE);
InitializeCriticalSection(&g_CriticalSection);
g_RunningMode = e->GetConfigBool(L"BuildWhitelist", false) ? MODE_BUILD_WHITELIST : MODE_CHECK_WHITELIST;
g_bBlockFilesNotWhitelisted = e->GetConfigBool(L"BlockFilesNotWhitelisted", false);
e->GetProcFilterPath(g_WhitelistFile, sizeof(g_WhitelistFile), NULL, L"whitelist.txt");
if (g_RunningMode == MODE_CHECK_WHITELIST && !LoadHashfile(e, g_WhitelistHashes, g_WhitelistFile)) e->Die("Unable to load whitelist");
} else if (e->dwEventId == PROCFILTER_EVENT_SHUTDOWN) {
if (g_RunningMode == MODE_BUILD_WHITELIST) {
SaveWhitelist(e, g_WhitelistFile);
}
DeleteCriticalSection(&g_CriticalSection);
} else if (e->dwEventId == PROCFILTER_EVENT_PROCESS_CREATE && e->lpszFileName) {
HASHES hashes;
bool bFileHashed = e->HashFile(e->lpszFileName, 0, &hashes);
if (g_RunningMode == MODE_BUILD_WHITELIST) {
if (bFileHashed && !Sha1InSet(e, g_WhitelistHashes, Hash(hashes.sha1_digest, 20))) {
EnterCriticalSection(&g_CriticalSection);
g_WhitelistAdditions.insert(std::make_pair(Hash(hashes.sha1_digest, 20), wstring(e->lpszFileName)));
LeaveCriticalSection(&g_CriticalSection);
}
} else if (g_RunningMode == MODE_CHECK_WHITELIST) {
bool bSha1Whitelisted = false;
if (bFileHashed) bSha1Whitelisted = Sha1InSet(e, g_WhitelistHashes, Hash(hashes.sha1_digest, 20));
if (g_bBlockFilesNotWhitelisted) {
if (!bFileHashed || !bSha1Whitelisted) {
dwResultFlags |= PROCFILTER_RESULT_BLOCK_PROCESS;
}
} else {
if (bSha1Whitelisted) dwResultFlags |= PROCFILTER_RESULT_DONT_SCAN;
}
}
}
return dwResultFlags;
}