-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathawswaf_classification.py
More file actions
106 lines (83 loc) · 3 KB
/
awswaf_classification.py
File metadata and controls
106 lines (83 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import os
import base64
import requests
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# CapSolver API configuration
CAPSOLVER_API_KEY = os.getenv("CAPSOLVER_API_KEY")
CREATE_TASK_URL = "https://api.capsolver.com/createTask"
# Question types for AWS WAF classification
# Grid type questions (support up to 9 images)
GRID_QUESTIONS = [
"aws:grid:bed",
"aws:grid:bag",
"aws:grid:hat",
"aws:grid:chair",
"aws:grid:bucket",
"aws:grid:curtain",
"aws:grid:mop",
"aws:grid:clock",
"aws:grid:suitcase",
"aws:grid:binocular",
"aws:grid:cooking pot"
]
# Toy car city type (supports 1 image, marks endpoint of car path)
TOYCARCITY_QUESTIONS = [
"aws:toycarcity:carcity"
]
def solve_awswaf_classification(images, question):
"""
Classify AWS WAF image challenges.
Args:
images: List of base64 encoded images (up to 9 for grid, 1 for toycarcity)
question: Question type (see GRID_QUESTIONS or TOYCARCITY_QUESTIONS)
Returns:
Solution containing objects (grid) or box coordinates (toycarcity)
"""
if not images:
raise ValueError("At least one image must be provided")
payload = {
"clientKey": CAPSOLVER_API_KEY,
"task": {
"type": "AwsWafClassification",
"images": images if isinstance(images, list) else [images],
"question": question
}
}
# AwsWafClassification returns result directly (no polling needed)
response = requests.post(CREATE_TASK_URL, json=payload)
result = response.json()
if result.get("errorId") != 0:
raise Exception(f"Failed to solve: {result.get('errorDescription')}")
return result.get("solution", {})
def load_images_as_base64(image_paths):
"""Helper to load multiple images as base64."""
images = []
for path in image_paths:
with open(path, "rb") as f:
images.append(base64.b64encode(f.read()).decode("utf-8"))
return images
def main():
if not CAPSOLVER_API_KEY:
print("Error: CAPSOLVER_API_KEY not found in .env file")
print("Please create a .env file with your API key:")
print("CAPSOLVER_API_KEY=your_api_key_here")
return
print("AWS WAF Image Classification")
print("\nGrid Questions (support up to 9 images):")
for q in GRID_QUESTIONS:
print(f" {q}")
print("\nToy Car City Questions (1 image):")
for q in TOYCARCITY_QUESTIONS:
print(f" {q}")
print("\nExample usage:")
print(" # For grid challenges:")
print(' images = load_images_as_base64(["img1.png", "img2.png", ...])')
print(' solution = solve_awswaf_classification(images, "aws:grid:chair")')
print(" # Returns: {'objects': [0, 2, 5]} # indices of matching images")
print("\n # For toy car city:")
print(' solution = solve_awswaf_classification([base64_image], "aws:toycarcity:carcity")')
print(" # Returns: {'box': [x, y]} # endpoint coordinates")
if __name__ == "__main__":
main()