Skip to content

Commit 833f978

Browse files
authored
Merge pull request HXSecurity#329 from Nizernizer/main
fix bug
2 parents ddb885e + f18094f commit 833f978

File tree

12 files changed

+214
-53
lines changed

12 files changed

+214
-53
lines changed

dongtai-agent/src/main/java/io/dongtai/iast/agent/manager/EngineManager.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ public class EngineManager {
2929
private static final String REMOTE_CONFIG_UTILS_CLASS = "io.dongtai.iast.core.utils.config.RemoteConfigUtils";
3030
private static final String ENGINE_MANAGER_CLASS = "io.dongtai.iast.core.EngineManager";
3131
private static final String INJECT_PACKAGE_REMOTE_URI = "/api/v1/engine/download?engineName=dongtai-spy";
32+
private static final String INJECT_PACKAGE_REMOTE_URI_JDK6 = "/api/v1/engine/download?engineName=dongtai-spy-jdk6";
3233
private static final String ENGINE_PACKAGE_REMOTE_URI = "/api/v1/engine/download?engineName=dongtai-core";
34+
private static final String ENGINE_PACKAGE_REMOTE_URI_JDK6 = "/api/v1/engine/download?engineName=dongtai-core-jdk6";
3335
private static final String API_PACKAGE_REMOTE_URI = "/api/v1/engine/download?engineName=dongtai-api";
36+
private static final String API_PACKAGE_REMOTE_URI_JDK6 = "/api/v1/engine/download?engineName=dongtai-api-jdk6";
3437
private static IastClassLoader IAST_CLASS_LOADER;
3538
private static EngineManager INSTANCE;
3639
private static String PID;
37-
3840
private final Instrumentation inst;
3941
private int runningStatus;
4042
private static boolean isCoreStop;
@@ -247,6 +249,22 @@ public boolean downloadPackageFromServer() {
247249
downloadJarPackageToCacheFromUrl(baseUrl + "/api/v1/engine/download?engineName=dongtai-grpc", getGrpcPackagePath());
248250
}
249251

252+
/**
253+
* 更新IAST引擎需要的jar包,用于启动时加载和热更新检测引擎 - iast-core.jar - iast-inject.jar
254+
*
255+
* @return 更新状态,成功为true,失败为false
256+
*/
257+
public boolean downloadPackageFromServerJdk6() {
258+
String baseUrl = properties.getBaseUrl();
259+
// 自定义jar下载地址
260+
String spyJarUrl = "".equals(properties.getCustomSpyJarUrl()) ? baseUrl + INJECT_PACKAGE_REMOTE_URI_JDK6 : properties.getCustomSpyJarUrl();
261+
String coreJarUrl = "".equals(properties.getCustomCoreJarUrl()) ? baseUrl + ENGINE_PACKAGE_REMOTE_URI_JDK6 : properties.getCustomCoreJarUrl();
262+
String apiJarUrl = "".equals(properties.getCustomApiJarUrl()) ? baseUrl + API_PACKAGE_REMOTE_URI_JDK6 : properties.getCustomApiJarUrl();
263+
return downloadJarPackageToCacheFromUrl(spyJarUrl, getInjectPackageCachePath()) &&
264+
downloadJarPackageToCacheFromUrl(coreJarUrl, getEnginePackageCachePath()) &&
265+
downloadJarPackageToCacheFromUrl(apiJarUrl, getApiPackagePath());
266+
}
267+
250268
/**
251269
* 从 dongtai-agent.jar 提取相关的jar包
252270
*
@@ -284,6 +302,24 @@ public boolean extractPackage() {
284302
}
285303
}
286304

305+
public boolean extractPackageJdk6() {
306+
// 解析jar包到本地
307+
String spyPackage = getInjectPackageCachePath();
308+
String enginePackage = getEnginePackageCachePath();
309+
String apiPackage = getApiPackagePath();
310+
if (properties.isDebug()) {
311+
DongTaiLog.info("current mode: debug, try to read package from directory {}", System.getProperty("java.io.tmpdir.dongtai"));
312+
if ((new File(spyPackage)).exists() && (new File(enginePackage)).exists() && (new File(apiPackage)).exists()) {
313+
return true;
314+
}
315+
}
316+
if(properties.getIsDownloadPackage().equals("true")){
317+
return downloadPackageFromServerJdk6();
318+
}else {
319+
return extractPackageFromAgent();
320+
}
321+
}
322+
287323
public boolean install() {
288324
String spyPackage = EngineManager.getInjectPackageCachePath();
289325
String corePackage = EngineManager.getEnginePackageCachePath();
@@ -311,7 +347,6 @@ public boolean install() {
311347
} catch (Throwable throwable) {
312348
DongTaiLog.error("Throwable: DongTai engine start failed, please contact staff for help.");
313349
DongTaiLog.error(throwable);
314-
315350
}
316351
return false;
317352
}

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,18 @@ public void run() {
7070

7171

7272
public void startEngine() {
73-
boolean status = couldInstallEngine();
74-
// todo: 下载功能优先走本地缓存
75-
status = status && engineManager.extractPackage();
76-
status = status && engineManager.install();
77-
status = status && engineManager.start();
73+
boolean status = true;
74+
if(couldInstallEngine()){
75+
// jdk8以上
76+
status = status && engineManager.extractPackage();
77+
status = status && engineManager.install();
78+
status = status && engineManager.start();
79+
}else {
80+
// jdk6-7
81+
status = status && engineManager.extractPackageJdk6();
82+
status = status && engineManager.install();
83+
status = status && engineManager.start();
84+
}
7885
if (!status) {
7986
DongTaiLog.info("DongTai IAST started failure");
8087
}

dongtai-agent/src/main/java/io/dongtai/iast/agent/monitor/collector/MetricsBindCollectorEnum.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public enum MetricsBindCollectorEnum {
2020

2121
MEM_USAGE_COLLECTOR(MEM_USAGE, MemUsageCollector.class, "绑定内存使用率收集器"),
2222

23-
MEM_NO_HEAP_USAGE_COLLECTOR(MEM_NO_HEAP_USAGE, MemNoHeapUsageCollector.class, "绑定堆外内存使用率收集器"),
23+
MEM_NO_HEAP_USAGE_COLLECTOR(MEM_NO_HEAP_USAGE, SystemMemUsageCollector.class, "绑定系统内存使用率收集器"),
2424

2525
GARBAGE_INFO_COLLECTOR(GARBAGE_INFO, GarbageInfoCollector.class, "绑定垃圾回收信息收集器"),
2626

dongtai-agent/src/main/java/io/dongtai/iast/agent/monitor/collector/impl/CpuUsageCollector.java

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import io.dongtai.iast.common.entity.performance.PerformanceMetrics;
44
import io.dongtai.iast.common.entity.performance.metrics.CpuInfoMetrics;
55
import io.dongtai.iast.common.enums.MetricsKey;
6+
import io.dongtai.iast.common.utils.version.JavaVersionUtils;
67
import oshi.SystemInfo;
78
import oshi.hardware.CentralProcessor;
89

10+
import java.lang.management.ManagementFactory;
911
import java.util.concurrent.TimeUnit;
1012

1113
/**
@@ -18,27 +20,35 @@ public class CpuUsageCollector extends AbstractPerformanceCollector {
1820

1921
@Override
2022
public PerformanceMetrics getMetrics() {
21-
SystemInfo systemInfo = new SystemInfo();
22-
CentralProcessor processor = systemInfo.getHardware().getProcessor();
23-
long[] prevTicks = processor.getSystemCpuLoadTicks();
24-
try {
25-
TimeUnit.SECONDS.sleep(1);
26-
} catch (InterruptedException ignored) {
27-
}
28-
long[] ticks = processor.getSystemCpuLoadTicks();
29-
long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
30-
long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
31-
long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
32-
long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
33-
long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
34-
long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
35-
long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
36-
long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
37-
long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
23+
if (JavaVersionUtils.isJava6()){
24+
com.sun.management.OperatingSystemMXBean osmxb = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
25+
double systemCpuLoad = osmxb.getSystemCpuLoad()*100;
26+
CpuInfoMetrics metricsValue = new CpuInfoMetrics();
27+
metricsValue.setCpuUsagePercentage(systemCpuLoad);
28+
return buildMetricsData(MetricsKey.CPU_USAGE, metricsValue);
29+
}else {
30+
SystemInfo systemInfo = new SystemInfo();
31+
CentralProcessor processor = systemInfo.getHardware().getProcessor();
32+
long[] prevTicks = processor.getSystemCpuLoadTicks();
33+
try {
34+
TimeUnit.SECONDS.sleep(1);
35+
} catch (InterruptedException ignored) {
36+
}
37+
long[] ticks = processor.getSystemCpuLoadTicks();
38+
long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
39+
long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
40+
long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
41+
long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
42+
long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
43+
long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
44+
long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
45+
long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
46+
long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
3847

39-
CpuInfoMetrics metricsValue = new CpuInfoMetrics();
40-
metricsValue.setCpuUsagePercentage((1.0 - (idle * 1.0 / totalCpu)) * 100);
41-
return buildMetricsData(MetricsKey.CPU_USAGE, metricsValue);
48+
CpuInfoMetrics metricsValue = new CpuInfoMetrics();
49+
metricsValue.setCpuUsagePercentage((1.0 - (idle * 1.0 / totalCpu)) * 100);
50+
return buildMetricsData(MetricsKey.CPU_USAGE, metricsValue);
51+
}
4252
}
4353

4454
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.dongtai.iast.agent.monitor.collector.impl;
2+
3+
import io.dongtai.iast.common.entity.performance.PerformanceMetrics;
4+
import io.dongtai.iast.common.entity.performance.metrics.MemoryUsageMetrics;
5+
import io.dongtai.iast.common.enums.MetricsKey;
6+
7+
import java.lang.management.ManagementFactory;
8+
import java.lang.management.MemoryUsage;
9+
10+
public class SystemMemUsageCollector extends AbstractPerformanceCollector {
11+
/**
12+
* 获取性能指标
13+
*
14+
* @return 性能指标
15+
*/
16+
@Override
17+
public PerformanceMetrics getMetrics() {
18+
com.sun.management.OperatingSystemMXBean osmxb = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
19+
long totalPhysicalMemorySize = osmxb.getTotalPhysicalMemorySize();
20+
long usedPhysicalMemorySize = totalPhysicalMemorySize - osmxb.getFreePhysicalMemorySize();
21+
MemoryUsageMetrics metricsValue = MemoryUsageMetrics.clone(new MemoryUsage(totalPhysicalMemorySize, usedPhysicalMemorySize, usedPhysicalMemorySize, totalPhysicalMemorySize));
22+
return buildMetricsData(MetricsKey.MEM_NO_HEAP_USAGE, metricsValue);
23+
}
24+
}

dongtai-agent/src/main/java/io/dongtai/iast/agent/monitor/impl/EngineMonitor.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.dongtai.iast.agent.util.ThreadUtils;
99
import io.dongtai.iast.agent.util.http.HttpClientUtils;
1010
import io.dongtai.iast.agent.Constant;
11+
import io.dongtai.iast.common.utils.version.JavaVersionUtils;
1112
import io.dongtai.log.DongTaiLog;
1213
import org.json.JSONObject;
1314

@@ -98,14 +99,32 @@ private String checkForStatus() {
9899
}
99100

100101
public void startEngine() {
101-
boolean status = engineManager.extractPackage();
102-
status = status && engineManager.install();
103-
status = status && engineManager.start();
102+
boolean status = true;
103+
if(couldInstallEngine()){
104+
// jdk8以上
105+
status = status && engineManager.extractPackage();
106+
status = status && engineManager.install();
107+
status = status && engineManager.start();
108+
}else {
109+
// jdk6-7
110+
status = status && engineManager.extractPackage();
111+
status = status && engineManager.install();
112+
status = status && engineManager.start();
113+
}
104114
if (!status) {
105115
DongTaiLog.info("DongTai IAST started failure");
106116
}
107117
}
108118

119+
private boolean couldInstallEngine() {
120+
// 低版本jdk暂不支持安装引擎core包
121+
if (JavaVersionUtils.isJava6() || JavaVersionUtils.isJava7()) {
122+
DongTaiLog.info("DongTai Engine core couldn't install because of low JDK version:" + JavaVersionUtils.javaVersionStr());
123+
return false;
124+
}
125+
return true;
126+
}
127+
109128
@Override
110129
public void run() {
111130
try {

dongtai-agent/src/main/java/io/dongtai/iast/agent/monitor/impl/PerformanceMonitor.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,6 @@ public void check() throws Exception {
131131
updatePerformanceMetrics(performanceMetrics);
132132
// 检查性能指标(用于熔断降级)
133133
checkPerformanceMetrics(performanceMetrics);
134-
/* int UsedRate = CPU_USAGE;
135-
PerformanceMonitor.AGENT_THRESHOLD_VALUE = PerformanceMonitor.checkThresholdValue();
136-
int preStatus = this.engineManager.getRunningStatus();
137-
if (isStart(UsedRate, preStatus)) {
138-
this.engineManager.start();
139-
DongTaiLog.info("The current CPU usage is " + UsedRate + "%, lower than the threshold " + AGENT_THRESHOLD_VALUE + "%,and the detection engine is starting");
140-
} else if (isStop(UsedRate, preStatus)) {
141-
this.engineManager.stop();
142-
DongTaiLog.info("The current CPU usage is " + UsedRate + "%, higher than the threshold " + AGENT_THRESHOLD_VALUE + "%,and the detection engine is stopping");
143-
}*/
144134
}
145135

146136
private void updatePerformanceMetrics(List<PerformanceMetrics> performanceMetrics) {

dongtai-agent/src/test/java/com/secnium/iast/agent/AgentTest.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import java.io.*;
44
import java.lang.management.ManagementFactory;
5+
import java.lang.management.OperatingSystemMXBean;
56
import java.lang.management.RuntimeMXBean;
7+
import java.lang.reflect.Method;
8+
import java.lang.reflect.Modifier;
69
import java.net.InetAddress;
710
import java.net.UnknownHostException;
811
import java.util.Arrays;
@@ -95,7 +98,48 @@ public void doAaaa() {
9598
}
9699

97100
public static void main(String[] args) {
98-
String a = "52.81.92.214:30158";
99-
System.out.println(a.substring(a.indexOf(":")+1));
101+
// com.sun.management.OperatingSystemMXBean osmxb = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
102+
// double systemCpuLoad = osmxb.getSystemCpuLoad()/osmxb.getAvailableProcessors();
103+
// double processCpuLoad = osmxb.getProcessCpuLoad();
104+
// System.out.println(systemCpuLoad);
105+
// System.out.println(processCpuLoad);
106+
107+
108+
// OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
109+
// for (Method method : operatingSystemMXBean.getClass().getDeclaredMethods()) {
110+
// method.setAccessible(true);
111+
// if (method.getName().startsWith("get")
112+
// && Modifier.isPublic(method.getModifiers())) {
113+
// Object value;
114+
// try {
115+
// value = method.invoke(operatingSystemMXBean);
116+
// } catch (Exception e) {
117+
// value = e;
118+
// } // try
119+
// System.out.println(method.getName() + " = " + value);
120+
// } // if
121+
// } // for
122+
123+
124+
OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
125+
ManagementFactory.getOperatingSystemMXBean();
126+
double load;
127+
for(int i=0; i<10; i++) {
128+
load = ((com.sun.management.OperatingSystemMXBean) mbean).getSystemCpuLoad();
129+
System.out.println(load);
130+
if((load<0.0 || load>1.0) && load != -1.0) {
131+
throw new RuntimeException("getSystemCpuLoad() returns " + load
132+
+ " which is not in the [0.0,1.0] interval");
133+
}
134+
try {
135+
Thread.sleep(200);
136+
} catch(InterruptedException e) {
137+
e.printStackTrace();
138+
}
139+
}
140+
}
141+
142+
private static void printUsage() {
143+
100144
}
101145
}

dongtai-core/src/main/java/io/dongtai/iast/core/handler/hookpoint/graphy/GraphBuilder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ public static List<GraphNode> build() {
6464
event.getSourceHashes(),
6565
event.getTargetHashes(),
6666
properties.isLocal() ? event.obj2String(event.inValue) : "",
67+
properties.isLocal() && event.objIsReference(event.inValue),
6768
properties.isLocal() ? event.obj2String(event.outValue) : "",
69+
properties.isLocal() && event.objIsReference(event.inValue),
6870
event.getSourceHashForRpc(),
6971
event.getTargetHashForRpc(),
7072
event.getTraceId(),

dongtai-core/src/main/java/io/dongtai/iast/core/handler/hookpoint/graphy/GraphNode.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ public class GraphNode {
3434

3535
private final String sourceValues;
3636

37+
private final boolean sourceIsReference;
38+
3739
private final String targetValues;
3840

41+
private final boolean targetIsReference;
42+
3943
/**
4044
* 当前方法所在类继承的类名称
4145
*/
@@ -126,7 +130,9 @@ public GraphNode(boolean isSource,
126130
Set<Integer> sourceHash,
127131
Set<Integer> targetHash,
128132
String sourceValues,
133+
boolean sourceIsReference,
129134
String targetValues,
135+
boolean targetIsReference,
130136
Set<Integer> sourceHashForRpc,
131137
Set<Integer> targetHashForRpc,
132138
String traceId,
@@ -149,7 +155,9 @@ public GraphNode(boolean isSource,
149155
this.sourceHash = sourceHash;
150156
this.targetHash = targetHash;
151157
this.sourceValues = sourceValues;
158+
this.sourceIsReference = sourceIsReference;
152159
this.targetValues = targetValues;
160+
this.targetIsReference = targetIsReference;
153161
this.sourceHashForRpc = sourceHashForRpc;
154162
this.targetHashForRpc = targetHashForRpc;
155163
this.traceId = traceId;
@@ -180,7 +188,9 @@ public JSONObject toJson() {
180188
value.put("retClassName", retClassName);
181189
value.put("sourceHash", sourceHashArray);
182190
value.put("sourceValues", sourceValues);
191+
value.put("sourceIsReference",sourceIsReference);
183192
value.put("targetHash", targetHashArray);
193+
value.put("targetIsReference", targetIsReference);
184194
value.put("targetValues", targetValues);
185195
value.put("sourceHashForRpc", sourceHashForRpcArray);
186196
value.put("targetHashForRpc", targetHashForRpcArray);

0 commit comments

Comments
 (0)