예제 #1
0
파일: plugin.py 프로젝트: zack7wong/faraday
    def createAndAddInterface(
            self,
            host_id,
            name="",
            mac="00:00:00:00:00:00",
            ipv4_address="0.0.0.0",
            ipv4_mask="0.0.0.0",
            ipv4_gateway="0.0.0.0",
            ipv4_dns=[],
            ipv6_address="0000:0000:0000:0000:0000:0000:0000:0000",
            ipv6_prefix="00",
            ipv6_gateway="0000:0000:0000:0000:0000:0000:0000:0000",
            ipv6_dns=[],
            network_segment="",
            hostname_resolution=[]):

        # We don't use interface anymore, so return a host id to maintain
        # backwards compatibility
        # Little hack because we dont want change all the plugins for add hostnames in Host object.
        # SHRUG
        try:
            host = get_host(self.workspace, host_id=host_id)
            host.hostnames += hostname_resolution
            host.mac = mac
            update_host(self.workspace, host, command_id=self.command_id)
        except:
            logger.info(
                "Error updating Host with right hostname resolution...")
        return host_id
예제 #2
0
def main(workspace='', args=None, parser=None):
    parser.add_argument('ip', help='Host IP')
    parser.add_argument('os', help='OS')

    parser.add_argument('mac', help='Interface MAC Address')

    parser.add_argument('--gateway',
                        help='IPV4 or IPV6 Gateway',
                        default='0.0.0.0')

    parser.add_argument('--netsegment', help='Network Segment', default='')

    parser.add_argument(
        '--dry-run',
        action='store_true',
        help='Do not touch the database. Only print the object ID')

    parsed_args = parser.parse_args(args)

    params = {
        'ip': parsed_args.ip,
    }

    obj_host = factory.createModelObject(
        models.Host.class_signature,
        parsed_args.ip,
        workspace,
        os=parsed_args.os,
        mac=parsed_args.mac,
        network_segment=parsed_args.netsegment,
        parent_id=None)

    old_host = models.get_host(workspace, **params)

    if old_host is None:
        if not parsed_args.dry_run:
            models.create_host(workspace, obj_host)
            old_host = models.get_host(workspace, **params)
        else:
            return 0, None
    else:
        print("A host with ID %s already exists!" % old_host.getID())
        return 2, None

    return 0, old_host.getID()
예제 #3
0
 def createAndAddInterface(
         self,
         host_id,
         name="",
         mac="00:00:00:00:00:00",
         ipv4_address="0.0.0.0",
         ipv4_mask="0.0.0.0",
         ipv4_gateway="0.0.0.0",
         ipv4_dns=None,
         ipv6_address="0000:0000:0000:0000:0000:0000:0000:0000",
         ipv6_prefix="00",
         ipv6_gateway="0000:0000:0000:0000:0000:0000:0000:0000",
         ipv6_dns=None,
         network_segment="",
         hostname_resolution=None):
     if ipv4_dns is None:
         ipv4_dns = []
     if ipv6_dns is None:
         ipv6_dns = []
     if hostname_resolution is None:
         hostname_resolution = []
     if not isinstance(hostname_resolution, list):
         logger.warning(
             "hostname_resolution parameter must be a list and is (%s)",
             type(hostname_resolution))
         hostname_resolution = [hostname_resolution]
     # We don't use interface anymore, so return a host id to maintain
     # backwards compatibility
     # Little hack because we dont want change all the plugins for add hostnames in Host object.
     # SHRUG
     try:
         host = get_host(self.workspace, host_id=host_id)
         host.hostnames += hostname_resolution
         host.mac = mac
         update_host(self.workspace, host, command_id=self.command_id)
     except:
         logger.info(
             "Error updating Host with right hostname resolution...")
     return host_id
예제 #4
0
 def get_host(self, host_id):
     return models.get_host(self.active_workspace, host_id)
예제 #5
0
def main(workspace="", args=None, parser=None):

    WORKSPACE = workspace

    parser.add_argument("--csv", help="Csv file to import")
    parsed_args = parser.parse_args(args)

    if not parsed_args.csv:
        print("Error: Give a CSV file to import with --csv")
        return 2, None

    try:
        file_csv = open(parsed_args.csv, "r")
    except:
        print("Error: Unreadeable CSV file, check the path")
        raise

    counter = 0
    csv_reader = csv.DictReader(file_csv, delimiter=",", quotechar='"')
    for register in csv_reader:
        try:
            host, service, vulnerability, vulnerability_web = parse_register(register)

            # Set all IDs and create objects
            if host is not None:
                old_host = models.get_host(WORKSPACE, ip=host.getName())
                if not old_host:

                    counter += 1

                    print("New host: " + host.getName())
                    try:
                        models.create_host(WORKSPACE, host)
                    except Exception as ex:
                        print(ex)
                host = models.get_host(WORKSPACE, ip=host.getName())

            if service is not None:
                service.setParent(host.getID())
                service_params = {
                    'name': service.getName(),
                    'port': service.getPorts()[0],
                    'protocol': service.getProtocol(),
                    'host_id': service.getParent()
                }
                old_service = models.get_service(WORKSPACE, **service_params)
                if not old_service:

                    counter += 1
                    print("New service: " + service.getName())
                    models.create_service(WORKSPACE, service)
                service = models.get_service(WORKSPACE, **service_params)

            # Check if Service exist, then create the vuln with parent Service.
            # If not exist the Service, create the vuln with parent Host.
            if vulnerability is not None:
                if host and not service:
                    parent_type = 'Host'
                    parent_id = host.getID()
                if host and service:
                    parent_type = 'Service'
                    parent_id = service.getID()
                vulnerability.setParent(parent_id)
                vulnerability.setParentType(parent_type)

                vuln_params = {
                    'name': vulnerability.getName(),
                    'description': vulnerability.getDescription(),
                    'parent_type': parent_type,
                    'parent': parent_id,
                }

                if not models.get_vuln(WORKSPACE, **vuln_params):
                    counter += 1
                    print("New vulnerability: " + vulnerability.getName())
                    models.create_vuln(WORKSPACE, vulnerability)

            elif vulnerability_web is not None:

                vuln_web_params = {
                    'name': vulnerability_web.getName(),
                    'description': vulnerability_web.getDescription(),
                    'parent': service.getID(),
                    'parent_type': 'Service',
                    'method': vulnerability_web.getMethod(),
                    'parameter_name': vulnerability_web.getParams(),
                    'path': vulnerability_web.getPath(),
                    'website': vulnerability_web.getWebsite(),
                }
                vulnerability_web.setParent(service.getID())
                if not models.get_web_vuln(WORKSPACE, **vuln_web_params):

                    counter += 1
                    print("New web vulnerability: " + vulnerability_web.getName())
                    models.create_vuln_web(WORKSPACE, vulnerability_web)
        except ConflictInDatabase:
            print('Conflict in Database, skiping csv row')
        except CantCommunicateWithServerError as ex:
            print(register)
            print('Error', ex)
    print("[*]", counter, "new Faraday objects created.")
    file_csv.close()
    return 0, None
예제 #6
0
SERVICES = {
    'http': [80, 443, 8080, 8443],
    'ftp': [21],
    'ssh': [22],
    'telnet': [23],
    'smtp': [25],
    'domain': [53],
    'pop3': [110, 995],
    'imap': [143, 993],
    'vnc': [5900],
}

# FIXME Update when persistence API changes
COLUMNS = {
    'host': lambda service, workspace: models.get_host(workspace, service.getParent()).name,
    'host_os': lambda service, workspace: models.get_host(workspace, service.getParent()).os,
    'service': lambda service, workspace: service.name,
    'ports': lambda service, workspace: str(service.ports[0]),
    'protocol': lambda service, workspace: service.protocol,
    'status': lambda service, workspace: service.status,
}


def main(workspace='', args=None, parser=None):
    parser.add_argument('-p', type=int, nargs='+', metavar='port', help='List of ports to filter', default=[])
    parser.add_argument('services', nargs='*', help='List of service names', default=[]),
    parser.add_argument('--columns', help='Comma separated list of columns to show.',
                        default="host,service,ports,protocol,status,host_os", choices=COLUMNS.keys())

    parser.add_argument('--status', help='Comma separated list of status to filter for.')
예제 #7
0
def main(workspace='', args=None, parser=None):

    parser.add_argument('-s',
                        '--source',
                        nargs='*',
                        help='Filter packets by source'),
    parser.add_argument('-d',
                        '--dest',
                        nargs='*',
                        help='Filter packets by destination'),

    parser.add_argument(
        '--dry-run',
        action='store_true',
        help='Do not touch the database. Only print the object ID')

    parser.add_argument('-v',
                        '--verbose',
                        action='store_true',
                        help='Verbose output from the pcapfile library.')
    parser.add_argument('pcap', help='Path to the PCAP file'),

    parsed_args = parser.parse_args(args)

    try:
        from pcapfile import savefile
        import pcapfile
    except ImportError:
        print('capfile not found, please install it to use this plugin.' \
              ' You can do it executing pip2 install pcapfile in a shell.')
        return 1, None

    if not os.path.isfile(parsed_args.pcap):
        print("pcap file not found: " % parsed_args.pcap)
        return 2, None

    testcap = open(parsed_args.pcap, 'rb')

    try:
        capfile = savefile.load_savefile(testcap,
                                         layers=2,
                                         verbose=parsed_args.verbose)
    except pcapfile.Error:
        print("Invalid pcap file")
        return 3, None

    print('pcap file loaded. Parsing packets...')

    # Set() to store already added hosts. This will save an enormous amount of time by not querying the database
    # for hosts we already know are in Faraday
    added = set()

    for packet in capfile.packets:

        if packet.packet.type != 2048:
            continue

        src = packet.packet.payload.src
        dst = packet.packet.payload.dst

        if parsed_args.source and not src in parsed_args.source:
            continue

        if parsed_args.dest and not dst in parsed_args.dest:
            continue

        if src not in added:

            # Lets save additional queries for this IP, it will already be on the database anyway!
            added.add(packet.packet.payload.src)

            # Parsing of source field
            obj = factory.createModelObject(models.Host.class_signature,
                                            src,
                                            workspace,
                                            os=None,
                                            parent_id=None)

            old = models.get_host(workspace, obj.getID())

            if old is None:
                if not parsed_args.dry_run:
                    models.create_host(workspace, obj)
                print('%s\t%s' % (src, obj.getID()))

        if dst not in added:

            # Lets save additional queries for this IP, it will already be on the database anyway!
            added.add(packet.packet.payload.dst)

            # Parsing of destination field
            obj = factory.createModelObject(models.Host.class_signature,
                                            dst,
                                            workspace,
                                            os=None,
                                            parent_id=None)

            old = models.get_host(workspace, obj.getID())

            if old is None:
                if not parsed_args.dry_run:
                    models.create_host(workspace, obj)
                print('%s\t%s' % (dst, obj.getID()))

    return 0, None