-
Notifications
You must be signed in to change notification settings - Fork 6
/
gargoyle_regex_tester.cpp
306 lines (227 loc) · 7.27 KB
/
gargoyle_regex_tester.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*****************************************************************************
*
* GARGOYLE_PSCAND: Gargoyle - Protection for Linux
*
* Program to test a given regex against a set of log data
*
* Copyright (c) 2017 - 2018, Bayshore Networks, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that
* the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#include <iostream>
#include <string>
#include <fstream>
#include <regex>
#include <vector>
#include <algorithm>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include "config.h"
#ifdef USE_LIBPCRECPP
#include <pcrecpp.h>
#else
#include <regex>
#endif
#include "system_functions.h"
//std::vector<std::string> sshd_regexes;
size_t line_sz = 2048;
size_t line_cnt = 0;
size_t regex_num_hits = 0;
size_t valid_ip_addr_cnt = 0;
std::vector<std::string> valid_ip_addr;
bool validate_ip_address(const std::string &);
bool valid_ip_addr_exists(const std::string &);
std::string hunt_for_ip_addr(std::string);
#ifndef USE_LIBPCRECPP
void do_regex_check(const std::string &, const std::regex &);
#else
void do_regex_check(const std::string &line, pcrecpp::RE &);
#endif
void add_valid_ip_addr(const std::string &);
void display_valid_ip_addr();
bool validate_ip_address(const std::string &ip_address) {
struct sockaddr_in sa;
int result = inet_pton(AF_INET, ip_address.c_str(), &(sa.sin_addr));
return result != 0;
}
bool valid_ip_addr_exists(const std::string& ip_addr) {
if(std::find(valid_ip_addr.cbegin(), valid_ip_addr.cend(), ip_addr) != valid_ip_addr.cend())
return true;
return false;
}
std::string hunt_for_ip_addr(const std::string &line, const char& c) {
std::string resp = "";
std::string buff{""};
for(auto n:line)
{
if(n != c) {
buff+=n;
} else {
if(n == c && buff != "") {
if (validate_ip_address(buff))
return buff;
buff = "";
}
}
}
return resp;
}
#ifndef USE_LIBPCRECPP
void do_regex_check(const std::string &line, const std::regex &l_regex) {
#else
void do_regex_check(const std::string &line, pcrecpp::RE &l_regex) {
#endif
std::string ip_addr;
line_cnt++;
#ifndef USE_LIBPCRECPP
std::smatch match;
if (std::regex_search(line, match, l_regex)) {
#else
if (l_regex.PartialMatch(line, &ip_addr)) {
#endif
regex_num_hits++;
/*
std::cout << "MATCH SIZE: " << match.size() << std::endl;
std::cout << "MATCH 0: " << match.str(0) << std::endl;
std::cout << "MATCH 1: " << match.str(1) << std::endl;
*/
#ifndef USE_LIBPCRECPP
ip_addr = match.str(1);
#endif
//std::cout << "IP: " << ip_addr << std::endl;
if (validate_ip_address(ip_addr)) {
//std::cout << "VALID IP ADDR MATCH: " << ip_addr << std::endl;
add_valid_ip_addr(ip_addr);
valid_ip_addr_cnt++;
} else {
std::string hip = hunt_for_ip_addr(ip_addr, ' ');
// the hack found an ip addr
if (hip.size()) {
//std::cout << "HIP IP ADDR MATCH: " << hip << std::endl;
if (validate_ip_address(hip)) {
//std::cout << "VALID IP ADDR MATCH [HIP]: " << hip << std::endl;
add_valid_ip_addr(hip);
valid_ip_addr_cnt++;
}
}
}
} else {
//std::cout << "No regex hit" << std::endl;
}
//std::cout << std::endl;
}
void add_valid_ip_addr(const std::string &ip_addr) {
if (!valid_ip_addr_exists(ip_addr))
valid_ip_addr.push_back(ip_addr);
}
void display_valid_ip_addr() {
std::cout << "Unique list of valid IP ADDRs discovered:" << std::endl << "-----------------" << std::endl;
for (auto& it : valid_ip_addr) {
std::cout << it << std::endl;
}
}
int main(int argc, char *argv[]) {
std::string log_entity = "";
std::string regex_str = "";
int c;
bool using_file = false;
while ((c = getopt(argc, argv, "r:l:")) != -1)
switch (c) {
case 'r':
regex_str = optarg;
break;
case 'l':
log_entity = optarg;
break;
case '?':
std::cout << std::endl << "Problem with args dude ..." << std::endl << std::endl;
break;
}
/*
std::cout << std::endl;
std::cout << log_entity << std::endl;
std::cout << regex_str << std::endl;
std::cout << std::endl;
*/
if (does_file_exist(log_entity.c_str())) {
using_file = true;
}
// if file ....
//std::ifstream ifs(log_entity.c_str(), std::ios::ate);
// remember file position
//std::ios::streampos gpos = ifs.tellg();
std::string line;
#ifndef USE_LIBPCRECPP
try {
std::regex l_regex(regex_str);
#else
pcrecpp::RE l_regex(regex_str);
#endif
// working with one target string, not file
if (!using_file) {
do_regex_check(log_entity, l_regex);
} else {
// iterate on file line by line
std::ifstream inf(log_entity);
if(!inf) {
std::cout << std::endl << "Cannot open input file: " << log_entity << std::endl << std::endl;
return 1;
}
char str[line_sz];
while(inf) {
inf.getline(str, line_sz);
if(inf) {
do_regex_check(str, l_regex);
//std::cout << str << std::endl;
}
}
inf.close();
}
#ifndef USE_LIBPCRECPP
} catch (std::regex_error& e) {
std::cout << std::endl << "Regex exception: " << e.what() << std::endl;
std::cout << "Regex exception code is: " << e.code() << std::endl;
std::cout << "Cannot continue ..." << std::endl << std::endl;
return 1;
}
#endif
std::cout << std::endl << "Results" << std::endl << "=======" << std::endl << std::endl;
std::cout << "Entity scanned is a ";
if (using_file)
std::cout << "file named ";
else
std::cout << "string: ";
std::cout << "\"" << log_entity << "\"" << std::endl;
if (using_file) {
std::cout << line_cnt << " lines were consumed and scanned" << std::endl;
}
std::cout << "Regex used: " << regex_str << std::endl;
std::cout << "Number of hits for this regex: " << regex_num_hits << std::endl;
std::cout << "Number of valid IP ADDRs from regex hits: " << valid_ip_addr_cnt << std::endl;
std::cout << std::endl;
if (valid_ip_addr.size()) {
display_valid_ip_addr();
}
std::cout << std::endl << std::endl;
return 0;
}