Commit 3786ab39 by 钱炳权

kitty全流程贯通

parent 6c96d5bf
......@@ -71,20 +71,7 @@ public class aroundAop implements Ordered {
}
if ("generation".equals(function)) {
/*运行前处理*/
/*存入kitty启动时的任务信息*/
try {
// int missionId = GlobalClass.missionInfoMapper.selectTopMissionId() + 1;
// SystemRunningParams.kittyMissionId = missionId;
// MissionInfo missionInfo = new MissionInfo(missionId, TableClassEnum.KITTY_RESULT.getTableId(), new Date(), SystemRunningParams.kittyData.get("missionName"),
// MissionStateEnum.RUNNING.getStateCode(), 0L);
// GlobalClass.missionInfoMapper.insertMission(missionInfo);
} catch (Exception e) {
e.printStackTrace();
throw new MysqlException("Kitty start backup failed!");
}
SystemRunningParams.testTimeMessage.get("kitty").put("start", System.currentTimeMillis());
System.out.println("**************1**********");
System.out.println(SystemRunningParams.testTimeMessage.get("kitty").put("start", System.currentTimeMillis()));
/*放行方法*/
try {
result = point.proceed();
......
......@@ -31,11 +31,11 @@ public class TestMissionController {
@RequestMapping(value = "/startmission", method = RequestMethod.GET)
public AjaxResult startmission(@RequestParam int id) {
try {
testMissionService.startMission(id);
return testMissionService.startMission(id);
} catch (Exception e) {
return AjaxResult.error("测试启动失败!");
}
return AjaxResult.success("测试启动成功!");
}
/**
* 测试信息获取
......
......@@ -37,21 +37,6 @@ public class KittyController {
VulnerabilityTypeService vulnerabilityTypeService;
/**
* 模板
*/
@RequestMapping(value = "/protocolTemplate", method = RequestMethod.POST)
public AjaxResult protocolTemplate(@RequestBody TestEntity testEntity) {
try {
SystemRunningParams.kittyData.put("missionName",testEntity.getTestClassName());
Map<String, List<String>> result = protocolTemplateService.generation(testEntity);
return AjaxResult.success(result == null ? "模板文件生成未成功运行!第三方接口可能存在问题。" : result);
} catch (CmdException | FuzzException e) {
log.error(e.getDefaultMessage());
return AjaxResult.error("模板生成失败!系统存在问题。");
}
}
/**
* 生成方法
*/
@RequestMapping(value = "/generate", method = RequestMethod.POST)
......
package com.example.fuzzControll.service;
import com.example.fuzzControll.domain.bo.TestEntity;
import com.example.fuzzControll.domain.po.MissionInfo;
import java.util.List;
import java.util.Map;
public interface ProtocolTemplateService {
Map<String,List<String>> generation(TestEntity testEntity);
Map<String,List<String>> generation(TestEntity testEntity, MissionInfo missionInfo);
}
......@@ -6,6 +6,7 @@ import com.example.fuzzControll.domain.bo.FuzzParams;
import com.example.fuzzControll.domain.bo.TestEntity;
import com.example.fuzzControll.domain.po.Test;
import com.example.fuzzControll.domain.po.TestAndParams;
import com.example.fuzzControll.domain.vo.AjaxResult;
import java.util.List;
......@@ -22,5 +23,5 @@ public interface TestMissionService {
boolean editTestName(String testName,int id);
void startMission(int id) ;
AjaxResult startMission(int id) ;
}
......@@ -2,6 +2,7 @@ package com.example.fuzzControll.service.impl;
import com.example.fuzzControll.domain.vo.AflnetResult;
import com.example.fuzzControll.domain.bo.FuzzLogTransEntity;
import com.example.fuzzControll.domain.vo.KittyPackageFile;
import com.example.fuzzControll.domain.vo.KittyResult;
import com.example.fuzzControll.domain.po.MissionInfo;
import com.example.fuzzControll.exception.mysqlException.MysqlException;
......@@ -51,28 +52,28 @@ public class FuzzLogServiceImpl implements FuzzLogService {
public List<FuzzLogTransEntity> downloadKittyProtocalFile(MissionInfo missionInfo) {
/*获取结果日志*/
List<FuzzLogTransEntity> fuzzLogList = new ArrayList<FuzzLogTransEntity>();
// KittyResult kittyResult = kittyMapper.getKittyResultByMissionId(missionInfo.getMissionId());
// StringBuffer buffer = new StringBuffer();
// buffer.append(kittyResult.getResultError()).append(System.lineSeparator()).append(kittyResult.getResultOut());
// FuzzLogTransEntity fuzzLogTransEntity = new FuzzLogTransEntity(buffer.toString().getBytes(), missionInfo.getMissionName() + ".txt");
// fuzzLogList.add(fuzzLogTransEntity);
KittyResult kittyResult = kittyMapper.getKittyResultByMissionId(missionInfo.getId());
StringBuffer buffer = new StringBuffer();
buffer.append(kittyResult.getResultError()).append(System.lineSeparator()).append(kittyResult.getResultOut());
FuzzLogTransEntity fuzzLogTransEntity = new FuzzLogTransEntity(buffer.toString().getBytes(), missionInfo.getMissionName() + ".txt");
fuzzLogList.add(fuzzLogTransEntity);
/*获取数据包文件*/
// KittyPackageFile kittyPackageFile = kittyMapper.getKittyPackageFileById(missionInfo.getMissionId());
// FuzzLogTransEntity fuzzLogTransEntitySend = new FuzzLogTransEntity(kittyPackageFile.getKittySendFile(), kittyPackageFile.getKittyRecvFileName());
// FuzzLogTransEntity fuzzLogTransEntityRecv = new FuzzLogTransEntity(kittyPackageFile.getKittyRecvFile(), kittyPackageFile.getKittyRecvFileName());
// fuzzLogList.add(fuzzLogTransEntitySend);
// fuzzLogList.add(fuzzLogTransEntityRecv);
KittyPackageFile kittyPackageFile = kittyMapper.getKittyPackageFileById(missionInfo.getId());
FuzzLogTransEntity fuzzLogTransEntitySend = new FuzzLogTransEntity(kittyPackageFile.getKittySendFile(), kittyPackageFile.getKittyRecvFileName());
FuzzLogTransEntity fuzzLogTransEntityRecv = new FuzzLogTransEntity(kittyPackageFile.getKittyRecvFile(), kittyPackageFile.getKittyRecvFileName());
fuzzLogList.add(fuzzLogTransEntitySend);
fuzzLogList.add(fuzzLogTransEntityRecv);
return fuzzLogList;
}
@Transactional(rollbackFor = MysqlException.class)
public List<FuzzLogTransEntity> downloadKittyOtherMethodFile(MissionInfo missionInfo) {
List<FuzzLogTransEntity> fuzzLogList = new ArrayList<FuzzLogTransEntity>();
// KittyResult kittyResult = kittyMapper.getKittyResultByMissionId(missionInfo.getMissionId());
// StringBuffer buffer = new StringBuffer();
// buffer.append(kittyResult.getResultError()).append(System.lineSeparator()).append(kittyResult.getResultOut());
// FuzzLogTransEntity fuzzLogTransEntity = new FuzzLogTransEntity(buffer.toString().getBytes(), missionInfo.getMissionName() + ".txt");
// fuzzLogList.add(fuzzLogTransEntity);
KittyResult kittyResult = kittyMapper.getKittyResultByMissionId(missionInfo.getId());
StringBuffer buffer = new StringBuffer();
buffer.append(kittyResult.getResultError()).append(System.lineSeparator()).append(kittyResult.getResultOut());
FuzzLogTransEntity fuzzLogTransEntity = new FuzzLogTransEntity(buffer.toString().getBytes(), missionInfo.getMissionName() + ".txt");
fuzzLogList.add(fuzzLogTransEntity);
return fuzzLogList;
}
......
......@@ -44,7 +44,7 @@ public class GenerateMethodServiceImpl implements GenerateMethodService {
if (cmd.isEmpty()) {
throw new FuzzException("cmd is null ! The number of parameters does not match!");
}
return cmdTools.runProgramCmdAndResult(cmd,"generate","Generate-"+testEntity.getTestClassName());
return null;
}
public String parseParameters(TestEntity testEntity) {
......
......@@ -46,7 +46,8 @@ class MutationServiceImpl implements MutationService {
if (cmd.isEmpty()) {
throw new FuzzException("cmd is null ! The number of parameters does not match!");
}
return cmdTools.runProgramCmdAndResult(cmd, "mutation", "Mutation-" + testEntity.getTestClassName());
// cmdTools.runProgramCmdAndResult(cmd, "mutation", "Mutation-" + testEntity.getTestClassName());
return null;
}
public String parseParameters(TestEntity testEntity) {
......
......@@ -6,8 +6,10 @@ import com.example.fuzzControll.annotion.NeedCutAround;
import com.example.fuzzControll.annotion.NeedCutBefore;
import com.example.fuzzControll.conf.KittyProperties;
import com.example.fuzzControll.constents.CmdConstent;
import com.example.fuzzControll.constents.MissionStateEnum;
import com.example.fuzzControll.constents.ProtocolConstent;
import com.example.fuzzControll.domain.bo.FuzzParams;
import com.example.fuzzControll.domain.po.MissionInfo;
import com.example.fuzzControll.domain.po.TestAndParams;
import com.example.fuzzControll.exception.testException.AflnetException;
import com.example.fuzzControll.exception.testException.CmdException;
......@@ -15,8 +17,8 @@ import com.example.fuzzControll.exception.testException.FuzzException;
import com.example.fuzzControll.domain.bo.TestEntity;
import com.example.fuzzControll.service.FuzzParamsService;
import com.example.fuzzControll.service.ProtocolTemplateService;
import com.example.fuzzControll.service.TestMissionService;
import com.example.fuzzControll.tools.system.GlobalClass;
import com.example.fuzzControll.tools.system.SystemRunningParams;
import com.example.fuzzControll.tools.test.SingleCmdTools;
import com.example.fuzzControll.tools.test.TestCmdTools;
import com.example.fuzzControll.tools.test.TestTools;
......@@ -37,25 +39,28 @@ public class ProtocolTemplateImpl implements ProtocolTemplateService {
KittyProperties kitty;
@Autowired
FuzzParamsService fuzzParamsService;
@Autowired
TestMissionService testMissionService;
@Override
@NeedCutAround(name = "kitty", function = "generation")
public Map<String, List<String>> generation(TestEntity testEntity) throws FuzzException, CmdException {
/*存入参数*/
// int missionId = GlobalClass.missionInfoMapper.selectTopMissionId();
// boolean flag = fuzzParamsService.saveFuzzParams(new FuzzParams(JSON.toJSONString(testEntity.getParamJson()), new Date(), missionId));
// if (!flag) {
// throw new AflnetException("Save params error!");
// }
public Map<String, List<String>> generation(TestEntity testEntity, MissionInfo missionInfo) throws FuzzException, CmdException {
Long startTime = System.currentTimeMillis();
/*生成日志前先清除日志*/
singleCmdTools.runCmd(CmdConstent.DELETE_FILE + GlobalClass.kittyProperties.getLogOutPath(), "delete kittyLogs");
String cmd = parseParameters(testEntity);
if (cmd == null || "".equals(cmd)) {
throw new FuzzException("cmd is null ! The number of parameters does not match!");
}
return testCmdTools.runProgramCmdAndResult(cmd, "protocolTemplate", "ProtocolTemplate-" + testEntity.getTestClassName());
/*运行测试*/
Map<String, List<String>> result = testCmdTools.runProgramCmdAndResult(cmd, "protocolTemplate", "ProtocolTemplate-" + testEntity.getTestClassName(),missionInfo);
/*运行完成后存入运行时间等信息*/
Long total = System.currentTimeMillis() - startTime;
missionInfo.setState(MissionStateEnum.DONE.getStateCode());
missionInfo.setRunTime(total);
try {
GlobalClass.missionInfoMapper.updateMission(missionInfo);//更新该次任务的执行时间;
} catch (Exception e) {
log.error("Update mission failed:[{}]", e.getMessage());
}
return result;
}
public String parseParameters(TestEntity testEntity) throws FuzzException {
......
......@@ -12,10 +12,13 @@ import com.example.fuzzControll.domain.po.Test;
import com.example.fuzzControll.domain.po.TestAndParams;
import com.example.fuzzControll.domain.vo.AjaxResult;
import com.example.fuzzControll.exception.mysqlException.MysqlException;
import com.example.fuzzControll.exception.testException.CmdException;
import com.example.fuzzControll.exception.testException.FuzzException;
import com.example.fuzzControll.exception.threadException.MyExceptionHandler;
import com.example.fuzzControll.mapper.FuzzParamsMapper;
import com.example.fuzzControll.mapper.MissionInfoMapper;
import com.example.fuzzControll.mapper.TestMapper;
import com.example.fuzzControll.service.ProtocolTemplateService;
import com.example.fuzzControll.service.TestMissionService;
import com.example.fuzzControll.service.TestService;
import com.example.fuzzControll.tools.component.StringUtils;
......@@ -42,7 +45,8 @@ public class TestMissionServiceImpl implements TestMissionService {
MissionInfoMapper missionInfoMapper;
@Autowired
TestService service;
@Autowired
ProtocolTemplateService protocolTemplateService;
@Override
public List<TestAndParams> getTestAndParamsList() {
List<TestAndParams> testParamsRelevantList = new ArrayList();
......@@ -125,13 +129,13 @@ public class TestMissionServiceImpl implements TestMissionService {
Test test = testMapper.getTestById(id);
int fuzzParamsId;
if (test.getFuzzParamsId() == 0) {//为0则插入id
FuzzParams fuzzParams = new FuzzParams(JSON.toJSONString(testEntity.getParamJson()), testEntity.getTestClassName());
FuzzParams fuzzParams = new FuzzParams(JSON.toJSONString(testEntity), testEntity.getTestClassName());
fuzzParamsMapper.insertFuzzParamsReturnId(fuzzParams);
fuzzParamsId = fuzzParams.getId();
test.setFuzzParamsId(fuzzParamsId);
} else {//有则更新数据
FuzzParams fuzzParams = new FuzzParams(JSON.toJSONString(testEntity.getParamJson()), testEntity.getTestClassName());
if (!fuzzParamsMapper.editFuzzParams(fuzzParams.getId(), fuzzParams.getParams(), fuzzParams.getProtocol())) {
FuzzParams fuzzParams = new FuzzParams(JSON.toJSONString(testEntity), testEntity.getTestClassName());
if (!fuzzParamsMapper.editFuzzParams(test.getFuzzParamsId(), fuzzParams.getParams(), fuzzParams.getProtocol())) {
throw new RuntimeException("EditFuzzParams failed!");
}
}
......@@ -158,7 +162,7 @@ public class TestMissionServiceImpl implements TestMissionService {
@Transactional(rollbackFor = RuntimeException.class)
@Override
public void startMission(int id) {
public AjaxResult startMission(int id) {
Boolean isAflnet = false;
Test test = testMapper.getTestById(id);
if (test.getFuzzParamsId() == 0) {
......@@ -207,9 +211,11 @@ public class TestMissionServiceImpl implements TestMissionService {
/*运行程序*/
try {
if (isAflnet) {// 启动alfnet
aflnetMissionStart(fuzzParams,missionInfo);
aflnetMissionStart(fuzzParams, missionInfo);
return AjaxResult.success("启动成功!");
} else {
Map<String, List<String>> result = kittyMissionStart(fuzzParams, missionInfo);
return AjaxResult.success(result);
}
} catch (Exception e) {
log.error("Mission start error:[{}]", e.getMessage());
......@@ -218,10 +224,36 @@ public class TestMissionServiceImpl implements TestMissionService {
}
/**
* 启动kitty任务
*/
private Map<String, List<String>> kittyMissionStart(FuzzParams fuzzParams, MissionInfo missionInfo) {
TestEntity testEntity = (TestEntity) JSONObject.parseObject(fuzzParams.getParams(), TestEntity.class);
try {
try {
missionInfo.setState(MissionStateEnum.RUNNING.getStateCode());
missionInfoMapper.updateMission(missionInfo);
} catch (Exception e) {
log.error("Error updating mission:[{}]", e.getMessage());
throw new RuntimeException(e);
}
SystemRunningParams.kittyData.put("missionName", testEntity.getTestClassName());
Map<String, List<String>> result = protocolTemplateService.generation(testEntity,missionInfo);
if (result == null) {
log.error("模板文件生成未成功运行!第三方接口可能存在问题。");
throw new RuntimeException();
}
return result;
} catch (CmdException | FuzzException e) {
log.error("Run error:[{}]",e.getMessage());
throw new RuntimeException();
}
}
/**
* 启动aflnet任务
*/
private void aflnetMissionStart(FuzzParams fuzzParams,MissionInfo missionInfo) {
CmdStartParams cmdStartParams = (CmdStartParams)JSONObject.parseObject(fuzzParams.getParams(), CmdStartParams.class);
private void aflnetMissionStart(FuzzParams fuzzParams, MissionInfo missionInfo) {
CmdStartParams cmdStartParams = (CmdStartParams) JSONObject.parseObject(fuzzParams.getParams(), CmdStartParams.class);
try {
SystemRunningParams.aflnetData.put("missionName", cmdStartParams.getProtopcol());
Thread subThread = new Thread(new Runnable() {
......@@ -231,10 +263,10 @@ public class TestMissionServiceImpl implements TestMissionService {
missionInfo.setState(MissionStateEnum.RUNNING.getStateCode());
missionInfoMapper.updateMission(missionInfo);
} catch (Exception e) {
log.error("Error updating mission:[{}]",e.getMessage());
log.error("Error updating mission:[{}]", e.getMessage());
throw new RuntimeException(e);
}
service.testStart(cmdStartParams,missionInfo);
service.testStart(cmdStartParams, missionInfo);
}
});
SystemRunningParams.ThreadState.put(subThread, "start");
......
......@@ -29,8 +29,9 @@ public class VulnerabilityTypeServiceImpl implements VulnerabilityTypeService {
KittyProperties kitty;
@Autowired
FuzzParamsService fuzzParamsService;
@Override
@NeedCutAround(name ="kitty",function = "generation")
@NeedCutAround(name = "kitty", function = "generation")
public Map<String, List<String>> generation(TestEntity testEntity) throws FuzzException, CmdException {
/*存入参数*/
// int missionId = GlobalClass.missionInfoMapper.selectTopMissionId();
......@@ -38,11 +39,11 @@ public class VulnerabilityTypeServiceImpl implements VulnerabilityTypeService {
// if (!flag) {
// throw new AflnetException("Save params error!");
// }
String cmd = parseParameters(testEntity);
if (cmd.isEmpty()) {
throw new FuzzException("cmd is null ! The number of parameters does not match!");
}
return cmdTools.runProgramCmdAndResult(cmd,"vulnerability","Vulnerability-"+testEntity.getTestClassName());
// String cmd = parseParameters(testEntity);
// if (cmd.isEmpty()) {
// throw new FuzzException("cmd is null ! The number of parameters does not match!");
// }
return null;
}
public String parseParameters(TestEntity testEntity) {
......
......@@ -136,7 +136,7 @@ public class TestCmdTools {
* 运行需要后台运行cmd
* 将数据存入文件中
*/
public Map<String, List<String>> runProgramCmdAndResult(String cmd, String caller, String missionName) throws
public Map<String, List<String>> runProgramCmdAndResult(String cmd, String caller, String missionName, MissionInfo missionInfo) throws
CmdException {
Map<String, List<String>> result = new HashMap();
List<String> out = Collections.synchronizedList(new ArrayList<String>());
......@@ -161,7 +161,7 @@ public class TestCmdTools {
new Thread(new Runnable() {
@Override
public void run() {
dataBackUpTransaction(caller, finalOut, finalError, missionName);
dataBackUpTransaction(caller, finalOut, finalError, missionName, missionInfo);
}
}).start();
//todo 需要传入任务参数,后期再开发
......@@ -426,23 +426,14 @@ public class TestCmdTools {
}
@Transactional(rollbackFor = MysqlException.class)
public void dataBackUpTransaction(String caller, List<String> out, List<String> error, String missionName) {
int missionId = SystemRunningParams.kittyMissionId;
public void dataBackUpTransaction(String caller, List<String> out, List<String> error, String missionName, MissionInfo missionInfo) {
try {
/*kitty结果存入数据库*/
KittyResult kittyResult = new KittyResult(missionId, out.toString(), error.toString());
KittyResult kittyResult = new KittyResult(missionInfo.getId(), out.toString(), error.toString());
GlobalClass.kittyMapper.kittyResultsBackup(kittyResult);
} catch (Exception e) {
e.printStackTrace();
log.error("kitty backup error!");
}
// /*kitty的协议模板方法的日志和数据包文件存入数据库*/
// if (caller.equals("protocolTemplate")) {
//// GlobalClass.kittyFuzzPersistenceService.KittyPackagesBackup(missionId);
// GlobalClass.missionInfoMapper.insertMission(new MissionInfo(missionId, 2, new Date(), missionName,));
// } else {
// /*存入missionInfo信息*/
// GlobalClass.missionInfoMapper.insertMission(new MissionInfo(missionId, 3, new Date(), missionName));
// }
}
}
\ No newline at end of file
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