Guest User

Solution for a challenge

a guest
Nov 10th, 2024
900
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | Source Code | 0 0
  1. # Please don't judge my scripting too harshly, I am very sleep deprived.
  2.  
  3.  
  4. import requests
  5. import string
  6.  
  7. BASE_URL = "http://localhost:3000"  # Server URL
  8. ALPHABET = string.ascii_lowercase  # All alphabets
  9. flags = {}  # Dictionary to store the most recent filename for each flag
  10. found_patterns = set()  # Set to track checked patterns
  11. request_count = 0  # Counter to track the number of requests made
  12.  
  13. def check_pattern(pattern):
  14.     global found_patterns, request_count
  15.     response = requests.get(f"{BASE_URL}/{pattern}")
  16.     request_count += 1  
  17.  
  18.     # Print request count at intervals of 100
  19.     if request_count % 100 == 0:
  20.         print(f"Requests made: {request_count}",end='\r')
  21.  
  22.     if response.status_code == 200:
  23.         content = response.text.strip()
  24.        
  25.         if pattern not in found_patterns:
  26.             found_patterns.add(pattern)
  27.            
  28.             flags[content] = pattern  # Update the dictionary with the most recent pattern for each flag content by overwriting it
  29.         return True
  30.     return False
  31.  
  32. def brute_force():
  33.     # Step 1: Start with single characters [I did it like this because my brain wasn't working on 2 hours of sleep]
  34.     to_check = list(ALPHABET)
  35.  
  36.     # Step 2: Iteratively build patterns via extension
  37.     while to_check:
  38.         current_pattern = to_check.pop(0) # Pop off patterns that are being checked
  39.  
  40.         if check_pattern(current_pattern):
  41.             # Generate new patterns by extending current pattern with each alphabet letter
  42.             for char in ALPHABET:
  43.                 new_pattern = current_pattern + char
  44.                 if new_pattern not in found_patterns:
  45.                     to_check.append(new_pattern)
  46.            
  47.             # Also try appending a dot if not already present
  48.             if '.' not in current_pattern:
  49.                 dot_pattern = current_pattern + '.'
  50.                 if dot_pattern not in found_patterns:
  51.                     to_check.append(dot_pattern)
  52.  
  53.     # Print final summary with all unique flags and their most recent associated filename
  54.     print("\nSummary of found flags:")
  55.     print(f"Total requests made: {request_count}")
  56.     for content, filename in flags.items():
  57.         print(f"Flag: {content} | Most recent filename: {filename}")
  58.  
  59. if __name__ == "__main__":
  60.     brute_force()
  61.  
Advertisement
Add Comment
Please, Sign In to add comment