Skip to content

Commit b5e7200

Browse files
authored
Merge pull request #296 from Nizernizer/main
fix bug
2 parents 8a76d1a + 9adf202 commit b5e7200

File tree

32 files changed

+584
-179
lines changed

32 files changed

+584
-179
lines changed

dongtai-agent/src/main/java/io/dongtai/iast/agent/AgentLauncher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@ public static synchronized void uninstall() {
149149
* @param inst inst
150150
*/
151151
private static void install(final Instrumentation inst) {
152-
IastProperties iastProperties = IastProperties.getInstance();
152+
IastProperties.getInstance();
153153
Boolean send = AgentRegisterReport.send();
154154
if (send) {
155155
DongTaiLog.info("Agent registered successfully.");
156156
Boolean agentStat = AgentRegisterReport.agentStat();
157157
if (!agentStat) {
158158
EngineMonitor.isCoreRegisterStart = false;
159-
DongTaiLog.info("Agent wait for confirm.");
159+
DongTaiLog.info("Detection engine not started, agent waiting to be audited.");
160160
} else {
161161
EngineMonitor.isCoreRegisterStart = true;
162162
}

dongtai-agent/src/main/java/io/dongtai/iast/agent/monitor/MonitorDaemonThread.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ public MonitorDaemonThread(EngineManager engineManager) {
3232
this.engineManager = engineManager;
3333
try {
3434
delayTime = Integer.parseInt(System.getProperty("iast.engine.delay.time", "0"));
35-
DongTaiLog.info("dongtai engine delay time is " + delayTime + " s");
36-
delayTime = delayTime * 1000;
35+
if (delayTime != 0){
36+
DongTaiLog.info("dongtai engine delay time is " + delayTime + " s");
37+
delayTime = delayTime * 1000;
38+
}
3739
} catch (Exception e) {
3840
DongTaiLog.error("engine delay time must be int,eg: 10、20");
3941
delayTime = 0;

dongtai-agent/src/main/java/io/dongtai/iast/agent/report/AgentRegisterReport.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.net.*;
1313
import java.util.Enumeration;
1414

15+
import org.json.JSONArray;
1516
import org.json.JSONObject;
1617

1718
/**
@@ -174,9 +175,8 @@ private String getClusterVersion() {
174175
*/
175176
private String readIpInfo() {
176177
try {
177-
StringBuilder sb = new StringBuilder();
178-
boolean first = true;
179178
Enumeration<?> interfaces = NetworkInterface.getNetworkInterfaces();
179+
JSONArray network = new JSONArray();
180180
while (interfaces.hasMoreElements()) {
181181
NetworkInterface networkInterface = (NetworkInterface) interfaces.nextElement();
182182
if (networkInterface.isLoopback() || !networkInterface.isUp()) {
@@ -188,21 +188,20 @@ private String readIpInfo() {
188188
if (inetAddress instanceof Inet6Address) {
189189
continue;
190190
}
191-
if (first) {
192-
sb.append("{\"name\"").append(":").append("\"").append(networkInterface.getDisplayName())
193-
.append("\"");
194-
sb.append(",\"ip\"").append(":").append("\"").append(inetAddress.getHostAddress())
195-
.append("\"}");
196-
first = false;
197-
} else {
198-
sb.append(",{\"name\"").append(":").append("\"").append(networkInterface.getDisplayName())
199-
.append("\"");
200-
sb.append(",\"ip\"").append(":").append("\"").append(inetAddress.getHostAddress())
201-
.append("\"}");
191+
JSONObject jsonObject = new JSONObject();
192+
String displayName = networkInterface.getDisplayName();
193+
String hostAddress = inetAddress.getHostAddress();
194+
jsonObject.put("name",displayName);
195+
jsonObject.put("ip",hostAddress);
196+
if (displayName.startsWith("en")){
197+
jsonObject.put("isAddress","1");
198+
}else {
199+
jsonObject.put("isAddress","0");
202200
}
201+
network.put(jsonObject);
203202
}
204203
}
205-
return sb.toString();
204+
return network.toString();
206205
} catch (SocketException e) {
207206
return "{}";
208207
}

dongtai-agent/src/main/java/io/dongtai/iast/agent/util/LogUtils.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

dongtai-core/src/main/java/io/dongtai/iast/core/EngineManager.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public class EngineManager {
5454
private static final AtomicInteger reqCounts = new AtomicInteger(0);
5555
private static int enableDongTai = 0;
5656

57+
public static BooleanThreadLocal ENTER_REPLAY_ENTRYPOINT = new BooleanThreadLocal(false);
58+
5759
public static void turnOnDongTai() {
5860
DONGTAI_STATE.set(true);
5961
}
@@ -130,6 +132,7 @@ public static void cleanThreadState() {
130132
EngineManager.TAINT_POOL.remove();
131133
EngineManager.TAINT_HASH_CODES.remove();
132134
EngineManager.SCOPE_TRACKER.remove();
135+
// EngineManager.ENTER_REPLAY_ENTRYPOINT.remove();
133136
FallbackSwitch.clearHeavyHookFallback();
134137
EngineManager.getFallbackManager().getHookRateLimiter().remove();
135138
}
@@ -223,12 +226,26 @@ public static void enterHttpEntry(Map<String, Object> requestMeta) {
223226
ServiceFactory.startService();
224227
if (null == SERVER) {
225228
// todo: read server addr and send to OpenAPI Service
229+
String url = null;
230+
String protocol = null;
231+
if (null != requestMeta.get("serverName")){
232+
url = (String) requestMeta.get("serverName");
233+
}else {
234+
url = "";
235+
}
236+
if(null != requestMeta.get("protocol")){
237+
protocol = (String) requestMeta.get("protocol");
238+
}else {
239+
protocol = "";
240+
}
241+
226242
SERVER = new IastServer(
227-
(String) requestMeta.get("serverName"),
243+
url,
228244
(Integer) requestMeta.get("serverPort"),
245+
protocol,
229246
true
230247
);
231-
ServerAddressReport serverAddressReport = new ServerAddressReport(EngineManager.SERVER.getServerAddr(),EngineManager.SERVER.getServerPort());
248+
ServerAddressReport serverAddressReport = new ServerAddressReport(EngineManager.SERVER.getServerAddr(),EngineManager.SERVER.getServerPort(),EngineManager.SERVER.getProtocol());
232249
serverAddressReport.run();
233250
}
234251
Map<String, String> headers = (Map<String, String>) requestMeta.get("headers");
@@ -278,9 +295,9 @@ public static void enterDubboEntry(String dubboService, Map<String, String> atta
278295
}
279296
if (null == SERVER) {
280297
// todo: read server addr and send to OpenAPI Service
281-
SERVER = new IastServer(requestHeaders.get("dubbo"), 0, true);
298+
SERVER = new IastServer(requestHeaders.get("dubbo"), 0, "TCP",true);
282299
String serverAddr = EngineManager.SERVER.getServerAddr();
283-
ServerAddressReport serverAddressReport = new ServerAddressReport(serverAddr,0);
300+
ServerAddressReport serverAddressReport = new ServerAddressReport(serverAddr,0, SERVER.getProtocol());
284301
serverAddressReport.run();
285302
}
286303
Map<String, Object> requestMeta = new HashMap<String, Object>(12);

dongtai-core/src/main/java/io/dongtai/iast/core/bytecode/IastClassFileTransformer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ public byte[] transform(final ClassLoader loader,
161161
}
162162
} catch (
163163
Throwable ignore) {
164+
DongTaiLog.debug(ignore);
164165
} finally {
165166
if (isRunning) {
166167
EngineManager.turnOnDongTai();
@@ -225,7 +226,7 @@ private byte[] dumpClassIfNecessary(String className, byte[] data, byte[] origin
225226

226227
writeByteArrayToFile(enhancedClass, data);
227228
writeByteArrayToFile(originalClass, originalData);
228-
DongTaiLog.debug("dump class {} to {} success.", className, enhancedClass);
229+
DongTaiLog.trace("dump class {} to {} success.", className, enhancedClass);
229230
} catch (IOException e) {
230231
DongTaiLog.error("dump class {} failed. reason: {}", className, e);
231232
}
@@ -282,7 +283,7 @@ public Class<?>[] findForRetransform() {
282283
// 所以当尝试获取这个类更多详细信息的时候会引起关联类的ClassNotFoundException等未知的错误(取决于底层ClassLoader的实现)
283284
// 这里没有办法穷举出所有的异常情况,所以catch Throwable来完成异常容灾处理
284285
// 当解析类出现异常的时候,直接简单粗暴的认为根本没有这个类就好了
285-
DongTaiLog.debug("remove from findForReTransform, because loading class:" + clazz.getName()
286+
DongTaiLog.trace("remove from findForReTransform, because loading class:" + clazz.getName()
286287
+ " occur an exception", cause);
287288
}
288289
}

dongtai-core/src/main/java/io/dongtai/iast/core/bytecode/enhance/asm/AsmMethods.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,8 @@ static Method getAsmMethod(final Class<?> clazz,
182182
String.class,
183183
String.class
184184
);
185+
Method SPY$isReplayRequest = InnerHelper.getAsmMethod(
186+
SpyDispatcher.class,
187+
"isReplayRequest"
188+
);
185189
}

dongtai-core/src/main/java/io/dongtai/iast/core/bytecode/enhance/plugin/PluginRegister.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.dongtai.iast.core.bytecode.enhance.plugin;
22

33
import io.dongtai.iast.core.bytecode.enhance.IastContext;
4+
import io.dongtai.iast.core.bytecode.enhance.plugin.authentication.jwt.DispatchHandlerInterceptor;
5+
import io.dongtai.iast.core.bytecode.enhance.plugin.authentication.shiro.DispatchShiro;
46
import io.dongtai.iast.core.bytecode.enhance.plugin.cookie.DispatchCookie;
57
import io.dongtai.iast.core.bytecode.enhance.plugin.core.DispatchClassPlugin;
68
import io.dongtai.iast.core.bytecode.enhance.plugin.framework.dubbo.DispatchDubbo;
@@ -27,6 +29,8 @@ public class PluginRegister {
2729

2830
public PluginRegister() {
2931
this.plugins = new ArrayList<DispatchPlugin>();
32+
// this.plugins.add(new DispatchShiro());
33+
// this.plugins.add(new DispatchHandlerInterceptor());
3034
this.plugins.add(new DispatchSpringApplication());
3135
this.plugins.add(new DispatchJ2ee());
3236
//PLUGINS.add(new DispatchJsp());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.dongtai.iast.core.bytecode.enhance.plugin.authentication.jwt;
2+
3+
import io.dongtai.iast.core.bytecode.enhance.IastContext;
4+
import io.dongtai.iast.core.bytecode.enhance.plugin.DispatchPlugin;
5+
import org.objectweb.asm.ClassVisitor;
6+
7+
import java.util.Set;
8+
9+
public class DispatchHandlerInterceptor implements DispatchPlugin {
10+
11+
private static final String INTERFACE_HANDLER_SPRING = " org.springframework.web.servlet.HandlerInterceptor".substring(1);
12+
private Set<String> ancestors;
13+
14+
@Override
15+
public ClassVisitor dispatch(ClassVisitor classVisitor, IastContext context) {
16+
ancestors = context.getAncestors();
17+
String supportedClassName = isMatch();
18+
if (supportedClassName != null) {
19+
classVisitor = new HandlerInterceptorAdapter(classVisitor, context);
20+
}
21+
return classVisitor;
22+
}
23+
24+
@Override
25+
public String isMatch() {
26+
for (String superClassName : ancestors) {
27+
if (hookBySuperClass(superClassName)) {
28+
return superClassName;
29+
}
30+
}
31+
return null;
32+
}
33+
34+
public static boolean hookBySuperClass(String classname) {
35+
if (classname != null) {
36+
return INTERFACE_HANDLER_SPRING.equals(classname);
37+
} else {
38+
return false;
39+
}
40+
}
41+
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.dongtai.iast.core.bytecode.enhance.plugin.authentication.jwt;
2+
3+
import io.dongtai.iast.core.bytecode.enhance.IastContext;
4+
import io.dongtai.iast.core.bytecode.enhance.plugin.AbstractClassVisitor;
5+
import io.dongtai.iast.core.bytecode.enhance.plugin.spring.SpringApplicationAdviceAdapter;
6+
import org.objectweb.asm.ClassVisitor;
7+
import org.objectweb.asm.Label;
8+
import org.objectweb.asm.MethodVisitor;
9+
import org.objectweb.asm.Opcodes;
10+
11+
import static org.objectweb.asm.Opcodes.*;
12+
13+
public class HandlerInterceptorAdapter extends AbstractClassVisitor {
14+
15+
public HandlerInterceptorAdapter(ClassVisitor classVisitor, IastContext context) {
16+
super(classVisitor, context);
17+
}
18+
19+
@Override
20+
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
21+
22+
MethodVisitor methodVisitor = super.visitMethod(access,
23+
name,
24+
descriptor,
25+
signature,
26+
exceptions);
27+
if ("preHandle".equals(name)) {
28+
methodVisitor = new HandlerInterceptorAdviceAdapter(
29+
methodVisitor,
30+
access,
31+
name,
32+
descriptor,
33+
context,
34+
"SPRINGAPPLICATION_FOR_API",
35+
"SPRINGAPPLICATION_FOR_API"
36+
);
37+
setTransformed();
38+
}
39+
return methodVisitor;
40+
41+
}
42+
}
43+
44+
45+

0 commit comments

Comments
 (0)