mme_test_s1_pcap2pdml 3.34 KB
Newer Older
gauthier's avatar
gauthier committed
1 2 3 4 5 6 7 8 9 10
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import subprocess
import re
import socket
import datetime
from datetime import date
import os, errno
import argparse
gauthier's avatar
gauthier committed
11
import tempfile
gauthier's avatar
gauthier committed
12
from lxml import etree
gauthier's avatar
gauthier committed
13
from xml.dom.minidom import parse, parseString
gauthier's avatar
gauthier committed
14

gauthier's avatar
gauthier committed
15 16 17
#####################
# program arguments
#####################
gauthier's avatar
gauthier committed
18 19 20 21
parser = argparse.ArgumentParser()
parser.add_argument("--pcap_file", "-p", type=str,help="input pcap file to be translated")
args = parser.parse_args()

gauthier's avatar
gauthier committed
22 23 24 25 26
#####################
# get xml document from pcap
#####################
orig_pcap_file_name = args.pcap_file.strip()
orig_pdml_string = subprocess.check_output(["tshark", '-T', 'pdml', '-r', orig_pcap_file_name])
gauthier's avatar
gauthier committed
27

gauthier's avatar
gauthier committed
28
orig_dom = parseString(orig_pdml_string)
gauthier's avatar
gauthier committed
29

gauthier's avatar
gauthier committed
30 31 32 33 34 35 36 37
#####################
# filtering unwanted packets
#####################
#cases = orig_etree.findall(".//proto[@name='sctp']")
packets = orig_dom.getElementsByTagName("packet")
for packet in packets:
    found_sctp = False
    found_s1ap = False
gauthier's avatar
gauthier committed
38 39 40
    sctp_node = None
    frame_node = None
    ip_node = None
gauthier's avatar
gauthier committed
41 42 43 44
    protos = packet.getElementsByTagName("proto")
    for proto in protos:
        attrs   = proto.attributes
        urlnode = attrs['name']
gauthier's avatar
gauthier committed
45 46 47 48 49 50
        if urlnode.nodeValue == 'frame':
            frame_node = proto
        elif urlnode.nodeValue == 'ip':
            frame_node.appendChild(proto)
            ip_node = proto
        elif urlnode.nodeValue == 'sctp':
gauthier's avatar
gauthier committed
51
            found_sctp = True
gauthier's avatar
gauthier committed
52 53
            ip_node.appendChild(proto)
            sctp_node = proto
gauthier's avatar
gauthier committed
54 55
        elif urlnode.nodeValue == 's1ap':
            found_s1ap = True
gauthier's avatar
gauthier committed
56
            sctp_node.appendChild(proto)
gauthier's avatar
gauthier committed
57 58 59 60
        elif urlnode.nodeValue == 'geninfo':
            packet.removeChild(proto)
        elif urlnode.nodeValue == 'eth':
            packet.removeChild(proto)
61
    if found_sctp == False:
gauthier's avatar
gauthier committed
62 63
        # hopefully it seems to work (remove iterated packet)
        packet.parentNode.removeChild(packet)
gauthier's avatar
gauthier committed
64

gauthier's avatar
gauthier committed
65 66 67
#####################
# dom to xml string
#####################
gauthier's avatar
gauthier committed
68
filtered_pdml_string = orig_dom.toprettyxml(indent="  ")
gauthier's avatar
gauthier committed
69 70 71 72 73
cleaned_pdml_string = ""
#####################
# remove blank lines in xml string
#####################
lines = filtered_pdml_string.splitlines()
gauthier's avatar
gauthier committed
74
for line in lines:
gauthier's avatar
gauthier committed
75 76 77
    if len(line.strip()):
        if line[:-1]:
            cleaned_pdml_string += line + '\r\n'
gauthier's avatar
gauthier committed
78 79
#print "'%s'" %  cleaned_pdml_string 
#####################
gauthier's avatar
gauthier committed
80
# write pdml string to pdml file
gauthier's avatar
gauthier committed
81
#####################
gauthier's avatar
gauthier committed
82
out_pdml_file_name = os.path.dirname(orig_pcap_file_name) + '/' + os.path.splitext(os.path.basename(orig_pcap_file_name))[0] + '.pdml'
gauthier's avatar
gauthier committed
83 84 85
out_file = open(out_pdml_file_name, "w")
out_file.write(cleaned_pdml_string)
out_file.close()
gauthier's avatar
gauthier committed
86

gauthier's avatar
gauthier committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100
############################################################
# DECEIVING HTML BONUS: DO NOT SEEM TO WORK CORRECTLY IN FIREFOX
# DID NOT INVESTIGATE
#####################
# write xml string to html file
#####################
xsl_root = etree.fromstring(open('/usr/share/wireshark/pdml2html.xsl').read())
transform = etree.XSLT(xsl_root)
xml_root = etree.fromstring(cleaned_pdml_string)
trans_root = transform(xml_root)
filtered_html_string = etree.tostring(trans_root)
#####################
# write html string to html file
#####################
gauthier's avatar
gauthier committed
101
out_html_file_name = os.path.dirname(orig_pcap_file_name) + '/' +os.path.splitext(os.path.basename(orig_pcap_file_name))[0] + '.html'
gauthier's avatar
gauthier committed
102 103 104
out_file = open(out_html_file_name, "w")
out_file.write(filtered_html_string)
out_file.close()