home *** CD-ROM | disk | FTP | other *** search
/ Minami 83 / MINAMI83.iso / Extra / DivXInstaller.exe / $PLUGINSDIR / GoogleToolbarFirefox.msi / xpi / amulet-jslib / enchash-decrypter.js < prev    next >
Text File  |  2006-08-07  |  11KB  |  353 lines

  1. function PROT_EnchashDecrypter() {
  2. this.debugZone = "enchashdecrypter";
  3. this.REs_ = PROT_EnchashDecrypter.REs;
  4. this.hasher_ = new G_CryptoHasher();
  5. this.base64_ = new G_Base64();
  6. this.rc4_ = new ARC4();
  7. }
  8. PROT_EnchashDecrypter.DATABASE_SALT = "oU3q.72p";
  9. PROT_EnchashDecrypter.MAX_DOTS = 5;
  10. PROT_EnchashDecrypter.SALT_LENGTH = 8;
  11. PROT_EnchashDecrypter.REs = {};
  12. PROT_EnchashDecrypter.REs.FIND_DODGY_CHARS =
  13. new RegExp("[\x01-\x1f\x7f-\xff]+");
  14. PROT_EnchashDecrypter.REs.FIND_DODGY_CHARS_GLOBAL =
  15. new RegExp("[\x01-\x1f\x7f-\xff]+", "g");
  16. PROT_EnchashDecrypter.REs.FIND_END_DOTS = new RegExp("^\\.+|\\.+$");
  17. PROT_EnchashDecrypter.REs.FIND_END_DOTS_GLOBAL = new RegExp("^\\.+|\\.+$", "g");
  18. PROT_EnchashDecrypter.REs.FIND_MULTIPLE_DOTS = new RegExp("\\.{2,}");
  19. PROT_EnchashDecrypter.REs.FIND_MULTIPLE_DOTS_GLOBAL =
  20. new RegExp("\\.{2,}", "g");
  21. PROT_EnchashDecrypter.REs.FIND_TRAILING_DOTS = new RegExp("\\.+$");
  22. PROT_EnchashDecrypter.REs.POSSIBLE_IP =
  23. new RegExp("^((?:0x[0-9a-f]+|[0-9\\.])+)$", "i");
  24. PROT_EnchashDecrypter.REs.FIND_BAD_OCTAL = new RegExp("(^|\\.)0\\d*[89]");
  25. PROT_EnchashDecrypter.REs.IS_OCTAL = new RegExp("^0[0-7]*$");
  26. PROT_EnchashDecrypter.REs.IS_DECIMAL = new RegExp("^[0-9]+$");
  27. PROT_EnchashDecrypter.REs.IS_HEX = new RegExp("^0[xX]([0-9a-fA-F]+)$");
  28. PROT_EnchashDecrypter.REs.CASE_INSENSITIVE = /\(\?i\)/g;
  29. PROT_EnchashDecrypter.prototype.lastNChars_ = function(str, n) {
  30. n = -n;
  31. return str.substr(n);
  32. }
  33. PROT_EnchashDecrypter.prototype.hexDecode_ = function(str) {
  34. var output = [];
  35. var i = 0;
  36. while (i < str.length) {
  37. var c = str.charAt(i);
  38. if (c == "%" && i + 2 < str.length) {
  39. var asciiVal = Number("0x" + str.charAt(i + 1) + str.charAt(i + 2));
  40. if (!isNaN(asciiVal)) {
  41. i += 2;
  42. c = String.fromCharCode(asciiVal);
  43. }
  44. }
  45. output[output.length] = c;
  46. ++i;
  47. }
  48. return output.join("");
  49. }
  50. PROT_EnchashDecrypter.prototype.parseRegExps = function(data) {
  51. var res = data.split("\t");
  52. G_Debug(this, "Got " + res.length + " regular rexpressions");
  53. for (var i = 0; i < res.length; i++) {
  54. var flags = (this.REs_.CASE_INSENSITIVE.test(res[i])) ? "i" : "";
  55. res[i] = res[i].replace(this.REs_.CASE_INSENSITIVE, "");
  56. res[i] = new RegExp(res[i], flags);
  57. }
  58. return res;
  59. }
  60. PROT_EnchashDecrypter.prototype.getCanonicalHost = function(str) {
  61. var urlObj = Cc["@mozilla.org/network/standard-url;1"]
  62. .createInstance(Ci.nsIURL);
  63. urlObj.spec = str;
  64. var asciiHost = urlObj.asciiHost;
  65. var unescaped = this.hexDecode_(asciiHost);
  66. unescaped = unescaped.replace(this.REs_.FIND_DODGY_CHARS_GLOBAL, "")
  67. .replace(this.REs_.FIND_END_DOTS_GLOBAL, "")
  68. .replace(this.REs_.FIND_MULTIPLE_DOTS_GLOBAL, ".");
  69. var temp = this.parseIPAddress_(unescaped);
  70. if (temp)
  71. unescaped = temp;
  72. var escaped = encodeURI(unescaped);
  73. var k;
  74. var index = escaped.length;
  75. for (k = 0; k < PROT_EnchashDecrypter.MAX_DOTS + 1; k++) {
  76. temp = escaped.lastIndexOf(".", index - 1);
  77. if (temp == -1) {
  78. break;
  79. } else {
  80. index = temp;
  81. }
  82. }
  83. if (k == PROT_EnchashDecrypter.MAX_DOTS + 1 && index != -1) {
  84. escaped = escaped.substring(index + 1);
  85. }
  86. escaped = escaped.toLowerCase();
  87. return escaped;
  88. }
  89. PROT_EnchashDecrypter.prototype.parseIPAddress_ = function(host) {
  90. host = host.replace(this.REs_.FIND_TRAILING_DOTS_GLOBAL, "");
  91. if (!this.REs_.POSSIBLE_IP.test(host))
  92. return "";
  93. var parts = host.split(".");
  94. if (parts.length > 4)
  95. return "";
  96. var allowOctal = !this.REs_.FIND_BAD_OCTAL.test(host);
  97. for (var k = 0; k < parts.length; k++) {
  98. var canon;
  99. if (k == parts.length - 1) {
  100. canon = this.canonicalNum_(parts[k], 5 - parts.length, allowOctal);
  101. } else {
  102. canon = this.canonicalNum_(parts[k], 1, allowOctal);
  103. }
  104. if (canon != "")
  105. parts[k] = canon;
  106. }
  107. return parts.join(".");
  108. }
  109. PROT_EnchashDecrypter.prototype.canonicalNum_ = function(num, bytes, octal) {
  110. if (bytes < 0)
  111. return "";
  112. var temp_num;
  113. if (octal && this.REs_.IS_OCTAL.test(num)) {
  114. num = this.lastNChars_(num, 11);
  115. temp_num = parseInt(num, 8);
  116. if (isNaN(temp_num))
  117. temp_num = -1;
  118. } else if (this.REs_.IS_DECIMAL.test(num)) {
  119. num = this.lastNChars_(num, 32);
  120. temp_num = parseInt(num, 10);
  121. if (isNaN(temp_num))
  122. temp_num = -1;
  123. } else if (this.REs_.IS_HEX.test(num)) {
  124. num = this.lastNChars_(num, 8);
  125. temp_num = parseInt(num, 16);
  126. if (isNaN(temp_num))
  127. temp_num = -1;
  128. } else {
  129. return "";
  130. }
  131. if (temp_num == -1)
  132. return "";
  133. var parts = [];
  134. while (bytes--) {
  135. parts.push("" + (temp_num % 256));
  136. temp_num -= temp_num % 256;
  137. temp_num /= 256;
  138. }
  139. return parts.join(".");
  140. }
  141. PROT_EnchashDecrypter.prototype.getLookupKey = function(host) {
  142. var dataKey = PROT_EnchashDecrypter.DATABASE_SALT + host;
  143. dataKey = this.base64_.arrayifyString(dataKey);
  144. this.hasher_.init(G_CryptoHasher.algorithms.MD5);
  145. this.hasher_.updateFromArray(dataKey);
  146. var lookupKey = this.hasher_.digestHex();
  147. return lookupKey.toUpperCase();
  148. }
  149. PROT_EnchashDecrypter.prototype.decryptData = function(data, host) {
  150. var ascii = this.base64_.decodeString(data);
  151. var random_salt = ascii.slice(0, PROT_EnchashDecrypter.SALT_LENGTH);
  152. var encrypted_data = ascii.slice(PROT_EnchashDecrypter.SALT_LENGTH);
  153. var temp_decryption_key =
  154. this.base64_.arrayifyString(PROT_EnchashDecrypter.DATABASE_SALT);
  155. temp_decryption_key = temp_decryption_key.concat(random_salt);
  156. temp_decryption_key =
  157. temp_decryption_key.concat(this.base64_.arrayifyString(host));
  158. this.hasher_.init(G_CryptoHasher.algorithms.MD5);
  159. this.hasher_.updateFromArray(temp_decryption_key);
  160. var decryption_key = this.base64_.arrayifyString(this.hasher_.digestRaw());
  161. this.rc4_.setKey(decryption_key, decryption_key.length);
  162. this.rc4_.crypt(encrypted_data, encrypted_data.length);   // Works in-place
  163. return this.base64_.stringifyArray(encrypted_data);
  164. }
  165. function TEST_PROT_EnchashDecrypter() {
  166. if (G_GDEBUG) {
  167. var z = "enchash UNITTEST";
  168. G_debugService.enableZone(z);
  169. G_Debug(z, "Starting");
  170. var no_dots = "abcd123;[]";
  171. var one_dot = "abc.123";
  172. var two_dots = "two..dots";
  173. var lots_o_dots = "I have a lovely .... bunch of dots";
  174. var multi_dots = "dots ... and ... more .... dots";
  175. var leading_dot = ".leading";
  176. var trailing_dot = "trailing.";
  177. var trailing_dots = "I love trailing dots....";
  178. var end_dots = ".dots.";
  179. var decimal = "1234567890";
  180. var hex = "0x123452FAf";
  181. var bad_hex = "0xFF0xGG";
  182. var octal = "012034056";
  183. var bad_octal = "012034089";
  184. var garbage = "lk,.:asdfa-=";
  185. var mixed = "1230x78034";
  186. var spaces = "123 0xFA 045";
  187. var longstr = "";
  188. for(var k = 0; k < 100; k++) {
  189. longstr += "a";
  190. }
  191. var shortstr = "short";
  192. var r = PROT_EnchashDecrypter.REs;
  193. var l = new PROT_EnchashDecrypter();
  194. function testRE(re, inputValPairs) {
  195. for (var i = 0; i < inputValPairs.length; i += 2)
  196. G_Assert(z, re.test(inputValPairs[i]) == inputValPairs[i + 1],
  197. "RegExp broken: " + re + " (input: " + inputValPairs[i] + ")");
  198. };
  199. var tests =
  200. ["", false,
  201. "normal chars;!@#$%^&*&(", false,
  202. "MORE NORMAL ,./<>?;':{}", false,
  203. "Slightly less\2 normal", true,
  204. "\245 stuff \45", true,
  205. "\31", true];
  206. testRE(r.FIND_DODGY_CHARS, tests);
  207. tests =
  208. [no_dots, false,
  209. one_dot, false,
  210. leading_dot, true,
  211. trailing_dots, true,
  212. end_dots, true];
  213. testRE(r.FIND_END_DOTS, tests);
  214. tests =
  215. [no_dots, false,
  216. one_dot, false,
  217. two_dots, true,
  218. lots_o_dots, true,
  219. multi_dots, true];
  220. testRE(r.FIND_MULTIPLE_DOTS, tests);
  221. tests =
  222. [no_dots, false,
  223. one_dot, false,
  224. trailing_dot, true,
  225. trailing_dots, true];
  226. testRE(r.FIND_TRAILING_DOTS, tests);
  227. tests =
  228. ["random junk", false,
  229. "123.45.6-7.89", false,
  230. "012.12.123", true,
  231. "0x12.0xff.123", true,
  232. "225.0.0.1", true];
  233. testRE(r.POSSIBLE_IP, tests);
  234. tests =
  235. [decimal, false,
  236. hex, false,
  237. octal, false,
  238. bad_octal, true];
  239. testRE(r.FIND_BAD_OCTAL, tests);
  240. tests =
  241. [decimal, false,
  242. hex, false,
  243. bad_octal, false,
  244. garbage, false,
  245. mixed, false,
  246. spaces, false,
  247. octal, true];
  248. testRE(r.IS_OCTAL, tests);
  249. tests =
  250. [hex, false,
  251. garbage, false,
  252. mixed, false,
  253. spaces, false,
  254. octal, true,
  255. bad_octal, true,
  256. decimal, true];
  257. testRE(r.IS_DECIMAL, tests);
  258. tests =
  259. [decimal, false,
  260. octal, false,
  261. bad_octal, false,
  262. garbage, false,
  263. mixed, false,
  264. spaces, false,
  265. bad_hex, false,
  266. hex, true];
  267. testRE(r.IS_HEX, tests);
  268. var val = l.lastNChars_(longstr, 8);
  269. G_Assert(z, val.length == 8, "find last eight broken on long str");
  270. val = l.lastNChars_(shortstr, 8);
  271. G_Assert(z, val.length == 5, "find last 11 broken on short str");
  272. tests =
  273. ["", "", 1, true,
  274. "", "10", 0, true,
  275. "", "0x45", -1, true,
  276. "45", "45", 1, true,
  277. "16", "0x10", 1, true,
  278. "111.1", "367", 2, true,
  279. "229.20.0", "012345", 3, true,
  280. "123", "0173", 1, true,
  281. "9", "09", 1, false,
  282. "", "0x120x34", 2, true,
  283. "252.18", "0x12fc", 2, true];
  284. for (var i = 0; i < tests.length; i+= 4)
  285. G_Assert(z, tests[i] === l.canonicalNum_(tests[i + 1],
  286. tests[i + 2],
  287. tests[i + 3]),
  288. "canonicalNum broken on: " + tests[i + 1]);
  289. var testing = {};
  290. testing["123.123.0.0.1"] = "";
  291. testing["255.0.0.1"] = "255.0.0.1";
  292. testing["12.0x12.01234"] = "12.18.156.2";
  293. testing["276.2.3"] = "20.2.3.0";
  294. testing["012.034.01.055"] = "10.28.1.45";
  295. testing["0x12.0x43.0x44.0x01"] = "18.67.68.1";
  296. for (var key in testing)
  297. G_Assert(z, l.parseIPAddress_(key) === testing[key],
  298. "parseIPAddress broken on " + key + "(got: " +
  299. l.parseIPAddress_(key));
  300. var testing = {};
  301. testing["completely.bogus.url.with.a.whole.lot.of.dots"] =
  302. "with.a.whole.lot.of.dots";
  303. testing["http://poseidon.marinet.gr/~elani"] = "poseidon.marinet.gr";
  304. testing["http://www.google.com.."] = "www.google.com";
  305. testing["https://www.yaho%6F.com"] = "www.yahoo.com";
  306. testing["http://012.034.01.0xa"] = "10.28.1.10";
  307. testing["ftp://wierd..chars...%0f,%fa"] = "wierd.chars.,";
  308. for (var key in testing)
  309. G_Assert(z, l.getCanonicalHost(key) == testing[key],
  310. "getCanonicalHost broken on: " + key +
  311. "(got: " + l.getCanonicalHost(key) + ")");
  312. var testing = {};
  313. testing["www.google.com"] = "AF5638A09FDDDAFF5B7A6013B1BE69A9";
  314. testing["poseidon.marinet.gr"] = "01844755C8143C4579BB28DD59C23747";
  315. testing["80.53.164.26"] = "B775DDC22DEBF8BEBFEAC24CE40A1FBF";
  316. for (var key in testing)
  317. G_Assert(z, l.getLookupKey(key) === testing[key],
  318. "getlookupkey broken on " + key + " (got: " +
  319. l.getLookupKey(key) + ", expected: " +
  320. testing[key] + ")");
  321. var tests =
  322. [ "bGtEQWJuMl/z2ZxSBB2hsuWI8geMAwfSh3YBfYPejQ1O+wyRAJeJ1UW3V56zm" +
  323. "EpUvnaEiECN1pndxW5rEMNzE+gppPeel7PvH+OuabL3NXlspcP0xnpK8rzNgB1" +
  324. "JT1KcajQ9K3CCl24T9r8VGb0M3w==",
  325. "80.53.164.26",
  326. "^(?i)http\\:\\/\\/80\\.53\\.164\\.26(?:\\:80)?\\/\\.PayPal" +
  327. "\\.com\\/webscr\\-id\\/secure\\-SSL\\/cmd\\-run\\=\\/login\\.htm$",
  328. "ZTMzZjVnb3WW1Yc2ABorgQGAwYfcaCb/BG3sMFLTMDvOQxH8LkdGGWqp2tI5SK" +
  329. "uNrXIHNf2cyzcVocTqUIUkt1Ud1GKieINcp4tWcU53I0VZ0ZZHCjGObDCbv9Wb" +
  330. "CPSx1eS8vMREDv8Jj+UVL1yaZQ==",
  331. "80.53.164.26",
  332. "^(?i)http\\:\\/\\/80\\.53\\.164\\.26(?:\\:80)?\\/\\.PayPal\\.com" +
  333. "\\/webscr\\-id\\/secure\\-SSL\\/cmd\\-run\\=\\/login\\.htm$",
  334. "ZTMzZjVnb3WVb6VqoJ44hVo4V77XjDRcXTxOc2Zpn4yIHcpS0AQ0nn1TVlX4MY" +
  335. "IeNL/6ggzCmcJSWOOkj06Mpo56LNLrbxNxTBuoy9GF+xcm",
  336. "poseidon.marinet.gr",
  337. "^(?i)http\\:\\/\\/poseidon\\.marinet\\.gr(?:\\:80)?\\/\\~eleni" +
  338. "\\/eBay\\/index\\.php$",
  339. "bGtEQWJuMl9FA3Kl5RiXMpgFU8nDJl9J0hXjUck9+mMUQwAN6llf0gJeY5DIPP" +
  340. "c2f+a8MSBFJN17ANGJZl5oZVsQfSW4i12rlScsx4tweZAE",
  341. "poseidon.marinet.gr",
  342. "^(?i)http\\:\\/\\/poseidon\\.marinet\\.gr(?:\\:80)?\\/\\~eleni" +
  343. "\\/eBay\\/index\\.php$"];
  344. for (var i = 0; i < tests.length; i += 3) {
  345. var dec = l.decryptData(tests[i], tests[i + 1]);
  346. G_Assert(z, dec === tests[i + 2],
  347. "decryptdata broken on " + tests[i] + " (got: " + dec +
  348. ", expected: " + tests[i + 2] + ")");
  349. }
  350. G_Debug(z, "PASSED");
  351. }
  352. }
  353.