-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayload_encrypter.py
More file actions
231 lines (177 loc) · 7.25 KB
/
payload_encrypter.py
File metadata and controls
231 lines (177 loc) · 7.25 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
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
import base64
import time
from hashlib import pbkdf2_hmac
import os
# Fixed IVs used by Bubble.io (same as decrypter)
FIXED_IV_Y = 'po9'
FIXED_IV_X = 'fl1'
def pad_pkcs7(data):
"""Add PKCS7 padding to data"""
pad_length = 16 - (len(data) % 16)
return data + bytes([pad_length] * pad_length)
def encrypt_aes_cbc(plaintext, key, iv):
"""Encrypt data using AES-CBC mode"""
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
encryptor = cipher.encryptor()
return encryptor.update(plaintext) + encryptor.finalize()
def encrypt_with_fixed_iv(appname, data, fixed_iv):
"""Encrypt x or y values using fixed IV
Args:
appname: Application name
data: Data to encrypt (bytes or string)
fixed_iv: Fixed IV string ('po9' for y, 'fl1' for x)
Returns:
Base64 encoded encrypted data
"""
if isinstance(data, str):
data = data.encode('utf-8')
# Derive key and IV using PBKDF2-MD5
derived_iv = pbkdf2_hmac('md5', fixed_iv.encode('utf-8'), appname.encode('utf-8'), 7, dklen=16)
derived_key = pbkdf2_hmac('md5', appname.encode('utf-8'), appname.encode('utf-8'), 7, dklen=32)
# Add padding before encryption
padded_data = pad_pkcs7(data)
# Encrypt
ciphertext = encrypt_aes_cbc(padded_data, derived_key, derived_iv)
return base64.b64encode(ciphertext).decode('utf-8')
def encrypt_payload(appname, timestamp, iv_bytes, payload_data):
"""Encrypt main payload (z) using timestamp and IV
Args:
appname: Application name
timestamp: Timestamp string
iv_bytes: IV bytes (raw, not encrypted)
payload_data: Data to encrypt (bytes or string)
Returns:
Base64 encoded encrypted payload
"""
if isinstance(payload_data, str):
payload_data = payload_data.encode('utf-8')
# Add PKCS7 padding
padded_data = pad_pkcs7(payload_data)
# Derive key from appname + timestamp
key_material = f"{appname}{timestamp}".encode('utf-8').replace(b'\x01', b'')
derived_key = pbkdf2_hmac('md5', key_material, appname.encode('utf-8'), 7, dklen=32)
# Derive IV
derived_iv = pbkdf2_hmac('md5', iv_bytes, appname.encode('utf-8'), 7, dklen=16)
# Encrypt
ciphertext = encrypt_aes_cbc(padded_data, derived_key, derived_iv)
return base64.b64encode(ciphertext).decode('utf-8')
def generate_timestamp():
"""Generate current timestamp in milliseconds"""
return str(int(time.time() * 1000))
def generate_random_iv():
"""Generate random IV (16 bytes)"""
return os.urandom(16)
def encrypt_bubble_payload(appname, payload_data, timestamp=None, iv_bytes=None):
"""Main encryption function for Bubble.io payloads
Args:
appname: Application name
payload_data: Data to encrypt (bytes or string or dict/list for JSON)
timestamp: Optional timestamp (auto-generated if None)
iv_bytes: Optional IV bytes (auto-generated if None)
Returns:
dict: {
'x': encrypted IV (base64),
'y': encrypted timestamp (base64),
'z': encrypted payload (base64),
'timestamp': raw timestamp,
'iv': raw IV (hex)
}
"""
import json
# Auto-generate timestamp if not provided
if timestamp is None:
timestamp = generate_timestamp()
# Auto-generate IV if not provided
if iv_bytes is None:
iv_bytes = generate_random_iv()
# Convert dict/list to JSON string
if isinstance(payload_data, (dict, list)):
payload_data = json.dumps(payload_data, ensure_ascii=False)
# Step 1: Encrypt timestamp (y) with suffix _1
timestamp_with_suffix = timestamp + '_1'
y_encrypted = encrypt_with_fixed_iv(appname, timestamp_with_suffix, FIXED_IV_Y)
# Step 2: Encrypt IV (x) - just the raw IV bytes
x_encrypted = encrypt_with_fixed_iv(appname, iv_bytes, FIXED_IV_X)
# Step 3: Encrypt payload (z)
z_encrypted = encrypt_payload(appname, timestamp, iv_bytes, payload_data)
return {
'x': x_encrypted,
'y': y_encrypted,
'z': z_encrypted,
'timestamp': timestamp,
'iv': iv_bytes.hex()
}
if __name__ == "__main__":
import json
print("=" * 80)
print("🔒 Bubble.io Payload Encrypter")
print("=" * 80)
# Get inputs
appname = input("\nEnter AppName: ").strip()
if not appname:
print("Error: AppName is required!")
exit(1)
print("\nPayload input method:")
print("1. Enter JSON manually")
print("2. Enter plain text")
choice = input("\nSelect option (1 or 2): ").strip()
if choice == '1':
print("\nEnter your JSON payload (single line):")
payload_input = input().strip()
try:
payload_data = json.loads(payload_input)
except json.JSONDecodeError as e:
print(f"Invalid JSON: {e}")
exit(1)
else:
print("\nEnter your plain text payload:")
payload_data = input().strip()
# Optional: custom timestamp and IV
use_custom = input("\nUse custom timestamp/IV? (y/n, default=n): ").strip().lower()
timestamp = None
iv_bytes = None
if use_custom == 'y':
timestamp = input("Enter timestamp (leave empty for auto-generate): ").strip() or None
iv_hex = input("Enter IV in hex (leave empty for auto-generate): ").strip()
if iv_hex:
iv_bytes = bytes.fromhex(iv_hex)
# Encrypt
try:
result = encrypt_bubble_payload(appname, payload_data, timestamp, iv_bytes)
# Display results
print("\n" + "=" * 80)
print("ENCRYPTION RESULTS")
print("=" * 80)
print(f"\nAppName: {appname}")
print(f"Timestamp: {result['timestamp']}")
print(f"IV (hex): {result['iv']}")
print("\n" + "-" * 80)
print("Encrypted values (use these in Bubble.io):")
print("-" * 80)
print(f"\nx (IV):\n{result['x']}")
print(f"\ny (timestamp):\n{result['y']}")
print(f"\nz (payload):\n{result['z']}")
print("=" * 80)
# Save to file
save_file = input("\nSave results to file? (y/n): ").strip().lower()
if save_file == 'y':
output_file = f"encrypted_payload_{int(time.time())}.json"
with open(output_file, 'w', encoding='utf-8') as f:
json.dump({
'appname': appname,
'timestamp': result['timestamp'],
'iv_hex': result['iv'],
'encrypted': {
'x': result['x'],
'y': result['y'],
'z': result['z']
},
'original_payload': payload_data if isinstance(payload_data, str) else json.dumps(payload_data, ensure_ascii=False, indent=2)
}, f, ensure_ascii=False, indent=2)
print(f"✅ Results saved to: {output_file}")
except Exception as e:
print(f"\n❌ Encryption failed: {e}")
import traceback
traceback.print_exc()