Esempio n. 1
0
def main():

    # https://docs.python.org/3/library/argparse.html#module-argparse
    # http://tricksntweaks.blogspot.be/2013/05/advance-argument-parsing-in-python.html
    parser = argparse.ArgumentParser(
        description='Generate MPTCP stats & plots',
        fromfile_prefix_chars='@',
    )
    parser.add_argument('--relative', action="store_true", help="set to export relative TCP seq number")
    parser.add_argument('--tshark', dest="tshark_exe", action="store", default="tshark", help="Path to shark binary")
    parser.add_argument('--profile', dest="tshark_exe", action="store", default=None, 
        help="Wireshark profile which contains many options to customize output")

    # TODO tshark.py devrait plutot accepter des streams
    # argparse.FileType('r')
    # parser.add_argument('xpconfig', default="tests.ini", action="store", type=str,  help="Config filename. Describe experiment settings")

    # parser.add_argument('inputPcap', action="store", help="src IP")

    pcap_parser = argparse.ArgumentParser(
        description='Expecting pcap file as input',
        add_help=False,
    )
    pcap_parser.add_argument('inputPcap', action="store", help="Input pcap")

    subparsers = parser.add_subparsers(dest="subparser_name", title="Subparsers", help='sub-command help')

    subparser_csv = subparsers.add_parser('pcap2csv', parents=[pcap_parser], help='Converts pcap to a csv file')
    # subparser_csv.add_argument('inputPcap', action="store", help="Input pcap")
    subparser_csv.add_argument('--output', "-o", action="store", help="csv filename")
    subparser_csv.add_argument('--filter', "-f", action="store", help="Filter", default="")
    subparser_csv.add_argument('fields_filename', type=argparse.FileType('r'), action="store", 
        help="json file mapping name to their wireshark name"
    )

    # List MPTCP connections and subflows
    sp_csv2sql = subparsers.add_parser('csv2sql', help='Imports csv file to an sqlite database')
    sp_csv2sql.add_argument('inputCsv', action="store", help="Input Csv")
    sp_csv2sql.add_argument('output', nargs="?", action="store", help="db filename")

    sp_pcap2sql = subparsers.add_parser('pcap2sql', help='Converts pcap to an sqlite database')
    sp_pcap2sql.add_argument('inputPcap', action="store", help="Input pcap")
    sp_pcap2sql.add_argument('output', nargs="?", action="store", help="db filename")

    args = parser.parse_args(sys.argv[1:])

    exporter = TsharkExporter(tshark_exe, profile=args.profile)
    # exporter.tcp_relative_seq = args.relative if args.relative else True
    exporter.tcp_relative_seq = args.relative 
    # exporter.fields_to_export = fields_to_export

    log.debug("Relative #seq = %s" % exporter.tcp_relative_seq)
    if args.subparser_name == "pcap2csv":
        inputFilename = args.inputPcap
        outputFilename = args.output if args.output else get_basename(inputFilename, "csv")
        fields_to_export = load_fields_to_export_from_file(args.fields_filename)
        exporter.filter = args.filter
        print(fields_to_export)
        exporter.export_pcap_to_csv(inputFilename, outputFilename, fields_to_export)
    elif args.subparser_name == "csv2sql":
        inputFilename = args.inputCsv
        outputFilename = get_basename(inputFilename, "sqlite")
        convert_csv_to_sql(inputFilename, outputFilename, "connections")
    elif args.subparser_name == "pcap2sql":
        inputFilename = args.inputPcap
        outputFilename = get_basename(inputFilename, "sqlite")
        exporter.export_pcap_to_sql(inputFilename, outputFilename)
    else:
        parser.print_help()
Esempio n. 2
0
    def get_matching_csv_filename(self, filename, force_regen : bool):
        """
        Name is bad, since the function can generate  the file if required
        Expects a realpath as filename
        Accept either a .csv or a .pcap file
        Returns realpath towards resulting csv filename
        """
        realpath = filename
        basename, ext = os.path.splitext(realpath)
        # print("Basename=%s" % basename)
        # csv_filename = filename

        if ext == ".csv":
            log.debug("Filename already has a .csv extension")
            csv_filename = realpath
        else:
            print("%s format is not supported as is. Needs to be converted first" %
                (filename))

            def matching_cache_filename(filename):
                """
                Expects a realpath else
                """
                # create a list of path elements (separated by system separator '/' or '\'
                # from the absolute filename
                l = os.path.realpath(filename).split(os.path.sep)
                res = os.path.join(self.config["DEFAULT"]["cache"], '%'.join(l))
                _, ext = os.path.splitext(filename)
                if ext != ".csv":
                    res += ".csv"
                return res

            # csv_filename = filename + ".csv"  #  str(Filetype.csv.value)
            csv_filename = matching_cache_filename(realpath)
            cache_is_invalid = True

            log.debug("Checking for %s" % csv_filename)
            if os.path.isfile(csv_filename):
                log.info("A cache %s was found" % csv_filename)
                ctime_cached = os.path.getctime(csv_filename)
                ctime_pcap = os.path.getctime(filename)
                # print(ctime_cached , " vs ", ctime_pcap)

                if ctime_cached > ctime_pcap:
                    log.debug("Cache seems valid")
                    cache_is_invalid = False
                else:
                    log.debug("Cache seems outdated")


            # if matching csv does not exist yet or if generation forced
            if force_regen or cache_is_invalid:

                # recursively create the directories
                log.debug("Creating cache directory [%s]" % self.config["DEFAULT"]["cache"])
                os.makedirs(self.config["DEFAULT"]["cache"], exist_ok=True)

                log.info("Preparing to convert %s into %s" %
                        (filename, csv_filename))

                exporter = TsharkExporter(
                        self.config["DEFAULT"]["tshark_binary"],
                        self.config["DEFAULT"]["delimiter"],
                        self.config["DEFAULT"]["wireshark_profile"],
                )

                retcode, stderr = exporter.export_to_csv(
                        filename,
                        csv_filename,
                        mp.get_fields("fullname", "name"),
                        tshark_filter="mptcp and not icmp"
                )
                log.info("exporter exited with code=", retcode)
                if retcode:
                    raise Exception(stderr)
        return csv_filename