def getIp(iprange, macAddress):
    nm = nmap_python.PortScanner()
    nm.scan(iprange, arguments='-sP -n')
    for h in nm.all_hosts():
        if 'mac' in nm[h]['addresses']:
            if nm[h]['addresses']['mac'] == macAddress:
                return nm[h]['addresses']['ipv4']

    return None
Beispiel #2
0
    def initialize(self, result):
        self.running = True
        nm = nmap_python.PortScanner()
        updateEnabled = False
        # nm.scan(self.device["ip"], '0-65535', arguments='-sS -sU')
        args = "-sS"
        if self.config["udp"]:
            args = args + " -sU"
        if not self.config["optimization"] == "":
            args = args + " " + self.config["optimization"]
        macAddress = self.device['macAddress']
        if macAddress in self.config.keys() and not macAddress == '':
            result.update(self.config[macAddress])
            updateEnabled = True

        if not self.config['skip']:
            nm.scan(self.device["ip"],
                    self.config["port_range"],
                    arguments=args,
                    sudo=True,
                    timeout=0)

            for host in nm.all_hosts():
                # log.info('----------------------------------------------------')
                # log.info('Host : %s (%s)' % (host, nm[host].hostname()))
                # log.info('State : %s' % nm[host].state())
                for proto in nm[host].all_protocols():
                    # print('----------')
                    # print('Protocol : %s' % proto)
                    lport = nm[host][proto].keys()
                    lport.sort()
                    proto_ports_open = []
                    proto_ports_filtered = []
                    for port in lport:
                        if (nm[host][proto][port]['state'] == "open"):
                            proto_ports_open.append(port)
                        elif (nm[host][proto][port]['state'] == "open|filtered"
                              ):
                            proto_ports_filtered.append(port)
                        # print ('port : %s\tstate : %s' % (port, nm[host][proto][port]['state']))
                    if updateEnabled:
                        if proto in result.keys():
                            if "open" in result[proto].keys():
                                portRange = self.config["port_range"].split(
                                    "-")
                                for openPort in result[proto]['open']:
                                    if openPort >= int(
                                            portRange[0]) and openPort <= int(
                                                portRange[1]):
                                        result[proto]['open'].remove(openPort)
                                result[proto]['open'].extend(proto_ports_open)
                                result[proto]['open'].extend(
                                    proto_ports_filtered)
                            else:
                                result[proto].update(
                                    {"open": proto_ports_open})
                                result[proto].update(
                                    {"open": proto_ports_filtered})

                                # if "filtered" in result[proto].keys():
                            #     portRange = self.config["port_range"].split("-")
                            #     for filteredPort in result[proto]['filtered']:
                            #         if filteredPort >= int(portRange[0]) and openPort<=int(portRange[1]):
                            #             result[proto]['filtered'].remove(filteredPort)
                            #     result[proto]['filtered'].extend(proto_ports_filtered)
                            # else:
                            #     result[proto].update({"open": proto_ports_open})

                        else:
                            # result.update({proto: {"open": proto_ports_open, "filtered": proto_ports_filtered}})
                            result.update({proto: {"open": proto_ports_open}})
                    else:
                        # result.update({proto: {"open":proto_ports_open, "filtered":proto_ports_filtered}})
                        result.update({proto: {"open": proto_ports_open}})
        log.info(result)
        self.device.update({"vulnerable_ports": result})
        return
        host = socket.gethostbyname(hostname)
        netmask = ioutil.NetworkUtil.getNetMask(host)

    ipcidr = ioutil.NetworkUtil.getCidr(host, netmask)
    iprange = str(ipcidr[0].cidr)

    macAddress = data['mac_address']
    if macAddress is None or macAddress == "":
        choice = raw_input("To scan ip range press 1 or to skip press any key: ")
        if (choice == '1'):
            choice = raw_input("enter cidr default[%s]: " % (iprange))
            if (choice != ''):
                iprange = choice

            log.info("IP Scanner started for range %s, Please Wait...." % iprange)
            nm = nmap_python.PortScanner()
            nm.scan(iprange, arguments='-sP -n')
            for h in nm.all_hosts():
                if 'mac' in nm[h]['addresses']:
                    print(nm[h]['addresses'], nm[h]['vendor'])
        macAddressToAttack = ''
        while macAddressToAttack == '':
            macAddressToAttack = raw_input("MacAddress to attack: ")
        gateway = netifaces.gateways()['default'].values()[0][0]
        defaultGatewayIP = raw_input("Default Gateway[%s]" % gateway)
        if (defaultGatewayIP != ''):
            gateway = defaultGatewayIP;
        macAddress = macAddressToAttack
    else:
        gateway = netifaces.gateways()['default'].values()[0][0]
        # defaultGateway = data['default_gateway']