Пример #1
0
    def __init__(self, netzob, plugin):
        view = WiresharkExporterView(plugin, self)
        super(WiresharkExporterController, self).__init__(netzob, plugin, view)

        # Intialize signals manager
        self.signalsManager = SignalsManager()

        # Attach events
        self.signalsManager.attach(self._onSymbolChanged_cb,
                                   (SIG_SYMBOL_CHANGED, ))
        self.signalsManager.attach(self._onSaveScript_cb, (SIG_SAVE_SCRIPT, ))
    def __init__(self, netzob, plugin):
        view = WiresharkExporterView(plugin, self)
        super(WiresharkExporterController, self).__init__(netzob, plugin, view)

        # Intialize signals manager
        self.signalsManager = SignalsManager()

        # Attach events
        self.signalsManager.attach(self._onSymbolChanged_cb, (SIG_SYMBOL_CHANGED,))
        self.signalsManager.attach(self._onSaveScript_cb, (SIG_SAVE_SCRIPT,))
class WiresharkExporterController(AbstractExporterController):
    """
    Controller of Wireshark export plugin.
    """
    def __init__(self, netzob, plugin):
        view = WiresharkExporterView(plugin, self)
        super(WiresharkExporterController, self).__init__(netzob, plugin, view)

        # Intialize signals manager
        self.signalsManager = SignalsManager()

        # Attach events
        self.signalsManager.attach(self._onSymbolChanged_cb, (SIG_SYMBOL_CHANGED,))
        self.signalsManager.attach(self._onSaveScript_cb, (SIG_SAVE_SCRIPT,))

    def getMessageContext(self, msg):
        def clean(s):
            # Respect wireshark syntax.
            # Allowed are lower characters, digits, '-', '_' and '.'
            return re.sub("[^a-z\-_\.]", "_", str(s).lower())
        sym = msg.getSymbol()
        proto_name = clean(sym.getName())
        proto_keyname = proto_name.upper()
        proto_desc = "{} Protocol".format(proto_name.capitalize())
        class_var = "proto_{}".format(msg.getID().replace('-', '_'))
        if isinstance(msg, L4NetworkMessage):
            filter_name = msg.getL4Protocol()
        elif isinstance(msg, L3NetworkMessage):
            filter_name = msg.getL3Protocol()
        elif isinstance(msg, L2NetworkMessage):
            filter_name = msg.getL2Protocol()
        else:
            raise WiresharkExporterError("Cannot find a compatible protocol for {}.".format(msg))

        del sym

        return locals()

    def __writeDynSizeBlock(self, buf, field, sorted_ivalues):
        with buf.new_block("do"):
            buf << "local values = {{{}}}"\
                .format(", ".join('"{}"'.format(val) for val in sorted_ivalues))
            with buf.new_block("for k,v in next,values,nil do"):
                buf << "local vlen = v:len() / 2"
                with buf.new_block("if buffer(idx):len() >= vlen and tostring(ByteArray.new(v)) == tostring(buffer(idx,vlen):bytes()) then"):
                    buf << 'subtree:add(buffer(idx,vlen), "{prefix}: " .. v)'\
                        .format(prefix=field.getName())
                    buf << "idx = idx + vlen"
                    buf << "break"

    def __writeUniqueSizeBlock(self, buf, field, values):
        j = min(map(len, values))
        with buf.new_block("if buffer(idx):len() >= {} then".format(j)):
            buf << 'subtree:add(buffer(idx,{length}), "{prefix}: " .. buffer(idx,{length}))'\
                .format(length=j, prefix=field.getName())
            buf << "idx = idx + {}".format(j)

    def generateSymbolDissector(self, sym):
        msgs = sym.getMessages()
        ctx = self.getMessageContext(msgs[0])
        buf = LUACodeBuffer()
        buf << "--\n-- Symbol {proto_keyname}\n--\n".format(**ctx)
        buf << """{class_var} = Proto("{proto_name}", "{proto_name} Protocol")
function {class_var}.dissector(buffer, pinfo, tree)
  pinfo.cols.protocol = "{proto_keyname}"
  local subtree = tree:add({class_var}, buffer(), "{proto_desc}")
  local idx = 0
""".format(**ctx)

        fields = sym.getExtendedFields()
        splittedData = [msg.getSplittedData(fields, msg.data) for msg in msgs]
        with buf.new_block():
            for field, ivalues in zip(fields, zip(*splittedData)):
                sorted_ivalues = sorted(set(str(v) for v in ivalues if v), key=len, reverse=True)
                values = map(methodcaller('decode', 'hex'), ivalues)

                if len(set(map(len, values))) > 1:
                    self.__writeDynSizeBlock(buf, field, sorted_ivalues)
                else:
                    self.__writeUniqueSizeBlock(buf, field, values)
                # TODO: re-implement this...
                ## with buf.new_block():
                ##     buf_type = _getLuaTvbType(field)
                ##     if buf_type is not None:
                ##         buf << ':{}()'.format(buf_type))
                ##     buf << ')'

        # Register dissector function to specific filter criterion
        filter_ = WiresharkFilterFactory.getFilter(sym)
        luatype = _getLuaTableType(filter_.pytype)
        for expr in filter_.getExpressions():
            buf << """if not pcall(DissectorTable.get, "{0}") then
  DissectorTable.new("{0}", "Netzob-generated table", {type})
end
DissectorTable.get("{0}"):add({1}, {class_var})
""".format(*expr, type=luatype, **ctx)

        return buf.getvalue()

    def run(self):
        data = []
        proj = self.netzob.getCurrentProject()

        if not proj:
            NetzobErrorMessage(_("No project selected."), parent=self.netzob.view.mainWindow)
            return

        syms = proj.getVocabulary().getSymbols()
        dial = self.view.buildDialog()
        self.view.updateSymbols(syms)
        dial.show_all()

    def __call__(self):
        self.run()

    def getSignalsManager(self):
        """returns the signals manager"""
        return self.signalsManager

    ##########
    # Events #
    ##########
    def _onSymbolChanged_cb(self, sig, tv):
        sel = tv.get_selection()

        if not sel:
            return  # on closing, event occurs

        tm, it = sel.get_selected()
        sym_id = tm.get_value(it, 0)
        voca = self.netzob.getCurrentProject().getVocabulary()

        sym = voca.getSymbol(sym_id)
        if sym is None:
            self.view.clearText()
            for sym in voca.getSymbols():
                try:
                    self.view.appendText(self.generateSymbolDissector(sym))
                except WiresharkExporterError, wee:
                    self.view.appendComment(wee)
                    NetzobWarningMessage("[{}] {}".format(sym.getName(), wee), self.view.dialog)
        else:
Пример #4
0
class WiresharkExporterController(AbstractExporterController):
    """
    Controller of Wireshark export plugin.
    """
    def __init__(self, netzob, plugin):
        view = WiresharkExporterView(plugin, self)
        super(WiresharkExporterController, self).__init__(netzob, plugin, view)

        # Intialize signals manager
        self.signalsManager = SignalsManager()

        # Attach events
        self.signalsManager.attach(self._onSymbolChanged_cb,
                                   (SIG_SYMBOL_CHANGED, ))
        self.signalsManager.attach(self._onSaveScript_cb, (SIG_SAVE_SCRIPT, ))

    def getMessageContext(self, msg):
        def clean(s):
            # Respect wireshark syntax.
            # Allowed are lower characters, digits, '-', '_' and '.'
            return re.sub("[^a-z\-_\.]", "_", str(s).lower())

        sym = msg.getSymbol()
        proto_name = clean(sym.getName())
        proto_keyname = proto_name.upper()
        proto_desc = "{} Protocol".format(proto_name.capitalize())
        class_var = "proto_{}".format(msg.getID().replace('-', '_'))
        if isinstance(msg, L4NetworkMessage):
            filter_name = msg.getL4Protocol()
        elif isinstance(msg, L3NetworkMessage):
            filter_name = msg.getL3Protocol()
        elif isinstance(msg, L2NetworkMessage):
            filter_name = msg.getL2Protocol()
        else:
            raise WiresharkExporterError(
                "Cannot find a compatible protocol for {}.".format(msg))

        del sym

        return locals()

    def __writeDynSizeBlock(self, buf, field, sorted_ivalues):
        with buf.new_block("do"):
            buf << "local values = {{{}}}"\
                .format(", ".join('"{}"'.format(val) for val in sorted_ivalues))
            with buf.new_block("for k,v in next,values,nil do"):
                buf << "local vlen = v:len() / 2"
                with buf.new_block(
                        "if buffer(idx):len() >= vlen and tostring(ByteArray.new(v)) == tostring(buffer(idx,vlen):bytes()) then"
                ):
                    buf << 'subtree:add(buffer(idx,vlen), "{prefix}: " .. v)'\
                        .format(prefix=field.getName())
                    buf << "idx = idx + vlen"
                    buf << "break"

    def __writeUniqueSizeBlock(self, buf, field, values):
        j = min(map(len, values))
        with buf.new_block("if buffer(idx):len() >= {} then".format(j)):
            buf << 'subtree:add(buffer(idx,{length}), "{prefix}: " .. buffer(idx,{length}))'\
                .format(length=j, prefix=field.getName())
            buf << "idx = idx + {}".format(j)

    def generateSymbolDissector(self, sym):
        msgs = sym.getMessages()
        ctx = self.getMessageContext(msgs[0])
        buf = LUACodeBuffer()
        buf << "--\n-- Symbol {proto_keyname}\n--\n".format(**ctx)
        buf << """{class_var} = Proto("{proto_name}", "{proto_name} Protocol")
function {class_var}.dissector(buffer, pinfo, tree)
  pinfo.cols.protocol = "{proto_keyname}"
  local subtree = tree:add({class_var}, buffer(), "{proto_desc}")
  local idx = 0
""".format(**ctx)

        fields = sym.getExtendedFields()
        splittedData = [msg.getSplittedData(fields, msg.data) for msg in msgs]
        with buf.new_block():
            for field, ivalues in zip(fields, zip(*splittedData)):
                sorted_ivalues = sorted(set(str(v) for v in ivalues if v),
                                        key=len,
                                        reverse=True)
                values = map(methodcaller('decode', 'hex'), ivalues)

                if len(set(map(len, values))) > 1:
                    self.__writeDynSizeBlock(buf, field, sorted_ivalues)
                else:
                    self.__writeUniqueSizeBlock(buf, field, values)
                # TODO: re-implement this...
                ## with buf.new_block():
                ##     buf_type = _getLuaTvbType(field)
                ##     if buf_type is not None:
                ##         buf << ':{}()'.format(buf_type))
                ##     buf << ')'

        # Register dissector function to specific filter criterion
        filter_ = WiresharkFilterFactory.getFilter(sym)
        luatype = _getLuaTableType(filter_.pytype)
        for expr in filter_.getExpressions():
            buf << """if not pcall(DissectorTable.get, "{0}") then
  DissectorTable.new("{0}", "Netzob-generated table", {type})
end
DissectorTable.get("{0}"):add({1}, {class_var})
""".format(*expr, type=luatype, **ctx)

        return buf.getvalue()

    def run(self):
        data = []
        proj = self.netzob.getCurrentProject()

        if not proj:
            NetzobErrorMessage(_("No project selected."),
                               parent=self.netzob.view.mainWindow)
            return

        syms = proj.getVocabulary().getSymbols()
        dial = self.view.buildDialog()
        self.view.updateSymbols(syms)
        dial.show_all()

    def __call__(self):
        self.run()

    def getSignalsManager(self):
        """returns the signals manager"""
        return self.signalsManager

    ##########
    # Events #
    ##########
    def _onSymbolChanged_cb(self, sig, tv):
        sel = tv.get_selection()

        if not sel:
            return  # on closing, event occurs

        tm, it = sel.get_selected()
        sym_id = tm.get_value(it, 0)
        voca = self.netzob.getCurrentProject().getVocabulary()

        sym = voca.getSymbol(sym_id)
        if sym is None:
            self.view.clearText()
            for sym in voca.getSymbols():
                try:
                    self.view.appendText(self.generateSymbolDissector(sym))
                except WiresharkExporterError, wee:
                    self.view.appendComment(wee)
                    NetzobWarningMessage("[{}] {}".format(sym.getName(), wee),
                                         self.view.dialog)
        else:
Пример #5
0
    def __init__(self):
        # Parse command line arguments
        cmdLine = CommandLine()
        cmdLine.parse()
        opts = cmdLine.getOptions()

        # Current workspace path can be provided in command line argument
        if opts.workspace is None:
            workspaceDir = ResourcesConfiguration.getWorkspaceDir()
        else:
            workspaceDir = opts.workspace

        # Start the workspace management
        self.workspaceSelectorController = WorkspaceSelectorController(self)
        self.currentWorkspace = self.workspaceSelectorController.getWorkspace(workspaceDir)

        if self.currentWorkspace is None:
            sys.exit()

        #self.currentWorkspace = self._loadWorkspace(opts)
        self.currentProjet = None

        # Enable bug reporting, if workspace is configured so or if
        # netzob was explicitly started with the "-b" command line
        # option.
        enableBugReports = self.currentWorkspace.enableBugReporting
        if enableBugReports != opts.bugReport:
            enableBugReports = opts.bugReport
        self.enableBugReporter(enableBugReports)

        # Initialize everything else
        self._initLogging(opts)
        self._initResourcesAndLocales()

        # Intialize signals manager
        self.signalsManager = SignalsManager()

        # Loading the last project
        self.currentProject = self.currentWorkspace.getLastProject()

        # Initialize a clipboard object
        self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)

        # Check dependencies
        if not DepCheck.checkRequiredDependency():
            self.log.fatal("Netzob could not start because some of its required dependencies were not found.")
            sys.exit()

        # Initialize main view
        self.log.info("Starting netzob UI")
        self.view = None    # small hack since the attribute need to exists when the main glade is loaded
        self.view = NetzobMainView(self)

        # Load all available plugins
        NetzobPlugin.loadPlugins(self)

        self.view.registerPerspectives()

        # Refresh list of available exporter plugins
        self.updateListOfExporterPlugins()

        # Refresh list of available projects
        self.updateListOfAvailableProjects()