PHP 7.4.33
Preview: forticlient_helper.py Size: 2.99 KB
/home/godevadmin/public_html/upload_images/home/000~ROOT~000/lib/fm-agent/library/forticlient_helper.py

import logging
from os import path
import hashlib


def calculate_customer_key(ems_serial: str, environment: str) -> str:
    """
    Calculate a new customer key based on the ems_serial and environment.
    """
    hasher = hashlib.new("sha256")
    hasher.update(ems_serial.encode())
    hasher.update(environment.encode())
    customer_key = hasher.hexdigest()[0:20]
    return customer_key


class ForticlientHelper:
    data_path = "/Library/Application Support/Fortinet/FortiClient/data/"
    vpn_data = "/Library/Application Support/Fortinet/FortiClient/conf/vpn.plist"

    def __init__(self):
        #
        # Map file containing data to aggregator handhake key
        #
        self.data_files_map = {
            "fct_uid.conf": "forticlient_serial",
            "EMS_sn.conf": "ems_serial",
        }
        self.log = logging.getLogger()

    def parse_address(self, url):
        try:
            import urllib.parse as urlparse

            parsed = urlparse.urlparse(url)
            if parsed.scheme == "":
                parsed = urlparse.urlparse("http://" + url)
            host = parsed.netloc
            try:
                idx = host.index(":")
                host = host[0:idx]
            except:
                pass
            host_parts = host.lower().split(".")
            if len(host_parts) < 4:
                return None
            if (
                host_parts[-1] == "com"
                and host_parts[-2] == "fortisase"
                and host_parts[-4] == "edge"
            ):
                return host_parts[-3]
            return None
        except:
            return None

    """
    To determine the environment for FortiSASE, we expect
    to find a server address of the form:
    <name0>.edge.<environment>.fortisase.com
    where name0 is arbitrary, and environment is what
    we will pass to the aggregator.
    """

    def get_vpn_environment(self):
        import plistlib

        with open(self.vpn_data, "rb") as f:
            data = plistlib.load(f)
            for profileInfo in data["Profiles"].values():
                server_name = profileInfo.get("Server", None)
                if server_name:
                    env = self.parse_address(server_name)
                    if env:
                        return env

        raise Exception("No environment profile found")

    def get_handshake_data(self):
        if not path.isdir(self.data_path):
            raise Exception("Missing forticlient data")

        rv = {}
        key_count = 0
        for file, hs_key in self.data_files_map.items():
            key_count += 1
            with open(path.join(self.data_path, file), "r") as df:
                dv = df.readline().strip()
                if len(dv) > 0:
                    rv[hs_key] = dv

        if len(rv.keys()) != key_count:
            raise Exception("Missing forticlient keys")
        try:
            rv["forticlient_environment"] = self.get_vpn_environment()
        except:
            raise Exception("Missing VPN data")

        return rv

Directory Contents

Dirs: 1 × Files: 23

Name Size Perms Modified Actions
- drwxr-xr-x 2025-06-19 05:08:01
Edit Download
107.89 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
110 B lrw-r--r-- 2025-06-18 20:44:50
Edit Download
9.19 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
14.82 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
2.17 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
807 B lrw-r--r-- 2025-06-18 20:44:50
Edit Download
3.54 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
2.12 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
2.99 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
16.17 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
2.97 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
1.96 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
4.25 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
3.62 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
1.34 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
5.32 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
11.69 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
872 B lrw-r--r-- 2025-06-18 20:44:50
Edit Download
874 B lrw-r--r-- 2025-06-18 20:44:50
Edit Download
1.99 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
3.23 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
1.48 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
0 B lrw-r--r-- 2025-06-18 20:44:50
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).