Поиск по subbytes

This commit is contained in:
serega6531
2019-11-26 17:13:45 +03:00
parent 0ba3078073
commit 98a5eddd0a
4 changed files with 54 additions and 10 deletions

View File

@@ -27,7 +27,6 @@ dependencies {
implementation 'org.springframework.session:spring-session-core' implementation 'org.springframework.session:spring-session-core'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.7' compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.7'
compile group: 'commons-io', name: 'commons-io', version: '2.6' compile group: 'commons-io', name: 'commons-io', version: '2.6'
compile group: 'com.google.guava', name: 'guava', version: '11.0.2'
compile 'org.pcap4j:pcap4j-core:1.+' compile 'org.pcap4j:pcap4j-core:1.+'
compile 'org.pcap4j:pcap4j-packetfactory-static:1.+' compile 'org.pcap4j:pcap4j-packetfactory-static:1.+'
compileOnly 'org.projectlombok:lombok' compileOnly 'org.projectlombok:lombok'

View File

@@ -4,16 +4,18 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.security.crypto.codec.Hex;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; 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.enums.PatternDirectionType;
import ru.serega6531.packmate.model.Stream; import ru.serega6531.packmate.model.Stream;
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;
import ru.serega6531.packmate.model.pojo.SubscriptionMessage; import ru.serega6531.packmate.model.pojo.SubscriptionMessage;
import ru.serega6531.packmate.repository.PatternRepository; import ru.serega6531.packmate.repository.PatternRepository;
import ru.serega6531.packmate.utils.Bytes;
import java.util.*; import java.util.*;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@@ -51,20 +53,19 @@ public class PatternService {
} }
public Set<FoundPattern> findMatches(byte[] bytes, boolean incoming) { public Set<FoundPattern> findMatches(byte[] bytes, boolean incoming) {
String content = new String(bytes);
return patterns.values().stream() return patterns.values().stream()
.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)
.map(pattern -> match(pattern, content)) .map(pattern -> match(pattern, bytes))
.flatMap(List::stream) .flatMap(List::stream)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
} }
private List<FoundPattern> match(Pattern pattern, String content) { private List<FoundPattern> match(Pattern pattern, byte[] bytes) {
List<FoundPattern> found = new ArrayList<>(); List<FoundPattern> found = new ArrayList<>();
if (pattern.getSearchType() == PatternSearchType.REGEX) { if (pattern.getSearchType() == PatternSearchType.REGEX) {
String content = new String(bytes);
final java.util.regex.Pattern regex = compilePattern(pattern); final java.util.regex.Pattern regex = compilePattern(pattern);
final Matcher matcher = regex.matcher(content); final Matcher matcher = regex.matcher(content);
@@ -78,9 +79,10 @@ public class PatternService {
return found; return found;
} else if (pattern.getSearchType() == PatternSearchType.SUBSTRING) { } else if (pattern.getSearchType() == PatternSearchType.SUBSTRING) {
String content = new String(bytes);
int startSearch = 0; int startSearch = 0;
final String value = pattern.getValue(); final String value = pattern.getValue();
while (true) { while (true) {
int start = StringUtils.indexOfIgnoreCase(content, value, startSearch); int start = StringUtils.indexOfIgnoreCase(content, value, startSearch);
@@ -98,9 +100,26 @@ public class PatternService {
startSearch = end + 1; startSearch = end + 1;
} }
} else { // SUBBYTES } else { // SUBBYTES
// TODO int startSearch = 0;
final byte[] value = Hex.decode(pattern.getValue());
while (true) {
int start = Bytes.indexOf(bytes, value, startSearch, bytes.length);
if (start == -1) {
return found; return found;
} }
int end = start + value.length - 1;
found.add(FoundPattern.builder()
.patternId(pattern.getId())
.startPosition(start)
.endPosition(end)
.build());
startSearch = end + 1;
}
}
} }
@Transactional @Transactional

View File

@@ -1,6 +1,5 @@
package ru.serega6531.packmate.service; package ru.serega6531.packmate.service;
import com.google.common.primitives.Bytes;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
@@ -17,6 +16,7 @@ import ru.serega6531.packmate.model.pojo.Pagination;
import ru.serega6531.packmate.model.pojo.SubscriptionMessage; import ru.serega6531.packmate.model.pojo.SubscriptionMessage;
import ru.serega6531.packmate.model.pojo.UnfinishedStream; import ru.serega6531.packmate.model.pojo.UnfinishedStream;
import ru.serega6531.packmate.repository.StreamRepository; import ru.serega6531.packmate.repository.StreamRepository;
import ru.serega6531.packmate.utils.Bytes;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;

View File

@@ -0,0 +1,26 @@
package ru.serega6531.packmate.utils;
public class Bytes {
public static int indexOf(byte[] array, byte[] target) {
return indexOf(array, target, 0, array.length);
}
public static int indexOf(byte[] array, byte[] target, int start, int end) {
if (target.length == 0) {
return 0;
}
outer:
for (int i = start; i < end - target.length + 1; i++) {
for (int j = 0; j < target.length; j++) {
if (array[i + j] != target[j]) {
continue outer;
}
}
return i;
}
return -1;
}
}