def get_whois(self, **kwargs):
        client = WhoisRequest(self.username, self.apikey)

        keys = ['query', 'compact', 'field']

        params = self._cleanup_params(keys, **kwargs)

        if not params.get('field'):
            return client.get_whois_details(**params)
        else:
            return client.search_whois_by_field(**params)
    def get_whois(self, **kwargs):
        client = WhoisRequest(self.username, self.apikey)

        keys = ['query', 'compact', 'field']

        params = self._cleanup_params(keys, **kwargs)

        if not params.get('field'):
            return client.get_whois_details(**params)
        else:
            return client.search_whois_by_field(**params)
Exemple #3
0
    def run(self, conf, args, plugins):
        if 'subcommand' in args:
            if args.subcommand == 'whois':
                client = WhoisRequest(conf['PassiveTotal']['username'],
                                      conf['PassiveTotal']['key'])
                if args.domain:
                    raw_results = client.search_whois_by_field(query=unbracket(
                        args.domain.strip()),
                                                               field="domain")
                    print(
                        json.dumps(raw_results,
                                   sort_keys=True,
                                   indent=4,
                                   separators=(',', ': ')))
                elif args.file:
                    with open(args.file, 'r') as infile:
                        data = infile.read().split()
                    print(
                        "Domain|Date|Registrar|name|email|Phone|organization|Street|City|Postal Code|State|Country"
                    )
                    for d in data:
                        do = unbracket(d.strip())
                        # FIXME: bulk request here
                        raw_results = client.search_whois_by_field(
                            query=do, field="domain")
                        if "results" not in raw_results:
                            print("%s|||||||||||" % bracket(do))
                        else:
                            if len(raw_results["results"]) == 0:
                                print("%s|||||||||||" % bracket(do))
                            else:
                                r = raw_results["results"][0]
                                if "registered" in r:
                                    dd = datetime.datetime.strptime(
                                        r["registered"],
                                        "%Y-%m-%dT%H:%M:%S.%f%z")
                                    ddo = dd.strftime("%m/%d/%Y %H:%M:%S")
                                else:
                                    ddo = ""

                                print(
                                    "%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s" %
                                    (bracket(do), ddo, r["registrar"]
                                     if "registrar" in r else "",
                                     r["registrant"]["name"]
                                     if "name" in r["registrant"] else "",
                                     r["registrant"]["email"]
                                     if "email" in r["registrant"] else "",
                                     r["registrant"]["telephone"]
                                     if "telephone" in r["registrant"] else "",
                                     r["registrant"]["organization"] if
                                     "organization" in r["registrant"] else "",
                                     r["registrant"]["street"]
                                     if "street" in r["registrant"] else "",
                                     r["registrant"]["city"]
                                     if "city" in r["registrant"] else "",
                                     r["registrant"]["postalCode"] if
                                     "postalCode" in r["registrant"] else "",
                                     r["registrant"]["state"]
                                     if "state" in r["registrant"] else "",
                                     r["registrant"]["country"]
                                     if "country" in r["registrant"] else ""))

                elif args.email:
                    raw_results = client.search_whois_by_field(
                        query=args.email.strip(), field="email")
                    print(
                        json.dumps(raw_results,
                                   sort_keys=True,
                                   indent=4,
                                   separators=(',', ': ')))
                else:
                    self.parser.print_help()
            elif args.subcommand == "dns":
                client = DnsRequest(conf['PassiveTotal']['username'],
                                    conf['PassiveTotal']['key'])
                raw_results = client.get_passive_dns(query=unbracket(
                    args.DOMAIN), )
                print(
                    json.dumps(raw_results,
                               sort_keys=True,
                               indent=4,
                               separators=(',', ': ')))
            elif args.subcommand == "malware":
                client = EnrichmentRequest(conf["PassiveTotal"]["username"],
                                           conf["PassiveTotal"]['key'])
                if args.domain:
                    raw_results = client.get_malware(query=args.domain)
                    print(
                        json.dumps(raw_results,
                                   sort_keys=True,
                                   indent=4,
                                   separators=(',', ': ')))
                elif args.file:
                    with open(args.file, 'r') as infile:
                        data = infile.read().split()
                    domain_list = list(set([a.strip() for a in data]))
                    if len(domain_list) < 51:
                        raw_results = client.get_bulk_malware(
                            query=domain_list)
                        if "results" not in raw_results or not raw_results[
                                "success"]:
                            print("Request failed")
                            print(
                                json.dumps(raw_results,
                                           sort_keys=True,
                                           indent=4,
                                           separators=(',', ': ')))
                            sys.exit(1)
                        else:
                            results = raw_results["results"]
                    else:
                        results = {}
                        bulk_size = 50
                        i = 0
                        while i * bulk_size < len(domain_list):
                            raw_results = client.get_bulk_malware(
                                query=domain_list[i * bulk_size:(i + 1) *
                                                  bulk_size])
                            if "results" not in raw_results or not raw_results[
                                    "success"]:
                                print("Request failed")
                                print(
                                    json.dumps(raw_results,
                                               sort_keys=True,
                                               indent=4,
                                               separators=(',', ': ')))
                                sys.exit(1)
                            else:
                                results.update(raw_results["results"])
                            i += 1
                    if args.raw:
                        print(
                            json.dumps(results,
                                       sort_keys=True,
                                       indent=4,
                                       separators=(',', ': ')))
                    else:
                        print("Domain|Date|Sample|Source|Source URL")
                        for domain in results:
                            if "results" in results[domain]:
                                for sample in results[domain]["results"]:
                                    print("%s|%s|%s|%s|%s" %
                                          (domain, sample["collectionDate"],
                                           sample["sample"], sample["source"],
                                           sample["sourceUrl"]))

                else:
                    self.parser.print_help()

            elif args.subcommand == "osint":
                # FIXME: add research of projects
                client = EnrichmentRequest(conf["PassiveTotal"]["username"],
                                           conf["PassiveTotal"]['key'])
                if args.domain:
                    raw_results = client.get_osint(query=args.domain)
                    print(
                        json.dumps(raw_results,
                                   sort_keys=True,
                                   indent=4,
                                   separators=(',', ': ')))
                elif args.file:
                    with open(args.file, 'r') as infile:
                        data = infile.read().split()
                    domain_list = list(set([a.strip() for a in data]))
                    if len(domain_list) < 51:
                        raw_results = client.get_bulk_osint(query=domain_list)
                        if "results" not in raw_results or not raw_results[
                                "success"]:
                            print("Request failed")
                            print(
                                json.dumps(raw_results,
                                           sort_keys=True,
                                           indent=4,
                                           separators=(',', ': ')))
                            sys.exit(1)
                        else:
                            results = raw_results["results"]
                    else:
                        results = {}
                        bulk_size = 50
                        i = 0
                        while i * bulk_size < len(domain_list):
                            raw_results = client.get_bulk_osint(
                                query=domain_list[i * bulk_size:(i + 1) *
                                                  bulk_size])
                            if "results" not in raw_results or not raw_results[
                                    "success"]:
                                print("Request failed")
                                print(
                                    json.dumps(raw_results,
                                               sort_keys=True,
                                               indent=4,
                                               separators=(',', ': ')))
                                sys.exit(1)
                            else:
                                results.update(raw_results["results"])
                            i += 1
                    if args.raw:
                        print(
                            json.dumps(results,
                                       sort_keys=True,
                                       indent=4,
                                       separators=(',', ': ')))
                    else:
                        print("Domain|Source|URL|Tags")
                        for domain in results:
                            if "results" in results[domain]:
                                for report in results[domain]["results"]:
                                    print("%s|%s|%s|%s" %
                                          (domain, report["source"],
                                           report["source_url"], " / ".join(
                                               report["tags"])))
                else:
                    self.parser.print_help()

            else:
                self.parser.print_help()
        else:
            self.parser.print_help()
Exemple #4
0
class WhoisTestCase(unittest.TestCase):

    """Test case for WHOIS methods."""

    formats = ['json']

    def setup_class(self):
        self.patcher = patch('passivetotal.api.Client._get', fake_request)
        self.patcher.start()
        self.client = WhoisRequest('--No-User--', '--No-Key--')

    def teardown_class(self):
        self.patcher.stop()

    def test_whois_details(self):
        """Test getting WHOIS details."""
        payload = {'query': 'passivetotal.org'}
        response = self.client.get_whois_details(**payload)
        assert (response.get('domain')) == 'passivetotal.org'

    def test_process_whois_details(self):
        """Test processing WHOIS details."""
        payload = {'query': 'passivetotal.org'}
        response = self.client.get_whois_details(**payload)
        wrapped = Response(response)
        for item in self.formats:
            assert (getattr(wrapped, item))

    def test_property_load(self):
        """Test loading properties on a result."""
        payload = {'query': 'passivetotal.org'}
        response = self.client.get_whois_details(**payload)
        wrapped = Response(response)

        for key, value in iteritems(response):
            assert (getattr(wrapped, key)) == value

    def test_whois_search(self):
        """Test getting a WHOIS search."""
        payload = {'query': '18772064254', 'field': 'phone'}
        response = self.client.search_whois_by_field(**payload)
        assert (response['results'][0].get('domain')) == 'passivetotal.org'

    def test_whois_search_bad_field(self):
        """Test sending a bad field in a search."""
        with pytest.raises(INVALID_FIELD_TYPE) as excinfo:
            def invalid_field():
                payload = {'query': '18772064254', 'field': '_'}
                self.client.search_whois_by_field(**payload)
            invalid_field()
        assert 'must be one of the following' in str(excinfo.value)

    def test_whois_search_missing_field(self):
        """Test missing a field in a search."""
        with pytest.raises(MISSING_FIELD) as excinfo:
            def missing_field():
                payload = {'query': '18772064254', 'no-field': '_'}
                self.client.search_whois_by_field(**payload)
            missing_field()
        assert 'value is required' in str(excinfo.value)

    def test_process_whois_search(self):
        """Test processing search results."""
        payload = {'query': '18772064254', 'field': 'phone'}
        response = self.client.search_whois_by_field(**payload)
        results = Response(response)
        assert (Response(results.results[0]).domain) == 'passivetotal.org'