-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatbox.py
68 lines (60 loc) · 2.78 KB
/
catbox.py
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
import sys
import os
import requests
import random
print("Placing cat in box....")
print()
print(" ,")
print(" ,-. _,---._ __ / \\") #fun fact: this ascii art is sourced from the catbox.moe HTML source code :)
print(" / ) .-' `./ / \\")
print(" ( ( ,' `/ /|")
print(" \ `-\" \'\ / |")
print(" `. , \ \ / |")
print(" /`. ,'-`----Y |")
print(" ( ; | '")
print(" | ,-. ,-' | /")
print(" | | ( | | /")
print(" ) | \ `.___________|/")
print(" `--' `--'")
print()
# Catbox file-upload URL
url = "https://catbox.moe/user/api.php"
#CatBox generates a random 29-digit integer string to be used in the content boundary -- see: <input type="hidden" name="userhash" value=""> on their HTML source
#appears to be arbitrary, random-gen on our end works fine. I could _not_ find out where catbox generates it in their JavaScript to confirm this, however.
user_hash = str(random.randint(1000000000000000000000000, 999999999999999999999999999))
# Set the boundary string; see: https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
boundary = "-----------------------------"+user_hash
headers = {
"Content-Type": f"multipart/form-data; boundary={boundary}"
}
# File to Upload
# Check if a file was passed as a command-line argument
if len(sys.argv) < 2:
print("Error: No file specified")
exit(1)
# Get the file path from the command-line argument
file_path = sys.argv[1]
# Check if the file exists
if not os.path.exists(file_path):
print(f"Error: File not found: {file_path}")
exit(1)
file_name = file_path #file is found, let's load the contents and prepare the request...
#print("Attempting "+file_name+" upload...")
# Open the file in binary mode
with open(file_name, "rb") as file:
# Set the reqtype form of payload
reqtype = f"--{boundary}\r\nContent-Disposition: form-data; name=\"reqtype\"\r\n\r\nfileupload\r\n"
# Set the file form portion of payload
file_form = f"--{boundary}\r\nContent-Disposition: form-data; name=\"fileToUpload\"; filename=\"{file_name}\"\r\nContent-Type: application/octet-stream\r\n\r\n{file.read().decode()}\r\n--{boundary}--"
# Combine the form fields and add the closing boundary
data = f"{reqtype}{file_form}--{boundary}--"
#print(data)
print()
# Send the POST request with the file as the body
response = requests.post(url, headers=headers, data=data)
# Print the server's response, the file-upload link
if response.status_code == 200:
print(file_name+" successfully uploaded!")
print(response.text)
else:
print("Request failed - expected 200, received "+str(response.status_code))