forked from Danny-Dasilva/cycletls_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform_submission.py
More file actions
328 lines (257 loc) · 10 KB
/
Copy pathform_submission.py
File metadata and controls
328 lines (257 loc) · 10 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
"""
Form Submission Examples
This example demonstrates different ways to submit forms with CycleTLS:
- Multipart form data (file uploads)
- URL-encoded form data
- JSON form submissions
- Mixed form data with files and fields
"""
import cycletls
import json
import urllib.parse
def submit_urlencoded_form():
"""Example: Submit a URL-encoded form (application/x-www-form-urlencoded)."""
print("=" * 60)
print("URL-Encoded Form Submission")
print("=" * 60)
client = cycletls.CycleTLS()
try:
# Example 1: URL-encoded form data
print("\n1. Submitting URL-encoded form...")
# Form data as dictionary
form_data = {
"username": "johndoe",
"password": "secret123",
"remember": "true",
"email": "john@example.com"
}
# Encode form data
encoded_data = urllib.parse.urlencode(form_data)
response = client.post(
url="https://httpbin.org/post",
body=encoded_data,
headers={
"Content-Type": "application/x-www-form-urlencoded",
}
)
print(f"Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"Form data received by server:")
for key, value in data.get('form', {}).items():
print(f" {key}: {value}")
except Exception as e:
print(f"Error submitting URL-encoded form: {e}")
finally:
client.close()
def submit_multipart_form():
"""Example: Submit multipart form data with file upload."""
print("\n" + "=" * 60)
print("Multipart Form Data Submission")
print("=" * 60)
client = cycletls.CycleTLS()
try:
# Example 2: Multipart form data
print("\n2. Submitting multipart form with file...")
# Define boundary for multipart data
boundary = "----CycleTLSFormBoundary7MA4YWxkTrZu0gW"
# Build multipart form data manually
multipart_data = []
# Add text field
multipart_data.append(f'--{boundary}')
multipart_data.append('Content-Disposition: form-data; name="username"')
multipart_data.append('')
multipart_data.append('johndoe')
# Add another text field
multipart_data.append(f'--{boundary}')
multipart_data.append('Content-Disposition: form-data; name="email"')
multipart_data.append('')
multipart_data.append('john@example.com')
# Add file field with text content
multipart_data.append(f'--{boundary}')
multipart_data.append('Content-Disposition: form-data; name="file"; filename="test.txt"')
multipart_data.append('Content-Type: text/plain')
multipart_data.append('')
multipart_data.append('This is a test file content.')
# Close boundary
multipart_data.append(f'--{boundary}--')
multipart_data.append('')
# Join with CRLF
body = '\r\n'.join(multipart_data)
response = client.post(
url="https://httpbin.org/post",
body=body,
headers={
"Content-Type": f"multipart/form-data; boundary={boundary}",
}
)
print(f"Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"Form fields received:")
for key, value in data.get('form', {}).items():
print(f" {key}: {value}")
print(f"Files received:")
for key, value in data.get('files', {}).items():
print(f" {key}: {value[:50]}..." if len(value) > 50 else f" {key}: {value}")
except Exception as e:
print(f"Error submitting multipart form: {e}")
finally:
client.close()
def submit_multipart_binary_file():
"""Example: Submit multipart form with binary file (image)."""
print("\n" + "=" * 60)
print("Multipart Form with Binary File")
print("=" * 60)
client = cycletls.CycleTLS()
try:
# Example 3: Multipart with binary file
print("\n3. Submitting multipart form with binary file (PNG)...")
# Create a simple 1x1 PNG image
png_data = bytes([
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
0x08, 0x06, 0x00, 0x00, 0x00, 0x1F, 0x15, 0xC4,
0x89, 0x00, 0x00, 0x00, 0x0A, 0x49, 0x44, 0x41,
0x54, 0x78, 0x9C, 0x63, 0x00, 0x01, 0x00, 0x00,
0x05, 0x00, 0x01, 0x0D, 0x0A, 0x2D, 0xB4, 0x00,
0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE,
0x42, 0x60, 0x82
])
# Build multipart form data with binary file
boundary = "----CycleTLSBinaryBoundary9QZ8xN3bP"
# Build body parts as bytes
parts = []
# Text field
parts.append(f'--{boundary}\r\n'.encode())
parts.append(b'Content-Disposition: form-data; name="title"\r\n\r\n')
parts.append(b'My Profile Picture\r\n')
# Binary file field
parts.append(f'--{boundary}\r\n'.encode())
parts.append(b'Content-Disposition: form-data; name="image"; filename="profile.png"\r\n')
parts.append(b'Content-Type: image/png\r\n\r\n')
parts.append(png_data)
parts.append(b'\r\n')
# Close boundary
parts.append(f'--{boundary}--\r\n'.encode())
# Combine all parts
body_bytes = b''.join(parts)
response = client.post(
url="https://httpbin.org/post",
body_bytes=body_bytes,
headers={
"Content-Type": f"multipart/form-data; boundary={boundary}",
}
)
print(f"Status: {response.status_code}")
print(f"Uploaded {len(png_data)} bytes of image data")
if response.status_code == 200:
data = response.json()
print(f"Form fields: {list(data.get('form', {}).keys())}")
print(f"Files uploaded: {list(data.get('files', {}).keys())}")
except Exception as e:
print(f"Error submitting binary file: {e}")
finally:
client.close()
def submit_json_form():
"""Example: Submit form data as JSON."""
print("\n" + "=" * 60)
print("JSON Form Submission")
print("=" * 60)
client = cycletls.CycleTLS()
try:
# Example 4: JSON form submission
print("\n4. Submitting form as JSON...")
form_data = {
"user": {
"username": "johndoe",
"email": "john@example.com",
"profile": {
"firstName": "John",
"lastName": "Doe",
"age": 30
}
},
"preferences": {
"newsletter": True,
"notifications": False
}
}
response = client.post(
url="https://httpbin.org/post",
body=json.dumps(form_data),
headers={
"Content-Type": "application/json",
}
)
print(f"Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
json_received = data.get('json', {})
print(f"JSON data received by server:")
print(f" Username: {json_received.get('user', {}).get('username')}")
print(f" Email: {json_received.get('user', {}).get('email')}")
print(f" Newsletter: {json_received.get('preferences', {}).get('newsletter')}")
except Exception as e:
print(f"Error submitting JSON form: {e}")
finally:
client.close()
def submit_form_with_authentication():
"""Example: Submit form with custom headers and authentication."""
print("\n" + "=" * 60)
print("Form Submission with Authentication")
print("=" * 60)
client = cycletls.CycleTLS()
try:
# Example 5: Form with authentication headers
print("\n5. Submitting form with authentication...")
form_data = {
"action": "update_profile",
"field": "email",
"value": "newemail@example.com"
}
response = client.post(
url="https://httpbin.org/post",
body=json.dumps(form_data),
headers={
"Content-Type": "application/json",
"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"X-CSRF-Token": "abc123def456",
"Origin": "https://example.com",
"Referer": "https://example.com/profile"
},
# TLS fingerprinting for realistic browser behavior
ja3="771,4865-4866-4867-49195-49199-49196-49200-52393-52392-49171-49172-156-157-47-53,0-23-65281-10-11-35-16-5-13-18-51-45-43-27-17513,29-23-24,0",
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
timeout=15
)
print(f"Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
headers_sent = data.get('headers', {})
print(f"Authentication headers sent:")
print(f" Authorization: {headers_sent.get('Authorization', 'N/A')[:50]}...")
print(f" X-CSRF-Token: {headers_sent.get('X-Csrf-Token', 'N/A')}")
print(f" Origin: {headers_sent.get('Origin', 'N/A')}")
except Exception as e:
print(f"Error submitting authenticated form: {e}")
finally:
client.close()
def main():
"""Run all form submission examples."""
print("\n")
print("╔" + "═" * 58 + "╗")
print("║" + " " * 13 + "CycleTLS Form Examples" + " " * 22 + "║")
print("╚" + "═" * 58 + "╝")
# Run all examples
submit_urlencoded_form()
submit_multipart_form()
submit_multipart_binary_file()
submit_json_form()
submit_form_with_authentication()
print("\n" + "=" * 60)
print("All form submission examples completed!")
print("=" * 60)
if __name__ == "__main__":
main()