Merge remote-tracking branch 'origin/master' into parse-pcap
# Conflicts: # src/main/java/ru/serega6531/packmate/model/enums/SubscriptionMessageType.java
This commit is contained in:
2
frontend
2
frontend
Submodule frontend updated: 4e6a685e69...28169642b4
@@ -23,13 +23,14 @@ public class PatternController {
|
|||||||
return service.findAll();
|
return service.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@PostMapping("/{id}")
|
||||||
public void deletePattern(@PathVariable int id) {
|
public void enable(@PathVariable int id, @RequestParam boolean enabled) {
|
||||||
service.deleteById(id);
|
service.enable(id, enabled);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public Pattern addPattern(@RequestBody Pattern pattern) {
|
public Pattern addPattern(@RequestBody Pattern pattern) {
|
||||||
|
pattern.setEnabled(true);
|
||||||
return service.save(pattern);
|
return service.save(pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,6 @@ public class CtfService {
|
|||||||
|
|
||||||
private boolean mergeAdjacentPackets;
|
private boolean mergeAdjacentPackets;
|
||||||
|
|
||||||
private boolean inflateWebSockets;
|
private boolean parseWebSockets;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -53,7 +53,7 @@ public class Packet {
|
|||||||
|
|
||||||
private boolean ungzipped;
|
private boolean ungzipped;
|
||||||
|
|
||||||
private boolean webSocketInflated;
|
private boolean webSocketParsed;
|
||||||
|
|
||||||
private byte[] content;
|
private byte[] content;
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,8 @@ public class Pattern {
|
|||||||
@GeneratedValue(generator = "pattern_generator")
|
@GeneratedValue(generator = "pattern_generator")
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
|
private boolean enabled;
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
private String value;
|
private String value;
|
||||||
|
|||||||
@@ -5,5 +5,6 @@ public enum SubscriptionMessageType {
|
|||||||
DELETE_SERVICE, DELETE_PATTERN,
|
DELETE_SERVICE, DELETE_PATTERN,
|
||||||
NEW_STREAM,
|
NEW_STREAM,
|
||||||
COUNTERS_UPDATE,
|
COUNTERS_UPDATE,
|
||||||
|
ENABLE_PATTERN, DISABLE_PATTERN,
|
||||||
PCAP_STARTED, PCAP_STOPPED
|
PCAP_STARTED, PCAP_STOPPED
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,10 +3,8 @@ package ru.serega6531.packmate.service;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
import ru.serega6531.packmate.model.FoundPattern;
|
import ru.serega6531.packmate.model.FoundPattern;
|
||||||
import ru.serega6531.packmate.model.Pattern;
|
import ru.serega6531.packmate.model.Pattern;
|
||||||
import ru.serega6531.packmate.model.Stream;
|
|
||||||
import ru.serega6531.packmate.model.enums.PatternDirectionType;
|
import ru.serega6531.packmate.model.enums.PatternDirectionType;
|
||||||
import ru.serega6531.packmate.model.enums.PatternSearchType;
|
import ru.serega6531.packmate.model.enums.PatternSearchType;
|
||||||
import ru.serega6531.packmate.model.enums.SubscriptionMessageType;
|
import ru.serega6531.packmate.model.enums.SubscriptionMessageType;
|
||||||
@@ -46,34 +44,32 @@ public class PatternService {
|
|||||||
|
|
||||||
public Set<FoundPattern> findMatches(byte[] bytes, boolean incoming) {
|
public Set<FoundPattern> findMatches(byte[] bytes, boolean incoming) {
|
||||||
final List<Pattern> list = patterns.values().stream()
|
final List<Pattern> list = patterns.values().stream()
|
||||||
|
.filter(Pattern::isEnabled)
|
||||||
.filter(p -> p.getDirectionType() == (incoming ? PatternDirectionType.INPUT : PatternDirectionType.OUTPUT)
|
.filter(p -> p.getDirectionType() == (incoming ? PatternDirectionType.INPUT : PatternDirectionType.OUTPUT)
|
||||||
|| p.getDirectionType() == PatternDirectionType.BOTH)
|
|| p.getDirectionType() == PatternDirectionType.BOTH)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
return new PatternMatcher(bytes, list).findMatches();
|
return new PatternMatcher(bytes, list).findMatches();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
public void enable(int id, boolean enabled) {
|
||||||
public void deleteById(int id) {
|
final Pattern pattern = find(id);
|
||||||
final Optional<Pattern> optional = repository.findById(id);
|
if (pattern != null) {
|
||||||
if (optional.isPresent()) {
|
pattern.setEnabled(enabled);
|
||||||
final Pattern pattern = optional.get();
|
final Pattern saved = repository.save(pattern);
|
||||||
log.info("Removed pattern {} with value {}", pattern.getName(), pattern.getValue());
|
patterns.put(id, saved);
|
||||||
|
|
||||||
for (Stream stream : pattern.getMatchedStreams()) {
|
if (enabled) {
|
||||||
stream.getFoundPatterns().remove(pattern);
|
log.info("Включен паттерн {} со значением {}", pattern.getName(), pattern.getValue());
|
||||||
stream.getPackets().forEach(p ->
|
subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.ENABLE_PATTERN, id));
|
||||||
p.getMatches().removeIf(m ->
|
} else {
|
||||||
m.getPatternId() == pattern.getId()));
|
log.info("Выключен паттерн {} со значением {}", pattern.getName(), pattern.getValue());
|
||||||
|
subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.DISABLE_PATTERN, id));
|
||||||
}
|
}
|
||||||
|
|
||||||
patterns.remove(id);
|
|
||||||
repository.delete(pattern);
|
|
||||||
subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.DELETE_PATTERN, id));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Pattern save(Pattern pattern) {
|
public Pattern save(Pattern pattern) {
|
||||||
if(pattern.getSearchType() == PatternSearchType.REGEX) {
|
if (pattern.getSearchType() == PatternSearchType.REGEX) {
|
||||||
try {
|
try {
|
||||||
PatternMatcher.compilePattern(pattern);
|
PatternMatcher.compilePattern(pattern);
|
||||||
} catch (PatternSyntaxException e) {
|
} catch (PatternSyntaxException e) {
|
||||||
|
|||||||
@@ -36,8 +36,8 @@ public class StreamOptimizer {
|
|||||||
unpackGzip();
|
unpackGzip();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (service.isInflateWebSockets()) {
|
if (service.isParseWebSockets()) {
|
||||||
inflateWebSocket();
|
parseWebSockets();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (service.isUrldecodeHttpRequests()) {
|
if (service.isUrldecodeHttpRequests()) {
|
||||||
@@ -89,7 +89,7 @@ public class StreamOptimizer {
|
|||||||
final List<Packet> cut = packets.subList(start, end);
|
final List<Packet> cut = packets.subList(start, end);
|
||||||
final long timestamp = cut.get(0).getTimestamp();
|
final long timestamp = cut.get(0).getTimestamp();
|
||||||
final boolean ungzipped = cut.stream().anyMatch(Packet::isUngzipped);
|
final boolean ungzipped = cut.stream().anyMatch(Packet::isUngzipped);
|
||||||
final boolean webSocketInflated = cut.stream().anyMatch(Packet::isWebSocketInflated);
|
final boolean webSocketParsed = cut.stream().anyMatch(Packet::isWebSocketParsed);
|
||||||
boolean incoming = cut.get(0).isIncoming();
|
boolean incoming = cut.get(0).isIncoming();
|
||||||
//noinspection OptionalGetWithoutIsPresent
|
//noinspection OptionalGetWithoutIsPresent
|
||||||
final byte[] content = cut.stream()
|
final byte[] content = cut.stream()
|
||||||
@@ -102,7 +102,7 @@ public class StreamOptimizer {
|
|||||||
.incoming(incoming)
|
.incoming(incoming)
|
||||||
.timestamp(timestamp)
|
.timestamp(timestamp)
|
||||||
.ungzipped(ungzipped)
|
.ungzipped(ungzipped)
|
||||||
.webSocketInflated(webSocketInflated)
|
.webSocketParsed(webSocketParsed)
|
||||||
.content(content)
|
.content(content)
|
||||||
.build());
|
.build());
|
||||||
}
|
}
|
||||||
@@ -227,7 +227,7 @@ public class StreamOptimizer {
|
|||||||
.incoming(false)
|
.incoming(false)
|
||||||
.timestamp(cut.get(0).getTimestamp())
|
.timestamp(cut.get(0).getTimestamp())
|
||||||
.ungzipped(true)
|
.ungzipped(true)
|
||||||
.webSocketInflated(false)
|
.webSocketParsed(false)
|
||||||
.content(newContent)
|
.content(newContent)
|
||||||
.build();
|
.build();
|
||||||
} catch (ZipException e) {
|
} catch (ZipException e) {
|
||||||
@@ -239,7 +239,7 @@ public class StreamOptimizer {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void inflateWebSocket() {
|
private void parseWebSockets() {
|
||||||
if (!packets.get(0).getContentString().contains("HTTP/")) {
|
if (!packets.get(0).getContentString().contains("HTTP/")) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,8 +31,6 @@ public class WebSocketsParser {
|
|||||||
private static final java.util.regex.Pattern WEBSOCKET_ACCEPT_PATTERN =
|
private static final java.util.regex.Pattern WEBSOCKET_ACCEPT_PATTERN =
|
||||||
java.util.regex.Pattern.compile("Sec-WebSocket-Accept: (.+)\\r\\n");
|
java.util.regex.Pattern.compile("Sec-WebSocket-Accept: (.+)\\r\\n");
|
||||||
|
|
||||||
private static final String WEBSOCKET_EXTENSION_HEADER = "Sec-WebSocket-Extension: permessage-deflate";
|
|
||||||
private static final String WEBSOCKET_EXTENSIONS_HEADER = "Sec-WebSocket-Extensions: permessage-deflate";
|
|
||||||
private static final String WEBSOCKET_UPGRADE_HEADER = "upgrade: websocket\r\n";
|
private static final String WEBSOCKET_UPGRADE_HEADER = "upgrade: websocket\r\n";
|
||||||
private static final String WEBSOCKET_CONNECTION_HEADER = "connection: upgrade\r\n";
|
private static final String WEBSOCKET_CONNECTION_HEADER = "connection: upgrade\r\n";
|
||||||
|
|
||||||
@@ -103,7 +101,6 @@ public class WebSocketsParser {
|
|||||||
final List<Packet> handshakes = packets.subList(0, httpEnd);
|
final List<Packet> handshakes = packets.subList(0, httpEnd);
|
||||||
|
|
||||||
parse(wsPackets, handshakes, draft);
|
parse(wsPackets, handshakes, draft);
|
||||||
parsed = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void parse(final List<Packet> wsPackets, final List<Packet> handshakes, Draft_6455 draft) {
|
private void parse(final List<Packet> wsPackets, final List<Packet> handshakes, Draft_6455 draft) {
|
||||||
@@ -136,12 +133,14 @@ public class WebSocketsParser {
|
|||||||
.timestamp(lastPacket.getTimestamp())
|
.timestamp(lastPacket.getTimestamp())
|
||||||
.ttl(lastPacket.getTtl())
|
.ttl(lastPacket.getTtl())
|
||||||
.ungzipped(lastPacket.isUngzipped())
|
.ungzipped(lastPacket.isUngzipped())
|
||||||
.webSocketInflated(true)
|
.webSocketParsed(true)
|
||||||
.build()
|
.build()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parsed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Packet> getParsedPackets() {
|
public List<Packet> getParsedPackets() {
|
||||||
@@ -190,11 +189,6 @@ public class WebSocketsParser {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!handshake.contains(WEBSOCKET_EXTENSION_HEADER) &&
|
|
||||||
!handshake.contains(WEBSOCKET_EXTENSIONS_HEADER)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return handshake;
|
return handshake;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,10 +206,10 @@ public class WebSocketsParser {
|
|||||||
String key = matcher.group(1);
|
String key = matcher.group(1);
|
||||||
|
|
||||||
matcher = WEBSOCKET_EXTENSIONS_PATTERN.matcher(clientHandshake);
|
matcher = WEBSOCKET_EXTENSIONS_PATTERN.matcher(clientHandshake);
|
||||||
if (!matcher.find()) {
|
String extensions = null;
|
||||||
return null;
|
if (matcher.find()) {
|
||||||
|
extensions = matcher.group(1);
|
||||||
}
|
}
|
||||||
String extensions = matcher.group(1);
|
|
||||||
|
|
||||||
HandshakeImpl1Client clientHandshakeImpl = new HandshakeImpl1Client();
|
HandshakeImpl1Client clientHandshakeImpl = new HandshakeImpl1Client();
|
||||||
|
|
||||||
@@ -223,7 +217,10 @@ public class WebSocketsParser {
|
|||||||
clientHandshakeImpl.put("Connection", "Upgrade");
|
clientHandshakeImpl.put("Connection", "Upgrade");
|
||||||
clientHandshakeImpl.put("Sec-WebSocket-Version", version);
|
clientHandshakeImpl.put("Sec-WebSocket-Version", version);
|
||||||
clientHandshakeImpl.put("Sec-WebSocket-Key", key);
|
clientHandshakeImpl.put("Sec-WebSocket-Key", key);
|
||||||
|
|
||||||
|
if(extensions != null) {
|
||||||
clientHandshakeImpl.put("Sec-WebSocket-Extensions", extensions);
|
clientHandshakeImpl.put("Sec-WebSocket-Extensions", extensions);
|
||||||
|
}
|
||||||
|
|
||||||
return clientHandshakeImpl;
|
return clientHandshakeImpl;
|
||||||
}
|
}
|
||||||
@@ -236,10 +233,10 @@ public class WebSocketsParser {
|
|||||||
String accept = matcher.group(1);
|
String accept = matcher.group(1);
|
||||||
|
|
||||||
matcher = WEBSOCKET_EXTENSIONS_PATTERN.matcher(serverHandshake);
|
matcher = WEBSOCKET_EXTENSIONS_PATTERN.matcher(serverHandshake);
|
||||||
if (!matcher.find()) {
|
String extensions = null;
|
||||||
return null;
|
if (matcher.find()) {
|
||||||
|
extensions = matcher.group(1);
|
||||||
}
|
}
|
||||||
String extensions = matcher.group(1);
|
|
||||||
|
|
||||||
HandshakeImpl1Server serverHandshakeImpl = new HandshakeImpl1Server();
|
HandshakeImpl1Server serverHandshakeImpl = new HandshakeImpl1Server();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user