Guest User

Untitled

a guest
Nov 12th, 2017
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         crypto
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.1
  5. // @description  try to take over the world!
  6. // @author       anonymousse
  7. // @match        https://avenoel.org/*
  8. // @exclude      https://avenoel.org/chat*
  9. // @exclude      https://avenoel.org/admin*
  10. // @grant        none
  11. // ==/UserScript==
  12.  
  13.  
  14. function createKey (str_key) {
  15.     let raw_key = Uint8Array.from(btoa(str_key), c => c.charCodeAt(0)).buffer;
  16.     return crypto.subtle.digest('sha-256', raw_key).then(hash_key => {
  17.         return crypto.subtle.importKey('raw', hash_key, { name: 'AES-CBC' }, false, ['encrypt', 'decrypt']);
  18.     });
  19. }
  20.  
  21. function encrypt (msg, str_key) {
  22.     let data = Uint8Array.from(btoa(msg), c => c.charCodeAt(0));
  23.  
  24.     let initVector = new Uint8Array(16);
  25.  
  26.     crypto.getRandomValues(initVector);
  27.  
  28.     return createKey(str_key).then(key => {
  29.         return crypto.subtle.encrypt({ name: 'AES-CBC', iv: initVector}, key, data);
  30.     }).then(encrypted => {
  31.         encrypted = new Uint8Array(encrypted);
  32.         return btoa(encrypted) + '.' + btoa(initVector);
  33.     });
  34. }
  35.  
  36. function decrypt (msg, str_key) {
  37.     let [encrypted, initVector] = msg.split('.');
  38.  
  39.     encrypted = new Uint8Array(atob(encrypted).split(','));
  40.     initVector = new Uint8Array(atob(initVector).split(','));
  41.  
  42.  
  43.     return createKey(str_key).then(key => {
  44.         return crypto.subtle.decrypt({name: 'AES-CBC', iv: initVector}, key, encrypted);
  45.     }).then(decrypted => {
  46.         decrypted = new Uint8Array(decrypted);
  47.         let decoded = atob(String.fromCharCode.apply(null, decrypted));
  48.  
  49.         return decoded;
  50.     });
  51. }
  52.  
  53. function previsu (msg, balise) {
  54.     let req = new XMLHttpRequest();
  55.     let form = new FormData();
  56.     form.append('content', '[DECRYPTED CONTENT]\n' + msg + '\n[END]');
  57.  
  58.  
  59.     req.open('POST', 'https://avenoel.org/previsualisation', true);
  60.     req.setRequestHeader('x-csrf-token', document.token);
  61.     req.setRequestHeader('x-requested-with', 'XMLHttpRequest');
  62.     req.onload = function (res) {
  63.         balise.innerHTML = JSON.parse(res.srcElement.response).content;
  64.     };
  65.     req.send(form);
  66. }
  67.  
  68. (function() {
  69.     'use strict';
  70.  
  71.     document.token = document.querySelector('meta[name=csrf-token]').getAttribute('content');
  72.     let bbcodes = document.getElementsByClassName('bbcodes')[0];
  73.     let field = document.getElementsByTagName('textarea')[0];
  74.  
  75.     //on créé le bouton bbcode
  76.     let button = document.createElement('button');
  77.     button.setAttribute('type', 'button');
  78.     button.setAttribute('class', 'btn');
  79.     button.setAttribute('tabindex', '-1');
  80.     button.setAttribute('href', '#');
  81.  
  82.     let img = document.createElement('img');
  83.     img.setAttribute('src', 'https://avenoel.org/images/smilies/724771109.gif');
  84.  
  85.     button.appendChild(img);
  86.     button.addEventListener('click', event => {
  87.         let key = document.getElementsByClassName('bloc-title')[0].innerText.split(' ').pop();
  88.         if (field.selectionStart === field.selectionEnd) {
  89.             return ;
  90.         }
  91.         let msg = field.value.substr(field.selectionStart, field.selectionEnd);
  92.         previsu(msg);
  93.         encrypt(msg, key).then(encrypted => {
  94.             let a = `<spoiler>[WARNING ENCRYPTED CONTENT]
  95. ${encrypted}
  96. [END]</spoiler>`;
  97.             field.value = field.value.slice(0, field.selectionStart) + a + field.value.slice(field.selectionEnd);
  98.         });
  99.     });
  100.     bbcodes.appendChild(button);
  101.  
  102.     if (document.location.pathname.split('/')[1] === 'topic') {
  103.         Array.from(document.getElementsByClassName('message-content')).map(msg => {
  104.             let author = msg.parentNode.getElementsByTagName('header')[0].getElementsByTagName('a')[0].pathname.split('/').pop();
  105.             Array.from(msg.getElementsByClassName('spoiler-content')).map(spoiler => {
  106.                 if (!spoiler) return ;
  107.                 let tab = spoiler.innerText.split('\n\n');
  108.                 console.log(tab);
  109.                 if (tab.length === 3 && tab[0] === '[WARNING ENCRYPTED CONTENT]' && tab[2] === '[END]') {
  110.                                     console.log('message encrypté;;; key = ' + author);
  111.  
  112.                     decrypt(tab[1], author).then(decrypted => {
  113.                         previsu(decrypted, spoiler);
  114.  
  115.                     });
  116.                 }
  117.             });
  118.         });
  119.     }
  120. })();
Advertisement
Add Comment
Please, Sign In to add comment