2023-02-23 15:34:42 -05:00
|
|
|
import requests
|
|
|
|
|
import subprocess
|
|
|
|
|
import gzip
|
|
|
|
|
import urllib.request
|
|
|
|
|
import base64
|
|
|
|
|
|
|
|
|
|
# Variables of the needed elements
|
2024-08-07 19:37:41 +02:00
|
|
|
extract_id = "ENTER_EXTRACT_ID"
|
|
|
|
|
auth_token = "ENTER_AUTH_TOKEN"
|
2023-02-23 15:34:42 -05:00
|
|
|
base_url = "https://analytics.northpass.io/extracts"
|
2024-08-07 19:37:41 +02:00
|
|
|
encryption_key = "ENTER_ENCRYPTION_KEY"
|
2023-02-23 15:34:42 -05:00
|
|
|
current_dir = "./"
|
|
|
|
|
|
|
|
|
|
# Set the API endpoint, authentication headers, and other parameters
|
|
|
|
|
headers = {
|
|
|
|
|
"accept": "application/json",
|
2024-08-07 19:37:41 +02:00
|
|
|
"X-Api-Key": "ENTER_API_KEY",
|
2023-02-23 15:34:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# This first call provides you with the fileID and IV value to download the encrypted file
|
|
|
|
|
def get_extract_id(base_url, headers, extract_id, encryption_key):
|
|
|
|
|
url = f"{base_url}/{extract_id}/files/latest/"
|
|
|
|
|
response = requests.get(url, headers=headers)
|
|
|
|
|
data = response.json()
|
|
|
|
|
file_id = data["fileId"]
|
|
|
|
|
iv = data["initializationVector"]
|
|
|
|
|
download_file(base_url, headers, extract_id, file_id, encryption_key, iv)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# This function downloads the file, changes the IV and EK from base64 to Hex, and then decrypts it.
|
|
|
|
|
# It then opens and prints the results for verification that the data is human readable.
|
|
|
|
|
def download_file(base_url, headers, extract_id, file_id, encryption_key, iv):
|
|
|
|
|
url = f"{base_url}/{extract_id}/files/{file_id}/download"
|
|
|
|
|
response = requests.get(url, headers=headers)
|
|
|
|
|
data = response.json()
|
|
|
|
|
download_url = data["url"]
|
|
|
|
|
|
|
|
|
|
# Downloads the file to the cwd
|
2024-08-07 19:37:41 +02:00
|
|
|
url_resp = urllib.request.urlretrieve(download_url, "Client_API_Extract.csv.gz")
|
2023-02-23 15:34:42 -05:00
|
|
|
file_name = url_resp[0]
|
2024-08-07 19:37:41 +02:00
|
|
|
decrypted_file = "Client_File.csv.gz"
|
2023-02-23 15:34:42 -05:00
|
|
|
|
|
|
|
|
# Base64 > Hex
|
|
|
|
|
encryption_key = base64.b64decode(encryption_key).hex()
|
|
|
|
|
print(encryption_key)
|
|
|
|
|
iv = base64.b64decode(iv).hex()
|
|
|
|
|
print(iv)
|
|
|
|
|
|
|
|
|
|
# Decryption Command as provided in Northpass Documentation
|
|
|
|
|
resp = subprocess.run(
|
|
|
|
|
[
|
|
|
|
|
"openssl",
|
|
|
|
|
"enc",
|
|
|
|
|
"-aes-256-cbc",
|
|
|
|
|
"-d",
|
|
|
|
|
"-nosalt",
|
|
|
|
|
"-in",
|
|
|
|
|
file_name,
|
|
|
|
|
"-out",
|
|
|
|
|
decrypted_file,
|
|
|
|
|
"-K",
|
|
|
|
|
encryption_key,
|
|
|
|
|
"-iv",
|
|
|
|
|
iv,
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
print(resp)
|
|
|
|
|
|
|
|
|
|
# Print data in CSV to ensure it is human readable
|
|
|
|
|
with gzip.open(decrypted_file, "rb") as uncompressed:
|
|
|
|
|
file = uncompressed.read()
|
|
|
|
|
print(file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
get_extract_id(base_url, headers, extract_id, encryption_key)
|