import xml.etree.ElementTree as ET import os import subprocess import requests def request(function,variable): ##API-Server url = (f"http://api.stines.de:8001/{function}") response = requests.post(url,json = variable,headers={'access_token':'^YWUbG7yX*V!tV^KBSd*2c&vdN3wV9a2i7f3hfGFMBYFxi6#mMiJGiaA5KEHE%B*miK%qb7rQ67gmcYP@gqmux8'}) return response def extract_gpo_permissions(xml_file_path, output_file_path): # XML-Datei laden with open(xml_file_path, 'r', encoding="utf-8") as xml_file: xml_content = xml_file.read() root = ET.fromstring(xml_content) count = 0 # XML-Namespace deklarieren namespaces = { "gp": "http://www.microsoft.com/GroupPolicy/Types/Security", "type" : "http://www.microsoft.com/GroupPolicy/Types", "gp_drive": "http://www.microsoft.com/GroupPolicy/Settings/DriveMaps" } gruppe = [] # Alle TrusteePermissions-Blöcke aus der XML-Datei auswählen trustee_permissions_blocks = root.findall(".//gp:TrusteePermissions", namespaces) for trustee_permissions_block in trustee_permissions_blocks: trustee = trustee_permissions_block.find(".//type:Name", namespaces).text # Überprüfe, ob der Trustee-Name mit "G-" beginnt oder "scannerworkst" enthält if "G-" in trustee or "scannerwork" in trustee: trustee = trustee.split('\\') result = f"{trustee[-1]}" # print(result) if result != "": gruppe.append(result) drives = [] # Alle DriveMapSettings-Blöcke aus der XML-Datei auswählen drive_map_settings_blocks = root.findall(".//gp_drive:DriveMapSettings/gp_drive:Drive", namespaces) for drive_map_settings_block in drive_map_settings_blocks: properties = drive_map_settings_block.find(".//gp_drive:Properties", namespaces) if properties.get("action") != "D": drive_path = properties.get("path") drive_letter = properties.get("letter") drive_label = properties.get("label") # Erstelle ein neues Array für die Ergebnisse der aktuellen Schleifeniteration aktuelles_ergebnis = [gruppe, drive_letter, drive_path, drive_label] # Füge das aktuelle Array zur Liste der Ergebnisse hinzu drives.append(aktuelles_ergebnis) return drives def get_local_ip(): powershell_script = ''' (Get-NetIPAddress | Where-Object { $_.PrefixOrigin -eq "Dhcp" }).IPAddress ''' result = subprocess.check_output(["powershell.exe", "-command", powershell_script], universal_newlines=True) ip = result.rsplit('.',1) print(ip[0]) ip = ip[0] + '.' + "0" return ip def powershell_gpo_export(): script = r'''# Verzeichnis, in dem die XML-Berichte gespeichert werden sollen $reportDirectory = "C:\GPO_Export" # Verzeichnis, in dem die DriveMapSettings-Berichte gespeichert werden sollen $driveMapSettingsDirectory = "C:\GPO_Export\DriveMapSettings" # Erstellen Sie den Ordner "DriveMapSettings", wenn er nicht vorhanden ist if (-not (Test-Path -Path $driveMapSettingsDirectory)) { New-Item -Path $driveMapSettingsDirectory -ItemType Directory } # Abrufen aller GPOs in Ihrer Domäne $allGPOs = Get-GPO -All # Iterieren durch alle GPOs und erstellen Sie einen Bericht für jedes GPO foreach ($GPO in $allGPOs) { # GPO-Namen ohne Doppelpunkte $GPONameWithoutColon = $GPO.DisplayName -replace ':', '' # Berichtsnamen basierend auf dem GPO-Namen ohne Doppelpunkte $reportName = "$GPONameWithoutColon.xml" # Vollständiger Pfad zum XML-Bericht $reportPath = Join-Path -Path $reportDirectory -ChildPath $reportName # GPO-Bericht erstellen und in XML speichern $GPO | Get-GPOReport -ReportType XML | Out-File -FilePath "$reportPath" -Encoding UTF8 # Überprüfen, ob der Bericht DriveMapSettings-Einstellungen enthält $containsDriveMapSettings = (Get-Content -Path $reportPath) -match "DriveMapSettings" if ($containsDriveMapSettings) { # Verschieben Sie den Bericht in den "DriveMapSettings"-Ordner $newReportPath = Join-Path -Path $driveMapSettingsDirectory -ChildPath $reportName Move-Item -Path $reportPath -Destination $newReportPath Write-Host "Bericht für $($GPO.DisplayName) mit DriveMapSettings wurde in $newReportPath verschoben." } else { # Löschen Sie den Bericht, wenn er keine DriveMapSettings-Einstellungen enthält Remove-Item -Path $reportPath } } # Verzeichnis, in dem die XML-Dateien gespeichert sind $xmlDirectoryPath = $driveMapSettingsDirectory # Verzeichnis, in dem die Ergebnisse gespeichert werden sollen $outputDirectory = $driveMapSettingsDirectory if (-not (Test-Path -Path $outputDirectory)) { New-Item -Path $outputDirectory -ItemType Directory } ''' subprocess.run(["powershell", "-Command", script], capture_output = True,text = True) powershell_gpo_export() database = request("host/get/database",{"ip":get_local_ip()}) ##GET DATABASE FROM SQL-SERVER ordner_pfad = 'C:\\GPO_Export\\DriveMapSettings\\' # Durchlaufe alle Dateien im Ordner for datei in os.listdir(ordner_pfad): count = 0 if datei.endswith('.xml'): gruppe = extract_gpo_permissions(f"{ordner_pfad}\\{datei}", "output.txt") for i in gruppe: try: gruppe_name = i[0][0] letter = i[1] path = i[2] label = i[3] print(gruppe_name) print(request("add/group/volume",{"group": gruppe_name,"letter": letter, "path": path.replace("\\","\\\\"),"label": label, "dbname": database.text.replace('"','')}).text) except: next