Improv arguments parsing
[anna.git] / example / diameter / pcapDecoder / tsharkDecoder.sh
1 #!/bin/bash
2
3 # ANNA - Anna is Not Nothingness Anymore
4 #
5 # (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
6 #
7 # http://redmine.teslayout.com/projects/anna-suite
8 #
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
11 # are met:
12 #
13 #     * Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 #     * Redistributions in binary form must reproduce the above
16 # copyright notice, this list of conditions and the following disclaimer
17 # in the documentation and/or other materials provided with the
18 # distribution.
19 #     *  Neither the name of the copyright holder nor the names of its
20 # contributors may be used to endorse or promote products derived from
21 # this software without specific prior written permission.
22 #
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #
35 # Authors: eduardo.ramos.testillano@gmail.com
36 #          cisco.tierra@gmail.com
37
38
39 # Decoder version using tshark tool
40
41 #############
42 # VARIABLES #
43 #############
44 tmpdir=$(mktemp -d)
45
46 #############
47 # FUNCTIONS #
48 #############
49
50 usage() {
51   echo
52   echo "Usage: $0 [-h|--help] [-o|--other-ports] [-d|--results-dir] <pcap>"
53   echo
54   echo "       -h|--help:        this usage help."
55   echo "       -o|--other-ports: space-separated list of ports which frames"
56   echo "                         will be decoded as diameter protocol although"
57   echo "                         not being standard. For example, we could use"
58   echo "                         \"13868\" to disect the Ericsson Sy variant."
59   echo "       -d|--results-dir: directory where results are stored."
60   echo "                         By default, pcap dirname."
61   echo
62   echo "       pcap:             pcap formatted file to be processed."
63   echo
64   echo "       The utility, dumps the extracted hexadecimal content"
65   echo "       and useful information as timestamps, source and"
66   echo "       destination:"
67   echo "          <results directory>/<frame sequence>.hex"
68   echo "          <results directory>/<frame sequence>.metadata"
69   echo
70   _exit
71 }
72
73 parse_arguments() {
74   OTHER_PORTS=
75   RESULTS_DIR=
76   PCAP_FILE=
77
78   while [ $# -gt 0 ]; do
79     case $1 in
80       -h|--help)
81         usage
82       ;;
83
84       -o|--other-ports)
85         OTHER_PORTS="$2"
86         [ -z "$OTHER_PORTS" ] && _exit "Missing non-standard ports list"
87         shift
88       ;;
89
90       -d|--results-dir)
91         RESULTS_DIR=$2
92         shift
93       ;;
94
95       *)
96         first=$(echo $1 | cut -c1)
97         [ "$first" = "-" ] && _exit "Unsupported script option: $1. Type '$SCR_BN -h' (or --help) to print the available options."
98         PCAP_FILE=$1
99       ;;
100     esac
101     shift
102   done
103
104   [ -z "$PCAP_FILE" ] && _exit "Missing pcap file"
105   [ ! -f "$PCAP_FILE" ] && _exit "Cannot found provided pcap file '$PCAP_FILE' !!"
106   [ -z "$RESULTS_DIR" ] && RESULTS_DIR=`dirname $PCAP_FILE`
107   [ ! -d $RESULTS_DIR ] && _exit "The results directory '$RESULTS_DIR' must exists !!"
108 }
109
110 _exit () {
111   echo
112   echo -e $1
113   echo
114
115   # Cleanup
116   rm -rf $tmpdir
117
118   rc=1
119   [ -n "$2" ] && rc=$2
120   exit $rc
121 }
122
123
124 #############
125 # EXECUTION #
126 #############
127
128 echo
129 echo "============================================"
130 echo "Diameter buffer extractor from PCAP raw file"
131 echo "============================================"
132 echo
133
134 # Arguments:
135 [ "$1" = "" -o "$1" = "--help" -o "$1" = "-h" ] && usage
136 parse_arguments "$@"
137
138 # Tshark available:
139 which tshark >/dev/null
140 [ $? -ne 0 ] && _exit "Missing 'tshark' tool !!"
141
142 # 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):
143 # Fields needed (we won't need diameter.hopbyhopid & diameter.endtoendid to verify diameter message as hint patterns; length management will be enough):
144 FIELDS_DIAMETER="-e diameter.cmd.code -e diameter.flags.request -e diameter.applicationId -e diameter.hopbyhopid -e diameter.endtoendid -e diameter.length"
145 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"
146 # Disect selectors for non-standard diameter ports:
147 for port in $OTHER_PORTS
148 do
149   echo "Taking tcp port $port to be decoded as diameter protocol"
150   DISECT_SELECTORS="$DISECT_SELECTORS -d tcp.port=$port,diameter"
151 done
152
153 tshark -E separator="|" -r $PCAP_FILE -N mntC -Tfields $FIELDS $DISECT_SELECTORS 2>/dev/null | grep -i diameter > $tmpdir/diameter_frames
154 # Example output:
155 #                                                                               /length\
156 # frame     timestamp        src     dst   code R  App-ID   HopByHop   EndToEnd DIAM TCP          protocol                         segments 
157 #   1|1427215933.697904000|gt_traf|vcbavipt|272|1|16777238|0x0004e6e6|0x000bd986|432|432|eth:ip:tcp:diameter:diameter:diameter3gpp|
158 #   3|1427215934.449523000|vcbavipt|gt_traf|272|0|16777238|0x0004e6e6|0x000bd986|292|292|eth:ip:tcp:diameter:diameter:diameter3gpp|
159 #   5|1427215934.456160000|gt_traf|vcbavipt|||||||1400|eth:ip:tcp:diameter|
160 #   6|1427215934.456204000|gt_traf|vcbavipt|265|1|16777236|0x000c73c3|0x0004cee4|1972|572|eth:ip:tcp:diameter:diameter:diameter3gpp|5,6
161 #   8|1427215935.123559000|vcbavipt|gt_traf|265|0|16777236|0x000c73c3|0x0004cee4|248|248|eth:ip:tcp:diameter:diameter:diameter3gpp|
162 all_frames=( $(cat $tmpdir/diameter_frames | cut -d\| -f1) )
163 needs_join=( $(cat $tmpdir/diameter_frames | cut -d\| -f13) )
164 main_frames=( $(cat $tmpdir/diameter_frames | awk -F\| '{ if ($11 != "") print $1 }') )
165
166 # Reassemble procedure (using frame 1 as example):
167 # (for non segmented frames, it is enough with tcp or diameter length within the frame content itself)
168 # 1) Get the TCP length: 432 bytes. 432*2 = 864 characters per byte in hexadecimal string format
169 # 2) Get the frame length: `wc -c $tmpdir/block.$frame` => 997
170 # 3) Get 864 from the tail: `cat $tmpdir/block.$frame | cut -c133
171
172 # Dump the hex blocks for all the diameter frames:
173 cat $PCAP_FILE | rawshark -s -r - -d proto:diameter -F data 2>/dev/null > $tmpdir/all_hex_data
174 for frame in ${all_frames[@]}; do
175   grep "^$frame " $tmpdir/all_hex_data | cut -d\" -f2 | sed 's/://g' > $tmpdir/block.$frame
176   frame_info=$(grep "^${frame}|" $tmpdir/diameter_frames)
177
178   # Get the diameter part:
179   tcp_len=$(echo $frame_info | cut -d\| -f11)
180   frm_len=$(wc -c $tmpdir/block.$frame | awk '{ print $1 }')
181   cut_len=$((frm_len-2*tcp_len))
182   cat $tmpdir/block.$frame | cut -c${cut_len}- > $RESULTS_DIR/$frame.hex
183   echo -n "Created $RESULTS_DIR/$frame.hex"
184
185   # Metadata:
186   ts=$(echo $frame_info | cut -d\| -f2)
187   date=$(date -d @$ts)
188   src=$(echo $frame_info | cut -d\| -f3)
189   dst=$(echo $frame_info | cut -d\| -f4)
190   code=$(echo $frame_info | cut -d\| -f5)
191   isreq=$(echo $frame_info | cut -d\| -f6)
192   appid=$(echo $frame_info | cut -d\| -f7)
193   hbh=$(echo $frame_info | cut -d\| -f8)
194   e2e=$(echo $frame_info | cut -d\| -f9)
195   # To decimal:
196   hbh=$(printf "%d\n" $hbh)
197   e2e=$(printf "%d\n" $e2e)
198   echo "date=$date" > $RESULTS_DIR/$frame.metadata
199   echo "timestamp=$ts" >> $RESULTS_DIR/$frame.metadata
200   echo "src=$src" >> $RESULTS_DIR/$frame.metadata
201   echo "dst=$dst" >> $RESULTS_DIR/$frame.metadata
202   echo "code=$code" >> $RESULTS_DIR/$frame.metadata
203   echo "isrequest=$isreq" >> $RESULTS_DIR/$frame.metadata
204   echo "applicationid=$appid" >> $RESULTS_DIR/$frame.metadata
205   #echo "sequence=${hbh}.${e2e}" >> $RESULTS_DIR/$frame.metadata
206 #  echo "hopbyhop=$hbh" >> $RESULTS_DIR/$frame.metadata
207 #  echo "endtoend=$e2e" >> $RESULTS_DIR/$frame.metadata
208
209   echo " and $RESULTS_DIR/$frame.metadata"
210 done
211
212 # Join frames which need to be reassembled:
213 for group in ${needs_join[@]}; do
214   echo "Grouping frames $group ..."
215   group_array=( $(echo $group | sed 's/,/ /g') )
216   for frame in ${group_array[@]}; do
217     cat $RESULTS_DIR/$frame.hex >> $tmpdir/diam.$group
218   done
219   cat $tmpdir/diam.$group | tr -d '\n' > $RESULTS_DIR/$frame.hex
220 done
221
222 # Delete superfluous metadata:
223 echo "Deleting superfluous buffers & metadata ..."
224 segments=( $(cat $tmpdir/diameter_frames | awk -F\| '{ if ($10 == "") print $1 }') )
225 for s in ${segments[@]}; do rm $RESULTS_DIR/$s.*; done
226
227
228 _exit "Done!" 0
229