<commit_before>var util = require('util');
var vm = require('vm');
require('./cycle');

util.log("Malware sandbox ver. 0.3");
util.log("------------------------");

var config = require("./config.json");

// commandline overwrites config.json values
var argv = require('minimist')(process.argv.slice(2));

if (argv.h || argv.help) {
    util.log("Usage: node jailme.js  [[-e file1] [-e file2] .. ] [-o ofile] [-s odir] [--down=y] [malware1 [malware2] .. ]");
    util.log("\t-e ifile ... js that simulates specific environment");
    util.log("\t-o ofile ... name of the file where sandbox shall be dumped at the end");
    util.log("\t-s odir  ... output directory for generated files (malware payload)");
    util.log("\t--down=y ... use http request to download malware components automatically");
    util.log("\tmalware  ... js with the malware code");
    util.log("If no arguments are specified the default values are taken from config.json");
    return;
}

function parse_argv_array(a, b) {
    if (a) {
        if (Array.isArray(a) && a.length > 0)
            return a;
        else if (typeof a === 'string')
            return [a];
        else if (typeof a === 'boolean')
            return [];
    }
    return b;
}
config.sandbox_sequence = parse_argv_array(argv.e, config.sandbox_sequence);
util.log("Sandbox environment sequence: " + config.sandbox_sequence);

config.malware_files = parse_argv_array(argv._, config.malware_files);
util.log("Malware files: " + config.malware_files);

if (typeof argv.o === 'string')
    config.context_dump_after = argv.o
util.log("Output file for sandbox dump: " + config.context_dump_after);
if (typeof argv.s === 'string')
    config.save_files = argv.s
util.log("Output directory for generated files: " + config.save_files);

//if (process.platform === "win32") {
//    var rl = require("readline").createInterface({
//        input: process.stdin,
//        output: process.stdout
//    });
//
//    rl.on("SIGINT", function() {
//        console.log("here");
//        process.emit("SIGINT");
//    });
//}
//
//process.on("SIGINT", function() {
//    //graceful shutdown
//    console.log("here");
//    process.exit();
//});

var sandbox = {};



sandbox.id = 1;
sandbox.util_log = util.log
if (argv.down)
    sandbox._download = true;
    //require('http');
sandbox.require = require;
//sandbox._setTimeout = setTimeout; // does not work yet
sandbox._decodeHTML = require("entities").decodeHTML;

fs = require('fs');

var ctx = vm.createContext(sandbox);
var i, fc;

function run_in_ctx(files) {
    try {
        for (i = 0; i < files.length; i++) {
            util.log(" => Executing: " + files[i]);
            fc = fs.readFileSync(files[i], 'utf8');
            vm.runInContext(fc, ctx);
        }
    } catch (err) {
        util.log(err.stack.toString());
        return false;
    }
    return true;
}

util.log("==> Preparing Sandbox environment.");
if (!run_in_ctx(config.sandbox_sequence)) return;
util.log("==> Executing malware file(s).");
run_in_ctx(config.malware_files);

util.log("==> Script execution finished, dumping sandbox environment to a file.");

var out = util.inspect(sandbox);<commit_msg>Catch uncaught<commit_after>var util = require('util');
var vm = require('vm');
require('./cycle');

process.on('uncaughtException', (err) => {
    util.log('Uncaught Exception!!!');
    util.log(err.stack.toString());
});

util.log("Malware sandbox ver. 0.3");
util.log("------------------------");

var config = require("./config.json");

// commandline overwrites config.json values
var argv = require('minimist')(process.argv.slice(2));

if (argv.h || argv.help) {
    util.log("Usage: node jailme.js  [[-e file1] [-e file2] .. ] [-o ofile] [-s odir] [--down=y] [malware1 [malware2] .. ]");
    util.log("\t-e ifile ... js that simulates specific environment");
    util.log("\t-o ofile ... name of the file where sandbox shall be dumped at the end");
    util.log("\t-s odir  ... output directory for generated files (malware payload)");
    util.log("\t--down=y ... use http request to download malware components automatically");
    util.log("\tmalware  ... js with the malware code");
    util.log("If no arguments are specified the default values are taken from config.json");
    return;
}

function parse_argv_array(a, b) {
    if (a) {
        if (Array.isArray(a) && a.length > 0)
            return a;
        else if (typeof a === 'string')
            return [a];
        else if (typeof a === 'boolean')
            return [];
    }
    return b;
}
config.sandbox_sequence = parse_argv_array(argv.e, config.sandbox_sequence);
util.log("Sandbox environment sequence: " + config.sandbox_sequence);

config.malware_files = parse_argv_array(argv._, config.malware_files);
util.log("Malware files: " + config.malware_files);

if (typeof argv.o === 'string')
    config.context_dump_after = argv.o
util.log("Output file for sandbox dump: " + config.context_dump_after);
if (typeof argv.s === 'string')
    config.save_files = argv.s
util.log("Output directory for generated files: " + config.save_files);

//if (process.platform === "win32") {
//    var rl = require("readline").createInterface({
//        input: process.stdin,
//        output: process.stdout
//    });
//
//    rl.on("SIGINT", function() {
//        console.log("here");
//        process.emit("SIGINT");
//    });
//}
//
//process.on("SIGINT", function() {
//    //graceful shutdown
//    console.log("here");
//    process.exit();
//});

var sandbox = {};



sandbox.id = 1;
sandbox.util_log = util.log
if (argv.down)
    sandbox._download = true;
    //require('http');
sandbox.require = require;
//sandbox._setTimeout = setTimeout; // does not work yet
sandbox._decodeHTML = require("entities").decodeHTML;

fs = require('fs');

var ctx = vm.createContext(sandbox);
var i, fc;

function run_in_ctx(files) {
    try {
        for (i = 0; i < files.length; i++) {
            util.log(" => Executing: " + files[i]);
            fc = fs.readFileSync(files[i], 'utf8');
            vm.runInContext(fc, ctx);
        }
    } catch (err) {
        util.log(err.stack.toString());
        return false;
    }
    return true;
}

util.log("==> Preparing Sandbox environment.");
if (!run_in_ctx(config.sandbox_sequence)) return;

util.log("==> Executing malware file(s).");
run_in_ctx(config.malware_files);

util.log("==> Script execution finished, dumping sandbox environment to a file.");

var out = util.inspect(sandbox);
{'fraction_non_alphanumeric': 0.18181818181818182, 'fraction_numerical': 0.004012964963728971, 'mean_word_length': 2.9730226854690374, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7261979', 'n_tokens_mistral': 2128, 'n_tokens_neox': 2081, 'n_words': 521}