import os
import requests

# Base URL of the insecure web application
target_url = "http://127.0.0.1:5001/"

if __name__ == "__main__":

    # Or load a dictionary of common file names from a file
    DICT_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "cybercorp_dict.txt")  

    with open(DICT_FILE, "r") as f:
        directories = f.readlines()
        directories = [x.strip().replace("\n", "") for x in directories]

    print(F"Loaded {len(directories)} directory names from wordlist.")

    # Store results of found files
    found_dirs = {}

    # Loop through the dictionary of files
    for dir in directories:
        # Construct the full URL
        url = f"{target_url}/{dir}"
        try:
            # Make the HTTP GET request
            response = requests.get(url)

            # If the status code is 200, the file exists
            if response.status_code == 200:
                print(f"✅ Found: {url}")
                found_dirs[dir] = response.text  # Store the file's content
            # else:
            #     print(f"❌ Not Found: {url} (Status Code: {response.status_code})")

        except requests.RequestException as e:
            print(f"Error making request to {url}: {e}")

    # Display found directories
    if found_dirs:
        print("\nDirectories found:")
        print("="*60)
        for dir_name, content in found_dirs.items():
            print(f"{target_url}/{dir_name}")
        print("=" * 60)
    else:
        print("\nNo directories were found from the dictionary attack.")