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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
// ssdeep
// (C) Copyright 2010 ManTech International Corporation
//
// $Id: match.c 100 2010-05-06 12:08:12Z jessekornblum $
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "ssdeep.h"
// The longest line we should encounter when reading files of known hashes
#define MAX_STR_LEN 2048
// ------------------------------------------------------------------
// LINKED LIST FUNCTIONS
// ------------------------------------------------------------------
static
int lsh_list_init(lsh_list *l)
{
if (NULL == l)
return TRUE;
l->top = NULL;
l->bottom = NULL;
return FALSE;
}
// Insert a signature from the file match_file into the list l.
// The value consists of the filename fn and the hash value sum.
static
int lsh_list_insert(state * s,
char * match_file,
lsh_list * l,
TCHAR * fn,
char * sum)
{
lsh_node *new;
if (NULL == s || NULL == l || NULL == fn || NULL == sum)
return TRUE;
if ((new = (lsh_node *)malloc(sizeof(lsh_node))) == NULL)
fatal_error("%s: Out of memory", __progname);
new->next = NULL;
if (((new->hash = strdup(sum)) == NULL) ||
((new->fn = _tcsdup(fn)) == NULL))
{
print_error(s,"%s: out of memory", __progname);
return TRUE;
}
if (match_file != NULL)
{
new->match_file = strdup(match_file);
if (NULL == new->match_file)
{
print_error(s,"%s: out of memory", __progname);
return TRUE;
}
}
else
new->match_file = NULL;
if (l->bottom == NULL)
{
if (l->top != NULL)
fatal_error("%s: internal data structure inconsistency", fn);
l->top = new;
l->bottom = new;
return FALSE;
}
l->bottom->next = new;
l->bottom = new;
return FALSE;
}
// ------------------------------------------------------------------
// SIGNATURE FILE FUNCTIONS
// ------------------------------------------------------------------
typedef struct _file_info_t
{
FILE * handle;
TCHAR known_file_name[MAX_STR_LEN];
char known_hash[MAX_STR_LEN];
} file_info_t;
// Open a signature file and determine if it contains a valid header
// Returns TRUE on an error, otherwise FALSE.
static
int sig_file_open(state *s, char * fn, file_info_t * info)
{
char str[MAX_STR_LEN];
if (NULL == s || NULL == fn || NULL == info)
return TRUE;
info->handle = fopen(fn,"rb");
if (NULL == info->handle)
{
if (!(MODE(mode_silent)))
perror(fn);
return TRUE;
}
// The first line should be the header. We don't need to chop it
// as we're only comparing it to the length of the known header.
if (NULL == fgets(str,MAX_STR_LEN,info->handle))
{
if (!(MODE(mode_silent)))
perror(fn);
fclose(info->handle);
return TRUE;
}
if (strncmp(str,OUTPUT_FILE_HEADER,strlen(OUTPUT_FILE_HEADER)))
{
if (!MODE(mode_silent))
print_error(s,"%s: invalid file header", fn);
fclose(info->handle);
return TRUE;
}
return FALSE;
}
// Close a signature file
static
void sig_file_close(file_info_t * info)
{
if (NULL == info)
return;
fclose(info->handle);
}
// Read the next entry in a signature file and store it in the structure 'info'
// If there is no next entry (EOF) or an error occurs, returns TRUE,
// otherwise FALSE.
static
int sig_file_next(state *s, file_info_t * info)
{
char str[MAX_STR_LEN];
if (NULL == s || NULL == info)
return TRUE;
if (NULL == fgets(str,MAX_STR_LEN,info->handle))
return TRUE;
chop_line(str);
// The file format is:
// hash,filename
strncpy(info->known_hash,str,MIN(MAX_STR_LEN,strlen(str)));
find_comma_separated_string(info->known_hash,0);
find_comma_separated_string(str,1);
// On Win32 we have to do a kludgy cast from ordinary char
// values to the TCHAR values we use internally.
size_t i, sz = strlen(str);
for (i = 0 ; i < sz ; i++)
{
#ifdef _WIN32
info->known_file_name[i] = (TCHAR)(str[i]);
#else
info->known_file_name[i] = str[i];
#endif
}
info->known_file_name[i] = 0;
return FALSE;
}
// ------------------------------------------------------------------
// MATCHING FUNCTIONS
// ------------------------------------------------------------------
int match_init(state *s)
{
if (NULL == s)
return TRUE;
s->known_hashes = (lsh_list *)malloc(sizeof(lsh_list));
if (s->known_hashes == NULL)
return TRUE;
lsh_list_init(s->known_hashes);
return FALSE;
}
#define STRINGS_EQUAL(A,B) !_tcsncmp(A,B,MAX(_tcslen(A),_tcslen(B)))
// Match the file named fn with the hash sum against the set of knowns
// Display any matches.
// Return FALSE is there are no matches, TRUE if at least one match
/// @param s State variable
/// @param match_file Filename where we got the hash of the unknown file.
/// May be NULL.
/// @param fn Filename of the unknown file we are comparing
/// @param sum Fuzzy hash of the unknown file we are comparing
int match_compare(state *s, char * match_file, TCHAR *fn, char *sum)
{
if (NULL == s || NULL == fn || NULL == sum)
fatal_error("%s: Null values passed into match_compare", __progname);
size_t fn_len = _tcslen(fn);
size_t sum_len = strlen(sum);
int status = FALSE;
int score;
lsh_node *tmp = s->known_hashes->top;
while (tmp != NULL)
{
if (s->mode & mode_match_pretty)
{
// Prevent printing the redundant "A matches A"
if (!(_tcsncmp(fn,tmp->fn,MAX(fn_len,_tcslen(tmp->fn)))) &&
!(strncmp(sum,tmp->hash,MAX(sum_len,strlen(tmp->hash)))))
{
// Unless these results from different matching files (such as
// what happens in sigcompare mode). That being said, we have to
// be careful to avoid NULL values such as when working in
// normal pretty print mode.
if (NULL == match_file || NULL == tmp->match_file ||
(!(strncmp(match_file, tmp->match_file, MAX(strlen(match_file),strlen(tmp->match_file))))))
{
tmp = tmp->next;
continue;
}
}
}
score = fuzzy_compare(sum,tmp->hash);
if (-1 == score)
{
print_error(s, "%s: Bad hashes in comparison", __progname);
}
else
{
if (score > s->threshold || MODE(mode_display_all))
{
if (s->mode & mode_csv)
{
display_filename(stdout,fn);
printf(",");
display_filename(stdout,tmp->fn);
print_status(",%"PRIu32, score);
}
else
{
if (match_file != NULL)
printf ("%s:", match_file);
display_filename(stdout,fn);
printf(" matches ");
if (tmp->match_file != NULL)
printf ("%s:", tmp->match_file);
display_filename(stdout,tmp->fn);
print_status(" (%"PRIu32")", score);
}
// We don't return right away as this file could match more than
// one signature.
status = TRUE;
}
}
tmp = tmp->next;
}
return status;
}
int match_pretty(state *s)
{
if (NULL == s)
return TRUE;
lsh_node *tmp = s->known_hashes->top;
while (tmp != NULL)
{
if (match_compare(s,tmp->match_file,tmp->fn,tmp->hash))
print_status("");
tmp = tmp->next;
}
return FALSE;
}
int match_add(state *s, char * match_file, TCHAR *fn, char *hash)
{
return (lsh_list_insert(s,match_file,s->known_hashes,fn,hash));
}
int match_load(state *s, char *fn)
{
file_info_t info;
int status = FALSE;
if (NULL == s || NULL == fn)
return TRUE;
if (sig_file_open(s,fn,&info))
return TRUE;
while ( ! sig_file_next(s,&info))
{
if (match_add(s,fn,info.known_file_name,info.known_hash))
{
// If we can't insert this value, we're probably out of memory.
// There's no sense trying to read the rest of the file.
print_error(s,"%s: unable to insert hash", fn);
status = TRUE;
break;
}
}
sig_file_close(&info);
return status;
}
int match_compare_unknown(state *s, char * fn)
{
file_info_t info;
if (NULL == s || NULL == fn)
return TRUE;
if (sig_file_open(s,fn,&info))
return TRUE;
while ( ! sig_file_next(s,&info))
match_compare(s,fn,info.known_file_name,info.known_hash);
sig_file_close(&info);
return FALSE;
}
|