Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ This library is not yet at a 1:1 parity with the original [rapid7/recog](https:/
Missing features:

- Matching against multi-line input strings
- Matching against base64 encoded strings
- Command line tools like `recog_match`

## Development
Expand Down
14 changes: 12 additions & 2 deletions recog/src/main/java/com/rapid7/recog/FingerprintExample.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
package com.rapid7.recog;

import java.util.Base64;
import java.util.Map;
import static java.util.Objects.requireNonNull;

// Represents a fingerprint example and associated data.
public class FingerprintExample {
private static final String ENCODING_KEY = "_encoding";

private final String text;
private final Map<String, String> attributeMap;

public FingerprintExample(String text, Map<String, String> attributeMap) {
this.text = text;
this.attributeMap = attributeMap;
String tmpText = requireNonNull(text);
this.attributeMap = requireNonNull(attributeMap);
if (attributeMap.containsKey(ENCODING_KEY) && attributeMap.get(ENCODING_KEY).equals("base64")) {
byte[] exampleContentBytes = Base64.getDecoder().decode(tmpText.replaceAll("\\s+", ""));
this.text = new String(exampleContentBytes);
} else {
this.text = text;
}
}

public String getText() {
Expand Down
2 changes: 1 addition & 1 deletion recog/src/main/java/com/rapid7/recog/RecogMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public String getPattern() {
* @return A {@link Pattern} with the compiled flags provided. Will not be {@code null}.
*/
public static Pattern pattern(String regex, int... flags) {
int patternFlags = 0;
int patternFlags = Pattern.UNIX_LINES;
for (int flag : flags)
patternFlags |= flag;

Expand Down
28 changes: 11 additions & 17 deletions recog/src/main/java/com/rapid7/recog/parser/RecogParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,23 +177,18 @@ public RecogMatchers parse(Reader reader, String path, String name)
NodeList examples = fingerprint.getElementsByTagName("example");
for (int examplesIndex = 0; examplesIndex < examples.getLength(); examplesIndex++) {
Element example = (Element) examples.item(examplesIndex);
String exampleContent = example.getTextContent();

if ("base64".equals(example.getAttribute("_encoding"))) {
// TODO: these are currently ignored as the Base64 decoding isn't working properly
} else {
HashMap<String, String> attributeMap = new HashMap<>();
NamedNodeMap exAttributes = example.getAttributes();

for (int i = 0; i < exAttributes.getLength(); i++) {
Node attr = exAttributes.item(i);
String attrName = attr.getNodeName();
String attrValue = attr.getNodeValue();
attributeMap.put(attrName, attrValue);
}
HashMap<String, String> attributeMap = new HashMap<>();
NamedNodeMap exAttributes = example.getAttributes();

fingerprintPattern.addExample(new FingerprintExample(exampleContent, attributeMap));
for (int i = 0; i < exAttributes.getLength(); i++) {
Node attr = exAttributes.item(i);
String attrName = attr.getNodeName();
String attrValue = attr.getNodeValue();
attributeMap.put(attrName, attrValue);
}

fingerprintPattern.addExample(new FingerprintExample(example.getTextContent(), attributeMap));
}

// parse and add parameter specifications
Expand Down Expand Up @@ -234,7 +229,7 @@ public RecogMatchers parse(Reader reader, String path, String name)
/////////////////////////////////////////////////////////////////////////

private int parseFlags(String flags) {
int cflags = 0;
int cflags = Pattern.UNIX_LINES;
if (flags != null && flags.length() != 0) {
StringTokenizer tok = new StringTokenizer(flags, "|,; \t");
while (tok.hasMoreTokens()) {
Expand All @@ -244,9 +239,8 @@ private int parseFlags(String flags) {
cflags |= Pattern.CASE_INSENSITIVE;
break;
case "REG_DOT_NEWLINE":
cflags |= Pattern.DOTALL;
break;
case "REG_MULTILINE":
cflags |= Pattern.DOTALL;
cflags |= Pattern.MULTILINE;
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void twoValidFingerprints() throws ParseException {
assertThat(patterns.size(), is(2));
assertThat(patterns, hasItems(
new RecogMatcher(pattern("^Apache/\\d$", CASE_INSENSITIVE)).addValue("service.vendor", "Apache").addValue("service.product", "HTTPD").addValue("service.family", "Apache"),
new RecogMatcher(pattern("^Apache$", MULTILINE)).addValue("service.vendor", "Apache").addValue("service.product", "HTTPD").addValue("service.family", "Apache")));
new RecogMatcher(pattern("^Apache$", DOTALL, MULTILINE)).addValue("service.vendor", "Apache").addValue("service.product", "HTTPD").addValue("service.family", "Apache")));
}

@Test
Expand Down