-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
decryptTemplate.html
299 lines (269 loc) · 9.81 KB
/
decryptTemplate.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="robots" content="noindex, nofollow">
<title>Password Protected Page</title>
<style>
html, body {
margin: 0;
width: 100%;
height: 100%;
font-family: Arial, Helvetica, sans-serif;
}
#dialogText {
color: white;
background-color: #333333;
}
#dialogWrap {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: table;
background-color: #EEEEEE;
}
#dialogWrapCell {
display: table-cell;
text-align: center;
vertical-align: middle;
}
#mainDialog {
max-width: 400px;
margin: 5px;
border: solid #AAAAAA 1px;
border-radius: 10px;
box-shadow: 3px 3px 5px 3px #AAAAAA;
margin-left: auto;
margin-right: auto;
background-color: #FFFFFF;
overflow: hidden;
text-align: left;
}
#mainDialog > * {
padding: 10px 30px;
}
#passArea {
padding: 20px 30px;
background-color: white;
}
#passArea > * {
margin: 5px auto;
}
#pass {
width: 100%;
height: 40px;
font-size: 30px;
}
#messageWrapper {
float: left;
vertical-align: middle;
line-height: 30px;
}
.notifyText {
display: none;
}
#invalidPass {
color: red;
}
#success {
color: green;
}
#submitPass {
font-size: 20px;
border-radius: 5px;
background-color: #E7E7E7;
border: solid gray 1px;
float: right;
cursor: pointer;
}
#contentFrame {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
#attribution {
position: absolute;
bottom: 0;
left: 0;
right: 0;
text-align: center;
padding: 10px;
font-weight: bold;
font-size: 0.8em;
}
#attribution, #attribution a {
color: #999;
}
.error {
display: none;
color: red;
}
</style>
</head>
<body>
<iframe id="contentFrame" frameBorder="0" allowfullscreen></iframe>
<div id="dialogWrap">
<div id="dialogWrapCell">
<div id="mainDialog">
<div id="dialogText">This page is password protected.</div>
<div id="passArea">
<p id="passwordPrompt">Password</p>
<input id="pass" type="password" name="pass" autofocus>
<div>
<span id="messageWrapper">
<span id="invalidPass" class="error">Sorry, please try again.</span>
<span id="trycatcherror" class="error">Sorry, something went wrong.</span>
<span id="success" class="notifyText">Success!</span>
</span>
<button id="submitPass" type="button">Submit</button>
<div style="clear: both;"></div>
</div>
</div>
<div id="securecontext" class="error">
<p>
Sorry, but password protection only works over a secure connection. Please load this page via HTTPS.
</p>
</div>
<div id="nocrypto" class="error">
<p>
Your web browser appears to be outdated. Please visit this page using a modern browser.
</p>
</div>
</div>
</div>
</div>
<div id="attribution">
Protected by <a href="https://www.maxlaumeister.com/pagecrypt/">PageCrypt</a>
</div>
<script>
(function() {
var pl = /*{{ENCRYPTED_PAYLOAD}}*/"";
var submitPass = document.getElementById('submitPass');
var passEl = document.getElementById('pass');
var invalidPassEl = document.getElementById('invalidPass');
var trycatcherror = document.getElementById('trycatcherror');
var successEl = document.getElementById('success');
var contentFrame = document.getElementById('contentFrame');
// Sanity checks
if (pl === "") {
submitPass.disabled = true;
passEl.disabled = true;
alert("This page is meant to be used with the encryption tool. It doesn't work standalone.");
return;
}
if (!isSecureContext) {
document.querySelector("#passArea").style.display = "none";
document.querySelector("#securecontext").style.display = "block";
return;
}
if (!crypto.subtle) {
document.querySelector("#passArea").style.display = "none";
document.querySelector("#nocrypto").style.display = "block";
return;
}
function str2ab(str) {
var ustr = atob(str);
var buf = new ArrayBuffer(ustr.length);
var bufView = new Uint8Array(buf);
for (var i=0, strLen=ustr.length; i < strLen; i++) {
bufView[i] = ustr.charCodeAt(i);
}
return bufView;
}
async function deriveKey(salt, password) {
const encoder = new TextEncoder()
const baseKey = await crypto.subtle.importKey(
'raw',
encoder.encode(password),
'PBKDF2',
false,
['deriveKey'],
)
return await crypto.subtle.deriveKey(
{ name: 'PBKDF2', salt, iterations: 100000, hash: 'SHA-256' },
baseKey,
{ name: 'AES-GCM', length: 256 },
true,
['decrypt'],
)
}
async function doSubmit(evt) {
submitPass.disabled = true;
passEl.disabled = true;
let iv, ciphertext, key;
try {
var unencodedPl = str2ab(pl);
const salt = unencodedPl.slice(0, 32)
iv = unencodedPl.slice(32, 32 + 16)
ciphertext = unencodedPl.slice(32 + 16)
key = await deriveKey(salt, passEl.value);
} catch (e) {
trycatcherror.style.display = "inline";
console.error(e);
return;
}
try {
const decryptedArray = new Uint8Array(
await crypto.subtle.decrypt({ name: 'AES-GCM', iv }, key, ciphertext)
);
let decrypted = new TextDecoder().decode(decryptedArray);
if (decrypted === "") throw "No data returned";
const basestr = '<base href="." target="_top">';
const anchorfixstr = `
<script>
Array.from(document.links).forEach((anchor) => {
const href = anchor.getAttribute("href");
if (href.startsWith("#")) {
anchor.addEventListener("click", function(e) {
e.preventDefault();
const targetId = this.getAttribute("href").substring(1);
const targetEl = document.getElementById(targetId);
targetEl.scrollIntoView();
});
}
});
<\/script>
`;
// Set default iframe link targets to _top so all links break out of the iframe
if (decrypted.includes("<head>")) decrypted = decrypted.replace("<head>", "<head>" + basestr);
else if (decrypted.includes("<!DOCTYPE html>")) decrypted = decrypted.replace("<!DOCTYPE html>", "<!DOCTYPE html>" + basestr);
else decrypted = basestr + decrypted;
// Fix fragment links
if (decrypted.includes("</body>")) decrypted = decrypted.replace("</body>", anchorfixstr + '</body>');
else if (decrypted.includes("</html>")) decrypted = decrypted.replace("</html>", anchorfixstr + '</html>');
else decrypted = decrypted + anchorfixstr;
contentFrame.srcdoc = decrypted;
successEl.style.display = "inline";
setTimeout(function() {
dialogWrap.style.display = "none";
}, 1000);
} catch (e) {
invalidPassEl.style.display = "inline";
passEl.value = "";
submitPass.disabled = false;
passEl.disabled = false;
console.error(e);
return;
}
}
submitPass.onclick = doSubmit;
passEl.onkeypress = function(e){
if (!e) e = window.event;
var keyCode = e.keyCode || e.which;
invalidPassEl.style.display = "none";
if (keyCode == '13'){
// Enter pressed
doSubmit();
return false;
}
}
})();
</script>
</body>
</html>