-
Notifications
You must be signed in to change notification settings - Fork 303
Exploit prevention for Shell Injection / Command Injection #7615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
bf9a508
SHI exploit prevention on one sink for java.lang.Runtime.exec(java.la…
jandro996 bf04013
fix spotless
jandro996 7043b2c
first steps to add cmdArray support (not blocking)
jandro996 f3bb30f
Fix known addresses
jandro996 8323532
Fix test
jandro996 663712b
Add support for arrayCmd methods and more smoke tests
jandro996 68f08b2
Add support for arrayCmd methods and more smoke tests
jandro996 3b62d1c
Add support for arrayCmd methods and more smoke tests
jandro996 d7226e1
Add support for ProcessBuilder
jandro996 b488d8a
Move to ProcessImplInstrumentation approach
jandro996 6d97145
Change to cmdi keeping ProcessImpl approach
jandro996 2c7672e
fix
jandro996 f3c4fe1
Add SHI
jandro996 996ab77
Add metrics to cmdi and shi with rule_variant tag
jandro996 cf855f7
Add another test
jandro996 92f9021
fix cmdi capability
jandro996 7846c74
change cmdi payloads
jandro996 dd3d414
Merge branch 'master' into alejandro.gonzalez/rasp-command-injection
jandro996 431cba1
format test
jandro996 650378e
fix comment
jandro996 c37357d
Merge branch 'master' into alejandro.gonzalez/rasp-command-injection
jandro996 5615a0a
change Runtime instrumentation to Appsec
jandro996 f384a09
Merge branch 'master' into alejandro.gonzalez/rasp-command-injection
jandro996 a2bc8f8
Merge branch 'master' into alejandro.gonzalez/rasp-command-injection
jandro996 15ba143
Merge branch 'master' into alejandro.gonzalez/rasp-command-injection
jandro996 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
20 changes: 20 additions & 0 deletions
20
...a-lang/src/main/java/datadog/trace/instrumentation/java/lang/RuntimeExecStringAdvice.java
This file contains hidden or 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,20 @@ | ||
package datadog.trace.instrumentation.java.lang; | ||
|
||
import datadog.trace.bootstrap.instrumentation.api.java.lang.ProcessImplInstrumentationHelpers; | ||
import java.io.IOException; | ||
import net.bytebuddy.asm.Advice; | ||
|
||
class RuntimeExecStringAdvice { | ||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void beforeExec(@Advice.Argument(0) final String command) throws IOException { | ||
if (command == null) { | ||
return; | ||
} | ||
ProcessImplInstrumentationHelpers.shiRaspCheck(command); | ||
} | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) | ||
public static void afterExec() { | ||
ProcessImplInstrumentationHelpers.resetCheckShi(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...va-lang/src/main/java/datadog/trace/instrumentation/java/lang/RuntimeInstrumentation.java
This file contains hidden or 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,37 @@ | ||
package datadog.trace.instrumentation.java.lang; | ||
|
||
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
||
import com.google.auto.service.AutoService; | ||
import datadog.trace.agent.tooling.Instrumenter; | ||
import datadog.trace.agent.tooling.InstrumenterModule; | ||
import datadog.trace.api.Platform; | ||
import java.io.File; | ||
|
||
@AutoService(InstrumenterModule.class) | ||
public class RuntimeInstrumentation extends InstrumenterModule.AppSec | ||
implements Instrumenter.ForSingleType, Instrumenter.ForBootstrap { | ||
|
||
public RuntimeInstrumentation() { | ||
super("java-lang-appsec"); | ||
} | ||
|
||
@Override | ||
protected boolean defaultEnabled() { | ||
return super.defaultEnabled() | ||
&& !Platform.isNativeImageBuilder(); // not applicable in native-image | ||
} | ||
|
||
@Override | ||
public String instrumentedType() { | ||
return "java.lang.Runtime"; | ||
} | ||
|
||
@Override | ||
public void methodAdvice(MethodTransformer transformer) { | ||
transformer.applyAdvice( | ||
named("exec").and(takesArguments(String.class, String[].class, File.class)), | ||
packageName + ".RuntimeExecStringAdvice"); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...og/trace/instrumentation/java/lang/ProcessImplInstrumentationExecCmdRaspForkedTest.groovy
This file contains hidden or 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,68 @@ | ||
package datadog.trace.instrumentation.java.lang | ||
|
||
import datadog.trace.agent.test.AgentTestRunner | ||
import datadog.trace.api.config.AppSecConfig | ||
import datadog.trace.api.gateway.CallbackProvider | ||
import datadog.trace.api.gateway.Flow | ||
import datadog.trace.api.gateway.RequestContext | ||
import datadog.trace.api.gateway.RequestContextSlot | ||
import datadog.trace.api.internal.TraceSegment | ||
import datadog.trace.bootstrap.instrumentation.api.AgentSpan | ||
import datadog.trace.bootstrap.instrumentation.api.AgentTracer | ||
import datadog.trace.bootstrap.instrumentation.api.java.lang.ProcessImplInstrumentationHelpers | ||
import spock.lang.Shared | ||
|
||
import java.util.function.BiFunction | ||
|
||
import static datadog.trace.api.gateway.Events.EVENTS | ||
|
||
class ProcessImplInstrumentationExecCmdRaspForkedTest extends AgentTestRunner { | ||
|
||
@Shared | ||
protected static final ORIGINAL_TRACER = AgentTracer.get() | ||
|
||
protected traceSegment | ||
protected reqCtx | ||
protected span | ||
protected tracer | ||
|
||
void setup() { | ||
traceSegment = Stub(TraceSegment) | ||
reqCtx = Stub(RequestContext) { | ||
getTraceSegment() >> traceSegment | ||
} | ||
span = Stub(AgentSpan) { | ||
getRequestContext() >> reqCtx | ||
} | ||
tracer = Stub(AgentTracer.TracerAPI) { | ||
activeSpan() >> span | ||
} | ||
AgentTracer.forceRegister(tracer) | ||
} | ||
|
||
void cleanup() { | ||
AgentTracer.forceRegister(ORIGINAL_TRACER) | ||
} | ||
|
||
@Override | ||
protected void configurePreAgent() { | ||
injectSysConfig(AppSecConfig.APPSEC_ENABLED, 'true') | ||
injectSysConfig(AppSecConfig.APPSEC_RASP_ENABLED, 'true') | ||
} | ||
|
||
void 'test cmdiRaspCheck'() { | ||
|
||
setup: | ||
final callbackProvider = Mock(CallbackProvider) | ||
final listener = Mock(BiFunction) | ||
final flow = Mock(Flow) | ||
tracer.getCallbackProvider(RequestContextSlot.APPSEC) >> callbackProvider | ||
|
||
when: | ||
ProcessImplInstrumentationHelpers.cmdiRaspCheck(['/bin/../usr/bin/reboot', '-f'] as String[]) | ||
|
||
then: | ||
1 * callbackProvider.getCallback(EVENTS.execCmd()) >> listener | ||
1 * listener.apply(reqCtx, ['/bin/../usr/bin/reboot', '-f']) >> flow | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not for this PR, since this just follows the current convention, but we should probably move RASP/APPSEC/IAST code in instrumentations to
*.iast
packages, to make sure codeowners apply to our team instead of APM IDM.