Commit 2b6461e2 by 钱炳权

ssh至服务器并成功调用服务

parent 336793c0
package com.example.fuzzControll.constents;
public class AlgorithmOneCmd {
public static final String ALFNET_PATH = "/home/lddc/SHENYANLONG/SMGfuzz/afl-fuzz ";//aflnet服务启动路径
public static final String ALFNET_INPUT_PATH=" -i /home/lddc/SHENYANLONG";//aflnet种子根路径
public static final String ALFNET_OUTPUT_PATH=" -o /home/lddc/SHENYANLONG";//aflnet种子根路径
}
......@@ -4,9 +4,8 @@ import com.example.fuzzControll.exception.AflnetException;
import com.example.fuzzControll.exception.BaseException;
import com.example.fuzzControll.exception.CmdException;
import com.example.fuzzControll.pojo.vo.AjaxResult;
import com.example.fuzzControll.pojo.vo.CmdStartParams;
import com.example.fuzzControll.pojo.vo.AlgorithmOneCmdParams;
import com.example.fuzzControll.service.TestService;
import com.example.fuzzControll.tools.GlobalParameters;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
......@@ -25,17 +24,16 @@ public class AlfnetController {
TestService service;
/**
* 测试启动
* 算法1 有参数型
*/
@RequestMapping(value = "/testStart", method = RequestMethod.POST)
public AjaxResult start(@RequestBody final CmdStartParams cmdStartParams) {
System.out.println(cmdStartParams);
//todo 捕获子线程错误
@RequestMapping(value = "/algorithmOneStart", method = RequestMethod.POST)
public AjaxResult algorithmOneStart(@RequestBody final AlgorithmOneCmdParams algorithmOneCmdParams) {
System.out.println(algorithmOneCmdParams);
try {
new Thread(new Runnable() {
@Override
public void run() {
service.testStart(cmdStartParams);
service.algorithmOneStart(algorithmOneCmdParams);
}
}).start();
} catch (Exception e) {
......@@ -48,6 +46,28 @@ public class AlfnetController {
}
/**
* 算法2 无参数型
*/
@RequestMapping(value = "/algorithmTwoStart", method = RequestMethod.POST)
public AjaxResult algorithmTwoStart(@RequestBody final AlgorithmOneCmdParams algorithmOneCmdParams) {
System.out.println(algorithmOneCmdParams);
//todo 捕获子线程错误
try {
new Thread(new Runnable() {
@Override
public void run() {
service.algorithmTwoStart(algorithmOneCmdParams);
}
}).start();
} catch (Exception e) {
if (e instanceof AflnetException || e instanceof CmdException) {
log.error(((BaseException) e).getDefaultMessage());
}
return AjaxResult.error("测试启动失败!");
}
return AjaxResult.success("测试已启动!");
}
/**
* 测试停止
*/
@RequestMapping(value = "/testStop", method = RequestMethod.GET)
......
package com.example.fuzzControll.pojo.vo;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
@Data
@Getter
@Setter
public class AlgorithmOneCmdParams {
String netinfo; //netInfo
String protopcol; //protocol
int waiting; //usec
String nsname;//netnsname
Boolean kill;
Boolean awareMode;
Boolean regionMutationOperators;
Boolean falseNegativeMode;
String script;//script
int stateSelectionAlgo;
int seedSelectionAlgo;
String trainedModelInputPath;
@Override
public String toString() {
return "AlgorithmTwoCmdParams{" +
"netinfo='" + netinfo + '\'' +
", protopcol='" + protopcol + '\'' +
", waiting=" + waiting +
", nsname='" + nsname + '\'' +
", kill=" + kill +
", awareMode=" + awareMode +
", regionMutationOperators=" + regionMutationOperators +
", falseNegativeMode=" + falseNegativeMode +
", script='" + script + '\'' +
", stateSelectionAlgo=" + stateSelectionAlgo +
", seedSelectionAlgo=" + seedSelectionAlgo +
", trainedModelInputPath='" + trainedModelInputPath + '\'' +
'}';
}
}
......@@ -7,7 +7,7 @@ import lombok.Setter;
@Data
@Getter
@Setter
public class CmdStartParams {
public class AlgorithmTwoCmdParams {
String netinfo; //netInfo
String protopcol; //protocol
int waiting; //usec
......
package com.example.fuzzControll.service;
import com.example.fuzzControll.pojo.vo.CmdStartParams;
import java.util.concurrent.CountDownLatch;
import com.example.fuzzControll.pojo.vo.AlgorithmOneCmdParams;
public interface TestService {
void testStart(CmdStartParams cmdStartParams);
void algorithmOneStart(AlgorithmOneCmdParams algorithmOneCmdParams);
void algorithmTwoStart(AlgorithmOneCmdParams algorithmOneCmdParams);
void testStop();
}
package com.example.fuzzControll.service.impl;
import com.example.fuzzControll.conf.AflnetProperties;
import com.example.fuzzControll.constents.AlgorithmOneCmd;
import com.example.fuzzControll.constents.CmdConstent;
import com.example.fuzzControll.exception.AflnetException;
import com.example.fuzzControll.exception.CmdException;
import com.example.fuzzControll.pojo.vo.CmdStartParams;
import com.example.fuzzControll.pojo.vo.AlgorithmOneCmdParams;
import com.example.fuzzControll.service.TestService;
import com.example.fuzzControll.tools.CmdTools;
import com.example.fuzzControll.tools.GlobalParameters;
import com.example.fuzzControll.tools.TestControlTools;
import com.jcraft.jsch.JSchException;
import lombok.extern.slf4j.Slf4j;
......@@ -18,7 +17,6 @@ import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service("testService")
@Slf4j
......@@ -29,18 +27,143 @@ public class TestServiceImpl implements TestService {
@Autowired
AflnetProperties aflnetProperties;
@Override
public void algorithmOneStart(AlgorithmOneCmdParams algorithmOneCmdParams) {
TestControlTools.setIsRunning(true);
String finalCmd = getAlgorithmOneFinalCmd(algorithmOneCmdParams);
if (finalCmd == null) {
log.error("AlgorithmOne Cmd is null!");
throw new AflnetException("Cmd Generate error!");
}
log.info("The cmd is [{}]", finalCmd);
try {
cmdTools.runProgramCmd(finalCmd);
} catch (JSchException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private String getAlgorithmOneFinalCmd(AlgorithmOneCmdParams algorithmOneCmdParams) {
switch (algorithmOneCmdParams.getProtopcol().toLowerCase()) {
case "dns":
return dnsCmd(algorithmOneCmdParams);
case "sip":
return sipCmd(algorithmOneCmdParams);
case "ftp":
return ftpCmd(algorithmOneCmdParams);
case "rtsp":
return rtspCmd(algorithmOneCmdParams);
default:
return null;
//todo 增加其他协议 的格式
}
}
private String dnsCmd(AlgorithmOneCmdParams algorithmOneCmdParams) {
// String before = AlgorithmOneCmd.ALFNET_PATH + " -d " + AlgorithmOneCmd.ALFNET_INPUT_PATH + "/dnsmasq/in-dns "
// +AlgorithmOneCmd.ALFNET_OUTPUT_PATH+"/dnsmasq/src/output_dnsmasq_RLGfuzz"+" -z "+algorithmOneCmdParams.getTrainedModelInputPath()
// +" -N "+algorithmOneCmdParams.getNetinfo()+" -m none "+" -P "+algorithmOneCmdParams.getProtopcol()+" -D "
// +algorithmOneCmdParams.getWaiting();
// String after = " -q "+algorithmOneCmdParams.getStateSelectionAlgo()+" -s "+algorithmOneCmdParams.getSeedSelectionAlgo()+" -r '4' /home/lddc/SHENYANLONG/dnsmasq/src/dnsmasq";
// String finalCmd = before;
// if(algorithmOneCmdParams.getKill()){
// finalCmd = finalCmd+" -K ";
// }
// if(algorithmOneCmdParams.getAwareMode())
// finalCmd = finalCmd+" -E ";
// if(algorithmOneCmdParams.getRegionMutationOperators())
// finalCmd = finalCmd+" -R ";
// if(!"".equals(algorithmOneCmdParams.getScript()))
// finalCmd = finalCmd+" -c "+algorithmOneCmdParams.getScript();
// if(!"".equals(algorithmOneCmdParams.getTrainedModelInputPath()))
// finalCmd = finalCmd+" -c "+algorithmOneCmdParams.getScript();
// finalCmd = finalCmd+after;
// return finalCmd;
return "/home/lddc/SHENYANLONG/SMGfuzz/afl-fuzz -d -i /home/lddc/SHENYANLONG/dnsmasq/in-dns -o /home/lddc/SHENYANLONG/dnsmasq/src/output_dnsmasq_RLGfuzz -z /home/lddc/SHENYANLONG/SMGfuzz/RL_Pytorch/Train_Result/dnsmasq/RL_Result -N udp://127.0.0.1/5353 -m none -P DNS -D 10000 -K -E -R -q 4 -s 4 -r '4' /home/lddc/SHENYANLONG/dnsmasq/src/dnsmasq";
}
private String sipCmd(AlgorithmOneCmdParams algorithmOneCmdParams) {
// String before = AlgorithmOneCmd.ALFNET_PATH + AlgorithmOneCmd.ALFNET_INPUT_PATH + "/kamailio/in-sip "
// +AlgorithmOneCmd.ALFNET_OUTPUT_PATH+"/kamailio/output_kamailio_RLGfuzz"+" -z "+algorithmOneCmdParams.getTrainedModelInputPath()
// +" -N "+algorithmOneCmdParams.getNetinfo()+" -m none -t 3000+ "+" -P "+algorithmOneCmdParams.getProtopcol()+" -l 5061 "+" -D "
// +algorithmOneCmdParams.getWaiting()+" -d ";
// String after = " -q "+algorithmOneCmdParams.getStateSelectionAlgo()+" -s "+algorithmOneCmdParams.getSeedSelectionAlgo()
// +" /home/lddc/SHENYANLONG/kamailio/src/kamailio -f /home/lddc/SHENYANLONG/kamailio/kamailio-basic.cfg -L /home/lddc/SHENYANLONG/kamailio/src/modules -n 1 -Y /home/lddc/SHENYANLONG/kamailio/runtime_dir";
// String finalCmd = before;
// if(algorithmOneCmdParams.getKill()){
// finalCmd = finalCmd+" -K ";
// }
// if(algorithmOneCmdParams.getAwareMode())
// finalCmd = finalCmd+" -E ";
// if(algorithmOneCmdParams.getRegionMutationOperators())
// finalCmd = finalCmd+" -R ";
// if(!"".equals(algorithmOneCmdParams.getScript()))
// finalCmd = finalCmd+" -c "+algorithmOneCmdParams.getScript();
// finalCmd = finalCmd+after;
// return finalCmd;
return "/home/lddc/SHENYANLONG/SMGfuzz/afl-fuzz -i /home/lddc/SHENYANLONG/kamailio/in-sip -o /home/lddc/SHENYANLONG/kamailio/output_kamailio_RLGfuzz -z /home/lddc/SHENYANLONG/SMGfuzz/RL_Pytorch/Train_Result/kamailio/RL_Result -N udp://127.0.0.1/5060 -m none -t 3000+ -P SIP -l 5061 -D 50000 -q 4 -s 4 -E -K -R -d -c /home/lddc/SHENYANLONG/kamailio/run_pjsip.sh /home/lddc/SHENYANLONG/kamailio/src/kamailio -f /home/lddc/SHENYANLONG/kamailio/kamailio-basic.cfg -L /home/lddc/SHENYANLONG/kamailio/src/modules -n 1 -Y /home/lddc/SHENYANLONG/kamailio/runtime_dir";
}
private String ftpCmd(AlgorithmOneCmdParams algorithmOneCmdParams) {
// String before = AlgorithmOneCmd.ALFNET_PATH + AlgorithmOneCmd.ALFNET_INPUT_PATH + "/SMGfuzz/tutorials/lightftp/in-ftp "
// +AlgorithmOneCmd.ALFNET_OUTPUT_PATH+"/LightFTP/Source/Release/output_lightftp_RLGfuzz"+" -z "+algorithmOneCmdParams.getTrainedModelInputPath()
// +" -N "+algorithmOneCmdParams.getNetinfo()+" -m none "+" -P "+algorithmOneCmdParams.getProtopcol()+" -l 5061 "+" -D "
// +algorithmOneCmdParams.getWaiting()+" -d ";
// String after = " -q "+algorithmOneCmdParams.getStateSelectionAlgo()+" -s "+algorithmOneCmdParams.getSeedSelectionAlgo()
// +" /home/lddc/SHENYANLONG/kamailio/src/kamailio -f /home/lddc/SHENYANLONG/kamailio/kamailio-basic.cfg -L /home/lddc/SHENYANLONG/kamailio/src/modules -n 1 -Y /home/lddc/SHENYANLONG/kamailio/runtime_dir";
// String finalCmd = before;
// if(algorithmOneCmdParams.getKill()){
// finalCmd = finalCmd+" -K ";
// }
// if(algorithmOneCmdParams.getAwareMode())
// finalCmd = finalCmd+" -E ";
// if(algorithmOneCmdParams.getRegionMutationOperators())
// finalCmd = finalCmd+" -R ";
// if(!"".equals(algorithmOneCmdParams.getScript()))
// finalCmd = finalCmd+" -c "+algorithmOneCmdParams.getScript();
// finalCmd = finalCmd+after;
// return finalCmd;
return "/home/lddc/SHENYANLONG/SMGfuzz/afl-fuzz -m none -i /home/lddc/SHENYANLONG/SMGfuzz/tutorials/lightftp/in-ftp -o /home/lddc/SHENYANLONG/LightFTP/Source/Release/output_lightftp_RLGfuzz -N tcp://127.0.0.1/2200 -z /home/lddc/SHENYANLONG/SMGfuzz/RL_Pytorch/Train_Result/lightftp/RL_Result -x /home/lddc/SHENYANLONG/SMGfuzz/tutorials/lightftp/ftp.dict -P FTP -D 10000 -q 4 -s 4 -E -K -R -d -c /home/lddc/SHENYANLONG/LightFTP/Source/Release/ftpclean.sh /home/lddc/SHENYANLONG/LightFTP/Source/Release/fftp /home/lddc/SHENYANLONG/LightFTP/Source/Release/fftp.conf 2200";
}
private String rtspCmd(AlgorithmOneCmdParams algorithmOneCmdParams) {
// String before = AlgorithmOneCmd.ALFNET_PATH + AlgorithmOneCmd.ALFNET_INPUT_PATH + "/kamailio/in-sip "
// +AlgorithmOneCmd.ALFNET_OUTPUT_PATH+"/kamailio/output_kamailio_RLGfuzz"+" -z "+algorithmOneCmdParams.getTrainedModelInputPath()
// +" -N "+algorithmOneCmdParams.getNetinfo()+" -m none -t 3000+ "+" -P "+algorithmOneCmdParams.getProtopcol()+" -l 5061 "+" -D "
// +algorithmOneCmdParams.getWaiting()+" -d ";
// String after = " -q "+algorithmOneCmdParams.getStateSelectionAlgo()+" -s "+algorithmOneCmdParams.getSeedSelectionAlgo()
// +" /home/lddc/SHENYANLONG/kamailio/src/kamailio -f /home/lddc/SHENYANLONG/kamailio/kamailio-basic.cfg -L /home/lddc/SHENYANLONG/kamailio/src/modules -n 1 -Y /home/lddc/SHENYANLONG/kamailio/runtime_dir";
// String finalCmd = before;
// if(algorithmOneCmdParams.getKill()){
// finalCmd = finalCmd+" -K ";
// }
// if(algorithmOneCmdParams.getAwareMode())
// finalCmd = finalCmd+" -E ";
// if(algorithmOneCmdParams.getRegionMutationOperators())
// finalCmd = finalCmd+" -R ";
// if(!"".equals(algorithmOneCmdParams.getScript()))
// finalCmd = finalCmd+" -c "+algorithmOneCmdParams.getScript();
// finalCmd = finalCmd+after;
// return finalCmd;
return "/home/lddc/SHENYANLONG/SMGfuzz/afl-fuzz -i /home/lddc/SHENYANLONG/SMGfuzz/tutorials/live555/in-rtsp -o /home/lddc/SHENYANLONG/live555/testProgs/output_live555_RLGfuzz -z /home/lddc/SHENYANLONG/SMGfuzz/RL_Pytorch/Train_Result/live555/RL_Result -N tcp://127.0.0.1/8554 -x /home/lddc/SHENYANLONG/SMGfuzz/tutorials/live555/rtsp.dict -m none -D 10000 -K -E -R -q 4 -s 4 -d /home/lddc/SHENYANLONG/live555/testProgs/testOnDemandRTSPServer 85540";
}
//todo 不同服务不同端口
@Override
public void testStart(CmdStartParams cmdStartParams) {
public void algorithmTwoStart(AlgorithmOneCmdParams algorithmOneCmdParams) {
TestControlTools.setIsRunning(true);
String cmd = cmdTools.parse(cmdStartParams);
String cmd = cmdTools.parse(algorithmOneCmdParams);
Date date = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss-");
String outputFileName = df.format(date)+cmdStartParams.getProtopcol()+"-output";
String outputFileName = df.format(date) + algorithmOneCmdParams.getProtopcol() + "-output";
String finalCmd = CmdConstent.RUN_AFLNET_BEFORE + outputFileName + CmdConstent.RUN_AFLNET_AFTER + cmd + aflnetProperties.getAflnetPath() + "live555/testProgs/testOnDemandRTSPServer 8554";
log.info("The cmd is [{}]",finalCmd);
log.info("The cmd is [{}]", finalCmd);
try {
cmdTools.runProgramCmd("source /etc/profile && "+finalCmd);
cmdTools.runProgramCmd("source /etc/profile && " + finalCmd);
} catch (JSchException e) {
throw new RuntimeException(e);
} catch (IOException e) {
......@@ -49,7 +172,6 @@ public class TestServiceImpl implements TestService {
}
@Override
public void testStop() {
TestControlTools.setIsRunning(false);
......
package com.example.fuzzControll.tools;
import com.alibaba.fastjson.JSONObject;
import com.example.fuzzControll.conf.AflnetSshProperties;
import com.example.fuzzControll.exception.AflnetException;
import com.example.fuzzControll.exception.CmdException;
import com.example.fuzzControll.pojo.vo.*;
import com.jcraft.jsch.JSchException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
//todo need modify
......@@ -88,46 +85,46 @@ public class CmdTools {
}
public String parse(CmdStartParams cmdStartParams) throws AflnetException {
if (cmdStartParams == null) {
public String parse(AlgorithmOneCmdParams algorithmOneCmdParams) throws AflnetException {
if (algorithmOneCmdParams == null) {
throw new AflnetException("CmdStartParams is null !");
}
StringBuilder cmd = new StringBuilder();
if (cmdStartParams.getNetinfo() != "" && cmdStartParams.getNetinfo() != null) {
cmd.append(" -N " + cmdStartParams.getNetinfo());
if (algorithmOneCmdParams.getNetinfo() != "" && algorithmOneCmdParams.getNetinfo() != null) {
cmd.append(" -N " + algorithmOneCmdParams.getNetinfo());
} else {
log.error("There is no netinfo !");
throw new AflnetException("There is no netinfo !");
}
if (cmdStartParams.getProtopcol() != "") {
cmd.append(" -P " + cmdStartParams.getProtopcol());
if (algorithmOneCmdParams.getProtopcol() != "") {
cmd.append(" -P " + algorithmOneCmdParams.getProtopcol());
}
if (cmdStartParams.getWaiting() != 0) {
cmd.append(" -D " + cmdStartParams.getWaiting());
if (algorithmOneCmdParams.getWaiting() != 0) {
cmd.append(" -D " + algorithmOneCmdParams.getWaiting());
}
if (cmdStartParams.getNsname() != "") {
cmd.append(" -e " + cmdStartParams.getNsname());
if (algorithmOneCmdParams.getNsname() != "") {
cmd.append(" -e " + algorithmOneCmdParams.getNsname());
}
if (cmdStartParams.getKill()) {
if (algorithmOneCmdParams.getKill()) {
cmd.append(" -K ");
}
if (cmdStartParams.getAwareMode()) {
if (algorithmOneCmdParams.getAwareMode()) {
cmd.append(" -E ");
}
if (cmdStartParams.getRegionMutationOperators()) {
if (algorithmOneCmdParams.getRegionMutationOperators()) {
cmd.append(" -R ");
}
if (cmdStartParams.getFalseNegativeMode()) {
if (algorithmOneCmdParams.getFalseNegativeMode()) {
cmd.append(" -F ");
}
if (cmdStartParams.getScript() != "") {
cmd.append(" -c " + cmdStartParams.getScript());
if (algorithmOneCmdParams.getScript() != "") {
cmd.append(" -c " + algorithmOneCmdParams.getScript());
}
if (cmdStartParams.getStateSelectionAlgo() != 0) {
cmd.append(" -q " + cmdStartParams.getStateSelectionAlgo());
if (algorithmOneCmdParams.getStateSelectionAlgo() != 0) {
cmd.append(" -q " + algorithmOneCmdParams.getStateSelectionAlgo());
}
if (cmdStartParams.getSeedSelectionAlgo() != 0) {
cmd.append(" -s " + cmdStartParams.getSeedSelectionAlgo() + " ");
if (algorithmOneCmdParams.getSeedSelectionAlgo() != 0) {
cmd.append(" -s " + algorithmOneCmdParams.getSeedSelectionAlgo() + " ");
}
return cmd.toString();
}
......
......@@ -94,14 +94,15 @@ public class LinuxCommandUtil {
TestReturnEntity returnEntity = new TestReturnEntity();
try {
while ((line = bf.readLine()) != null && TestControlTools.getIsRunning()) {
cmdTools.makeReturnEntity(line, returnEntity);
if (cmdTools.send) {
String data = JSONObject.toJSONString(returnEntity);
try {
GlobalParameters.webSocket.appointSending("fuzzMaster", "web", data);
} catch (Exception ignored) {
}
}
System.out.println(line);
// cmdTools.makeReturnEntity(line, returnEntity);
// if (cmdTools.send) {
// String data = JSONObject.toJSONString(returnEntity);
// try {
// GlobalParameters.webSocket.appointSending("fuzzMaster", "web", data);
// } catch (Exception ignored) {
// }
// }
}
//结束就关闭
if(!TestControlTools.getIsRunning()){
......
......@@ -22,7 +22,13 @@ web:
ip: 192.168.9.177
aflnetssh:
host: 192.168.9.177
host: 192.168.31.53
port: 22
user: root
password: 123456
\ No newline at end of file
user: lddc
password: sysulddc
#aflnetssh:
# host: 192.168.31.133
# port: 22
# user: root
# password: 123456
\ 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