X-Git-Url: https://git.teslayout.com/public/public/public/?a=blobdiff_plain;f=example%2Fdiameter%2FpcapDecoder%2FtsharkDecoder.sh;h=c290e1754d809ae089ff644cef58decc8f0515b1;hb=77105a89de724b0bc5b4b9c9f2844cb44db1de3e;hp=b131fe5389e00b98d1e293a27c36f09e1aff9e5e;hpb=0da803c3f1efaeffb1ccafe38013593e2de887a6;p=anna.git diff --git a/example/diameter/pcapDecoder/tsharkDecoder.sh b/example/diameter/pcapDecoder/tsharkDecoder.sh index b131fe5..c290e17 100755 --- a/example/diameter/pcapDecoder/tsharkDecoder.sh +++ b/example/diameter/pcapDecoder/tsharkDecoder.sh @@ -42,27 +42,85 @@ # VARIABLES # ############# tmpdir=$(mktemp -d) +TSHARK=tshark ############# # FUNCTIONS # ############# -usage () { - echo "Usage: $0 [results_dir]" +usage() { echo - echo " pcap_file: pcap formatted file to be processed." - echo " results_dir: directory where results are stored." - echo " By default, pcap file dirname is used." + echo "Usage: $0 [-h|--help] [-o|--other-ports ] [-d|--results-dir ] [-s|--sids ] " echo - echo " The utility, dumps the extracted hexadecimal content" - echo " and useful information as timestamps, source and" - echo " destination:" - echo " /.hex" - echo " /.metadata" + echo " -h|--help: this usage help." + echo " -o|--other-ports : space-separated list of ports which frames" + echo " will be decoded as diameter protocol although" + echo " not being standard. For example, we could use" + echo " \"13868\" to disect the Ericsson Sy variant." + echo " -d|--results-dir : directory where results are stored." + echo " By default, pcap dirname." + echo " -s|--sids : file containing a list of Session-Id values" + echo " (one per line) to be taken into account." + echo " If missing, all the frames will be extracted." + echo + echo " pcap: pcap formatted file to be processed." + echo + echo " The utility, dumps the extracted hexadecimal content and useful information" + echo " (timestamps, source, destination, etc.) within a metadata file:" + echo + echo " /.hex" + echo " /.metadata" echo _exit } +parse_arguments() { + OTHER_PORTS= + RESULTS_DIR= + SIDS_FILE= + PCAP_FILE= + + while [ $# -gt 0 ]; do + case $1 in + -h|--help) + usage + ;; + + -o|--other-ports) + OTHER_PORTS="$2" + [ -z "$OTHER_PORTS" ] && _exit "Missing non-standard ports list" + shift + ;; + + -d|--results-dir) + RESULTS_DIR=$2 + shift + ;; + + -s|--sids) + SIDS_FILE=$2 + shift + ;; + + *) + first=$(echo $1 | cut -c1) + [ "$first" = "-" ] && _exit "Unsupported script option: $1. Type '$SCR_BN -h' (or --help) to print the available options." + PCAP_FILE=$1 + ;; + esac + shift + done + + [ -z "$PCAP_FILE" ] && _exit "Missing pcap file" + [ ! -f "$PCAP_FILE" ] && _exit "Cannot found provided pcap file '$PCAP_FILE' !!" + [ -z "$RESULTS_DIR" ] && RESULTS_DIR=`dirname $PCAP_FILE` + [ ! -d $RESULTS_DIR ] && _exit "The results directory '$RESULTS_DIR' must exists !!" + if [ -n "$SIDS_FILE" ] + then + [ ! -f $SIDS_FILE ] && _exit "The Session-Id list file '$SIDS_FILE' provided, does not exist !!" + fi +} + _exit () { echo echo -e $1 @@ -87,42 +145,38 @@ echo "Diameter buffer extractor from PCAP raw file" echo "============================================" echo -# Usage: -[ "$1" = "" ] && usage - -# Pcap file: -PCAP_FILE=$1 -[ ! -f $PCAP_FILE ] && _exit "Cannot found provided pcap file '$1' !!" +# Arguments: +[ "$1" = "" -o "$1" = "--help" -o "$1" = "-h" ] && usage +parse_arguments "$@" # Tshark available: -which tshark >/dev/null +which $TSHARK >/dev/null 2>/dev/null [ $? -ne 0 ] && _exit "Missing 'tshark' tool !!" -# Optional result dir: -RESULTS_DIR=`dirname $PCAP_FILE` -[ "$2" != "" ] && RESULTS_DIR=$2 -[ ! -d $RESULTS_DIR ] && _exit "The results directory '$RESULTS_DIR' must exists !!" - # Get the frames with diameter content (take care about '-2' two-pass option and don't add it, because we need to get reassembled parts in their corresponding frames): -# Fields needed (we won't need diameter.hopbyhopid & diameter.endtoendid to verify diameter message as hint patterns; length management will be enough): -FIELDS_DIAMETER="-e diameter.cmd.code -e diameter.flags.request -e diameter.applicationId -e diameter.hopbyhopid -e diameter.endtoendid -e diameter.length" +# Fields needed (we won't need diameter.hopbyhopid & diameter.endtoendid to verify diameter message as hint patterns; length management will be enough): see https://www.wireshark.org/docs/dfref/d/diameter.html +FIELDS_DIAMETER="-e diameter.cmd.code -e diameter.flags.request -e diameter.applicationId -e diameter.hopbyhopid -e diameter.endtoendid -e diameter.Session-Id -e diameter.Origin-Host -e diameter.length" FIELDS="-e frame.number -e frame.time_epoch -e ip.src_host -e ip.dst_host $FIELDS_DIAMETER -e tcp.len -e frame.protocols -e tcp.segment" # Disect selectors for non-standard diameter ports: -# 13868 port for Ericsson Sy -DISECT_SELECTORS="-d tcp.port=13868,diameter" +for port in $OTHER_PORTS +do + echo "Taking tcp port $port to be decoded as diameter protocol" + DISECT_SELECTORS="$DISECT_SELECTORS -d tcp.port=$port,diameter" +done -tshark -E separator="|" -r $PCAP_FILE -N mntC -Tfields $FIELDS $DISECT_SELECTORS 2>/dev/null | grep -i diameter > $tmpdir/diameter_frames +$TSHARK -E separator="|" -r $PCAP_FILE -N mntC -Tfields $FIELDS $DISECT_SELECTORS 2>/dev/null | grep -i diameter > $tmpdir/diameter_frames # Example output: -# /length\ -# frame timestamp src dst code R App-ID HopByHop EndToEnd DIAM TCP protocol segments -# 1|1427215933.697904000|gt_traf|vcbavipt|272|1|16777238|0x0004e6e6|0x000bd986|432|432|eth:ip:tcp:diameter:diameter:diameter3gpp| -# 3|1427215934.449523000|vcbavipt|gt_traf|272|0|16777238|0x0004e6e6|0x000bd986|292|292|eth:ip:tcp:diameter:diameter:diameter3gpp| -# 5|1427215934.456160000|gt_traf|vcbavipt|||||||1400|eth:ip:tcp:diameter| -# 6|1427215934.456204000|gt_traf|vcbavipt|265|1|16777236|0x000c73c3|0x0004cee4|1972|572|eth:ip:tcp:diameter:diameter:diameter3gpp|5,6 -# 8|1427215935.123559000|vcbavipt|gt_traf|265|0|16777236|0x000c73c3|0x0004cee4|248|248|eth:ip:tcp:diameter:diameter:diameter3gpp| +# /length\ +# frame timestamp src dst code R App-ID HopByHop EndToEnd Session-Id Origin-Host DIAM TCP protocol segments +# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 +# 1|1427215933.697904000|gt_traf|vcbavipt|272|1|16777238|0x0004e6e6|0x000bd986|xxxxxxx|xxxxxxxxxxxxx|432|432|eth:ip:tcp:diameter:diameter:diameter3gpp| +# 3|1427215934.449523000|vcbavipt|gt_traf|272|0|16777238|0x0004e6e6|0x000bd986|xxxxxxx|xxxxxxxxxxxxx|292|292|eth:ip:tcp:diameter:diameter:diameter3gpp| +# 5|1427215934.456160000|gt_traf|vcbavipt||||||||1400|eth:ip:tcp:diameter| +# 6|1427215934.456204000|gt_traf|vcbavipt|265|1|16777236|0x000c73c3|0x0004cee4|xxxxxxx|xxxxxxxxxxxxx|1972|572|eth:ip:tcp:diameter:diameter:diameter3gpp|5,6 +# 8|1427215935.123559000|vcbavipt|gt_traf|265|0|16777236|0x000c73c3|0x0004cee4|xxxxxxx|xxxxxxxxxxxxx|248|248|eth:ip:tcp:diameter:diameter:diameter3gpp| all_frames=( $(cat $tmpdir/diameter_frames | cut -d\| -f1) ) -needs_join=( $(cat $tmpdir/diameter_frames | cut -d\| -f13) ) -main_frames=( $(cat $tmpdir/diameter_frames | awk -F\| '{ if ($11 != "") print $1 }') ) +needs_join=( $(cat $tmpdir/diameter_frames | cut -d\| -f15) ) +main_frames=( $(cat $tmpdir/diameter_frames | awk -F\| '{ if ($13 != "") print $1 }') ) # Reassemble procedure (using frame 1 as example): # (for non segmented frames, it is enough with tcp or diameter length within the frame content itself) @@ -137,7 +191,7 @@ for frame in ${all_frames[@]}; do frame_info=$(grep "^${frame}|" $tmpdir/diameter_frames) # Get the diameter part: - tcp_len=$(echo $frame_info | cut -d\| -f11) + tcp_len=$(echo $frame_info | cut -d\| -f13) frm_len=$(wc -c $tmpdir/block.$frame | awk '{ print $1 }') cut_len=$((frm_len-2*tcp_len)) cat $tmpdir/block.$frame | cut -c${cut_len}- > $RESULTS_DIR/$frame.hex @@ -151,11 +205,13 @@ for frame in ${all_frames[@]}; do code=$(echo $frame_info | cut -d\| -f5) isreq=$(echo $frame_info | cut -d\| -f6) appid=$(echo $frame_info | cut -d\| -f7) - hbh=$(echo $frame_info | cut -d\| -f8) - e2e=$(echo $frame_info | cut -d\| -f9) - # To decimal: - hbh=$(printf "%d\n" $hbh) - e2e=$(printf "%d\n" $e2e) + sid=$(echo $frame_info | cut -d\| -f10) + oh=$(echo $frame_info | cut -d\| -f11) + #hbh=$(echo $frame_info | cut -d\| -f8) + #e2e=$(echo $frame_info | cut -d\| -f9) + # HBH and ETE To decimal: + #hbh=$(printf "%d\n" $hbh) + #e2e=$(printf "%d\n" $e2e) echo "date=$date" > $RESULTS_DIR/$frame.metadata echo "timestamp=$ts" >> $RESULTS_DIR/$frame.metadata echo "src=$src" >> $RESULTS_DIR/$frame.metadata @@ -163,9 +219,10 @@ for frame in ${all_frames[@]}; do echo "code=$code" >> $RESULTS_DIR/$frame.metadata echo "isrequest=$isreq" >> $RESULTS_DIR/$frame.metadata echo "applicationid=$appid" >> $RESULTS_DIR/$frame.metadata - #echo "sequence=${hbh}.${e2e}" >> $RESULTS_DIR/$frame.metadata -# echo "hopbyhop=$hbh" >> $RESULTS_DIR/$frame.metadata -# echo "endtoend=$e2e" >> $RESULTS_DIR/$frame.metadata + [ -n "$sid" ] && echo "sessionid=$sid" >> $RESULTS_DIR/$frame.metadata + echo "originhost=$oh" >> $RESULTS_DIR/$frame.metadata + #echo "hopbyhop=$hbh" >> $RESULTS_DIR/$frame.metadata + #echo "endtoend=$e2e" >> $RESULTS_DIR/$frame.metadata echo " and $RESULTS_DIR/$frame.metadata" done @@ -182,9 +239,41 @@ done # Delete superfluous metadata: echo "Deleting superfluous buffers & metadata ..." -segments=( $(cat $tmpdir/diameter_frames | awk -F\| '{ if ($10 == "") print $1 }') ) +segments=( $(cat $tmpdir/diameter_frames | awk -F\| '{ if ($12 == "") print $1 }') ) for s in ${segments[@]}; do rm $RESULTS_DIR/$s.*; done +# Detecting Session-Id values: +grep ^sessionid= $RESULTS_DIR/*.metadata 2>/dev/null | cut -d= -f2- | sort -u > $RESULTS_DIR/session-ids +if [ -s $RESULTS_DIR/session-ids ] +then + count=0 + while read -r line; do count=$((count+1)) ; echo "Detected Session-Id $count: $line"; done < $RESULTS_DIR/session-ids + rm $RESULTS_DIR/session-ids +fi + +# Detecting Origin-Host values: +grep ^originhost= $RESULTS_DIR/*.metadata 2>/dev/null | cut -d= -f2- | sort -u > $RESULTS_DIR/origin-hosts +if [ -s $RESULTS_DIR/origin-hosts ] +then + count=0 + while read -r line; do count=$((count+1)) ; echo "Detected Origin-Host $count: $line"; done < $RESULTS_DIR/origin-hosts + #rm $RESULTS_DIR/origin-hosts +fi + +# Purge frames with Session-Id not wanted: +if [ -n "$SIDS_FILE" ] +then + grep -l -w -f $SIDS_FILE $RESULTS_DIR/*metadata > $RESULTS_DIR/.wanted + grep -l ^sessionid $RESULTS_DIR/*metadata > $RESULTS_DIR/.all + for file in `grep -vf $RESULTS_DIR/.wanted $RESULTS_DIR/.all` + do + frm=$(basename $file | cut -d\. -f1) + sid=$(grep ^sessionid= $file | cut -d= -f2-) + echo "Purge results for frame $frm (Session-Id: '$sid') ..." + rm $RESULTS_DIR/${frm}.* + done + rm $RESULTS_DIR/.wanted $RESULTS_DIR/.all +fi _exit "Done!" 0