encodeoverflow.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. function get_n_copies_of(ch, n)
  6. {
  7. var powers = new Array();
  8. powers[0] = ch;
  9. for (var i = 1; (1<<i) < n; i++)
  10. {
  11. powers[i] = powers[i-1] + powers[i-1];
  12. }
  13. var out = '';
  14. for (var i = powers.length-1; i >= 0; i--)
  15. {
  16. if ((1 << i) > n)
  17. continue;
  18. out += powers[i];
  19. n -= (1 << i);
  20. }
  21. return out;
  22. }
  23. function exploit()
  24. {
  25. // The choice of character is somewhat important -- we need
  26. // something that expands out to 3 bytes in UTF-8 encoding.
  27. // In this case, U+20AC satisfies that requirement.
  28. var s1 = "\u20ac";
  29. var ss;
  30. try
  31. {
  32. ss = get_n_copies_of(s1, 477218589);
  33. }
  34. catch (e)
  35. {
  36. WScript.Echo("You don't have enough free memory or VA to run this -- you'll need as much as possible.");
  37. return;
  38. }
  39. WScript.Echo("SS length = " + ss.length + "<br/>");
  40. // encodeURI sums (3 * [number of UTF-8 bytes required]) for each character
  41. // Since we use a char with 3 bytes required, that means the encodeURI memory
  42. // allocation is 3 * 3 * 477218589 = 0x100000005.
  43. // This truncates when fit into a ulong to just 5.
  44. WScript.Echo(encodeURI(ss).length);
  45. }
  46. try {
  47. exploit();
  48. }
  49. catch (e)
  50. {
  51. WScript.Echo("Message: " + e.message);
  52. }