Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions automate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import logging
import os
import socket
import sys
import threading
import time
Expand Down Expand Up @@ -32,6 +33,28 @@
else:
sys.exit(1)

def is_port_available(port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.bind(('localhost', port))
return True
except socket.error:
return False

def find_available_port(start_port=1234):
port = start_port
while port < start_port + 100:
if is_port_available(port):
print(f"Using port: {port}")
return port
else:
print(f"Port {port} is occupied, trying next...")
port += 1

raise RuntimeError(f"Cannot find available port (tried range: {start_port}-{port-1})")

flask_port = find_available_port()

# Start serving attacker app
app = Flask(__name__)

Expand All @@ -43,7 +66,7 @@ def root():
def static_dir(path):
return send_from_directory(f"part{opts.part}", path)

flask_thread = threading.Thread(target=app.run, kwargs={"port": 1234})
flask_thread = threading.Thread(target=app.run, kwargs={"port": flask_port})
flask_thread.setDaemon(True)
flask_thread.start()

Expand All @@ -60,7 +83,7 @@ def get_browser(victim):
return webdriver.Safari()

attacker = get_browser(victim=False)
attacker.get("http://localhost:1234")
attacker.get(f"http://localhost:{flask_port}")
attacker.execute_script(f"window.trace_length = {opts.trace_length}")
attacker.execute_script(f"window.using_automation_script = true")

Expand Down