-
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.
rasp lfi and iast using rasp fs-plugin
- Loading branch information
Showing
14 changed files
with
652 additions
and
4 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
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
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,93 @@ | ||
'use strict' | ||
|
||
const Plugin = require('../../plugins/plugin') | ||
const { storage } = require('../../../../datadog-core') | ||
const log = require('../../log') | ||
|
||
const enabledFor = { | ||
rasp: false, | ||
iast: false | ||
} | ||
|
||
let fsPlugin | ||
|
||
function enterWith (fsProps, store = storage.getStore()) { | ||
if (store && !store.fs?.opExcluded) { | ||
storage.enterWith({ | ||
...store, | ||
fs: { | ||
...store.fs, | ||
...fsProps, | ||
parentStore: store | ||
} | ||
}) | ||
} | ||
} | ||
|
||
class AppsecFsPlugin extends Plugin { | ||
enable () { | ||
this.addSub('apm:fs:operation:start', this._onFsOperationStart) | ||
this.addSub('apm:fs:operation:finish', this._onFsOperationFinishOrRenderEnd) | ||
this.addSub('tracing:datadog:express:response:render:start', this._onResponseRenderStart) | ||
this.addSub('tracing:datadog:express:response:render:end', this._onFsOperationFinishOrRenderEnd) | ||
|
||
super.configure(true) | ||
} | ||
|
||
disable () { | ||
super.configure(false) | ||
} | ||
|
||
_onFsOperationStart () { | ||
const store = storage.getStore() | ||
if (store) { | ||
enterWith({ root: store.fs?.root === undefined }, store) | ||
} | ||
} | ||
|
||
_onResponseRenderStart () { | ||
enterWith({ opExcluded: true }) | ||
} | ||
|
||
_onFsOperationFinishOrRenderEnd () { | ||
const store = storage.getStore() | ||
if (store?.fs?.parentStore) { | ||
storage.enterWith(store.fs.parentStore) | ||
} | ||
} | ||
} | ||
|
||
function enable (mod) { | ||
if (enabledFor[mod] !== false) return | ||
|
||
enabledFor[mod] = true | ||
|
||
if (!fsPlugin) { | ||
fsPlugin = new AppsecFsPlugin() | ||
fsPlugin.enable() | ||
} | ||
|
||
log.info(`Enabled AppsecFsPlugin for ${mod}`) | ||
} | ||
|
||
function disable (mod) { | ||
if (!mod || !enabledFor[mod]) return | ||
|
||
enabledFor[mod] = false | ||
|
||
const allDisabled = Object.values(enabledFor).every(val => val === false) | ||
if (allDisabled) { | ||
fsPlugin?.disable() | ||
|
||
fsPlugin = undefined | ||
} | ||
|
||
log.info(`Disabled AppsecFsPlugin for ${mod}`) | ||
} | ||
|
||
module.exports = { | ||
enable, | ||
disable, | ||
|
||
AppsecFsPlugin | ||
} |
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,65 @@ | ||
'use strict' | ||
|
||
const { fsOperationStart } = require('../channels') | ||
const { storage } = require('../../../../datadog-core') | ||
const web = require('../../plugins/util/web') | ||
const { enable: enableFsPlugin, disable: disableFsPlugin } = require('./fs-plugin') | ||
const addresses = require('../addresses') | ||
const waf = require('../waf') | ||
const { RULE_TYPES, handleResult } = require('./utils') | ||
const { block } = require('../blocking') | ||
const { isAbsolute } = require('path') | ||
|
||
let config | ||
|
||
function enable (_config) { | ||
config = _config | ||
|
||
enableFsPlugin('rasp') | ||
|
||
fsOperationStart.subscribe(analyzeLfi) | ||
} | ||
|
||
function disable () { | ||
if (fsOperationStart.hasSubscribers) fsOperationStart.unsubscribe(analyzeLfi) | ||
|
||
disableFsPlugin('rasp') | ||
} | ||
|
||
function analyzeLfi (ctx) { | ||
const path = ctx?.path | ||
if (!path) return | ||
|
||
const store = storage.getStore() | ||
if (!store) return | ||
|
||
const { req, fs, res } = store | ||
if (!req || !fs) return | ||
|
||
if (fs.root && !fs.opExcluded && shouldAnalyze(path)) { | ||
const persistent = { | ||
[addresses.FS_OPERATION_PATH]: path | ||
} | ||
|
||
const result = waf.run({ persistent }, req, RULE_TYPES.LFI) | ||
|
||
if (result) { | ||
const abortController = new AbortController() | ||
handleResult(result, req, res, abortController, config) | ||
|
||
const { aborted, reason } = abortController.signal | ||
if (aborted) { | ||
block(req, res, web.root(req), null, reason.blockingAction) | ||
} | ||
} | ||
} | ||
} | ||
|
||
function shouldAnalyze (path) { | ||
return isAbsolute(path) || path.contains('../') | ||
} | ||
|
||
module.exports = { | ||
enable, | ||
disable | ||
} |
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
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.