JavaScript snippet to create an elevated shell
- Copy code from shell.js into your code.
- Create new shell object in your code:
const shell = new Shell("root", "sh", "password"). This can also be done in a function. Theshellfield does not effect the shell at the time. - Call
shell.write("echo 'test'\n")to see if it is working or run a command. Always make sure to have a terminating\n. - Modify the class if you want to get the
dataof stderr and stdout somewhere else - Star (optional)
const chpr = require("child_process")
class Shell {
constructor(username, shell, password) {
this.username = username
this.shell = shell
this.password = password
this.s_process = chpr.exec(`su ${username}\n`)
this.s_process.stdin.write(this.password+"\n")
this.s_process.stderr.on("data", (data) => {console.log("Shell Err: " + data)})
this.s_process.stdout.on("data", (data) => {console.log("Shell Out: " + data)})
}
write(command) {
this.s_process.stdin.write(command)
console.log("Shell In: " + command)
}
}
const shell = new Shell("root", "sh", "root_password")
shell.write("echo 'You see this in your terminal, you did it'\n")