-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instrument vm for code injection vulnerability
- Loading branch information
1 parent
b36ce05
commit e1b4c14
Showing
4 changed files
with
394 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict' | ||
|
||
const { channel, addHook } = require('./helpers/instrument') | ||
const shimmer = require('../../datadog-shimmer') | ||
const names = ['vm', 'node:vm'] | ||
|
||
const createScriptStartChannel = channel('datadog:vm:run-script:start') | ||
|
||
addHook({ name: names }, function (vm) { | ||
vm.Script = class extends vm.Script { | ||
constructor (code) { | ||
super(...arguments) | ||
this.code = code | ||
} | ||
} | ||
|
||
shimmer.wrap(vm.Script.prototype, 'runInContext', wrapVMMethod(1)) | ||
shimmer.wrap(vm.Script.prototype, 'runInNewContext', wrapVMMethod()) | ||
shimmer.wrap(vm.Script.prototype, 'runInThisContext', wrapVMMethod()) | ||
|
||
shimmer.wrap(vm, 'runInContext', wrapVMMethod()) | ||
shimmer.wrap(vm, 'runInNewContext', wrapVMMethod()) | ||
shimmer.wrap(vm, 'runInThisContext', wrapVMMethod()) | ||
shimmer.wrap(vm, 'compileFunction', wrapVMMethod()) | ||
|
||
return vm | ||
}) | ||
|
||
function wrapVMMethod (codeIndex = 0) { | ||
return function wrap (original) { | ||
return function wrapped () { | ||
const code = arguments[codeIndex] ? arguments[codeIndex] : this.code | ||
|
||
if (createScriptStartChannel.hasSubscribers && code) { | ||
createScriptStartChannel.publish({ code }) | ||
} | ||
|
||
return original.apply(this, arguments) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.