Commit 167180d6 by 钱炳权

增加监控代理接口和pcap文件下载接口

parent 9d5c8d46
package com.example.fuzzControll.controller.agentController;
import com.example.fuzzControll.domain.bo.AflnetDataParams;
import com.example.fuzzControll.domain.vo.AjaxResult;
import com.example.fuzzControll.service.AgentOfKittyService;
import jdk.internal.agent.resources.agent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/agent")
@Slf4j
public class AgentController {
@Autowired
AgentOfKittyService agentOfKittyService;
/**
* 网络代理启动
*/
//todo 启动有问题;代码待检查;给/etc/profile 加进入python虚拟环境
@RequestMapping(value = "/kittyNetworkAgent", method = RequestMethod.GET)
public AjaxResult startNetworkAgent(@RequestParam String networkCard, @RequestParam String monitorName) {
try {
Boolean flag = agentOfKittyService.startNetworkAgent(networkCard, monitorName);
if (flag) {
return AjaxResult.success("NetworkAgent run success!");
} else {
return AjaxResult.error("The agent failed to start!");
}
} catch (Exception e) {
e.printStackTrace();
return AjaxResult.error("The agent failed to start!");
}
}
/**
* ssh代理启动
*/
//todo ssh使用存在问题我的理解是用来监控目标服务器的,通过ssh发送命令来监控
@RequestMapping(value = "/kittySshAgent", method = RequestMethod.GET)
public AjaxResult startSshAgent(@RequestParam String MonitorName, @RequestParam String SshUserName,
@RequestParam String SshPassword, @RequestParam String SshIp,
@RequestParam String SshPort, @RequestParam String Command) {
try {
new Thread(new Runnable() {
@Override
public void run() {
agentOfKittyService.startSshAgent(MonitorName, SshUserName, SshPassword, SshIp, SshPort, Command);
}
}).start();
return AjaxResult.success("SshAgent run success!");
} catch (Exception e) {
e.printStackTrace();
return AjaxResult.error("The agent failed to start!");
}
}
/**
* ssh网络代理的pcap文件下载接口
*/
//todo ssh使用存在问题我的理解是用来监控目标服务器的,通过ssh发送命令来监控
@RequestMapping(value = "/getAgentPcap", method = RequestMethod.GET)
public byte[] getAgentPcap() {
try {
return agentOfKittyService.readPcapToByte();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
\ No newline at end of file
package com.example.fuzzControll.service;
import java.util.List;
public interface AgentOfKittyService {
Boolean startNetworkAgent(String networkCard, String monitorName);
Boolean startSshAgent(String monitorName, String sshUserName, String sshPassword, String sshIp, String sshPort, String command);
byte[] readPcapToByte();
}
package com.example.fuzzControll.service.impl;
import com.example.fuzzControll.exception.testException.CmdException;
import com.example.fuzzControll.service.AgentOfKittyService;
import com.example.fuzzControll.tools.file.FileTools;
import com.example.fuzzControll.tools.test.TestCmdTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@Service
@Slf4j
public class AgentOfKittyServiceImpl implements AgentOfKittyService {
@Override
public Boolean startNetworkAgent(String networkCard, String monitorName) {
try {
String finalCmd = "python /usr/fuzzenv/fuzzenv/fuzz50/katnip/katnip/monitors/network.py " + networkCard + " " + monitorName;
TestCmdTools cmdTools = new TestCmdTools();
List<String> result = cmdTools.runCmd(finalCmd, "startNetworkAgent");
List<String> filter = result.stream().filter(s -> s.contains("calling mon.teardown")).collect(Collectors.toList());
log.info("Network Agent:{}", (Object) StringUtils.toStringArray(result));
return filter.size() > 0;
} catch (CmdException e) {
log.error("startNetworkAgent run error:{}", e.getMessage());
throw new RuntimeException(e);
}
}
@Override
public Boolean startSshAgent(String monitorName, String sshUserName, String sshPassword, String sshIp, String sshPort, String command) {
try {
String finalCmd = "python /usr/fuzzenv/fuzzenv/fuzz50/katnip/katnip/monitors/ssh.py " + monitorName
+ " " + sshUserName + " " + sshPassword + " " + sshIp + " " + sshPort + " " + command;
TestCmdTools cmdTools = new TestCmdTools();
List<String> result = cmdTools.runCmd(finalCmd, "startSshAgent");
List<String> filter = result.stream().filter(s -> s.contains("Failed to stop thread")).collect(Collectors.toList());
return !(filter.size() > 0);
} catch (CmdException e) {
log.error("startNetworkAgent run error:{}", e.getMessage());
throw new RuntimeException(e);
}
}
@Override
public byte[] readPcapToByte() {
byte[] fileByts = new byte[0];
try {
FileTools fileTools = new FileTools();
fileByts = fileTools.fileReadAndTranstoBytes("/tmp/","test_1.pcap");
} catch (Exception e) {
log.error("readPcapToByte error:{}", e.getMessage());
throw new RuntimeException(e);
}
return fileByts;
}
}
......@@ -50,7 +50,7 @@ public class FileTools {
}
int bytesRead;
while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
System.out.println(buffer.length);
log.info("File reading ....");
}
} catch (Exception e) {
e.printStackTrace();
......
......@@ -37,6 +37,7 @@ public class TestCmdTools {
List<String> result = new ArrayList<String>();
try {
log.info(caller + " is running!");
log.info("Running cmd:[{}]", cmd);
Process process = Runtime.getRuntime().exec(cmd);
printMessage(process.getInputStream(), result);
printMessage(process.getErrorStream(), new ArrayList<String>());
......
......@@ -49,11 +49,11 @@ integrationpath:
webSocketUri: ws://${fuzzmaster-docker.ip}:8101/websocket/testResult/
nacos-docker:
ip: 192.168.50.251
ip: 192.168.2.247
fuzzmaster-docker:
ip: 192.168.50.251
ip: 192.168.2.247
mysql-docker:
ip: 192.168.50.251
ip: 192.168.2.247
port: 3307
\ No newline at end of file
package com.example.fuzzbackendmaster.controller;
import com.example.fuzzbackendmaster.pojo.vo.AjaxResult;
import com.example.fuzzbackendmaster.service.FuzzIntegrationFileApi;
import com.example.fuzzbackendmaster.service.FuzzLogService;
import com.example.fuzzbackendmaster.service.KittyAgentService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.rmi.RemoteException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.EmptyStackException;
@RestController
@RequestMapping("/agent")
@Slf4j
public class KittyAgentController {
@Autowired
FuzzIntegrationFileApi fuzzIntegrationFileApi;
@Autowired
KittyAgentService kittyAgentService;
/**
* 启动网络代理
*
*/
@RequestMapping(value = "/kittyNetworkAgent", method = RequestMethod.GET)
public AjaxResult startNetworkAgent(@RequestParam String networkCard, @RequestParam String monitorName) {
try {
return fuzzIntegrationFileApi.startNetworkAgent(networkCard, monitorName);
} catch (Exception e) {
log.error("Start network agent error:{}", e.getMessage());
return AjaxResult.error();
}
}
/**
* 启动ssh代理
*/
@RequestMapping(value = "/kittySshAgent", method = RequestMethod.GET)
public AjaxResult startSshAgent(@RequestParam String MonitorName, @RequestParam String SshUserName,
@RequestParam String SshPassword, @RequestParam String SshIp,
@RequestParam String SshPort, @RequestParam String Command) {
try {
return fuzzIntegrationFileApi.startSshAgent(MonitorName,SshUserName,SshPassword,SshIp,SshPort,Command);
} catch (Exception e) {
log.error("Start network agent error:{}", e.getMessage());
return AjaxResult.error();
}
}
/**
* 下载对应任务的日志;不同任务返回数据类型不同,需要做个表来区分
*/
@RequestMapping(value = "/getAgentPcap", method = RequestMethod.GET)
public void getAgentPcap(HttpServletResponse response) throws IOException {
try {
byte[] fileBytes = fuzzIntegrationFileApi.getAgentPcap();
if (fileBytes == null || fileBytes.length == 0) {
throw new RemoteException("Pcap file is null!");
}
Date date = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss-");
kittyAgentService.downloadPcap(df.format(date) + "kitty.pcap", fileBytes, response);
} catch (Exception e) {
e.printStackTrace();
response.sendError(500, "Download failed!");
}
}
}
......@@ -77,10 +77,12 @@ public interface FuzzIntegrationFileApi {
/**
* 查询任务信息
*
* @return
*/
@RequestMapping(value = "/mission/getList", method = RequestMethod.GET)
AjaxResult selectMissionInfoList();
/**
* 查询测试参数
*/
......@@ -89,14 +91,16 @@ public interface FuzzIntegrationFileApi {
/**
* 跟据id查参数
*
* @param missionId
* @return
*/
@RequestMapping(value = "/fuzzParams/getParam/{missionId}", method = RequestMethod.GET)
AjaxResult getFuzzParamById(@PathVariable("missionId")int missionId);
AjaxResult getFuzzParamById(@PathVariable("missionId") int missionId);
/**
* 编辑参数
*
* @param fuzzParams
* @return
*/
......@@ -108,4 +112,23 @@ public interface FuzzIntegrationFileApi {
*/
@RequestMapping(value = "/test/testProcessInfo", method = RequestMethod.GET)
AjaxResult getProcessInfo();
/**
* 开启网络代理
*/
@RequestMapping(value = "/agent/kittyNetworkAgent", method = RequestMethod.GET)
AjaxResult startNetworkAgent(@RequestParam String networkCard,@RequestParam String monitorName);
/**
* 获取代理生成的pcap文件
*/
@RequestMapping(value = "/agent/getAgentPcap", method = RequestMethod.GET)
byte[] getAgentPcap();
/**
* 开启ssh代理
*/
@RequestMapping(value = "/agent/kittySshAgent", method = RequestMethod.GET)
AjaxResult startSshAgent(@RequestParam String MonitorName, @RequestParam String SshUserName,
@RequestParam String SshPassword, @RequestParam String SshIp,
@RequestParam String SshPort, @RequestParam String Command);
}
package com.example.fuzzbackendmaster.service;
import javax.servlet.http.HttpServletResponse;
public interface KittyAgentService {
void downloadPcap(String fileName, byte[] file,HttpServletResponse response);
}
package com.example.fuzzbackendmaster.service.impl;
import com.example.fuzzbackendmaster.service.KittyAgentService;
import com.example.fuzzbackendmaster.utils.FileTools;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
@Service
public class KittyAgentServiceImpl implements KittyAgentService {
@Override
public void downloadPcap(String fileName, byte[] file, HttpServletResponse response) {
FileTools fileTools = new FileTools();
fileTools.downloadFile(fileName,file,response);
}
}
......@@ -20,4 +20,4 @@ spring:
server-addr: http://${nacos-docker.ip}:8848
nacos-docker:
ip: 192.168.50.251
ip: 192.168.2.247
......@@ -8,7 +8,7 @@
var ws1 = null;
var ws2 = null;
function myFunction() {
ws1 = new WebSocket("ws://192.168.50.251:8101/websocket/testResult/web");
ws1 = new WebSocket("ws://192.168.2.247:8101/websocket/testResult/web");
ws1.onmessage = function (evt) {
console.log(evt);
var received_msg =JSON.parse(evt.data) ;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment