#!/usr/bin/env awk
#
# Returns information about hosts sending authentication in cleartext.
#
# Dependencies:
#   - pwX
#
# Arguments:
#   - val
#   - num
#
# If val is omitted or equal to "Flows" (case insensitive),
# counts the number of flows.
#
# If num is omitted, returns the full list, otherwise the top (num > 0) or
# bottom (num < 0) num results
#
# Example:
#   - tawk 'passwords()' file.txt
#   - tawk 'passwords($numBytesSnt)' file.txt
#   - tawk 'passwords($numBytesSnt "\t" $numPktsSnt)' file.txt
#   - tawk 'passwords("Flows", 5)' file.txt

@include "aggr"
@include "_validate_col"

function passwords(val, num,        _proto, _atype) {
    _validate_col(SrcIP4, "SrcIP4")
    _validate_col(extractedAuthType, "extractedAuthType")
    _validate_col(extractedUsername, "extractedUsername")
    _validate_col(extractedPassword, "extractedPassword")
    _proto[1]  = "FTP"
    _proto[2]  = "POP3"
    _proto[3]  = "IMAP"
    _proto[4]  = "SMTP"
    _proto[5]  = "HTTP Basic"
    _proto[6]  = "HTTP Proxy"
    _proto[7]  = "HTTP GET"
    _proto[8]  = "HTTP POST"
    _proto[9]  = "IRC"
    _proto[10] = "Telnet"
    _proto[11] = "LDAP"
    _atype = strtonum($extractedAuthType)
    if (hdr()) {
        # print the header
        print $SrcIP4 "\t" $DstIP4 "\t" "Proto" "\t" "Username" "\t" "Password" "\t" val
    } else if (_atype) {
        aggr($SrcIP4 "\t" $DstIP4 "\t" _proto[_atype] "\t" $extractedUsername "\t" $extractedPassword, val, num)
    }
}
