Python-Penetrationstests: Erstellen von Multi-Option-Tools

Mar 10 2023
R. Eric Kiser Während eines Penetrationstests kann es vorkommen, dass Sie ein Tool entwickeln müssen, das leicht und leise ist.

R. Eric Kiser

Während eines Penetrationstests kann es Situationen geben, in denen Sie ein leichtes und leises Tool entwickeln müssen. Nach der Erkundung des Systems erhalten Sie ein klareres Verständnis für die Struktur der Netzwerksysteme. Mit diesem Wissen können Sie ein Tool erstellen, das mehrere Aufgaben mit geringen Auswirkungen ausführt. In diesem Artikel werde ich zeigen, wie man ein Python-Skript erstellt, das ein Befehlszeilenprogramm enthält, das Linux-Systeme mithilfe von Ansible verwaltet.

Wir müssen die Module osund importieren subprocess , um mit dem Betriebssystem zu interagieren und Befehle auszuführen.

import os
import subprocess

Die erste Funktion bietet die Möglichkeit, Ansible auf dem System zu installieren, indem die subprocess.runFunktion zum Ausführen der erforderlichen Befehle verwendet wird.

def install_ansible():
    subprocess.run(["sudo", "apt", "update"])
    subprocess.run(["sudo", "apt", "install", "software-properties-common"])
    subprocess.run(["sudo", "apt-add-repository", "--yes", "--update", "ppa:ansible/ansible"])
    subprocess.run(["sudo", "apt", "install", "ansible"])

def add_ssh_key():
    ip = input("Enter the IP address of the remote server: ")
    username = input("Enter the username on the remote server: ")
    ssh_key_path = input("Enter the path to the SSH key: ")
    subprocess.run(["ssh-copy-id", f"{username}@{ip}", "-i", ssh_key_path])

def download_files():
    ips = input("Enter the IP addresses of the systems to download files from (comma-separated): ")
    files = input("Enter the files to download (comma-separated): ")
    dest = input("Enter the destination folder to save the files: ")
    for ip in ips.split(","):
        for file in files.split(","):
            subprocess.run(["ansible", f"{ip}", "-m", "fetch", "-a", f"src={file} dest={os.path.join(dest, ip)}"])

def download_files():
    ips = input("Enter the IP addresses of the systems to download files from (comma-separated): ")
    files = input("Enter the files to download (comma-separated): ")
    dest = input("Enter the destination folder to save the files: ")
    for ip in ips.split(","):
        for file in files.split(","):
            subprocess.run(["ansible", f"{ip}", "-m", "fetch", "-a", f"src={file} dest={os.path.join(dest, ip)}"])

def transfer_files():
    source_ip = input("Enter the IP address of the source server: ")
    dest_ip = input("Enter the IP address of the destination server: ")
    files = input("Enter the files to transfer (comma-separated): ")
    for file in files.split(","):
        subprocess.run(["ansible", f"{source_ip}", "-m", "copy", "-a", f"src={file} dest={dest_ip}:"])

choice = input("Enter your choice: ")
        if choice == "1":
            install_ansible()
        elif choice == "2":
            add_ssh_key()
        elif choice == "3":
            scan_files()
        elif choice == "4":
            download_files()
        elif choice == "5":
            transfer_files()
        elif choice == "0":
            break
        else:
            print("Invalid choice")

if __name__ == "__main__":
    main()

Hier hast du es. Sie haben jetzt die Möglichkeit, jedes gewünschte Werkzeug zu konstruieren! Klatschen Sie, folgen Sie und teilen Sie ihn, wenn Ihnen dieser Artikel gefällt. Viel Spaß beim Jagen!