-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Exploit prevention monitoring ssrf (#4361)
--------- Co-authored-by: Carles Capell <107924659+CarlesDD@users.noreply.github.com>
- Loading branch information
Showing
12 changed files
with
376 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,6 +116,9 @@ tracer.init({ | |
apiSecurity: { | ||
enabled: true, | ||
requestSampling: 1.0 | ||
}, | ||
rasp: { | ||
enabled: true | ||
} | ||
} | ||
}); | ||
|
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,35 @@ | ||
'use strict' | ||
|
||
const { storage } = require('../../../datadog-core') | ||
const addresses = require('./addresses') | ||
const { httpClientRequestStart } = require('./channels') | ||
const waf = require('./waf') | ||
|
||
function enable () { | ||
httpClientRequestStart.subscribe(analyzeSsrf) | ||
} | ||
|
||
function disable () { | ||
if (httpClientRequestStart.hasSubscribers) httpClientRequestStart.unsubscribe(analyzeSsrf) | ||
} | ||
|
||
function analyzeSsrf (ctx) { | ||
const store = storage.getStore() | ||
const req = store?.req | ||
const url = ctx.args.uri | ||
|
||
if (!req || !url) return | ||
|
||
const persistent = { | ||
[addresses.HTTP_OUTGOING_URL]: url | ||
} | ||
// TODO: Currently this is only monitoring, we should | ||
// block the request if SSRF attempt and | ||
// generate stack traces | ||
waf.run({ persistent }, req) | ||
} | ||
|
||
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
116 changes: 116 additions & 0 deletions
116
packages/dd-trace/test/appsec/rasp.express.plugin.spec.js
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,116 @@ | ||
'use strict' | ||
|
||
const Axios = require('axios') | ||
const agent = require('../plugins/agent') | ||
const getPort = require('get-port') | ||
const appsec = require('../../src/appsec') | ||
const Config = require('../../src/config') | ||
const path = require('path') | ||
const { assert } = require('chai') | ||
|
||
withVersions('express', 'express', expressVersion => { | ||
describe('RASP', () => { | ||
let app, server, port, axios | ||
|
||
before(() => { | ||
return agent.load(['http'], { client: false }) | ||
}) | ||
|
||
before((done) => { | ||
const express = require(`../../../../versions/express@${expressVersion}`).get() | ||
const expressApp = express() | ||
|
||
expressApp.get('/', (req, res) => { | ||
app(req, res) | ||
}) | ||
|
||
appsec.enable(new Config({ | ||
appsec: { | ||
enabled: true, | ||
rules: path.join(__dirname, 'rasp_rules.json'), | ||
rasp: { enabled: true } | ||
} | ||
})) | ||
|
||
getPort().then(newPort => { | ||
port = newPort | ||
axios = Axios.create({ | ||
baseURL: `http://localhost:${port}` | ||
}) | ||
server = expressApp.listen(port, () => { | ||
done() | ||
}) | ||
}) | ||
}) | ||
|
||
after(() => { | ||
appsec.disable() | ||
server.close() | ||
return agent.close({ ritmReset: false }) | ||
}) | ||
|
||
function getWebSpan (traces) { | ||
for (const trace of traces) { | ||
for (const span of trace) { | ||
if (span.type === 'web') { | ||
return span | ||
} | ||
} | ||
} | ||
throw new Error('web span not found') | ||
} | ||
|
||
describe('ssrf', () => { | ||
['http', 'https'].forEach(protocol => { | ||
describe(`Test using ${protocol}`, () => { | ||
it('Should not detect threat', async () => { | ||
app = (req, res) => { | ||
require(protocol).get(`${protocol}://${req.query.host}`) | ||
res.end('end') | ||
} | ||
|
||
axios.get('/?host=www.datadoghq.com') | ||
|
||
await agent.use((traces) => { | ||
const span = getWebSpan(traces) | ||
assert.notProperty(span.meta, '_dd.appsec.json') | ||
}) | ||
}) | ||
|
||
it('Should detect threat doing a GET request', async () => { | ||
app = (req, res) => { | ||
require(protocol).get(`${protocol}://${req.query.host}`) | ||
res.end('end') | ||
} | ||
|
||
axios.get('/?host=ifconfig.pro') | ||
|
||
await agent.use((traces) => { | ||
const span = getWebSpan(traces) | ||
assert.property(span.meta, '_dd.appsec.json') | ||
assert(span.meta['_dd.appsec.json'].includes('rasp-ssrf-rule-id-1')) | ||
}) | ||
}) | ||
|
||
it('Should detect threat doing a POST request', async () => { | ||
app = (req, res) => { | ||
const clientRequest = require(protocol) | ||
.request(`${protocol}://${req.query.host}`, { method: 'POST' }) | ||
clientRequest.write('dummy_post_data') | ||
clientRequest.end() | ||
res.end('end') | ||
} | ||
|
||
axios.get('/?host=ifconfig.pro') | ||
|
||
await agent.use((traces) => { | ||
const span = getWebSpan(traces) | ||
assert.property(span.meta, '_dd.appsec.json') | ||
assert(span.meta['_dd.appsec.json'].includes('rasp-ssrf-rule-id-1')) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.