~ubuntu-branches/ubuntu/saucy/clamav/saucy-backports

« back to all changes in this revision

Viewing changes to sigtool/sigtool.c

  • Committer: Package Import Robot
  • Author(s): Scott Kitterman
  • Date: 2014-07-15 01:08:10 UTC
  • mfrom: (0.35.47 sid)
  • Revision ID: package-import@ubuntu.com-20140715010810-ru66ek4fun2iseba
Tags: 0.98.4+dfsg-2~ubuntu13.10.1
No-change backport to saucy (LP: #1341962)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
 
2
 *  Copyright (C) 2014 Cisco Systems, Inc.
2
3
 *  Copyright (C) 2007 - 2013 Sourcefire, Inc.
3
4
 *  Copyright (C) 2002 - 2007 Tomasz Kojm <tkojm@clamav.net>
4
5
 *  CDIFF code (C) 2006 Sensory Networks, Inc.
24
25
#include "clamav-config.h"
25
26
#endif
26
27
 
27
 
#define _GNU_SOURCE
28
 
 
29
28
#include <stdio.h>
30
29
#include <stdlib.h>
31
30
#include <string.h>
53
52
#include <termios.h>
54
53
#endif
55
54
 
 
55
#include <openssl/ssl.h>
 
56
#include <openssl/err.h>
 
57
#include "libclamav/crypto.h"
 
58
 
56
59
#include "vba.h"
57
60
 
58
61
#include "shared/output.h"
59
62
#include "shared/optparser.h"
60
63
#include "shared/misc.h"
61
64
#include "shared/cdiff.h"
62
 
#include "libclamav/sha1.h"
63
 
#include "libclamav/sha256.h"
64
65
#include "shared/tar.h"
65
66
 
66
67
#include "libclamav/clamav.h"
400
401
        unsigned int i, bytes;
401
402
        unsigned char digest[32], buffer[FILEBUFF];
402
403
        char *sha;
403
 
        SHA256_CTX ctx;
404
 
 
405
 
 
406
 
    sha256_init(&ctx);
 
404
        void *ctx;
 
405
 
 
406
    ctx = cl_hash_init("sha256");
 
407
    if (!(ctx))
 
408
        return NULL;
 
409
 
407
410
    if(!(fh = fopen(file, "rb"))) {
408
411
        mprintf("!sha256file: Can't open file %s\n", file);
 
412
    cl_hash_destroy(ctx);
409
413
        return NULL;
410
414
    }
411
415
    if(size)
412
416
        *size = 0;
413
417
    while((bytes = fread(buffer, 1, sizeof(buffer), fh))) {
414
 
        sha256_update(&ctx, buffer, bytes);
 
418
        cl_update_hash(ctx, buffer, bytes);
415
419
        if(size)
416
420
            *size += bytes;
417
421
    }
418
 
    sha256_final(&ctx, digest);
 
422
    cl_finish_hash(ctx, digest);
419
423
    sha = (char *) malloc(65);
420
424
    if(!sha)
421
425
    {
435
439
        unsigned int i, bytes;
436
440
        char file[32], *pt, dbfile[32];
437
441
        unsigned char digest[32], buffer[FILEBUFF];
438
 
        SHA256_CTX ctx;
 
442
        void *ctx;
439
443
 
440
444
    snprintf(file, sizeof(file), "%s.info", dbname);
441
445
    if(!access(file, R_OK)) {
492
496
    }
493
497
    if(!optget(opts, "unsigned")->enabled) {
494
498
        rewind(fh);
495
 
        sha256_init(&ctx);
 
499
    ctx = cl_hash_init("sha256");
 
500
    if (!(ctx)) {
 
501
        fclose(fh);
 
502
        return -1;
 
503
    }
 
504
 
496
505
        while((bytes = fread(buffer, 1, sizeof(buffer), fh)))
497
 
            sha256_update(&ctx, buffer, bytes);
498
 
        sha256_final(&ctx, digest);
 
506
            cl_update_hash(ctx, buffer, bytes);
 
507
        cl_finish_hash(ctx, digest);
499
508
        if(!(pt = getdsig(optget(opts, "server")->strarg, builder, digest, 32, 3))) {
500
509
            mprintf("!writeinfo: Can't get digital signature from remote server\n");
501
510
            fclose(fh);
515
524
{
516
525
        char *cdiff, *pt, buffer[FILEBUFF];
517
526
        unsigned char digest[32];
518
 
        SHA256_CTX ctx;
 
527
        void *ctx;
519
528
        STATBUF sb;
520
529
        FILE *scripth, *cdiffh;
521
530
        gzFile gzh;
599
608
        return -1;
600
609
    }
601
610
 
602
 
    sha256_init(&ctx);
 
611
    ctx = cl_hash_init("sha256");
 
612
    if (!(ctx)) {
 
613
        unlink(cdiff);
 
614
        free(cdiff);
 
615
        fclose(cdiffh);
 
616
        return -1;
 
617
    }
603
618
 
604
619
    while((bytes = fread(buffer, 1, sizeof(buffer), cdiffh)))
605
 
        sha256_update(&ctx, (unsigned char *) buffer, bytes);
 
620
        cl_update_hash(ctx, (unsigned char *) buffer, bytes);
606
621
 
607
622
    fclose(cdiffh);
608
 
    sha256_final(&ctx, digest);
 
623
    cl_finish_hash(ctx, digest);
609
624
 
610
625
    if(!(pt = getdsig(optget(opts, "server")->strarg, builder, digest, 32, 2))) {
611
626
        mprintf("!script2cdiff: Can't get digital signature from remote server\n");
793
808
        version = oldcvd->version + 1;
794
809
        oldsigs = oldcvd->sigs;
795
810
        cl_cvdfree(oldcvd);
 
811
    } else if (optget(opts, "cvd-version")->numarg != 0) {
 
812
        version = optget(opts, "cvd-version")->numarg;
796
813
    } else {
797
814
        mprintf("Version number: ");
798
815
        if(scanf("%u", &version) == EOF) {
1509
1526
    }
1510
1527
    if(!(ctx = convenience_ctx(fd))) {
1511
1528
        close(fd);
 
1529
    free(dir);
1512
1530
        return -1;
1513
1531
    }
1514
1532
    if(cli_ole2_extract(dir, ctx, &vba)) {
1515
 
        destroy_ctx(ctx);
 
1533
        destroy_ctx(-1, ctx);
1516
1534
        cli_rmdirs(dir);
1517
1535
        free(dir);
1518
1536
        return -1;
1519
1537
    }
1520
 
    destroy_ctx(ctx);
 
1538
    destroy_ctx(-1, ctx);
1521
1539
    if (vba) 
1522
1540
      sigtool_vba_scandir(dir, hex_output, vba);
1523
1541
    cli_rmdirs(dir);
2522
2540
            case 9:
2523
2541
                mprintf("MACHO\n");
2524
2542
                break;
 
2543
            case 10:
 
2544
                mprintf("PDF\n");
 
2545
                break;
 
2546
            case 11:
 
2547
                mprintf("FLASH\n");
 
2548
                break;
 
2549
            case 12:
 
2550
                mprintf("JAVA CLASS\n");
 
2551
                break;
2525
2552
            default:
2526
2553
                mprintf("!decodesig: Invalid target type\n");
2527
2554
                return -1;
2788
2815
    char * filename = NULL;
2789
2816
    STATBUF sb;
2790
2817
    const char * fmptr;
2791
 
    SHA1Context sha1;
2792
2818
    struct cl_engine *engine;
2793
2819
    cli_ctx ctx;
2794
2820
    int fd, ret;
2869
2895
    }
2870
2896
 
2871
2897
    /* Generate SHA1 */
2872
 
    SHA1Init(&sha1);
2873
 
    SHA1Update(&sha1, fmptr, sb.st_size);
2874
 
    SHA1Final(&sha1, shash1);
 
2898
    cl_sha1(fmptr, sb.st_size, shash1, NULL);
2875
2899
 
2876
 
    ret = cli_checkfp_pe(&ctx, shash1);
 
2900
    ret = cli_checkfp_pe(&ctx, shash1, NULL, CL_CHECKFP_PE_FLAG_AUTHENTICODE);
2877
2901
    
2878
2902
    switch(ret) {
2879
2903
        case CL_CLEAN:
2903
2927
static void help(void)
2904
2928
{
2905
2929
    mprintf("\n");
2906
 
    mprintf("             Clam AntiVirus: Signature Tool (sigtool)  %s\n", get_version());
2907
 
    printf("           By The ClamAV Team: http://www.clamav.net/team\n");
2908
 
    printf("           (C) 2007-2009 Sourcefire, Inc. et al.\n\n");
 
2930
    mprintf("Clam AntiVirus: Signature Tool (sigtool)  %s\n", get_version());
 
2931
    mprintf("       By The ClamAV Team: http://www.clamav.net/team\n");
 
2932
    mprintf("       (C) 2007-2009 Sourcefire, Inc. et al.\n\n");
2909
2933
 
2910
2934
    mprintf("    --help                 -h              show help\n");
2911
2935
    mprintf("    --version              -V              print version number and exit\n");
2925
2949
    mprintf("    --utf16-decode=FILE                    decode UTF16 encoded files\n");
2926
2950
    mprintf("    --info=FILE            -i FILE         print database information\n");
2927
2951
    mprintf("    --build=NAME [cvd] -b NAME             build a CVD file\n");
2928
 
    mprintf("    --max-bad-sigs=NUMBER                  Maximum number of mismatched signatures when building a CVD. Default: 3000\n");
2929
 
    mprintf("    --flevel=FLEVEL                        Specify a custom flevel. Default: %u\n", cl_retflevel());
 
2952
    mprintf("    --max-bad-sigs=NUMBER                  Maximum number of mismatched signatures\n");
 
2953
    mprintf("                                           when building a CVD. Default: 3000\n");
 
2954
    mprintf("    --flevel=FLEVEL                        Specify a custom flevel.\n");
 
2955
    mprintf("                                           Default: %u\n", cl_retflevel());
 
2956
    mprintf("    --cvd-version=NUMBER                   Specify the version number to use for\n");
 
2957
    mprintf("                                           the build. Default is to use the value+1\n");
 
2958
    mprintf("                                           from the current CVD in --datadir.\n");
 
2959
    mprintf("                                           If no datafile is found the default\n");
 
2960
    mprintf("                                           behaviour is to prompt for a version\n");
 
2961
    mprintf("                                           number, this switch will prevent the\n");
 
2962
    mprintf("                                           prompt.  NOTE: If a CVD is found in the\n");
 
2963
    mprintf("                                           --datadir its version+1 is used and\n");
 
2964
    mprintf("                                           this value is ignored.\n");
2930
2965
    mprintf("    --no-cdiff                             Don't generate .cdiff file\n");
2931
2966
    mprintf("    --unsigned                             Create unsigned database file (.cud)\n");
2932
2967
    mprintf("    --print-certs=FILE                     Print Authenticode details from a PE\n");
2933
2968
    mprintf("    --server=ADDR                          ClamAV Signing Service address\n");
2934
 
    mprintf("    --datadir=DIR                          Use DIR as default database directory\n");
 
2969
    mprintf("    --datadir=DIR                          Use DIR as default database directory\n");
2935
2970
    mprintf("    --unpack=FILE          -u FILE         Unpack a CVD/CLD file\n");
2936
2971
    mprintf("    --unpack-current=SHORTNAME             Unpack local CVD/CLD into cwd\n");
2937
2972
    mprintf("    --list-sigs[=FILE]     -l[FILE]        List signature names\n");
2938
2973
    mprintf("    --find-sigs=REGEX      -fREGEX         Find signatures matching REGEX\n");
2939
2974
    mprintf("    --decode-sigs                          Decode signatures from stdin\n");
2940
 
    mprintf("    --test-sigs=DATABASE TARGET_FILE       Test signatures from DATABASE against TARGET_FILE\n");
 
2975
    mprintf("    --test-sigs=DATABASE TARGET_FILE       Test signatures from DATABASE against \n");
 
2976
    mprintf("                                           TARGET_FILE\n");
2941
2977
    mprintf("    --vba=FILE                             Extract VBA/Word6 macro code\n");
2942
2978
    mprintf("    --vba-hex=FILE                         Extract Word6 macro code with hex values\n");
2943
2979
    mprintf("    --diff=OLD NEW         -d OLD NEW      Create diff for OLD and NEW CVDs\n");
2944
 
    mprintf("    --compare=OLD NEW      -c OLD NEW      Show diff between OLD and NEW files in cdiff format\n");
 
2980
    mprintf("    --compare=OLD NEW      -c OLD NEW      Show diff between OLD and NEW files in\n");
 
2981
    mprintf("                                           cdiff format\n");
2945
2982
    mprintf("    --run-cdiff=FILE       -r FILE         Execute update script FILE in cwd\n");
2946
2983
    mprintf("    --verify-cdiff=DIFF CVD/CLD            Verify DIFF against CVD/CLD\n");
2947
2984
    mprintf("\n");
3050
3087
        help();
3051
3088
 
3052
3089
    optfree(opts);
 
3090
    cl_cleanup_crypto();
3053
3091
    return ret ? 1 : 0;
3054
3092
}