2017-12-15 21:37:02 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2022-11-05 10:58:21 +00:00
|
|
|
"use strict";
|
2017-12-14 22:38:34 +00:00
|
|
|
|
2022-04-23 10:00:18 +00:00
|
|
|
const fs = require("fs");
|
2018-01-17 11:31:57 +00:00
|
|
|
const path = require("path");
|
2022-11-05 10:58:21 +00:00
|
|
|
const Yargs = require("yargs");
|
2022-11-21 21:46:45 +00:00
|
|
|
|
|
|
|
// parse .env file into process.env
|
|
|
|
require('dotenv').config();
|
|
|
|
|
2022-11-20 14:25:16 +00:00
|
|
|
const cryptoEngine = require("../lib/cryptoEngine/cryptojsEngine");
|
|
|
|
const codec = require("../lib/codec");
|
2022-11-23 22:20:46 +00:00
|
|
|
const { convertCommonJSToBrowserJS, exitEarly, isOptionSetByUser, genFile, getPassword, getFileContent, getSalt} = require("./helpers");
|
2023-03-01 17:46:48 +00:00
|
|
|
const { isCustomPasswordTemplateLegacy, parseCommandLineArguments} = require("./helpers.js");
|
|
|
|
const { generateRandomSalt, generateRandomString } = cryptoEngine;
|
2022-11-20 14:25:16 +00:00
|
|
|
const { encode } = codec.init(cryptoEngine);
|
2017-12-14 22:38:34 +00:00
|
|
|
|
2022-11-05 10:58:21 +00:00
|
|
|
const SCRIPT_URL =
|
|
|
|
"https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js";
|
|
|
|
const SCRIPT_TAG =
|
|
|
|
'<script src="' +
|
|
|
|
SCRIPT_URL +
|
|
|
|
'" integrity="sha384-lp4k1VRKPU9eBnPePjnJ9M2RF3i7PC30gXs70+elCVfgwLwx1tv5+ctxdtwxqZa7" crossorigin="anonymous"></script>';
|
2017-12-14 22:00:11 +00:00
|
|
|
|
2023-03-01 17:46:48 +00:00
|
|
|
// parse arguments
|
|
|
|
const yargs = parseCommandLineArguments();
|
2022-02-27 18:37:17 +00:00
|
|
|
const namedArgs = yargs.argv;
|
2022-02-10 08:22:32 +00:00
|
|
|
|
2022-04-23 10:00:18 +00:00
|
|
|
// if the 's' flag is passed without parameter, generate a salt, display & exit
|
2022-11-05 10:58:21 +00:00
|
|
|
if (isOptionSetByUser("s", yargs) && !namedArgs.salt) {
|
|
|
|
console.log(generateRandomSalt());
|
|
|
|
process.exit(0);
|
2022-02-27 18:37:17 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 10:00:18 +00:00
|
|
|
// validate the number of arguments
|
2022-11-21 21:46:45 +00:00
|
|
|
const positionalArguments = namedArgs._;
|
|
|
|
if (positionalArguments.length > 2 || positionalArguments.length === 0) {
|
2023-03-01 17:46:48 +00:00
|
|
|
yargs.showHelp();
|
2022-11-05 10:58:21 +00:00
|
|
|
process.exit(1);
|
2022-02-10 08:22:32 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 21:46:45 +00:00
|
|
|
// parse input
|
|
|
|
const inputFilepath = positionalArguments[0].toString(),
|
|
|
|
password = getPassword(positionalArguments);
|
|
|
|
|
2023-03-01 17:46:48 +00:00
|
|
|
if (password.length < 16 && !namedArgs.short) {
|
|
|
|
console.log(
|
|
|
|
`WARNING: Your password is less than 16 characters (length: ${password.length}). Brute-force attacks are easy to `
|
|
|
|
+ `try on public files, and you are most safe when using a long password.\n\n`
|
|
|
|
+ `👉️ Here's a strong generated password you could use: `
|
|
|
|
+ generateRandomString(21)
|
|
|
|
+ "\n\nThe file was encrypted with your password. You can hide this warning by increasing your password length or"
|
|
|
|
+ " adding the '--short' flag."
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-04-23 10:00:18 +00:00
|
|
|
// get config file
|
2022-11-05 10:58:21 +00:00
|
|
|
const isUsingconfigFile = namedArgs.config.toLowerCase() !== "false";
|
|
|
|
const configPath = "./" + namedArgs.config;
|
2022-04-23 10:00:18 +00:00
|
|
|
let config = {};
|
|
|
|
if (isUsingconfigFile && fs.existsSync(configPath)) {
|
2022-11-05 10:58:21 +00:00
|
|
|
config = JSON.parse(fs.readFileSync(configPath, "utf8"));
|
2022-04-23 10:00:18 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 21:46:45 +00:00
|
|
|
// get the salt
|
|
|
|
const salt = getSalt(namedArgs, config);
|
2022-04-23 10:00:18 +00:00
|
|
|
|
|
|
|
// validate the salt
|
|
|
|
if (salt.length !== 32 || /[^a-f0-9]/.test(salt)) {
|
2022-11-20 14:25:16 +00:00
|
|
|
exitEarly(
|
2022-11-05 10:58:21 +00:00
|
|
|
"The salt should be a 32 character long hexadecimal string (only [0-9a-f] characters allowed)"
|
2022-11-20 14:25:16 +00:00
|
|
|
+ "\nDetected salt: " + salt
|
2022-11-05 10:58:21 +00:00
|
|
|
);
|
2022-04-23 10:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// write salt to config file
|
|
|
|
if (isUsingconfigFile && config.salt !== salt) {
|
2022-11-05 10:58:21 +00:00
|
|
|
config.salt = salt;
|
|
|
|
fs.writeFileSync(configPath, JSON.stringify(config, null, 4));
|
2022-04-23 10:00:18 +00:00
|
|
|
}
|
|
|
|
|
2022-11-20 18:16:27 +00:00
|
|
|
// display the share link with the hashed password if the --share flag is set
|
|
|
|
if (isOptionSetByUser("share", yargs)) {
|
|
|
|
const url = namedArgs.share || "";
|
2022-11-21 21:46:45 +00:00
|
|
|
const hashedPassphrase = cryptoEngine.hashPassphrase(password, salt);
|
2022-11-20 18:16:27 +00:00
|
|
|
|
|
|
|
console.log(url + "?staticrypt_pwd=" + hashedPassphrase);
|
|
|
|
}
|
|
|
|
|
2022-02-10 08:22:32 +00:00
|
|
|
// get the file content
|
2022-11-21 21:46:45 +00:00
|
|
|
const contents = getFileContent(inputFilepath);
|
2019-06-28 14:23:13 +00:00
|
|
|
|
2023-03-01 17:46:48 +00:00
|
|
|
// TODO: remove in the next major version bump. This is to allow a security update to some versions without breaking
|
|
|
|
// older ones. If the password template is custom AND created before 2.2.0 we need to use the old hashing algorithm.
|
|
|
|
const isLegacy = isCustomPasswordTemplateLegacy(namedArgs.f);
|
|
|
|
|
|
|
|
if (isLegacy) {
|
|
|
|
console.log(
|
|
|
|
"#################################\n\n" +
|
|
|
|
"SECURITY WARNING [StatiCrypt]: You are using an old version of the password template, which has been found to " +
|
|
|
|
"be less secure. Please update your custom password_template logic to match version 2.2.0 or higher." +
|
|
|
|
"\nYou can find instructions here: https://github.com/robinmoisson/staticrypt/issues/161" +
|
|
|
|
"\n\n#################################"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-12-28 11:32:27 +00:00
|
|
|
// encrypt input
|
2023-03-01 17:46:48 +00:00
|
|
|
const encryptedMessage = encode(contents, password, salt, isLegacy);
|
2017-12-14 22:38:34 +00:00
|
|
|
|
2017-12-28 11:32:27 +00:00
|
|
|
// create crypto-js tag (embedded or not)
|
2022-02-10 08:22:32 +00:00
|
|
|
let cryptoTag = SCRIPT_TAG;
|
2017-12-28 11:32:27 +00:00
|
|
|
if (namedArgs.embed) {
|
2022-11-05 10:58:21 +00:00
|
|
|
try {
|
|
|
|
const embedContents = fs.readFileSync(
|
2022-11-20 14:25:16 +00:00
|
|
|
path.join(__dirname, "..", "lib", "kryptojs-3.1.9-1.min.js"),
|
2022-11-05 10:58:21 +00:00
|
|
|
"utf8"
|
|
|
|
);
|
|
|
|
|
|
|
|
cryptoTag = "<script>" + embedContents + "</script>";
|
|
|
|
} catch (e) {
|
2022-11-20 14:25:16 +00:00
|
|
|
exitEarly("Failure: embed file does not exist!");
|
2022-11-05 10:58:21 +00:00
|
|
|
}
|
2017-12-14 22:38:34 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 08:22:32 +00:00
|
|
|
const data = {
|
2022-11-05 10:58:21 +00:00
|
|
|
crypto_tag: cryptoTag,
|
|
|
|
decrypt_button: namedArgs.decryptButton,
|
|
|
|
embed: namedArgs.embed,
|
|
|
|
encrypted: encryptedMessage,
|
|
|
|
instructions: namedArgs.instructions,
|
|
|
|
is_remember_enabled: namedArgs.noremember ? "false" : "true",
|
2023-03-01 17:46:48 +00:00
|
|
|
// TODO: remove on next major version bump. This is a hack to pass the salt to the injected js_codec in a backward
|
|
|
|
// compatible way (not requiring to update the password_template).
|
|
|
|
js_codec: convertCommonJSToBrowserJS("lib/codec").replace('##SALT##', salt),
|
2022-11-23 22:20:46 +00:00
|
|
|
js_crypto_engine: convertCommonJSToBrowserJS("lib/cryptoEngine/cryptojsEngine"),
|
2022-11-15 08:21:58 +00:00
|
|
|
label_error: namedArgs.labelError,
|
2022-11-05 10:58:21 +00:00
|
|
|
passphrase_placeholder: namedArgs.passphrasePlaceholder,
|
|
|
|
remember_duration_in_days: namedArgs.remember,
|
|
|
|
remember_me: namedArgs.rememberLabel,
|
|
|
|
salt: salt,
|
|
|
|
title: namedArgs.title,
|
2017-12-28 11:32:27 +00:00
|
|
|
};
|
|
|
|
|
2023-03-01 17:46:48 +00:00
|
|
|
const outputFilepath = namedArgs.output !== null
|
2022-11-20 14:25:16 +00:00
|
|
|
? namedArgs.output
|
2022-11-21 21:46:45 +00:00
|
|
|
: inputFilepath.replace(/\.html$/, "") + "_encrypted.html";
|
2017-12-14 22:38:34 +00:00
|
|
|
|
2023-03-01 17:46:48 +00:00
|
|
|
genFile(data, outputFilepath, namedArgs.f);
|