-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #565 from Nizernizer/feature/add-validator-rule
feature: add validator handler.
- Loading branch information
Showing
27 changed files
with
319 additions
and
21 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
54 changes: 54 additions & 0 deletions
54
...main/java/io/dongtai/iast/core/bytecode/enhance/plugin/core/adapter/ValidatorAdapter.java
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,54 @@ | ||
package io.dongtai.iast.core.bytecode.enhance.plugin.core.adapter; | ||
|
||
import io.dongtai.iast.core.bytecode.enhance.MethodContext; | ||
import io.dongtai.iast.core.handler.hookpoint.models.policy.PolicyNode; | ||
import io.dongtai.iast.core.handler.hookpoint.models.policy.ValidatorNode; | ||
import org.objectweb.asm.Label; | ||
import org.objectweb.asm.MethodVisitor; | ||
import org.objectweb.asm.Opcodes; | ||
|
||
import java.util.Set; | ||
|
||
public class ValidatorAdapter extends MethodAdapter { | ||
/** | ||
* @param adapter | ||
* @param mv | ||
* @param context | ||
* @param policyNodes | ||
*/ | ||
@Override | ||
public void onMethodEnter(MethodAdviceAdapter adapter, MethodVisitor mv, MethodContext context, Set<PolicyNode> policyNodes) { | ||
} | ||
|
||
/** | ||
* @param adapter | ||
* @param mv | ||
* @param opcode | ||
* @param context | ||
* @param policyNodes | ||
*/ | ||
@Override | ||
public void onMethodExit(MethodAdviceAdapter adapter, MethodVisitor mv, int opcode, MethodContext context, Set<PolicyNode> policyNodes) { | ||
for (PolicyNode policyNode : policyNodes) { | ||
if (!(policyNode instanceof ValidatorNode)) { | ||
continue; | ||
} | ||
|
||
Label elseLabel = new Label(); | ||
Label endLabel = new Label(); | ||
|
||
isEnterScope(adapter); | ||
mv.visitJumpInsn(Opcodes.IFEQ, elseLabel); | ||
|
||
adapter.trackMethod(opcode, policyNode, true); | ||
|
||
adapter.mark(elseLabel); | ||
adapter.mark(endLabel); | ||
} | ||
} | ||
|
||
private void isEnterScope(MethodAdviceAdapter adapter) { | ||
adapter.invokeStatic(ASM_TYPE_SPY_HANDLER, SPY_HANDLER$getDispatcher); | ||
adapter.invokeInterface(ASM_TYPE_SPY_DISPATCHER, SPY$enterValidator); | ||
} | ||
} |
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
90 changes: 90 additions & 0 deletions
90
...e/src/main/java/io/dongtai/iast/core/handler/hookpoint/controller/impl/ValidatorImpl.java
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,90 @@ | ||
package io.dongtai.iast.core.handler.hookpoint.controller.impl; | ||
|
||
import io.dongtai.iast.core.EngineManager; | ||
import io.dongtai.iast.core.handler.hookpoint.models.MethodEvent; | ||
import io.dongtai.iast.core.handler.hookpoint.models.policy.PolicyNodeType; | ||
import io.dongtai.iast.core.handler.hookpoint.models.policy.TaintPosition; | ||
import io.dongtai.iast.core.handler.hookpoint.models.policy.ValidatorNode; | ||
import io.dongtai.iast.core.handler.hookpoint.models.taint.range.TaintRange; | ||
import io.dongtai.iast.core.handler.hookpoint.models.taint.range.TaintRanges; | ||
import io.dongtai.iast.core.handler.hookpoint.models.taint.range.TaintRangesBuilder; | ||
import io.dongtai.iast.core.handler.hookpoint.models.taint.tag.TaintTag; | ||
import io.dongtai.iast.core.utils.StackUtils; | ||
import io.dongtai.iast.core.utils.TaintPoolUtils; | ||
|
||
import java.util.Set; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import static io.dongtai.iast.core.utils.TaintPoolUtils.getStringHash; | ||
|
||
public class ValidatorImpl { | ||
|
||
/** | ||
* 处理 Validator 点的事件 | ||
* | ||
* @param event Validator 点事件 | ||
*/ | ||
public static void solveValidator(MethodEvent event, ValidatorNode validatorNode, AtomicInteger invokeIdSequencer) { | ||
if (EngineManager.TAINT_HASH_CODES.isEmpty()) { | ||
return; | ||
} | ||
Set<TaintPosition> sources = validatorNode.getSources(); | ||
if (sources.isEmpty()) { | ||
return; | ||
} | ||
|
||
for (TaintPosition position : sources) { | ||
Long hash = null; | ||
Integer len = null; | ||
if (position.isObject()) { | ||
if (TaintPoolUtils.isNotEmpty(event.objectInstance) | ||
&& TaintPoolUtils.isAllowTaintType(event.objectInstance) | ||
&& TaintPoolUtils.poolContains(event.objectInstance, event)) { | ||
hash = getStringHash(event.objectInstance); | ||
len = TaintRangesBuilder.getLength(event.objectInstance); | ||
event.setObjectValue(event.objectInstance, true); | ||
} | ||
} else if (position.isParameter()) { | ||
int parameterIndex = position.getParameterIndex(); | ||
if (parameterIndex >= event.parameterInstances.length) { | ||
continue; | ||
} | ||
Object parameter = event.parameterInstances[parameterIndex]; | ||
if (TaintPoolUtils.isNotEmpty(parameter) | ||
&& TaintPoolUtils.isAllowTaintType(parameter) | ||
&& TaintPoolUtils.poolContains(parameter, event)) { | ||
hash = getStringHash(parameter); | ||
len = TaintRangesBuilder.getLength(parameter); | ||
event.addParameterValue(parameterIndex, parameter, true); | ||
} | ||
} else return; | ||
|
||
if (null != len && null != hash){ | ||
TaintRanges tr = new TaintRanges(new TaintRange(TaintTag.VALIDATED.getKey(), 0, len)); | ||
if (validatorNode.hasTags()) { | ||
String[] tags = validatorNode.getTags(); | ||
for (String tag : tags) { | ||
tr.add(new TaintRange(tag, 0, len)); | ||
} | ||
} | ||
event.sourceRanges.add(new MethodEvent.MethodEventTargetRange(hash, tr)); | ||
TaintRanges taintRanges = EngineManager.TAINT_RANGES_POOL.get().get(hash); | ||
if (null == taintRanges){ | ||
EngineManager.TAINT_RANGES_POOL.add(hash, tr); | ||
}else { | ||
taintRanges.addAll(tr); | ||
} | ||
}else return; | ||
} | ||
|
||
event.source = false; | ||
event.setCallStacks(StackUtils.createCallStack(4)); | ||
event.setTaintPositions(validatorNode.getSources(), null); | ||
|
||
int invokeId = invokeIdSequencer.getAndIncrement(); | ||
event.setInvokeId(invokeId); | ||
event.setPolicyType(PolicyNodeType.VALIDATOR.getName()); | ||
EngineManager.TRACK_MAP.addTrackMethod(invokeId, event); | ||
} | ||
|
||
} |
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.