wewillrockyou 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +217 -0
- package/package.json +13 -0
package/index.js
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { select } from '@inquirer/prompts';
|
|
4
|
+
import fs from 'fs';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
|
|
7
|
+
const codes = {
|
|
8
|
+
1: `# Experiment 1
|
|
9
|
+
import hmac
|
|
10
|
+
import hashlib
|
|
11
|
+
|
|
12
|
+
secret_key = b"my_secret_key"
|
|
13
|
+
message = b"Hello World!"
|
|
14
|
+
|
|
15
|
+
signature = hmac.new( secret_key, message, hashlib.sha256).hexdigest()
|
|
16
|
+
|
|
17
|
+
print("Message:", message.decode())
|
|
18
|
+
print("HMAC Signature:", signature)
|
|
19
|
+
`,
|
|
20
|
+
2: `# Experiment 2
|
|
21
|
+
from cryptography.hazmat.primitives import hashes
|
|
22
|
+
from cryptography.hazmat.primitives.asymmetric import rsa, padding
|
|
23
|
+
private_key = rsa.generate_private_key(
|
|
24
|
+
public_exponent=65537,
|
|
25
|
+
key_size=2048)
|
|
26
|
+
public_key = private_key.public_key()
|
|
27
|
+
message = b"Welcome To API Security"
|
|
28
|
+
signature = private_key.sign(
|
|
29
|
+
message,
|
|
30
|
+
padding.PSS(
|
|
31
|
+
mgf=padding.MGF1(hashes.SHA256()),
|
|
32
|
+
salt_length=padding.PSS.MAX_LENGTH),
|
|
33
|
+
hashes.SHA256())
|
|
34
|
+
print("Original Message:", message.decode())
|
|
35
|
+
print("\\nDigital Signature Generated Successfully!")
|
|
36
|
+
print("\\nSignature (Hex):")
|
|
37
|
+
print(signature.hex())
|
|
38
|
+
try:
|
|
39
|
+
public_key.verify(
|
|
40
|
+
signature,
|
|
41
|
+
message,
|
|
42
|
+
padding.PSS(
|
|
43
|
+
mgf=padding.MGF1(hashes.SHA256()),
|
|
44
|
+
salt_length=padding.PSS.MAX_LENGTH),
|
|
45
|
+
hashes.SHA256())
|
|
46
|
+
print("\\nVerification Successful!")
|
|
47
|
+
print("The signature is valid.")
|
|
48
|
+
except Exception:
|
|
49
|
+
print("\\nVerification Failed!")
|
|
50
|
+
print("The signature is invalid.")
|
|
51
|
+
`,
|
|
52
|
+
3: `# Experiment 3
|
|
53
|
+
import jwt
|
|
54
|
+
import datetime
|
|
55
|
+
SECRET_KEY = "my_secure_secret_key"
|
|
56
|
+
|
|
57
|
+
# Payload data
|
|
58
|
+
payload = {
|
|
59
|
+
"username": "Yashodhar",
|
|
60
|
+
"role": "student",
|
|
61
|
+
"exp": datetime.datetime.utcnow() + datetime.timedelta(minutes=5)
|
|
62
|
+
}
|
|
63
|
+
token = jwt.encode(payload, SECRET_KEY, algorithm="HS256")
|
|
64
|
+
print("Generated JWT Token:")
|
|
65
|
+
print(token)
|
|
66
|
+
try:
|
|
67
|
+
decoded = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
|
|
68
|
+
print("\\nToken Validation Successful!")
|
|
69
|
+
print("Decoded Payload:")
|
|
70
|
+
print(decoded)
|
|
71
|
+
except jwt.ExpiredSignatureError:
|
|
72
|
+
print("Token has expired!")
|
|
73
|
+
except jwt.InvalidTokenError:
|
|
74
|
+
print("Invalid Token!")
|
|
75
|
+
`,
|
|
76
|
+
4: `# Experiment 4
|
|
77
|
+
import jwt
|
|
78
|
+
import datetime
|
|
79
|
+
import time
|
|
80
|
+
SECRET_KEY = "my_secret_key"
|
|
81
|
+
payload = {
|
|
82
|
+
"username": "student",
|
|
83
|
+
"role": "user",
|
|
84
|
+
"exp": datetime.datetime.utcnow() + datetime.timedelta(seconds=10)
|
|
85
|
+
}
|
|
86
|
+
token = jwt.encode(payload, SECRET_KEY, algorithm="HS256")
|
|
87
|
+
print("Generated JWT Token:")
|
|
88
|
+
print(token)
|
|
89
|
+
try:
|
|
90
|
+
decoded = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
|
|
91
|
+
print("\\nToken Validation Successful")
|
|
92
|
+
print("Payload:", decoded)
|
|
93
|
+
except jwt.InvalidTokenError:
|
|
94
|
+
print("Invalid Token")
|
|
95
|
+
print("\\nWaiting for token to expire...")
|
|
96
|
+
time.sleep(12)
|
|
97
|
+
try:
|
|
98
|
+
decoded = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
|
|
99
|
+
print("Token is still valid")
|
|
100
|
+
except jwt.ExpiredSignatureError:
|
|
101
|
+
print("\\nToken Expired!")
|
|
102
|
+
print("Replay Attack Detected: Expired token reuse blocked")
|
|
103
|
+
except jwt.InvalidTokenError:
|
|
104
|
+
print("Invalid Token")
|
|
105
|
+
`,
|
|
106
|
+
5: `# Experiment 5
|
|
107
|
+
import hmac
|
|
108
|
+
import hashlib
|
|
109
|
+
from cryptography.hazmat.primitives import hashes
|
|
110
|
+
from cryptography.hazmat.primitives.asymmetric import rsa, padding
|
|
111
|
+
message = input("Enter Message: ").encode()
|
|
112
|
+
print("\\n===== SYMMETRIC AUTHENTICATION (HMAC) =====")
|
|
113
|
+
secret_key = b"shared_secret_key"
|
|
114
|
+
hmac_value = hmac.new(secret_key,message,hashlib.sha256).hexdigest()
|
|
115
|
+
print("Generated HMAC:")
|
|
116
|
+
print(hmac_value)
|
|
117
|
+
verify_hmac = hmac.new(
|
|
118
|
+
secret_key,
|
|
119
|
+
message,
|
|
120
|
+
hashlib.sha256
|
|
121
|
+
).hexdigest()
|
|
122
|
+
if hmac.compare_digest(hmac_value, verify_hmac):
|
|
123
|
+
print("HMAC Verification Successful")
|
|
124
|
+
else:
|
|
125
|
+
print("HMAC Verification Failed")
|
|
126
|
+
print("\\n===== ASYMMETRIC AUTHENTICATION (RSA) =====")
|
|
127
|
+
private_key = rsa.generate_private_key(
|
|
128
|
+
public_exponent=65537,
|
|
129
|
+
key_size=2048
|
|
130
|
+
)
|
|
131
|
+
public_key = private_key.public_key()
|
|
132
|
+
signature = private_key.sign(
|
|
133
|
+
message,
|
|
134
|
+
padding.PSS(
|
|
135
|
+
mgf=padding.MGF1(hashes.SHA256()),
|
|
136
|
+
salt_length=padding.PSS.MAX_LENGTH
|
|
137
|
+
),
|
|
138
|
+
hashes.SHA256()
|
|
139
|
+
)
|
|
140
|
+
print("Digital Signature Generated")
|
|
141
|
+
try:
|
|
142
|
+
public_key.verify(
|
|
143
|
+
signature,
|
|
144
|
+
message,
|
|
145
|
+
padding.PSS(
|
|
146
|
+
mgf=padding.MGF1(hashes.SHA256()),
|
|
147
|
+
salt_length=padding.PSS.MAX_LENGTH
|
|
148
|
+
),
|
|
149
|
+
hashes.SHA256()
|
|
150
|
+
)
|
|
151
|
+
print("Digital Signature Verification Successful")
|
|
152
|
+
except Exception:
|
|
153
|
+
print("Digital Signature Verification Failed")
|
|
154
|
+
print("\\n===== COMPARISON =====")
|
|
155
|
+
print("Symmetric Authentication : Uses One Shared Secret Key")
|
|
156
|
+
print("Asymmetric Authentication: Uses Public and Private Keys")
|
|
157
|
+
print("HMAC is Faster")
|
|
158
|
+
print("Digital Signature Provides Non-Repudiation")
|
|
159
|
+
`
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const fileNames = {
|
|
163
|
+
1: 'HMAC.py',
|
|
164
|
+
2: 'Digital signature.py',
|
|
165
|
+
3: 'JWT.py',
|
|
166
|
+
4: 'Token Expiry.py',
|
|
167
|
+
5: 'Symmetric and asymmetric mechanisms.py'
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
async function run() {
|
|
171
|
+
const subject = await select({
|
|
172
|
+
message: 'Select a subject:',
|
|
173
|
+
choices: [
|
|
174
|
+
{
|
|
175
|
+
name: 'API',
|
|
176
|
+
value: 'API',
|
|
177
|
+
description: 'API Security Experiments',
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
name: 'Other',
|
|
181
|
+
value: 'Other',
|
|
182
|
+
description: 'Other subjects (not available)',
|
|
183
|
+
},
|
|
184
|
+
],
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
if (subject !== 'API') {
|
|
188
|
+
console.log('Only API is available right now.');
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const experiment = await select({
|
|
193
|
+
message: 'Select an experiment number:',
|
|
194
|
+
choices: [
|
|
195
|
+
{ name: '1', value: 1, description: 'HMAC' },
|
|
196
|
+
{ name: '2', value: 2, description: 'Digital signature' },
|
|
197
|
+
{ name: '3', value: 3, description: 'JWT' },
|
|
198
|
+
{ name: '4', value: 4, description: 'Token Expiry' },
|
|
199
|
+
{ name: '5', value: 5, description: 'Symmetric and asymmetric mechanisms' },
|
|
200
|
+
],
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
const code = codes[experiment];
|
|
204
|
+
const fileName = fileNames[experiment];
|
|
205
|
+
const filePath = path.join(process.cwd(), fileName);
|
|
206
|
+
|
|
207
|
+
fs.writeFileSync(filePath, code, 'utf8');
|
|
208
|
+
console.log(`Successfully created '${fileName}' in the current directory.`);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
run().catch((err) => {
|
|
212
|
+
if (err.name === 'ExitPromptError') {
|
|
213
|
+
console.log('\nAborted by user.');
|
|
214
|
+
} else {
|
|
215
|
+
console.error(err);
|
|
216
|
+
}
|
|
217
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "wewillrockyou",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "CLI to cheat on API Security experiments",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": {
|
|
8
|
+
"wewillrockyou": "./index.js"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@inquirer/prompts": "^5.0.0"
|
|
12
|
+
}
|
|
13
|
+
}
|