Advertisement
Guest User

malwr.com - 1

a guest
Jan 7th, 2017
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. var localGl = {
  2. glType : 'function',
  3. appName : 'mutexTest',
  4. glHash : '',
  5. clientId: '0',
  6. glKey: '',
  7. createRandomElementId : function(salt){
  8. var result = 'i';
  9. var key = parseInt(this.clientId);
  10. key = key + 'x' + key;
  11. for( var i = 0; i < key.length; i++){
  12. var x = parseInt(key[i]);
  13. if( i < salt.length ){
  14. var charc = salt[i];
  15. x += charc.charCodeAt(0) % 25;
  16. }
  17. if( x < 25 ){
  18. charc = String.fromCharCode(97+x);
  19. result += charc;
  20. }
  21. }
  22. return result;
  23. },
  24. pbComputeHash : function(s){
  25. return s.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return (a&a)>>>1},0);
  26. },
  27. localScope : function(div, input, key){
  28. var attr = '(' + this.glType + '(){' + input;
  29. var value = attr + '})();'
  30. div.setAttribute(key, value);
  31. }
  32. }
  33. self.port.on('insertWarning', function(params){
  34. var warning = params.tokens;
  35. var res = insertWarning(warning, 0);
  36. });
  37. self.port.on('closeWarning', function(params){
  38. if( params.button ){
  39. var buttonId = params.button;
  40. var button = document.getElementById(buttonId);
  41. if( button ){
  42. button.click();
  43. }
  44. }
  45. if( params.navigate ){
  46. document.location.href = params.navigate;
  47. }
  48. });
  49. function registerDocumentMutex(tokens, tries){
  50. tries = (typeof tries !== typeof undefined) ? tries : 0;
  51. var maxTries = 40;
  52. var retryInterval = 500;
  53. if( tries > maxTries ){
  54. return false;
  55. }
  56. tries++;
  57. if( tries > maxTries ){
  58. return false;
  59. }
  60. if( !document ){
  61. setTimeout( function(){ registerDocumentMutex(tokens, tries); }, retryInterval );
  62. return false;
  63. }
  64. var doc = document;
  65. var readyState = doc.readyState;
  66. var body = doc.body;
  67. if( !body ){
  68. setTimeout( function(){ registerDocumentMutex(tokens, tries); }, retryInterval );
  69. return false;
  70. }
  71. /* create a wrapper for saving our mutexes */
  72. var sWrapperId = localGl.createRandomElementId('mutexWrapper');
  73. var mWrapper = doc.getElementById(sWrapperId);
  74. if( !mWrapper ){
  75. mWrapper = doc.createElement('div');
  76. mWrapper.id = sWrapperId;
  77. mWrapper.setAttribute('style', 'display: none;');
  78. body.appendChild(mWrapper);
  79. }
  80. var mutexIsPresentButNotActive = false;
  81. var className = 'mutex';
  82. var mCol = document.getElementsByClassName(className);
  83. if( mCol && mCol.length > 0 ){
  84. for (var i = 0; i < mCol.length; i++) {
  85. var el = mCol[i];
  86. var content = el.textContent;
  87. if( content.indexOf('/mutex') > 0 ){
  88. mutexIsPresentButNotActive = true;
  89. return true;
  90. }
  91. }
  92. }
  93. var key = localGl.createRandomElementId(tokens);
  94. var docMutexDiv = doc.createElement('div');
  95. docMutexDiv.id = key;
  96. var className = 'mutex';
  97. docMutexDiv.className=className;
  98. var checkContent = 'mutex'+'/'+key+'/'+'mutex';
  99. docMutexDiv.textContent = checkContent;
  100. docMutexDiv.setAttribute('origin', localGl.appName);
  101. docMutexDiv.setAttribute('style', 'display: none;');
  102. mWrapper.appendChild(docMutexDiv);
  103. return true;
  104. }
  105. function mutexIsOff(){
  106. var canProceed = false;
  107. var className = 'mutex';
  108. var mCol = document.getElementsByClassName(className);
  109. var key = localGl.createRandomElementId(localGl.appName);
  110. var identifier = 'stop'+'/'+key+'/'+localGl.appName;
  111. if( mCol && mCol.length > 0 ){
  112. for (var i = 0; i < mCol.length; i++) {
  113. var el = mCol[i];
  114. var content = el.textContent;
  115. if( content.indexOf('/mutex') > 0 ){
  116. el.textContent = identifier;
  117. canProceed = true;
  118. }
  119. else if( content == identifier ){
  120. canProceed = true;
  121. }
  122. else {
  123. var attribute = localGl.appName;
  124. var requestedBy = el.getAttribute('req');
  125. if( requestedBy ){
  126. if( requestedBy.indexOf(attribute) == -1 ){
  127. attribute = attribute + ' ' + requestedBy;
  128. }
  129. }
  130. else{
  131. requestedBy = attribute;
  132. }
  133. el.setAttribute('req', requestedBy);
  134. }
  135. }
  136. }
  137. return canProceed;
  138. }
  139. function insertWarning(warning, tries){
  140. var url = document.location.href;
  141. tries = (typeof tries !== 'undefined') ? tries : 0;
  142. var retryInterval = 100;
  143. var maxTries = 40;
  144. tries++;
  145. if( tries > 40 ){
  146. /* if you tried too many times, give it up */
  147. return;
  148. }
  149. var warningDone = false;
  150. var body = document.getElementsByTagName('body')[0];
  151. if( (document.readyState != 'loading' && document.readyState != 'interactive') || tries >= 10 || body ){
  152. /* heuristic approach: give your best last times (>=10), even if the document hasn't completely loaded */
  153. if( !body ){
  154. /* Because there is no body, but just frames */
  155. var frameCount = window.frames.length;
  156. if( frameCount > 0 ){
  157. if (top.location == location){
  158. self.port.emit('insertWarning', tries);
  159. }
  160. /* append to each frame, there is no way of knowing which one is visible */
  161. for( var i = 0; i < frameCount; i++){
  162. var doc = window.frames[i].document;
  163. var frameHost = doc.location.host;
  164. var docBody = doc.body;
  165. if( docBody ){
  166. var warningModal = doc.createElement('script');
  167. warningModal.textContent = warning;
  168. docBody.appendChild(warningModal);
  169. warningDone = true;
  170. return true;
  171. }
  172. else{
  173. setTimeout(function(){
  174. var docBody = doc.body;
  175. if( docBody ){
  176. docBody.appendChild(warningModal);
  177. }
  178. }, 3000);
  179. }
  180. }
  181. }
  182. else{
  183. }
  184. }
  185. else{
  186. /* body present, put append it there */
  187. var warningModal = document.createElement('script');
  188. warningModal.textContent = warning;
  189. body.appendChild(warningModal);
  190. warningDone = true;
  191. return true;
  192. }
  193. }
  194. /* if for some reason the document is not ready, try it again later */
  195. if( !warningDone ){
  196. var interval = retryInterval*tries;
  197. setTimeout(function(){
  198. insertWarning(warning, tries);
  199. }, interval);
  200. }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement