示例#1
0
    def listenbutton_clicked(self):
        ''' Triggered when the user click on hook, or unhook button and do the appropriate action.
        If the user unhook a connection while it is not stopped, the connecions will likely crash because
        of de-synchronisation.
        '''
        if self.hooked:
            print("Stop hooking (can cause the connection to crash)")
            self.inputbutton.setEnabled(False)
            self.inputtext.setEnabled(False)
            self.connectionlist.setEnabled(True)
            self.listenbutton.setText("Hook")
            self.unhook_all()  #Remove all the active nfqueues to stop hooking
            self.hooked = False
        else:
            print("Start Hooking")
            self.inputbutton.setEnabled(True)
            self.inputtext.setEnabled(True)
            self.connectionlist.setEnabled(False)
            self.textreceived.clear()
            self.textsent.clear()
            self.listenbutton.setText("Unhook")

            #Create the hacked session with the informations of the selected connection
            self.session = SessionMITM(self.interface,
                                       self)  #Create the session
            self.session.register_lower_layer(
                "default", self.tcpp)  #Register it on the stack
            self.app = MITMApplication(
                self)  #Create the MITM app and link it to the session
            self.session.register_layer(self.app)
            self.session.switch_state(State_ESTABLISHED(
                self.session))  #Put directly the session in ESTABLISHED

            #Create all the nfqueue and link them to the tcp session modified
            elts = self.connectionlist.selectedItems()
            if len(elts) != 1:
                QMessageBox.warning(self,
                                    "Warning",
                                    "A connection (only) should be selected",
                                    buttons=QMessageBox.Ok,
                                    defaultButton=QMessageBox.NoButton)
                print("One connection (only) should be selected")
                return
            else:
                s = elts[0].text(
                )  #Retrieve the selected connections and recover infos about it
                src, _, dst = s.split(" ")
                ipsrc, portsrc = src.split(":")
                ipdst, portdst = dst.split(":")

                self.ruleo = "-p tcp -s %s --sport %s -d %s --dport %s" % (
                    ipsrc, portsrc, ipdst, portdst)
                self.qmo = NFQueueManager(
                )  #Create a NfqueueManager to deal with outgoing packets
                self.qmo.add_queue_rule(
                    "OUTPUT", self.ruleo,
                    11)  #Add a nfqueue rule to the firewall
                self.qmo.run_queue(
                    self.session.outgoing_nfqueue_packet,
                    11)  #Start listening on the queue with the right handler

                self.rulei = "-p tcp -s %s --sport %s -d %s --dport %s" % (
                    ipdst, portdst, ipsrc, portsrc)
                self.qmi = NFQueueManager()  #Idem for incoming packets
                self.qmi.add_queue_rule("INPUT", self.rulei, 12)
                self.qmi.run_queue(self.session.incoming_nfqueue_packet, 12)
                self.hooked = True