#!/usr/bin/env awk
#
# Returns the top N EXE downloads
#
# Dependencies:
#   - httpSniffer
#
# Examples:
#   - tawk 'exeDL()' file.txt
#   - tawk 'exeDL(5)' file.txt

@include "aggr"
@include "bitisset"
@include "hdr"
@include "strisempty"
#@include "unquote"
@include "_validate_col"

function exeDL(n,        _i, _l, _m, _fname, _ftype, _fsize, _app, _appC, _aud, _audC, _img, _imgC, _txt, _txtC, _vid, _vidC, _md5, _md5out, _duhout) {
    _validate_col(SrcIP4, "SrcIP4")
    _validate_col(DstIP4, "DstIP4")
    _validate_col(httpAFlags, "httpAFlags")
    _validate_col(httpURL, "httpURL")
    _validate_col(httpMimes, "httpMimes")
    _validate_col(httpAppl, "httpAppl")
    _validate_col(httpAudios, "httpAudios")
    _validate_col(httpImages, "httpImages")
    _validate_col(httpText, "httpText")
    _validate_col(httpVideos, "httpVideos")
    if (hdr()) {
        print $DstIP4, $SrcIP4, "Mime", "Filename", "Size", "MD5", "Flows"
    } else if (bitisset($httpAFlags, 0x1000)) {
        # Split the different filenames
        _l = split($httpAppl, _app, "\";\"")
        gsub(/^"/, "", _app[1])  # remove leading quote
        gsub(/"$/, "", _app[_l]) # remove trailing quote
        _l = split($httpAudios, _aud, "\";\"")
        gsub(/^"/, "", _aud[1])  # remove leading quote
        gsub(/"$/, "", _aud[_l]) # remove trailing quote
        _l = split($httpImages, _img, "\";\"")
        gsub(/^"/, "", _img[1])  # remove leading quote
        gsub(/"$/, "", _img[_l]) # remove trailing quote
        _l = split($httpText, _txt, "\";\"")
        gsub(/^"/, "", _txt[1])  # remove leading quote
        gsub(/"$/, "", _txt[_l]) # remove trailing quote
        _l = split($httpVideos, _vid, "\";\"")
        gsub(/^"/, "", _vid[1])  # remove leading quote
        gsub(/"$/, "", _vid[_l]) # remove trailing quote
        # Split the MIME types
        _l = split($httpMimes, _m, "\";\"")
        gsub(/^"/, "", _m[1])  # remove leading quote
        gsub(/"$/, "", _m[_l]) # remove trailing quote
        for (_i = 1; _i <= _l; _i++) {
            if (!strisempty(_m[_i])) {
                if (_m[_i] ~ /application\//) {
                    _fname = _app[++_appC]
                } else if (_m[_i] ~ /audio\//) {
                    _fname = _aud[++_audC]
                } else if (_m[_i] ~ /image\//) {
                    _fname = _img[++_imgC]
                } else if (_m[_i] ~ /text\//) {
                    _fname = _txt[++_txtC]
                } else if (_m[_i] ~ /video\//) {
                    _fname = _vid[++_vidC]
                } else {
                    _fname = ""
                    continue # filename not available
                }
                if (("[ -f /tmp/http*/" _fname " ] && file -b /tmp/http*/" _fname | getline _ftype) > 0 && _ftype ~ /executable/) {
                    "md5sum /tmp/http*/" _fname | getline _md5out
                    split(_md5out, _md5, "\\s")
                    "du -h /tmp/http*/" _fname | getline _duhout
                    split(_duhout, _fsize, "\\s")
                }
                if (_ftype !~ /executable/) continue
                # Remove flow info from filename
                #fname = gensub(/^([^?]+)(\?.*)?_[0-9]+_[0-9]+_[0-9]+_[0-9]+$/, "\\1", "g", unquote(fname))
                # TODO url for virustotal
                aggr($DstIP4 "\t" $SrcIP4 "\t" _m[_i] "\t" _fname "\t" _fsize[1] "\t" _md5[1], "flows", n)
            }
        }
    }
}
