SUPPORT-8836:fixes
This commit is contained in:
parent
a3d0971f6f
commit
5aa56f344e
1 changed files with 23 additions and 65 deletions
|
|
@ -2,15 +2,9 @@ package ru.micord.av.service.service;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
@ -33,16 +27,9 @@ public class FileScanService {
|
||||||
|
|
||||||
public ScanResult scanFile(MultipartFile file) {
|
public ScanResult scanFile(MultipartFile file) {
|
||||||
File tempFile = null;
|
File tempFile = null;
|
||||||
LocalDateTime startTime;
|
|
||||||
LocalDateTime stopTime;
|
|
||||||
try {
|
try {
|
||||||
tempFile = saveFile(file);
|
tempFile = saveFile(file);
|
||||||
startTime = LocalDateTime.now();
|
return runKeslScan(tempFile);
|
||||||
String rawResult = runKeslScan(tempFile);
|
|
||||||
stopTime = LocalDateTime.now();
|
|
||||||
return parseKeslResponse(rawResult, startTime, stopTime, tempFile,
|
|
||||||
file.getOriginalFilename()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
throw new AvException("Error scanning file: " + file.getOriginalFilename(), e);
|
throw new AvException("Error scanning file: " + file.getOriginalFilename(), e);
|
||||||
|
|
@ -69,64 +56,35 @@ public class FileScanService {
|
||||||
return tempFile.toFile();
|
return tempFile.toFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String runKeslScan(File file) throws IOException, InterruptedException {
|
private ScanResult runKeslScan(File file) throws IOException, InterruptedException {
|
||||||
ProcessBuilder processBuilder = new ProcessBuilder(KESL_CONTROL, KESL_SCAN,
|
ProcessBuilder processBuilder = new ProcessBuilder(KESL_CONTROL, KESL_SCAN,
|
||||||
file.getAbsolutePath()
|
file.getAbsolutePath()
|
||||||
);
|
);
|
||||||
processBuilder.redirectErrorStream(true);
|
processBuilder.redirectErrorStream(true);
|
||||||
Process process = processBuilder.start();
|
Process process = processBuilder.start();
|
||||||
try (InputStream inputStream = process.getInputStream()) {
|
|
||||||
String result = new String(inputStream.readAllBytes());
|
|
||||||
int exitCode = process.waitFor();
|
int exitCode = process.waitFor();
|
||||||
if (exitCode != 0 && exitCode != 72) {
|
if (exitCode == 0) {
|
||||||
|
return new ScanResult(
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
100,
|
||||||
|
null,
|
||||||
|
"completed",
|
||||||
|
new String[] {ScanResult.Scan.VERDICT_CLEAN}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else if (exitCode == 72) {
|
||||||
|
return new ScanResult(
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
100,
|
||||||
|
null,
|
||||||
|
"completed",
|
||||||
|
new String[] {ScanResult.Scan.VERDICT_INFECTED}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else {
|
||||||
throw new AvException("KESL error, exit code: " + exitCode);
|
throw new AvException("KESL error, exit code: " + exitCode);
|
||||||
}
|
}
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private ScanResult parseKeslResponse(String rawResult, LocalDateTime startTime,
|
|
||||||
LocalDateTime stopTime, File file, String originalFilename) {
|
|
||||||
Map<String, ScanResult.Scan> scanResults = new HashMap<>();
|
|
||||||
List<String> verdicts = new ArrayList<>();
|
|
||||||
List<ScanResult.Scan.Threat> threats = new ArrayList<>();
|
|
||||||
String verdict = null;
|
|
||||||
|
|
||||||
for (String line : rawResult.split("\n")) {
|
|
||||||
if (line.startsWith("Infected objects")) {
|
|
||||||
verdict = extractInteger(line) > 0
|
|
||||||
? ScanResult.Scan.VERDICT_INFECTED
|
|
||||||
: ScanResult.Scan.VERDICT_CLEAN;
|
|
||||||
if (ScanResult.Scan.VERDICT_INFECTED.equals(verdict))
|
|
||||||
threats.add(new ScanResult.Scan.Threat("", file.getAbsolutePath()));
|
|
||||||
}
|
|
||||||
if (line.startsWith("Scan errors") && extractInteger(line) > 0) {
|
|
||||||
verdict = "error";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (verdict != null) {
|
|
||||||
scanResults.put(originalFilename,
|
|
||||||
new ScanResult.Scan(startTime.toString(), stopTime.toString(),
|
|
||||||
threats.toArray(new ScanResult.Scan.Threat[0]), verdict
|
|
||||||
)
|
|
||||||
);
|
|
||||||
verdicts.add(verdict);
|
|
||||||
}
|
|
||||||
return new ScanResult(startTime.toString(), stopTime.toString(), 100, scanResults, "completed",
|
|
||||||
verdicts.toArray(new String[0])
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private int extractInteger(String line) {
|
|
||||||
String[] parts = line.split(":");
|
|
||||||
if (parts.length > 1) {
|
|
||||||
try {
|
|
||||||
return Integer.parseInt(parts[1].trim());
|
|
||||||
}
|
|
||||||
catch (NumberFormatException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue