|
6 | 6 | * Encoder |
7 | 7 | */ |
8 | 8 | var encode = function(payload){ |
9 | | - |
10 | 9 | var convert2Ascii = function(text){ |
11 | 10 | return Array |
12 | 11 | .from(text) |
|
19 | 18 | } |
20 | 19 | else |
21 | 20 | return char; |
22 | | - }); |
23 | | - }; |
24 | | - |
25 | | - var mapping = function(char){ |
26 | | - if (char > 8) char+= 2; |
27 | | - if (char > 31) char+= 95; |
28 | | - return char; |
29 | | - }; |
30 | | - |
31 | | - var mapCharacters = function(text){ |
32 | | - return text |
33 | | - .split('') |
34 | | - .map(x=>x.charCodeAt()) |
35 | | - .map(x=>{ |
36 | | - var low = mapping( x & 0x1F); |
37 | | - var high = mapping( x >> 5 ); |
38 | | - return String.fromCharCode(high, low); |
39 | 21 | }) |
40 | 22 | .join(''); |
41 | 23 | }; |
42 | 24 |
|
43 | | - return mapCharacters( convert2Ascii(payload).join('') ); |
| 25 | + return convert2Ascii(payload) |
| 26 | + .split('') |
| 27 | + // Adds 9th bit, then slices it off for zero padding. |
| 28 | + .map(x=>(0x100 | x.charCodeAt()).toString(2).slice(1)) |
| 29 | + .join('') |
| 30 | + .split('') |
| 31 | + .map(x=>String.fromCharCode(x*0xFEFF)) |
| 32 | + .join(''); |
44 | 33 | }; |
45 | 34 |
|
46 | 35 |
|
47 | | - |
48 | 36 | /** |
49 | 37 | * Decoder |
50 | 38 | */ |
51 | 39 | var decode = function(val){ |
52 | | - return val |
53 | | - .split('') |
54 | | - .map(x=>x.charCodeAt()) |
55 | | - .filter( x=> (x <= 8) || (x >= 11 && x <= 31) || ( x >= 127 && x <= 159) ) |
56 | | - .map(x=>{ |
57 | | - if (x > 126) x-= 95; |
58 | | - if (x > 10) x-=2; |
59 | | - return x; |
60 | | - }) |
61 | | - .reduce((x,cur,i,v)=> i%2? x + String.fromCharCode((v[i-1] << 5) + cur) : x,''); |
| 40 | + return Array |
| 41 | + .from(val) |
| 42 | + .map(x=>x.charCodeAt() && 1 ) |
| 43 | + .join('') |
| 44 | + .match(/.{8}/g) |
| 45 | + .map(function(c){ |
| 46 | + return String.fromCharCode(parseInt(c,2)) |
| 47 | + }) |
| 48 | + .join(''); |
| 49 | + }; |
| 50 | + |
| 51 | + |
| 52 | + /** |
| 53 | + * DecodeEval - Actually eval the decoded payload. |
| 54 | + */ |
| 55 | + var decodeEval = function(code){ |
| 56 | + []["filter"]["constructor"]( decode(code) )(); |
62 | 57 | }; |
63 | 58 |
|
64 | 59 |
|
65 | 60 |
|
66 | 61 | var phantomScript = { |
67 | 62 | encode: encode, |
68 | | - decode: decode |
| 63 | + decode: decode, |
| 64 | + decodeEval: decodeEval |
69 | 65 | } |
70 | 66 |
|
71 | 67 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = phantomScript : |
|
0 commit comments