| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
#ifdef HAVE_CONFIG_H |
|---|
| 25 |
#include "config.h" |
|---|
| 26 |
#endif |
|---|
| 27 |
|
|---|
| 28 |
#include <string.h> |
|---|
| 29 |
#include <errno.h> |
|---|
| 30 |
|
|---|
| 31 |
#include "sf_snort_packet.h" |
|---|
| 32 |
#include "sf_dynamic_preprocessor.h" |
|---|
| 33 |
#include "sf_snort_plugin_api.h" |
|---|
| 34 |
#include "preprocids.h" |
|---|
| 35 |
|
|---|
| 36 |
#include "spp_pehunter.h" |
|---|
| 37 |
#include "pehunter.h" |
|---|
| 38 |
|
|---|
| 39 |
#include "profiler.h" |
|---|
| 40 |
#ifdef PERF_PROFILING |
|---|
| 41 |
PreprocStats pehuntPerfStats; |
|---|
| 42 |
PreprocStats pehuntDetectPerfStats; |
|---|
| 43 |
int pehuntDetectCalled = 0; |
|---|
| 44 |
#endif |
|---|
| 45 |
|
|---|
| 46 |
#define CONF_SEPARATORS " \t\n\r" |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
extern DynamicPreprocessorData _dpd; |
|---|
| 50 |
|
|---|
| 51 |
static void PEHunterInit(u_char *); |
|---|
| 52 |
void PEHunter_ParseArgs(u_char *args); |
|---|
| 53 |
static void PrintPEHunterConfig(void); |
|---|
| 54 |
static void FreePEHunterConfig(void); |
|---|
| 55 |
static void HuntPE(void *, void *context); |
|---|
| 56 |
static void PEHunterCleanExitFunction(int, void *); |
|---|
| 57 |
static void PEHunterRestartFunction(int, void *); |
|---|
| 58 |
static void HuntPE(void *pkt, void *context); |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
void SetupPEHunter() |
|---|
| 74 |
{ |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
_dpd.registerPreproc("pehunter", PEHunterInit); |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
static void PEHunterInit(u_char *args) |
|---|
| 93 |
{ |
|---|
| 94 |
static int bFirstConfig = 1; |
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
if(!_dpd.streamAPI) |
|---|
| 98 |
{ |
|---|
| 99 |
_dpd.fatalMsg("PEHunterInit(): The Stream preprocessor must be enabled.\n"); |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
PEHunter_ParseArgs(args); |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
if ( bFirstConfig ) |
|---|
| 112 |
{ |
|---|
| 113 |
_dpd.addPreproc(HuntPE, PRIORITY_APPLICATION, PP_PEHUNTER); |
|---|
| 114 |
_dpd.addPreprocExit(PEHunterCleanExitFunction, NULL, PRIORITY_LAST, PP_PEHUNTER); |
|---|
| 115 |
_dpd.addPreprocRestart(PEHunterRestartFunction, NULL, PRIORITY_LAST, PP_PEHUNTER); |
|---|
| 116 |
bFirstConfig = 0; |
|---|
| 117 |
|
|---|
| 118 |
#ifdef PERF_PROFILING |
|---|
| 119 |
_dpd.addPreprocProfileFunc("pehunter", (void*)&pehuntPerfStats, 0, _dpd.totalPerfStats); |
|---|
| 120 |
#endif |
|---|
| 121 |
} |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
void PEHunter_ParseArgs(u_char *args) |
|---|
| 139 |
{ |
|---|
| 140 |
char *arg; |
|---|
| 141 |
char *value; |
|---|
| 142 |
|
|---|
| 143 |
bzero(&_pehunterConfig, sizeof(PEHunterConfig)); |
|---|
| 144 |
|
|---|
| 145 |
if ((!_dpd.streamAPI) || (_dpd.streamAPI->version < STREAM_API_VERSION4)) |
|---|
| 146 |
_dpd.fatalMsg("%s(): Streaming & reassembly must be enabled\n", __PRETTY_FUNCTION__); |
|---|
| 147 |
|
|---|
| 148 |
if ( args == NULL ) |
|---|
| 149 |
{ |
|---|
| 150 |
return; |
|---|
| 151 |
} |
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
arg = strtok((char *)args, CONF_SEPARATORS); |
|---|
| 155 |
while (arg != NULL) |
|---|
| 156 |
{ |
|---|
| 157 |
if ( !strcasecmp("dump_dir", arg) ) { |
|---|
| 158 |
value = strtok(NULL, CONF_SEPARATORS); |
|---|
| 159 |
if ( value == NULL ) return; |
|---|
| 160 |
if ((_pehunterConfig.dumpDir = strdup(value)) == NULL) { |
|---|
| 161 |
_dpd.fatalMsg("%s(): Unable to allocate memory: %s\n", __PRETTY_FUNCTION__, strerror(errno)); |
|---|
| 162 |
} |
|---|
| 163 |
} else if ( !strcasecmp("debug", arg) ) { |
|---|
| 164 |
_pehunterConfig.debug = 1; |
|---|
| 165 |
} else { |
|---|
| 166 |
_dpd.fatalMsg("%s(%d) => Unknown PEHunter configuration option %s\n", |
|---|
| 167 |
*(_dpd.config_file), *(_dpd.config_line), arg); |
|---|
| 168 |
} |
|---|
| 169 |
|
|---|
| 170 |
arg = strtok(NULL, CONF_SEPARATORS); |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
PrintPEHunterConfig(); |
|---|
| 174 |
|
|---|
| 175 |
if (_pehunterConfig.dumpDir == NULL) |
|---|
| 176 |
{ |
|---|
| 177 |
_dpd.fatalMsg("%s(): No dump directory given. Check your config file.\n", __PRETTY_FUNCTION__); |
|---|
| 178 |
} |
|---|
| 179 |
} |
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
static void PrintPEHunterConfig(void) |
|---|
| 193 |
{ |
|---|
| 194 |
|
|---|
| 195 |
_dpd.logMsg("PEHunter config:\n"); |
|---|
| 196 |
_dpd.logMsg(" Dump Directory:\t %s\n", _pehunterConfig.dumpDir); |
|---|
| 197 |
_dpd.logMsg(" Debug:\t\t %s\n", _pehunterConfig.debug ? "yes" : "no"); |
|---|
| 198 |
_dpd.logMsg("\n"); |
|---|
| 199 |
|
|---|
| 200 |
return; |
|---|
| 201 |
} |
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
static void FreePEHunterConfig(void) |
|---|
| 215 |
{ |
|---|
| 216 |
free(_pehunterConfig.dumpDir); |
|---|
| 217 |
return; |
|---|
| 218 |
} |
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
static void HuntPE(void *initial_pkt, void *context) |
|---|
| 235 |
{ |
|---|
| 236 |
SFSnortPacket *p = (SFSnortPacket *)initial_pkt; |
|---|
| 237 |
PROFILE_VARS; |
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
if (( !p ) || |
|---|
| 241 |
( !p->payload ) || |
|---|
| 242 |
( !p->payload_size ) || |
|---|
| 243 |
|
|---|
| 244 |
( p->flags & FLAG_STREAM_INSERT)) |
|---|
| 245 |
{ |
|---|
| 246 |
return; |
|---|
| 247 |
} |
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
PREPROC_PROFILE_START(pehuntPerfStats); |
|---|
| 251 |
|
|---|
| 252 |
if (_pehunterConfig.debug) _dpd.logMsg("PEHunter: Processing packet with %u bytes.\n", p->payload_size); |
|---|
| 253 |
Hunt(p); |
|---|
| 254 |
|
|---|
| 255 |
PREPROC_PROFILE_END(pehuntPerfStats); |
|---|
| 256 |
#ifdef PERF_PROFILING |
|---|
| 257 |
if (pehuntDetectCalled) |
|---|
| 258 |
{ |
|---|
| 259 |
pehuntPerfStats.ticks -= pehuntDetectPerfStats.ticks; |
|---|
| 260 |
|
|---|
| 261 |
pehuntDetectPerfStats.ticks = 0; |
|---|
| 262 |
pehuntDetectCalled = 0; |
|---|
| 263 |
} |
|---|
| 264 |
#endif |
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
} |
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
static void PEHunterCleanExitFunction(int signal, void *data) |
|---|
| 288 |
{ |
|---|
| 289 |
FreePEHunterConfig(); |
|---|
| 290 |
|
|---|
| 291 |
} |
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
|
|---|
| 308 |
static void PEHunterRestartFunction(int signal, void *foo) |
|---|
| 309 |
{ |
|---|
| 310 |
|
|---|
| 311 |
} |
|---|