With IPstack’s geolocation endpoint, you can verify the geographic location of a user’s IP address and compare it with other available information, such as billing address or transaction history. This helps in detecting fraudulent activities quickly.
In this guide, we’ll walk you through collecting geolocation data from IPstack’s API to look for discrepancies between the location data and your transaction details.
We will use the following data:
Collect the user’s IP address, and if available, their billing address or transaction history.
Send a request to the IPstack API with the user’s IP address, and extract the geographic location from the response.
Compare the geographic location with the user’s billing address or transaction history.
If there are discrepancies between the geographic location and other information, flag the transaction as potentially fraudulent.
Depending on your needs, you may take further actions like blocking the transaction, sending a warning, or initiating additional verification steps.
Use the following Python code to implement the above steps:
import requests
def get_geolocation(api_key):
url = f"https://api.ipstack.com/check?access_key={api_key}"
response = requests.get(url)
return response.json()
def is_fraudulent(geolocation, billing_address):
# Compare the geolocation with the billing address or other transaction details.
return geolocation['country_code'] != billing_address['country_code']
def main():
api_key = "YOUR_IPSTACK_API_KEY" # Replace with your IPstack API key
# Example billing address; you would typically get this from your own system
billing_address = {
"country_code": "US",
# Other details like state, city, etc.
}
geolocation = get_geolocation(api_key)
if is_fraudulent(geolocation, billing_address):
print("Potential fraudulent activity detected!")
else:
print("No fraudulent activity detected.")
if __name__ == "__main__":
main()
The code provided performs an IP geolocation lookup to detect potential fraudulent activity based on the location of the user making the request. Here’s a detailed explanation of what each part of the code does:
1. Importing the requests library: The code imports the requests library, which is used to make HTTP requests to the IPstack API.
2. get_geolocation(api_key) function:
a. Input: Takes the IPstack API key as an argument.
b. Functionality: Constructs the URL to the “Requester IP Lookup” endpoint of the IPstack API and makes a GET request to it.
c. Output: Returns the JSON response from the API, which includes geolocation information such as the country code, region, city, and other details related to the IP address that made the request.
3. is_fraudulent(geolocation, billing_address) function:
a. Input: Takes the geolocation information and a billing address (containing a country code).
b. Functionality: Compares the country code from the geolocation information with the country code in the billing address.
c. Output: Returns True if the country codes do not match (indicating potential fraud) and False otherwise.
4. main() function:
a. Functionality: Defines the IPstack API key and an example billing address. Calls the get_geolocation function to get the geolocation information of the requester. Then, calls the is_fraudulent function to check if there is a mismatch between the geolocation country code and the billing address country code.
c. Output: Prints a message indicating whether potential fraudulent activity was detected or not.
5. Execution Entry Point: If the script is run as the main program (not imported as a module), the main() function is called, executing the entire process.
Beyond basic geolocation verification, IPstack provides additional features to detect the use of VPN, proxies, and various threat types. These advanced capabilities allow for a more nuanced understanding of potential security risks.
A common method employed by fraudulent users is to mask their real IP address using VPNs or proxies. These tools can be used to hide a user’s actual location, thus bypassing geographic restrictions and evading detection. By employing IPstack’s security features, you can detect VPNs and proxies and act accordingly.
Here’s how to incorporate VPN and proxy detection into your fraud detection process:
a. Modify the Geolocation Request
Add the security parameter to the URL when sending the request to IPstack’s API to get information related to VPN and proxy usage:
url = f”https://api.ipstack.com/check?access_key={api_key}&security=1″
If you want to look up an IP address other than that of the requester, you can replace check in the URL with the specific IP address you want to investigate.
For example:
url = f”https://api.ipstack.com/{ip_address}?access_key={api_key}&security=1″
b. Analyze the Security Information
Evaluate the security-related data in the response, including the VPN and proxy types:
def is_vpn_or_proxy(security_data):
return security_data['is_proxy'] or security_data['proxy_type'] == 'vpn'
This function can be used to determine if the IP address is utilizing a VPN or proxy, as follows:
Identifying the use of VPNs and proxies can be vital in detecting fraudulent activities, as they are commonly used to mask the true location of a user. By leveraging this information, you can establish additional verification processes for users employing VPNs or proxies, reducing the likelihood of fraudulent transactions.
These security and threat indicators can be used in conjunction to make more informed decisions about whether a request or transaction might be fraudulent. Integrating these features into your fraud detection system will enable you to better identify and respond to various kinds of malicious or suspicious activity, enhancing the protection of your platform and users.
c. Integrate VPN/Proxy Detection
You can integrate this information into your existing fraud detection logic to take appropriate action if a VPN or proxy is detected. This might include blocking the transaction, requiring additional verification, or flagging for manual review.
In addition to detecting VPNs and proxies, IPstack’s API provides information about various threat types associated with an IP address. These threat types can further enhance your fraud detection capabilities.
a. Evaluate Threat Types
You can analyze the threat types and levels provided in the response. Here’s a function that does this:
def analyze_threats(security_data):
threat_level = security_data['threat_level']
threat_types = security_data['threat_types']
# Additional logic to handle various threat scenarios
The threat_level can be one of the following:
The threat_types object may include information about the following potential threats:
These threat indicators can be used to make more informed decisions about whether a request or transaction might be fraudulent. For example, a high threat level with specific attack sources identified could be a strong indicator of malicious activity.
You can further tailor the analyze_threats function to match your specific needs, defining actions or responses based on the threat level or specific threat types detected.
By integrating these features into your fraud detection system, you’ll be better equipped to identify and respond to various kinds of malicious or suspicious activity, helping to protect your platform and users.
b. Customize Your Response
Based on the detected threats, you may customize your response to handle different levels of risk. For example, you might block interactions from high-risk IPs or require additional verification for medium-risk scenarios.
By leveraging IPstack’s advanced security features, including VPN and proxy detection and comprehensive threat analysis, you can build a robust and responsive fraud detection system. These capabilities provide deeper insights into the risks associated with an IP address, allowing you to tailor your defenses and respond effectively to various threat scenarios.
Remember to consult the official IPstack documentation for detailed information on all available parameters and response fields. Regularly update your logic to adapt to new risks and remain compliant with legal regulations.
By incorporating these advanced techniques, you’ll achieve enhanced security, more accurate fraud detection, and a safer environment for your users and transactions.