File: dconf.c

package info (click to toggle)
clamav 0.98.7+dfsg-0+deb6u2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 60,204 kB
  • ctags: 49,129
  • sloc: cpp: 267,090; ansic: 152,211; sh: 35,196; python: 2,630; makefile: 2,220; perl: 1,690; pascal: 1,218; lisp: 184; csh: 117; xml: 38; asm: 32; exp: 4
file content (448 lines) | stat: -rw-r--r-- 15,477 bytes parent folder | download | duplicates (2)
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
 *  Copyright (C) 2014 Cisco and/or its affiliates. All rights reserved.
 *  Copyright (C) 2007-2013 Sourcefire, Inc.
 *
 *  Authors: Tomasz Kojm
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  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 Street, Fifth Floor, Boston,
 *  MA 02110-1301, USA.
 */

#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <zlib.h>

#include "clamav.h"
#include "cltypes.h"
#include "dconf.h"
#include "readdb.h"
#include "str.h"
#include "others.h"

#include "mpool.h"

struct dconf_module {
    const char  *mname;     /* module name */
    const char  *sname;     /* submodule name */
    uint32_t    bflag;      /* bit flag */
    uint8_t state;      /* default state (on/off) */
};

#ifdef CL_EXPERIMENTAL
#define DCONF_ENABLE_EXPERIMENTAL 1
#else
#define DCONF_ENABLE_EXPERIMENTAL 0
#endif

static struct dconf_module modules[] = {

    { "PE",     "PARITE",       PE_CONF_PARITE,     1 },
    { "PE",     "KRIZ",     PE_CONF_KRIZ,       1 },
    { "PE",     "MAGISTR",      PE_CONF_MAGISTR,        1 },
    { "PE",     "POLIPOS",      PE_CONF_POLIPOS,        1 },
    { "PE",     "MD5SECT",      PE_CONF_MD5SECT,        1 },
    { "PE",     "UPX",      PE_CONF_UPX,        1 },
    { "PE",     "FSG",      PE_CONF_FSG,        1 },
    { "PE",     "SWIZZOR",      PE_CONF_SWIZZOR,        1 },

    { "PE",     "PETITE",       PE_CONF_PETITE,     1 },
    { "PE",     "PESPIN",       PE_CONF_PESPIN,     1 },
    { "PE",     "YC",       PE_CONF_YC,         1 },
    { "PE",     "WWPACK",       PE_CONF_WWPACK,     1 },

    { "PE",     "NSPACK",       PE_CONF_NSPACK,     1 },
    { "PE",     "MEW",      PE_CONF_MEW,        1 },
    { "PE",     "UPACK",        PE_CONF_UPACK,      1 },
    { "PE",     "ASPACK",       PE_CONF_ASPACK,     1 },
    { "PE",     "CATALOG",      PE_CONF_CATALOG,        1 },
    { "PE",     "DISABLECERT",  PE_CONF_DISABLECERT,    0 },
    { "PE",     "DUMPCERT",     PE_CONF_DUMPCERT,       0 },
    { "PE",     "MATCHICON",     PE_CONF_MATCHICON,       1 },

    { "ELF",        NULL,       0x1,            1 },

    { "MACHO",      NULL,       0x1,            1 },

    { "ARCHIVE",    "RAR",      ARCH_CONF_RAR,      1 },
    { "ARCHIVE",    "ZIP",      ARCH_CONF_ZIP,      1 },
    { "ARCHIVE",    "GZIP",     ARCH_CONF_GZ,       1 },
    { "ARCHIVE",    "BZIP",     ARCH_CONF_BZ,       1 },
    { "ARCHIVE",    "ARJ",          ARCH_CONF_ARJ,      1 },
    { "ARCHIVE",    "SZDD",     ARCH_CONF_SZDD,     1 },
    { "ARCHIVE",    "CAB",      ARCH_CONF_CAB,      1 },
    { "ARCHIVE",    "CHM",      ARCH_CONF_CHM,      1 },
    { "ARCHIVE",    "OLE2",     ARCH_CONF_OLE2,     1 },
    { "ARCHIVE",    "TAR",      ARCH_CONF_TAR,      1 },
    { "ARCHIVE",    "CPIO",     ARCH_CONF_CPIO,     1 },
    { "ARCHIVE",    "BINHEX",       ARCH_CONF_BINHEX,       1 },
    { "ARCHIVE",    "SIS",      ARCH_CONF_SIS,      1 },
    { "ARCHIVE",    "NSIS",     ARCH_CONF_NSIS,     1 },
    { "ARCHIVE",    "AUTOIT",       ARCH_CONF_AUTOIT,       1 },
    { "ARCHIVE",    "ISHIELD",      ARCH_CONF_ISHIELD,      1 },
    { "ARCHIVE",    "7zip",     ARCH_CONF_7Z,       1 },
    { "ARCHIVE",    "ISO9660",      ARCH_CONF_ISO9660,      1 },
    { "ARCHIVE",    "DMG",      ARCH_CONF_DMG,      1 },
    { "ARCHIVE",    "XAR",      ARCH_CONF_XAR,      1 },
    { "ARCHIVE",    "HFSPLUS",      ARCH_CONF_HFSPLUS,      1 },
    { "ARCHIVE",    "XZ",       ARCH_CONF_XZ,       1 },

    { "DOCUMENT",   "HTML",     DOC_CONF_HTML,      1 },
    { "DOCUMENT",   "RTF",      DOC_CONF_RTF,       1 },
    { "DOCUMENT",   "PDF",      DOC_CONF_PDF,       1 },
    { "DOCUMENT",   "SCRIPT",       DOC_CONF_SCRIPT,        1 },
    { "DOCUMENT",   "HTMLSKIPRAW",  DOC_CONF_HTML_SKIPRAW,  1 },
    { "DOCUMENT",   "JSNORM",       DOC_CONF_JSNORM,        1 },
    { "DOCUMENT",   "SWF",      DOC_CONF_SWF,       1 },

    { "MAIL",       "MBOX",     MAIL_CONF_MBOX,     1 },
    { "MAIL",       "TNEF",     MAIL_CONF_TNEF,     1 },

    { "OTHER",      "UUENCODED",    OTHER_CONF_UUENC,       1 },
    { "OTHER",      "SCRENC",       OTHER_CONF_SCRENC,      1 },
    { "OTHER",      "RIFF",     OTHER_CONF_RIFF,        1 },
    { "OTHER",      "JPEG",     OTHER_CONF_JPEG,        1 },
    { "OTHER",      "CRYPTFF",      OTHER_CONF_CRYPTFF,     1 },
    { "OTHER",      "DLP",      OTHER_CONF_DLP,     1 },
    { "OTHER",      "MYDOOMLOG",    OTHER_CONF_MYDOOMLOG,   1 },
    { "OTHER",      "PREFILTERING", OTHER_CONF_PREFILTERING,1 },
    { "OTHER",      "PDFNAMEOBJ",   OTHER_CONF_PDFNAMEOBJ,  1 },
    { "OTHER",      "PRTNINTXN",   OTHER_CONF_PRTNINTXN,  1 },

    { "PHISHING",   "ENGINE",       PHISHING_CONF_ENGINE,   1 },
    { "PHISHING",   "ENTCONV",      PHISHING_CONF_ENTCONV,  1 },

    { "BYTECODE",   "INTERPRETER",  BYTECODE_INTERPRETER,   1 },
    { "BYTECODE",   "JIT X86",      BYTECODE_JIT_X86,       1 },
    { "BYTECODE",   "JIT PPC",      BYTECODE_JIT_PPC,       1 },
    { "BYTECODE",   "JIT ARM",      BYTECODE_JIT_ARM,       0 },

    { "STATS",      "DISABLED",     DCONF_STATS_DISABLED,   0 },
    { "STATS",      "PESECTION DISABLED", DCONF_STATS_PE_SECTION_DISABLED, 0 },

    { NULL,     NULL,       0,              0 }
};

#ifdef USE_MPOOL 
struct cli_dconf *cli_dconf_init(mpool_t *mempool)
#else
struct cli_dconf *cli_dconf_init(void)
#endif
{
    unsigned int i;
    struct cli_dconf *dconf;

    dconf = (struct cli_dconf *) mpool_calloc(mempool, sizeof(struct cli_dconf), 1);
    if(!dconf)
        return NULL;

    for(i = 0; modules[i].mname; i++) {
        if(!strcmp(modules[i].mname, "PE")) {
            if(modules[i].state)
                dconf->pe |= modules[i].bflag;

        } else if(!strcmp(modules[i].mname, "ELF")) {
            if(modules[i].state)
                dconf->elf |= modules[i].bflag;

        } else if(!strcmp(modules[i].mname, "MACHO")) {
            if(modules[i].state)
                dconf->macho |= modules[i].bflag;

        } else if(!strcmp(modules[i].mname, "ARCHIVE")) {
            if(modules[i].state)
                dconf->archive |= modules[i].bflag;

        } else if(!strcmp(modules[i].mname, "DOCUMENT")) {
            if(modules[i].state)
                dconf->doc |= modules[i].bflag;

        } else if(!strcmp(modules[i].mname, "MAIL")) {
            if(modules[i].state)
                dconf->mail |= modules[i].bflag;

        } else if(!strcmp(modules[i].mname, "OTHER")) {
            if(modules[i].state)
                dconf->other |= modules[i].bflag;
        } else if(!strcmp(modules[i].mname, "PHISHING")) {
            if(modules[i].state)
                dconf->phishing |= modules[i].bflag;
        } else if(!strcmp(modules[i].mname, "BYTECODE")) {
            if (modules[i].state)
                dconf->bytecode |= modules[i].bflag;
        } else if (!strcmp(modules[i].mname, "STATS")) {
            if (modules[i].state)
                dconf->stats |= modules[i].bflag;
        }
    }

    return dconf;
}

void cli_dconf_print(struct cli_dconf *dconf)
{
    unsigned int pe = 0, elf = 0, macho = 0, arch = 0, doc = 0, mail = 0;
    unsigned int other = 0, phishing = 0, i, bytecode=0, stats=0;


    cli_dbgmsg("Dynamic engine configuration settings:\n");
    cli_dbgmsg("--------------------------------------\n");

    for(i = 0; modules[i].mname; i++) {
        if(!strcmp(modules[i].mname, "PE")) {
            if(!pe) {
                cli_dbgmsg("Module PE: %s\n", dconf->pe ? "On" : "Off");
                pe = 1;
            }

            if(dconf->pe)
                cli_dbgmsg("   * Submodule %10s:\t%s\n", modules[i].sname, (dconf->pe & modules[i].bflag) ? "On" : "** Off **");
            else
                continue;
        } else if(!strcmp(modules[i].mname, "ELF")) {
            if(!elf) {
                cli_dbgmsg("Module ELF: %s\n", dconf->elf ? "On" : "Off");
                elf = 1;
            }
        } else if(!strcmp(modules[i].mname, "MACHO")) {
            if(!macho) {
                cli_dbgmsg("Module MACHO: %s\n", dconf->elf ? "On" : "Off");
                macho = 1;
            }
        } else if(!strcmp(modules[i].mname, "ARCHIVE")) {
            if(!arch) {
                cli_dbgmsg("Module ARCHIVE: %s\n", dconf->archive ? "On" : "Off");
                arch = 1;
            }

            if(dconf->archive)
                cli_dbgmsg("   * Submodule %10s:\t%s\n", modules[i].sname, (dconf->archive & modules[i].bflag) ? "On" : "** Off **");
            else
                continue;
        } else if(!strcmp(modules[i].mname, "DOCUMENT")) {
            if(!doc) {
                cli_dbgmsg("Module DOCUMENT: %s\n", dconf->doc ? "On" : "Off");
                doc = 1;
            }

            if(dconf->doc)
                cli_dbgmsg("   * Submodule %10s:\t%s\n", modules[i].sname, (dconf->doc & modules[i].bflag) ? "On" : "** Off **");
            else
                continue;
        } else if(!strcmp(modules[i].mname, "MAIL")) {
            if(!mail) {
                cli_dbgmsg("Module MAIL: %s\n", dconf->mail ? "On" : "Off");
                mail = 1;
            }

            if(dconf->mail)
                cli_dbgmsg("   * Submodule %10s:\t%s\n", modules[i].sname, (dconf->mail & modules[i].bflag) ? "On" : "** Off **");
            else
                continue;
        } else if(!strcmp(modules[i].mname, "OTHER")) {
            if(!other) {
                cli_dbgmsg("Module OTHER: %s\n", dconf->other ? "On" : "Off");
                other = 1;
            }

            if(dconf->other)
                cli_dbgmsg("   * Submodule %10s:\t%s\n", modules[i].sname, (dconf->other & modules[i].bflag) ? "On" : "** Off **");
            else
                continue;
        } else if(!strcmp(modules[i].mname, "PHISHING")) {
            if(!phishing) {
                cli_dbgmsg("Module PHISHING %s\n", dconf->phishing ? "On" : "Off");
                phishing = 1;
            }

            if(dconf->phishing)
                cli_dbgmsg("   * Submodule %10s:\t%s\n", modules[i].sname, (dconf->phishing & modules[i].bflag) ? "On" : "** Off **");
            else
                continue;
        } else if(!strcmp(modules[i].mname, "BYTECODE")) {
            if(!bytecode) {
                cli_dbgmsg("Module BYTECODE %s\n", dconf->bytecode ? "On" : "Off");
                bytecode = 1;
            }

            if(dconf->bytecode)
                cli_dbgmsg("   * Submodule %10s:\t%s\n", modules[i].sname, (dconf->bytecode & modules[i].bflag) ? "On" : "** Off **");
            else
                continue;
        } else if (!strcmp(modules[i].mname, "STATS")) {
            if (!stats) {
                cli_dbgmsg("Module STATS %s\n", dconf->stats ? "On" : "Off");
                stats = 1;
            }

            if (dconf->stats)
                cli_dbgmsg("    * Submodule %10s:\t%s\n", modules[i].sname, (dconf->stats & modules[i].bflag) ? "On" : "** Off **");
            else
                continue;
        }
    }
}

static int chkflevel(const char *entry, int field)
{
    char *pt;


    if((pt = cli_strtok(entry, field, ":"))) { /* min version */
        if(!isdigit(*pt)) {
            free(pt);
            return 0;
        }

        if((unsigned int) atoi(pt) > CL_FLEVEL_DCONF) {
            free(pt);
            return 0;
        }

        free(pt);

        if((pt = cli_strtok(entry, field + 1, ":"))) { /* max version */
            if(!isdigit(*pt)) {
                free(pt);
                return 0;
            }

            if((unsigned int) atoi(pt) < CL_FLEVEL_DCONF) {
                free(pt);
                return 0;
            }

            free(pt);
        }
    }

    return 1;
}

int cli_dconf_load(FILE *fs, struct cl_engine *engine, unsigned int options, struct cli_dbio *dbio)
{
    char buffer[FILEBUFF];
    unsigned int line = 0;
    int ret = 0;
    uint32_t val;

    UNUSEDPARAM(options);

    while(cli_dbgets(buffer, FILEBUFF, fs, dbio)) {
        line++;
        cli_chomp(buffer);

        if(!strncmp(buffer, "PE:", 3) && chkflevel(buffer, 2)) {
            if(sscanf(buffer + 3, "0x%x", &val) == 1) {
                engine->dconf->pe = val;
            } else {
                ret = CL_EMALFDB;
                break;
            }
        }

        if(!strncmp(buffer, "ELF:", 4) && chkflevel(buffer, 2)) {
            if(sscanf(buffer + 4, "0x%x", &val) == 1) {
                engine->dconf->elf = val;
            } else {
                ret = CL_EMALFDB;
                break;
            }
        }

        if(!strncmp(buffer, "MACHO:", 4) && chkflevel(buffer, 2)) {
            if(sscanf(buffer + 4, "0x%x", &val) == 1) {
                engine->dconf->macho = val;
            } else {
                ret = CL_EMALFDB;
                break;
            }
        }

        if(!strncmp(buffer, "ARCHIVE:", 8) && chkflevel(buffer, 2)) {
            if(sscanf(buffer + 8, "0x%x", &val) == 1) {
                engine->dconf->archive = val;
            } else {
                ret = CL_EMALFDB;
                break;
            }
        }

        if(!strncmp(buffer, "DOCUMENT:", 9) && chkflevel(buffer, 2)) {
            if(sscanf(buffer + 9, "0x%x", &val) == 1) {
                engine->dconf->doc = val;
            } else {
                ret = CL_EMALFDB;
                break;
            }
        }

        if(!strncmp(buffer, "MAIL:", 5) && chkflevel(buffer, 2)) {
            if(sscanf(buffer + 5, "0x%x", &val) == 1) {
                engine->dconf->mail = val;
            } else {
                ret = CL_EMALFDB;
                break;
            }
        }

        if(!strncmp(buffer, "OTHER:", 6) && chkflevel(buffer, 2)) {
            if(sscanf(buffer + 6, "0x%x", &val) == 1) {
                engine->dconf->other = val;
            } else {
                ret = CL_EMALFDB;
                break;
            }
        }

        if(!strncmp(buffer, "PHISHING:", 9) && chkflevel(buffer, 2)) {
            if(sscanf(buffer + 9, "0x%x", &val) == 1) {
                engine->dconf->phishing = val;
            } else {
                ret = CL_EMALFDB;
                break;
            }
        }

        if(!strncmp(buffer, "BYTECODE:", 9) && chkflevel(buffer, 2)) {
            if(sscanf(buffer + 9, "0x%x", &val) == 1) {
                engine->dconf->bytecode = val;
            } else {
                ret = CL_EMALFDB;
                break;
            }
        }

        if(!strncmp(buffer, "STATS:", 6) && chkflevel(buffer, 2)) {
            if(sscanf(buffer + 6, "0x%x", &val) == 1) {
                engine->dconf->stats = val;
            } else {
                ret = CL_EMALFDB;
                break;
            }
        }
    }

    if(ret) {
        cli_errmsg("Problem parsing configuration file at line %u\n", line);
        return ret;
    }

    return CL_SUCCESS;
}