Skip to content

Instantly share code, notes, and snippets.

@3vincent
Last active July 25, 2021 17:45
Show Gist options
  • Save 3vincent/389e5ed6d3f6700e31676dba3216ebd0 to your computer and use it in GitHub Desktop.
Save 3vincent/389e5ed6d3f6700e31676dba3216ebd0 to your computer and use it in GitHub Desktop.
dockerRootChecker.js
const util = require('util')
const exec = util.promisify(require('child_process').exec)
const dockerCheck = async () => {
try {
const { stdout, stderr } = await exec("grep -E '(lxc|docker)' /proc/1/cgroup")
if (stderr) console.log('stderr:', stderr)
if (stdout) return true
return false
} catch (e) {
console.error(e)
return e
}
}
const rootCheck = async () => {
try {
const { stdout, stderr } = await exec('whoami')
if (stderr) console.log('stderr:', stderr)
if (stdout.trim().toLowerCase() == 'root') return true
return false
} catch (e) {
console.error(e)
return e
}
}
async function dockerRootChecker() {
console.log('\x1b[1m', '\x1b[37m', '\x1b[41m', 'Running inside Docker Container:', await dockerCheck(), '\x1b[0m')
console.log('\x1b[1m', '\x1b[37m', '\x1b[41m', 'Running node process as user root:', await rootCheck(), '\x1b[0m')
}
dockerRootChecker()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment