Skip to content

SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.

sflib: add countryCodeFromName and countryCodeFromTld

Brendan Coles8feec37f291a
diff --git a/modules/sfp_countryname.py b/modules/sfp_countryname.py
index b6e9103c..00023aef 100644
--- a/modules/sfp_countryname.py
+++ b/modules/sfp_countryname.py
@@ -27,22 +27,17 @@ class sfp_countryname(SpiderFootPlugin):
'categories': ["Content Analysis"]
}
- # Default options
opts = {
- # options specific to this module
'cohosted': True,
'affiliate': True,
'noncountrytld': True,
- 'noncountrytlddefault': "United States",
'similardomain': False,
}
- # Option descriptions
optdescs = {
'cohosted': "Obtain country name from co-hosted sites",
'affiliate': "Obtain country name from affiliate sites",
'noncountrytld': "Parse TLDs not associated with any country as default country domains",
- 'noncountrytlddefault': "Default country name for TLDs not associated with any country(.com, .net, .org, .gov, .mil)",
'similardomain': "Obtain country name from similar domains"
}
@@ -58,315 +53,47 @@ class sfp_countryname(SpiderFootPlugin):
for opt in userOpts.keys():
self.opts[opt] = userOpts[opt]
- # Getter method for country codes and country names dictionary
- def getCountryCodeDict(self):
-
- # Dictionary of country codes and country names
- abbvCountryCodes = {
- "AF": "Afghanistan",
- "AX": "Aland Islands",
- "AL": "Albania",
- "DZ": "Algeria",
- "AS": "American Samoa",
- "AD": "Andorra",
- "AO": "Angola",
- "AI": "Anguilla",
- "AQ": "Antarctica",
- "AG": "Antigua and Barbuda",
- "AR": "Argentina",
- "AM": "Armenia",
- "AW": "Aruba",
- "AU": "Australia",
- "AT": "Austria",
- "AZ": "Azerbaijan",
- "BS": "Bahamas",
- "BH": "Bahrain",
- "BD": "Bangladesh",
- "BB": "Barbados",
- "BY": "Belarus",
- "BE": "Belgium",
- "BZ": "Belize",
- "BJ": "Benin",
- "BM": "Bermuda",
- "BT": "Bhutan",
- "BO": "Bolivia",
- "BQ": "Bonaire, Saint Eustatius and Saba",
- "BA": "Bosnia and Herzegovina",
- "BW": "Botswana",
- "BV": "Bouvet Island",
- "BR": "Brazil",
- "IO": "British Indian Ocean Territory",
- "VG": "British Virgin Islands",
- "BN": "Brunei",
- "BG": "Bulgaria",
- "BF": "Burkina Faso",
- "BI": "Burundi",
- "KH": "Cambodia",
- "CM": "Cameroon",
- "CA": "Canada",
- "CV": "Cape Verde",
- "KY": "Cayman Islands",
- "CF": "Central African Republic",
- "TD": "Chad",
- "CL": "Chile",
- "CN": "China",
- "CX": "Christmas Island",
- "CC": "Cocos Islands",
- "CO": "Colombia",
- "KM": "Comoros",
- "CK": "Cook Islands",
- "CR": "Costa Rica",
- "HR": "Croatia",
- "CU": "Cuba",
- "CW": "Curacao",
- "CY": "Cyprus",
- "CZ": "Czech Republic",
- "CD": "Democratic Republic of the Congo",
- "DK": "Denmark",
- "DJ": "Djibouti",
- "DM": "Dominica",
- "DO": "Dominican Republic",
- "TL": "East Timor",
- "EC": "Ecuador",
- "EG": "Egypt",
- "SV": "El Salvador",
- "GQ": "Equatorial Guinea",
- "ER": "Eritrea",
- "EE": "Estonia",
- "ET": "Ethiopia",
- "FK": "Falkland Islands",
- "FO": "Faroe Islands",
- "FJ": "Fiji",
- "FI": "Finland",
- "FR": "France",
- "GF": "French Guiana",
- "PF": "French Polynesia",
- "TF": "French Southern Territories",
- "GA": "Gabon",
- "GM": "Gambia",
- "GE": "Georgia",
- "DE": "Germany",
- "GH": "Ghana",
- "GI": "Gibraltar",
- "GR": "Greece",
- "GL": "Greenland",
- "GD": "Grenada",
- "GP": "Guadeloupe",
- "GU": "Guam",
- "GT": "Guatemala",
- "GG": "Guernsey",
- "GN": "Guinea",
- "GW": "Guinea-Bissau",
- "GY": "Guyana",
- "HT": "Haiti",
- "HM": "Heard Island and McDonald Islands",
- "HN": "Honduras",
- "HK": "Hong Kong",
- "HU": "Hungary",
- "IS": "Iceland",
- "IN": "India",
- "ID": "Indonesia",
- "IR": "Iran",
- "IQ": "Iraq",
- "IE": "Ireland",
- "IM": "Isle of Man",
- "IL": "Israel",
- "IT": "Italy",
- "CI": "Ivory Coast",
- "JM": "Jamaica",
- "JP": "Japan",
- "JE": "Jersey",
- "JO": "Jordan",
- "KZ": "Kazakhstan",
- "KE": "Kenya",
- "KI": "Kiribati",
- "XK": "Kosovo",
- "KW": "Kuwait",
- "KG": "Kyrgyzstan",
- "LA": "Laos",
- "LV": "Latvia",
- "LB": "Lebanon",
- "LS": "Lesotho",
- "LR": "Liberia",
- "LY": "Libya",
- "LI": "Liechtenstein",
- "LT": "Lithuania",
- "LU": "Luxembourg",
- "MO": "Macao",
- "MK": "Macedonia",
- "MG": "Madagascar",
- "MW": "Malawi",
- "MY": "Malaysia",
- "MV": "Maldives",
- "ML": "Mali",
- "MT": "Malta",
- "MH": "Marshall Islands",
- "MQ": "Martinique",
- "MR": "Mauritania",
- "MU": "Mauritius",
- "YT": "Mayotte",
- "MX": "Mexico",
- "FM": "Micronesia",
- "MD": "Moldova",
- "MC": "Monaco",
- "MN": "Mongolia",
- "ME": "Montenegro",
- "MS": "Montserrat",
- "MA": "Morocco",
- "MZ": "Mozambique",
- "MM": "Myanmar",
- "NA": "Namibia",
- "NR": "Nauru",
- "NP": "Nepal",
- "NL": "Netherlands",
- "AN": "Netherlands Antilles",
- "NC": "New Caledonia",
- "NZ": "New Zealand",
- "NI": "Nicaragua",
- "NE": "Niger",
- "NG": "Nigeria",
- "NU": "Niue",
- "NF": "Norfolk Island",
- "KP": "North Korea",
- "MP": "Northern Mariana Islands",
- "NO": "Norway",
- "OM": "Oman",
- "PK": "Pakistan",
- "PW": "Palau",
- "PS": "Palestinian Territory",
- "PA": "Panama",
- "PG": "Papua New Guinea",
- "PY": "Paraguay",
- "PE": "Peru",
- "PH": "Philippines",
- "PN": "Pitcairn",
- "PL": "Poland",
- "PT": "Portugal",
- "PR": "Puerto Rico",
- "QA": "Qatar",
- "CG": "Republic of the Congo",
- "RE": "Reunion",
- "RO": "Romania",
- "RU": "Russia",
- "RW": "Rwanda",
- "BL": "Saint Barthelemy",
- "SH": "Saint Helena",
- "KN": "Saint Kitts and Nevis",
- "LC": "Saint Lucia",
- "MF": "Saint Martin",
- "PM": "Saint Pierre and Miquelon",
- "VC": "Saint Vincent and the Grenadines",
- "WS": "Samoa",
- "SM": "San Marino",
- "ST": "Sao Tome and Principe",
- "SA": "Saudi Arabia",
- "SN": "Senegal",
- "RS": "Serbia",
- "CS": "Serbia and Montenegro",
- "SC": "Seychelles",
- "SL": "Sierra Leone",
- "SG": "Singapore",
- "SX": "Sint Maarten",
- "SK": "Slovakia",
- "SI": "Slovenia",
- "SB": "Solomon Islands",
- "SO": "Somalia",
- "ZA": "South Africa",
- "GS": "South Georgia and the South Sandwich Islands",
- "KR": "South Korea",
- "SS": "South Sudan",
- "ES": "Spain",
- "LK": "Sri Lanka",
- "SD": "Sudan",
- "SR": "Suriname",
- "SJ": "Svalbard and Jan Mayen",
- "SZ": "Swaziland",
- "SE": "Sweden",
- "CH": "Switzerland",
- "SY": "Syria",
- "TW": "Taiwan",
- "TJ": "Tajikistan",
- "TZ": "Tanzania",
- "TH": "Thailand",
- "TG": "Togo",
- "TK": "Tokelau",
- "TO": "Tonga",
- "TT": "Trinidad and Tobago",
- "TN": "Tunisia",
- "TR": "Turkey",
- "TM": "Turkmenistan",
- "TC": "Turks and Caicos Islands",
- "TV": "Tuvalu",
- "VI": "U.S. Virgin Islands",
- "UG": "Uganda",
- "UA": "Ukraine",
- "AE": "United Arab Emirates",
- "GB": "United Kingdom",
- "US": "United States",
- "UM": "United States Minor Outlying Islands",
- "UY": "Uruguay",
- "UZ": "Uzbekistan",
- "VU": "Vanuatu",
- "VA": "Vatican",
- "VE": "Venezuela",
- "VN": "Vietnam",
- "WF": "Wallis and Futuna",
- "EH": "Western Sahara",
- "YE": "Yemen",
- "ZM": "Zambia",
- "ZW": "Zimbabwe",
- # Below are not country codes but recognized as TLDs
- "AC": "Ascension Island",
- "EU": "European Union",
- "SU": "Soviet Union",
- "UK": "United Kingdom"
- }
- return abbvCountryCodes
-
- # Getter method for dictionary of codes not associated with any country
- def getNonCountryCodesDict(self):
-
- # List of TLD not associated with any country
- nonCountryCodes = ["COM", "NET", "ORG", "GOV", "MIL"]
-
- # Get default country code set from opts dictionary
- defaultCountryCode = self.opts["noncountrytlddefault"]
-
- nonCountryCodesDict = dict()
-
- # Set default country name for all keys
- for nonCountryCode in nonCountryCodes:
- nonCountryCodesDict[nonCountryCode] = defaultCountryCode
-
- return nonCountryCodesDict
-
- # Detect name of country from phone number
def detectCountryFromPhone(self, srcPhoneNumber):
+ """Lookup name of country from phone number region code.
- # Get dictionary of country codes and country names
- abbvCountryCodes = self.getCountryCodeDict()
+ Args:
+ srcPhoneNumber (str): phone number
+
+ Returns:
+ str: country name
+ """
+
+ if not isinstance(srcPhoneNumber, str):
+ return None
- # Check if country code is present in the phone number
try:
- # Parse source phone nummber
phoneNumber = phonenumbers.parse(srcPhoneNumber)
- # Get country code of phone number
+ except:
+ self.sf.debug(f"Skipped invalid phone number: {srcPhoneNumber}")
+ return None
+
+ try:
countryCode = region_code_for_country_code(phoneNumber.country_code)
- # try block handles key not found exception
- return abbvCountryCodes[countryCode.upper()]
except:
- # Region code not present in source phone number
- self.sf.debug("Skipped invalid phone number: " + srcPhoneNumber)
+ self.sf.debug(f"Lookup of region code failed for phone number: {srcPhoneNumber}")
+ return None
+
+ if not countryCode:
return None
- # Detect name of country from TLD of domain name
- def detectCountryFromTLD(self, srcDomain):
+ return self.sf.countryNameFromCountryCode(countryCode.upper())
+
+ def detectCountryFromDomainName(self, srcDomain):
+ """Lookup name of country from TLD of domain name.
- # Get dictionary of TLD country codes and country names
- tldCountryCodes = self.getCountryCodeDict()
+ Args:
+ srcDomain (str): domain
- # Get dictionary of non country TLD codes
- tldNonCountryCodes = self.getNonCountryCodesDict()
+ Returns:
+ str: country name
+ """
+ if not isinstance(srcDomain, str):
+ return None
# Split domain into parts by '.'
# Country TLDs are reserved
@@ -374,59 +101,58 @@ class sfp_countryname(SpiderFootPlugin):
# Search for country TLD in the domain parts - reversed
for part in domainParts[::-1]:
- if part.upper() in tldCountryCodes.keys():
- return tldCountryCodes[part.upper()]
-
- # Search for non country TLD in the domain parts
- for part in domainParts[::-1]:
- if part.upper() in tldNonCountryCodes.keys():
- return tldNonCountryCodes[part.upper()]
+ country_name = self.sf.countryNameFromTld(part)
+ if country_name:
+ return country_name
- # No associated country name is found
return None
- # Detect name of country from IBAN
def detectCountryFromIBAN(self, srcIBAN):
+ """Detect name of country from IBAN.
- # Get dictionary of country codes and country names
- tldCountryCodes = self.getCountryCodeDict()
- try:
- # Get country code from IBAN
- countryCode = srcIBAN[0:2].upper()
- return tldCountryCodes[countryCode]
- except:
- # No country name is found in the IBAN
+ Args:
+ srcIBAN (str): IBAN
+
+ Returns:
+ str: country name
+ """
+ if not isinstance(srcIBAN, str):
return None
- # Detect name of country from whois lookup, Geo Info, Physical Address data
+ return self.sf.countryNameFromCountryCode(srcIBAN[0:2])
+
def detectCountryFromData(self, srcData):
+ """Detect name of country from event data (WHOIS lookup, Geo Info, Physical Address, etc)
- # Get dictionary of country codes and country names
- abbvCountryCodes = self.getCountryCodeDict()
+ Args:
+ srcData (str): event data
+
+ Returns:
+ list: list of countries
+ """
countries = list()
+ if not srcData:
+ return countries
+
+ # Get dictionary of country codes and country names
+ abbvCountryCodes = self.sf.getCountryCodeDict()
+
# Look for countrycodes and country in source data
for countryName in abbvCountryCodes.values():
+ if countryName.lower() not in srcData.lower():
+ continue
# Look for country name in source data
- # Spaces are not included since New Jersey and others
- # will get interpreted as Jersey, etc.
+ # Spaces are not included since "New Jersey" and others
+ # will get interpreted as "Jersey", etc.
matchCountries = re.findall(r"[,'\"\:\=\[\(\[\n\t\r\.] ?" + countryName + r"[,'\"\:\=\[\(\[\n\t\r\.]", srcData, re.IGNORECASE)
- if len(matchCountries) > 0:
- # Get country name from first index of list
- # Extract only the text part of the country code
- matchCountry = matchCountries[0].strip(",").strip("'").strip("\"").strip()
- countries.append(matchCountry)
-
- if len(matchCountries) > 0:
- # Get country name from first index of list
- # Extract only the text part of the country code
- matchCountry = matchCountries[0].strip(",").strip("'").strip("\"").strip()
- countries.append(matchCountry)
+ if matchCountries:
+ countries.append(countryName)
# Look for "Country: ", usually found in Whois records
- matchCountries = re.findall("country: (.*)", srcData, re.IGNORECASE)
+ matchCountries = re.findall("country: (.+?)", srcData, re.IGNORECASE)
if matchCountries:
for m in matchCountries:
m = m.strip()
@@ -435,11 +161,7 @@ class sfp_countryname(SpiderFootPlugin):
if m in abbvCountryCodes.values():
countries.append(m)
- # If any countries are found
- if len(countries) > 0:
- return countries
-
- return None
+ return list(set(countries))
# What events is this module interested in for input
def watchedEvents(self):
@@ -458,15 +180,17 @@ class sfp_countryname(SpiderFootPlugin):
srcModuleName = event.module
eventData = event.data
- evttype = "COUNTRY_NAME"
+ if event.moduleDataSource:
+ moduleDataSource = event.moduleDataSource
+ else:
+ moduleDataSource = "Unknown"
self.sf.debug(f"Received event, {eventName}, from {srcModuleName}")
- # Generate event data hash
eventDataHash = self.sf.hashstring(eventData)
- # Do not parse duplicate incoming data
+
if eventDataHash in self.results:
- self.sf.debug("Already found from this source")
+ self.sf.debug(f"Skipping {eventData}, already checked.")
return None
self.results[eventDataHash] = True
@@ -476,36 +200,37 @@ class sfp_countryname(SpiderFootPlugin):
# Process the event data based on incoming event type
if eventName == "PHONE_NUMBER":
countryNames.append(self.detectCountryFromPhone(eventData))
- elif eventName == "DOMAIN_NAME" or (eventName == "AFFILIATE_DOMAIN_NAME" and self.opts["affiliate"]) or (eventName == "CO_HOSTED_SITE_DOMAIN" and self.opts["cohosted"]) or (eventName == "SIMILARDOMAIN" and self.opts["similardomain"]):
- countryNames.append(self.detectCountryFromTLD(eventData))
+ elif eventName == "DOMAIN_NAME":
+ countryNames.append(self.detectCountryFromDomainName(eventData))
+ elif eventName == "AFFILIATE_DOMAIN_NAME" and self.opts["affiliate"]:
+ countryNames.append(self.detectCountryFromDomainName(eventData))
+ elif eventName == "CO_HOSTED_SITE_DOMAIN" and self.opts["cohosted"]:
+ countryNames.append(self.detectCountryFromDomainName(eventData))
+ elif eventName == "SIMILARDOMAIN" and self.opts["similardomain"]:
+ countryNames.append(self.detectCountryFromDomainName(eventData))
elif eventName == "IBAN_NUMBER":
countryNames.append(self.detectCountryFromIBAN(eventData))
- elif eventName in ["DOMAIN_WHOIS", "GEOINFO", "PHYSICAL_ADDRESS"] or (eventName == "AFFILIATE_DOMAIN_WHOIS" and self.opts["affiliate"]) or (eventName == "CO_HOSTED_SITE_DOMAIN_WHOIS" and self.opts["cohosted"]):
- tempDataList = self.detectCountryFromData(eventData)
- if tempDataList is None:
- countryNames.append(None)
- else:
- countryNames.extend(tempDataList)
-
- # Check if countryNames is empty
- if len(countryNames) == 0:
+ elif eventName in ["DOMAIN_WHOIS", "GEOINFO", "PHYSICAL_ADDRESS"]:
+ countryNames.extend(self.detectCountryFromData(eventData))
+ elif eventName == "AFFILIATE_DOMAIN_WHOIS" and self.opts["affiliate"]:
+ countryNames.extend(self.detectCountryFromData(eventData))
+ elif eventName == "CO_HOSTED_SITE_DOMAIN_WHOIS" and self.opts["cohosted"]:
+ countryNames.extend(self.detectCountryFromData(eventData))
+
+ if not countryNames:
+ self.sf.debug(f"Found no country names associated with {eventName}: {eventData}")
return None
- # Convert list to set to remove duplicates
- countryNames = set(countryNames)
-
- for countryName in countryNames:
+ for countryName in set(countryNames):
if not countryName:
continue
self.sf.debug(f"Found country name: {countryName}")
- evt = SpiderFootEvent(evttype, countryName, self.__name__, event)
- if event.moduleDataSource:
- evt.moduleDataSource = event.moduleDataSource
- else:
- evt.moduleDataSource = "Unknown"
+ evt = SpiderFootEvent("COUNTRY_NAME", countryName, self.__name__, event)
+ evt.moduleDataSource = moduleDataSource
self.notifyListeners(evt)
+
return None
# End of sfp_countryname class
diff --git a/modules/sfp_jsonwhoiscom.py b/modules/sfp_jsonwhoiscom.py
index 991385f1..0ce0be80 100644
--- a/modules/sfp_jsonwhoiscom.py
+++ b/modules/sfp_jsonwhoiscom.py
@@ -221,7 +221,8 @@ class sfp_jsonwhoiscom(SpiderFootPlugin):
phone = phone.replace(" ", "").replace("-", "").replace("(", "").replace(")", "").replace(".", "")
phones.append(phone)
- location = ', '.join([_f for _f in [contact.get('address'), contact.get('city'), contact.get('state'), contact.get('zip'), contact.get('country_code')] if _f])
+ country = self.sf.countryNameFromCountryCode(contact.get('country_code'))
+ location = ', '.join([_f for _f in [contact.get('address'), contact.get('city'), contact.get('state'), contact.get('zip'), country] if _f])
if location:
locations.append(location)
diff --git a/modules/sfp_numverify.py b/modules/sfp_numverify.py
index 90d06838..c0ebf867 100644
--- a/modules/sfp_numverify.py
+++ b/modules/sfp_numverify.py
@@ -157,7 +157,8 @@ class sfp_numverify(SpiderFootPlugin):
self.notifyListeners(evt)
if data.get('country_code'):
- location = ', '.join([_f for _f in [data.get('location'), data.get('country_code')] if _f])
+ country = self.sf.countryNameFromCountryCode(data.get('country_code'))
+ location = ', '.join([_f for _f in [data.get('location'), country] if _f])
evt = SpiderFootEvent("GEOINFO", location, self.__name__, event)
self.notifyListeners(evt)
else:
diff --git a/sflib.py b/sflib.py
index 89c2311e..b9eba502 100644
--- a/sflib.py
+++ b/sflib.py
@@ -1738,6 +1738,321 @@ class SpiderFoot:
self.debug("Skipped invalid credit card number: " + match)
return list(creditCards)
+ def getCountryCodeDict(self):
+ """Dictionary of country codes and associated country names.
+
+ Returns:
+ dict: country codes and associated country names
+ """
+
+ return {
+ "AF": "Afghanistan",
+ "AX": "Aland Islands",
+ "AL": "Albania",
+ "DZ": "Algeria",
+ "AS": "American Samoa",
+ "AD": "Andorra",
+ "AO": "Angola",
+ "AI": "Anguilla",
+ "AQ": "Antarctica",
+ "AG": "Antigua and Barbuda",
+ "AR": "Argentina",
+ "AM": "Armenia",
+ "AW": "Aruba",
+ "AU": "Australia",
+ "AT": "Austria",
+ "AZ": "Azerbaijan",
+ "BS": "Bahamas",
+ "BH": "Bahrain",
+ "BD": "Bangladesh",
+ "BB": "Barbados",
+ "BY": "Belarus",
+ "BE": "Belgium",
+ "BZ": "Belize",
+ "BJ": "Benin",
+ "BM": "Bermuda",
+ "BT": "Bhutan",
+ "BO": "Bolivia",
+ "BQ": "Bonaire, Saint Eustatius and Saba",
+ "BA": "Bosnia and Herzegovina",
+ "BW": "Botswana",
+ "BV": "Bouvet Island",
+ "BR": "Brazil",
+ "IO": "British Indian Ocean Territory",
+ "VG": "British Virgin Islands",
+ "BN": "Brunei",
+ "BG": "Bulgaria",
+ "BF": "Burkina Faso",
+ "BI": "Burundi",
+ "KH": "Cambodia",
+ "CM": "Cameroon",
+ "CA": "Canada",
+ "CV": "Cape Verde",
+ "KY": "Cayman Islands",
+ "CF": "Central African Republic",
+ "TD": "Chad",
+ "CL": "Chile",
+ "CN": "China",
+ "CX": "Christmas Island",
+ "CC": "Cocos Islands",
+ "CO": "Colombia",
+ "KM": "Comoros",
+ "CK": "Cook Islands",
+ "CR": "Costa Rica",
+ "HR": "Croatia",
+ "CU": "Cuba",
+ "CW": "Curacao",
+ "CY": "Cyprus",
+ "CZ": "Czech Republic",
+ "CD": "Democratic Republic of the Congo",
+ "DK": "Denmark",
+ "DJ": "Djibouti",
+ "DM": "Dominica",
+ "DO": "Dominican Republic",
+ "TL": "East Timor",
+ "EC": "Ecuador",
+ "EG": "Egypt",
+ "SV": "El Salvador",
+ "GQ": "Equatorial Guinea",
+ "ER": "Eritrea",
+ "EE": "Estonia",
+ "ET": "Ethiopia",
+ "FK": "Falkland Islands",
+ "FO": "Faroe Islands",
+ "FJ": "Fiji",
+ "FI": "Finland",
+ "FR": "France",
+ "GF": "French Guiana",
+ "PF": "French Polynesia",
+ "TF": "French Southern Territories",
+ "GA": "Gabon",
+ "GM": "Gambia",
+ "GE": "Georgia",
+ "DE": "Germany",
+ "GH": "Ghana",
+ "GI": "Gibraltar",
+ "GR": "Greece",
+ "GL": "Greenland",
+ "GD": "Grenada",
+ "GP": "Guadeloupe",
+ "GU": "Guam",
+ "GT": "Guatemala",
+ "GG": "Guernsey",
+ "GN": "Guinea",
+ "GW": "Guinea-Bissau",
+ "GY": "Guyana",
+ "HT": "Haiti",
+ "HM": "Heard Island and McDonald Islands",
+ "HN": "Honduras",
+ "HK": "Hong Kong",
+ "HU": "Hungary",
+ "IS": "Iceland",
+ "IN": "India",
+ "ID": "Indonesia",
+ "IR": "Iran",
+ "IQ": "Iraq",
+ "IE": "Ireland",
+ "IM": "Isle of Man",
+ "IL": "Israel",
+ "IT": "Italy",
+ "CI": "Ivory Coast",
+ "JM": "Jamaica",
+ "JP": "Japan",
+ "JE": "Jersey",
+ "JO": "Jordan",
+ "KZ": "Kazakhstan",
+ "KE": "Kenya",
+ "KI": "Kiribati",
+ "XK": "Kosovo",
+ "KW": "Kuwait",
+ "KG": "Kyrgyzstan",
+ "LA": "Laos",
+ "LV": "Latvia",
+ "LB": "Lebanon",
+ "LS": "Lesotho",
+ "LR": "Liberia",
+ "LY": "Libya",
+ "LI": "Liechtenstein",
+ "LT": "Lithuania",
+ "LU": "Luxembourg",
+ "MO": "Macao",
+ "MK": "Macedonia",
+ "MG": "Madagascar",
+ "MW": "Malawi",
+ "MY": "Malaysia",
+ "MV": "Maldives",
+ "ML": "Mali",
+ "MT": "Malta",
+ "MH": "Marshall Islands",
+ "MQ": "Martinique",
+ "MR": "Mauritania",
+ "MU": "Mauritius",
+ "YT": "Mayotte",
+ "MX": "Mexico",
+ "FM": "Micronesia",
+ "MD": "Moldova",
+ "MC": "Monaco",
+ "MN": "Mongolia",
+ "ME": "Montenegro",
+ "MS": "Montserrat",
+ "MA": "Morocco",
+ "MZ": "Mozambique",
+ "MM": "Myanmar",
+ "NA": "Namibia",
+ "NR": "Nauru",
+ "NP": "Nepal",
+ "NL": "Netherlands",
+ "AN": "Netherlands Antilles",
+ "NC": "New Caledonia",
+ "NZ": "New Zealand",
+ "NI": "Nicaragua",
+ "NE": "Niger",
+ "NG": "Nigeria",
+ "NU": "Niue",
+ "NF": "Norfolk Island",
+ "KP": "North Korea",
+ "MP": "Northern Mariana Islands",
+ "NO": "Norway",
+ "OM": "Oman",
+ "PK": "Pakistan",
+ "PW": "Palau",
+ "PS": "Palestinian Territory",
+ "PA": "Panama",
+ "PG": "Papua New Guinea",
+ "PY": "Paraguay",
+ "PE": "Peru",
+ "PH": "Philippines",
+ "PN": "Pitcairn",
+ "PL": "Poland",
+ "PT": "Portugal",
+ "PR": "Puerto Rico",
+ "QA": "Qatar",
+ "CG": "Republic of the Congo",
+ "RE": "Reunion",
+ "RO": "Romania",
+ "RU": "Russia",
+ "RW": "Rwanda",
+ "BL": "Saint Barthelemy",
+ "SH": "Saint Helena",
+ "KN": "Saint Kitts and Nevis",
+ "LC": "Saint Lucia",
+ "MF": "Saint Martin",
+ "PM": "Saint Pierre and Miquelon",
+ "VC": "Saint Vincent and the Grenadines",
+ "WS": "Samoa",
+ "SM": "San Marino",
+ "ST": "Sao Tome and Principe",
+ "SA": "Saudi Arabia",
+ "SN": "Senegal",
+ "RS": "Serbia",
+ "CS": "Serbia and Montenegro",
+ "SC": "Seychelles",
+ "SL": "Sierra Leone",
+ "SG": "Singapore",
+ "SX": "Sint Maarten",
+ "SK": "Slovakia",
+ "SI": "Slovenia",
+ "SB": "Solomon Islands",
+ "SO": "Somalia",
+ "ZA": "South Africa",
+ "GS": "South Georgia and the South Sandwich Islands",
+ "KR": "South Korea",
+ "SS": "South Sudan",
+ "ES": "Spain",
+ "LK": "Sri Lanka",
+ "SD": "Sudan",
+ "SR": "Suriname",
+ "SJ": "Svalbard and Jan Mayen",
+ "SZ": "Swaziland",
+ "SE": "Sweden",
+ "CH": "Switzerland",
+ "SY": "Syria",
+ "TW": "Taiwan",
+ "TJ": "Tajikistan",
+ "TZ": "Tanzania",
+ "TH": "Thailand",
+ "TG": "Togo",
+ "TK": "Tokelau",
+ "TO": "Tonga",
+ "TT": "Trinidad and Tobago",
+ "TN": "Tunisia",
+ "TR": "Turkey",
+ "TM": "Turkmenistan",
+ "TC": "Turks and Caicos Islands",
+ "TV": "Tuvalu",
+ "VI": "U.S. Virgin Islands",
+ "UG": "Uganda",
+ "UA": "Ukraine",
+ "AE": "United Arab Emirates",
+ "GB": "United Kingdom",
+ "US": "United States",
+ "UM": "United States Minor Outlying Islands",
+ "UY": "Uruguay",
+ "UZ": "Uzbekistan",
+ "VU": "Vanuatu",
+ "VA": "Vatican",
+ "VE": "Venezuela",
+ "VN": "Vietnam",
+ "WF": "Wallis and Futuna",
+ "EH": "Western Sahara",
+ "YE": "Yemen",
+ "ZM": "Zambia",
+ "ZW": "Zimbabwe",
+ # Below are not country codes but recognized as regions / TLDs
+ "AC": "Ascension Island",
+ "EU": "European Union",
+ "SU": "Soviet Union",
+ "UK": "United Kingdom"
+ }
+
+ def countryNameFromCountryCode(self, countryCode):
+ """Convert a country code to full country name
+
+ Args:
+ countryCode (str): country code
+
+ Returns:
+ str: country name
+ """
+ if not isinstance(countryCode, str):
+ return None
+
+ return self.getCountryCodeDict().get(countryCode.upper())
+
+ def countryNameFromTld(self, tld):
+ """Retrieve the country name associated with a TLD.
+
+ Args:
+ tld (str): Top level domain
+
+ Returns:
+ str: country name
+ """
+
+ if not isinstance(tld, str):
+ return None
+
+ country_name = self.getCountryCodeDict().get(tld.upper())
+
+ if country_name:
+ return country_name
+
+ country_tlds = {
+ # List of TLD not associated with any country
+ "COM": "United States",
+ "NET": "United States",
+ "ORG": "United States",
+ "GOV": "United States",
+ "MIL": "United States"
+ }
+
+ country_name = country_tlds.get(tld.upper())
+
+ if country_name:
+ return country_name
+
+ return None
+
def parseIBANNumbers(self, data):
"""Find all International Bank Account Numbers (IBANs) within the supplied content.
@@ -1752,7 +2067,6 @@ class SpiderFoot:
Returns:
list: list of IBAN
"""
-
if not isinstance(data, str):
return list()
diff --git a/test/unit/test_spiderfoot.py b/test/unit/test_spiderfoot.py
index a2a1872d..f2374112 100644
--- a/test/unit/test_spiderfoot.py
+++ b/test/unit/test_spiderfoot.py
@@ -1034,6 +1034,38 @@ class TestSpiderFoot(unittest.TestCase):
self.assertIsInstance(cards, list)
self.assertIn("4111111111111111", cards)
+ def test_getCountryCodeDict_should_return_a_dict(self):
+ """
+ Test getCountryCodeDict(self)
+ """
+ sf = SpiderFoot(dict())
+
+ country_code_dict = sf.getCountryCodeDict()
+ self.assertIsInstance(country_code_dict, dict)
+
+ def test_countryNameFromCountryCode_argument_countryCode_should_return_country_as_a_string(self):
+ """
+ Test countryNameFromCountryCode(self, countryCode)
+ """
+ sf = SpiderFoot(dict())
+
+ country_name = sf.countryNameFromCountryCode('US')
+ self.assertIsInstance(country_name, str)
+ self.assertEqual(country_name, "United States")
+
+ def test_countryNameFromTld_argument_tld_should_return_country_as_a_string(self):
+ """
+ Test countryNameFromTld(self, tld)
+ """
+ sf = SpiderFoot(dict())
+
+ tlds = ['com', 'net', 'org', 'gov', 'mil']
+ for tld in tlds:
+ with self.subTest(tld=tld):
+ country_name = sf.countryNameFromTld(tld)
+ self.assertIsInstance(country_name, str)
+ self.assertEqual(country_name, "United States")
+
def test_parse_iban_numbers_should_return_a_list(self):
"""
Test parseIBANNumbers(self, data)