19 package org.sleuthkit.autopsy.keywordsearch;
21 import com.google.common.base.CharMatcher;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.logging.Level;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30 import org.apache.commons.lang3.StringUtils;
31 import org.apache.commons.lang3.math.NumberUtils;
32 import org.apache.commons.validator.routines.DomainValidator;
33 import org.apache.solr.client.solrj.SolrQuery;
34 import org.apache.solr.client.solrj.SolrQuery.SortClause;
35 import org.apache.solr.client.solrj.SolrRequest;
36 import org.apache.solr.client.solrj.response.QueryResponse;
37 import org.apache.solr.common.SolrDocument;
38 import org.apache.solr.common.SolrDocumentList;
39 import org.apache.solr.common.params.CursorMarkParams;
40 import org.openide.util.NbBundle;
52 import org.
sleuthkit.datamodel.BlackboardAttribute.ATTRIBUTE_TYPE;
72 final class RegexQuery
implements KeywordSearchQuery {
74 public static final Logger LOGGER = Logger.getLogger(RegexQuery.class.getName());
86 private static final CharSequence[] UNSUPPORTED_CHARS = {
"\\d",
"\\D",
"\\w",
"\\W",
"\\s",
"\\S",
"\\n",
87 "\\t",
"\\r",
"\\f",
"\\a",
"\\e",
"\\v",
"\\V",
"\\h",
"\\H",
"\\p"};
89 private static final int MAX_RESULTS_PER_CURSOR_MARK = 512;
90 private static final int MIN_EMAIL_ADDR_LENGTH = 8;
91 private static final String SNIPPET_DELIMITER = String.valueOf(Character.toChars(171));
103 static final Pattern CREDIT_CARD_NUM_PATTERN
104 = Pattern.compile(
"(?<ccn>[2-6]([ -]?[0-9]){11,18})");
105 static final Pattern CREDIT_CARD_TRACK1_PATTERN = Pattern.compile(
117 +
"(?<accountNumber>[2-6]([ -]?[0-9]){11,18})"
119 +
"(?<name>[^^]{2,26})"
121 +
"(?:(?:\\^|(?<expiration>\\d{4}))"
122 +
"(?:(?:\\^|(?<serviceCode>\\d{3}))"
123 +
"(?:(?<discretionary>[^?]*)"
127 static final Pattern CREDIT_CARD_TRACK2_PATTERN = Pattern.compile(
138 +
"(?<accountNumber>[2-6]([ -]?[0-9]){11,18})"
140 +
"(?:(?<expiration>\\d{4})"
141 +
"(?:(?<serviceCode>\\d{3})"
142 +
"(?:(?<discretionary>[^:;<=>?]*)"
146 static final BlackboardAttribute.Type KEYWORD_SEARCH_DOCUMENT_ID =
new BlackboardAttribute.Type(ATTRIBUTE_TYPE.TSK_KEYWORD_SEARCH_DOCUMENT_ID);
148 private final List<KeywordQueryFilter> filters =
new ArrayList<>();
149 private final KeywordList keywordList;
150 private final Keyword originalKeyword;
151 private final String keywordString;
152 private final boolean queryStringContainsWildcardPrefix;
153 private final boolean queryStringContainsWildcardSuffix;
155 private boolean escaped;
156 private String escapedQuery;
157 private String field = Server.Schema.CONTENT_STR.toString();
165 RegexQuery(KeywordList keywordList, Keyword keyword) {
166 this.keywordList = keywordList;
167 this.originalKeyword = keyword;
168 this.keywordString = keyword.getSearchTerm();
170 this.queryStringContainsWildcardPrefix = this.keywordString.startsWith(
".*");
171 this.queryStringContainsWildcardSuffix = this.keywordString.endsWith(
".*");
175 public KeywordList getKeywordList() {
180 public boolean validate() {
181 if (keywordString.isEmpty()) {
186 Pattern.compile(keywordString, Pattern.UNICODE_CHARACTER_CLASS);
192 for (CharSequence c : UNSUPPORTED_CHARS) {
193 if (keywordString.contains(c)) {
198 }
catch (IllegalArgumentException ex) {
204 public QueryResults performQuery() throws NoOpenCoreException {
206 final Server solrServer = KeywordSearch.getServer();
207 SolrQuery solrQuery =
new SolrQuery();
226 boolean skipWildcardPrefix = queryStringContainsWildcardPrefix || getQueryString().startsWith(
"^");
227 boolean skipWildcardSuffix = queryStringContainsWildcardSuffix
228 || (getQueryString().endsWith(
"$") && (!getQueryString().endsWith(
"\\$")));
244 String queryString = (originalKeyword.searchTermIsLiteral() ? getEscapedQueryString() : getQueryString());
245 double indexSchemaVersion = NumberUtils.toDouble(solrServer.getIndexInfo().getSchemaVersion());
246 if (indexSchemaVersion >= 2.1) {
247 queryString = queryString.toLowerCase();
250 solrQuery.setQuery((field == null ? Server.Schema.CONTENT_STR.toString() : field) +
":/"
251 + (skipWildcardPrefix ?
"" :
".*")
255 + (skipWildcardSuffix ?
"" :
".*") +
"/");
258 solrQuery.setFields(Server.Schema.CONTENT_STR.toString(), Server.Schema.ID.toString(), Server.Schema.CHUNK_SIZE.toString());
261 .map(KeywordQueryFilter::toString)
262 .forEach(solrQuery::addFilterQuery);
264 solrQuery.setRows(MAX_RESULTS_PER_CURSOR_MARK);
266 solrQuery.setSort(SortClause.asc(Server.Schema.ID.toString()));
268 String cursorMark = CursorMarkParams.CURSOR_MARK_START;
269 SolrDocumentList resultList;
270 boolean allResultsProcessed =
false;
271 QueryResults results =
new QueryResults(
this);
273 while (!allResultsProcessed) {
275 solrQuery.set(CursorMarkParams.CURSOR_MARK_PARAM, cursorMark);
276 QueryResponse response = solrServer.query(solrQuery, SolrRequest.METHOD.POST);
277 resultList = response.getResults();
279 for (SolrDocument resultDoc : resultList) {
281 List<KeywordHit> keywordHits = createKeywordHits(resultDoc);
282 for (KeywordHit hit : keywordHits) {
283 Keyword keywordInstance =
new Keyword(hit.getHit(),
true,
true, originalKeyword.getListName(), originalKeyword.getOriginalTerm());
284 List<KeywordHit> hitsForKeyword = results.getResults(keywordInstance);
285 if (hitsForKeyword == null) {
286 hitsForKeyword =
new ArrayList<>();
287 results.addResult(keywordInstance, hitsForKeyword);
289 hitsForKeyword.add(hit);
291 }
catch (TskCoreException ex) {
292 LOGGER.log(Level.SEVERE,
"Error creating keyword hits", ex);
296 String nextCursorMark = response.getNextCursorMark();
297 if (cursorMark.equals(nextCursorMark)) {
298 allResultsProcessed =
true;
300 cursorMark = nextCursorMark;
301 }
catch (KeywordSearchModuleException ex) {
302 LOGGER.log(Level.SEVERE,
"Error executing Regex Solr Query: " + keywordString, ex);
303 MessageNotifyUtil.Notify.error(NbBundle.getMessage(Server.class,
"Server.query.exception.msg", keywordString), ex.getCause().getMessage());
310 private List<KeywordHit> createKeywordHits(SolrDocument solrDoc)
throws TskCoreException {
312 final HashMap<String, String> keywordsFoundInThisDocument =
new HashMap<>();
314 List<KeywordHit> hits =
new ArrayList<>();
315 final String docId = solrDoc.getFieldValue(Server.Schema.ID.toString()).toString();
316 final Integer chunkSize = (Integer) solrDoc.getFieldValue(Server.Schema.CHUNK_SIZE.toString());
318 final Collection<Object> content_str = solrDoc.getFieldValues(Server.Schema.CONTENT_STR.toString());
320 String searchPattern;
321 if (originalKeyword.searchTermIsLiteral()) {
335 searchPattern =
"[\\w[\\.']]*" + Pattern.quote(keywordString.toLowerCase()) +
"[\\w[\\.']]*";
337 searchPattern = keywordString;
340 final Pattern pattern = Pattern.compile(searchPattern, Pattern.CASE_INSENSITIVE);
343 for (Object content_obj : content_str) {
344 String content = (String) content_obj;
345 Matcher hitMatcher = pattern.matcher(content);
348 while (hitMatcher.find(offset)) {
353 if (chunkSize != null && hitMatcher.start() >= chunkSize) {
357 String hit = hitMatcher.group();
362 if (
"".equals(hit)) {
366 offset = hitMatcher.end();
367 final ATTRIBUTE_TYPE artifactAttributeType = originalKeyword.getArtifactAttributeType();
375 if (!queryStringContainsWildcardSuffix
376 && (artifactAttributeType == ATTRIBUTE_TYPE.TSK_PHONE_NUMBER
377 || artifactAttributeType == ATTRIBUTE_TYPE.TSK_IP_ADDRESS)) {
378 if (artifactAttributeType == ATTRIBUTE_TYPE.TSK_PHONE_NUMBER) {
380 hit = hit.replaceAll(
"^[^0-9\\(]",
"");
383 hit = hit.replaceAll(
"^[^0-9]",
"");
386 hit = hit.replaceAll(
"[^0-9]$",
"");
411 if (originalKeyword.searchTermIsLiteral()) {
412 hit = hit.replaceAll(
"^" + KeywordSearchList.BOUNDARY_CHARACTERS +
"*",
"");
413 hit = hit.replaceAll(KeywordSearchList.BOUNDARY_CHARACTERS +
"*$",
"");
428 if (keywordsFoundInThisDocument.containsKey(hit)) {
431 keywordsFoundInThisDocument.put(hit, hit);
433 if (artifactAttributeType == null) {
434 hits.add(
new KeywordHit(docId, makeSnippet(content, hitMatcher, hit), hit));
436 switch (artifactAttributeType) {
443 if (hit.length() >= MIN_EMAIL_ADDR_LENGTH
444 && DomainValidator.getInstance(
true).isValidTld(hit.substring(hit.lastIndexOf(
'.')))) {
445 hits.add(
new KeywordHit(docId, makeSnippet(content, hitMatcher, hit), hit));
449 case TSK_CARD_NUMBER:
455 Matcher ccnMatcher = CREDIT_CARD_NUM_PATTERN.matcher(hit);
457 for (
int rLength = hit.length(); rLength >= 12; rLength--) {
458 ccnMatcher.region(0, rLength);
459 if (ccnMatcher.find()) {
460 final String group = ccnMatcher.group(
"ccn");
461 if (CreditCardValidator.isValidCCN(group)) {
462 hits.add(
new KeywordHit(docId, makeSnippet(content, hitMatcher, hit), hit));
469 hits.add(
new KeywordHit(docId, makeSnippet(content, hitMatcher, hit), hit));
475 }
catch (Throwable error) {
484 throw new TskCoreException(
"Failed to create keyword hits for Solr document id " + docId +
" due to " + error.getMessage());
502 private String makeSnippet(String content, Matcher hitMatcher, String hit) {
504 int maxIndex = content.length() - 1;
505 final int end = hitMatcher.end();
506 final int start = hitMatcher.start();
508 return content.substring(Integer.max(0, start - 20), Integer.max(0, start))
509 + SNIPPET_DELIMITER + hit + SNIPPET_DELIMITER
510 + content.substring(Integer.min(maxIndex, end), Integer.min(maxIndex, end + 20));
514 public void addFilter(KeywordQueryFilter filter) {
515 this.filters.add(filter);
519 public void setField(String field) {
524 public void setSubstringQuery() {
528 synchronized public void escape() {
529 if (isEscaped() ==
false) {
530 escapedQuery = KeywordSearchUtil.escapeLuceneQuery(keywordString);
536 synchronized public boolean isEscaped() {
541 public boolean isLiteral() {
546 public String getQueryString() {
547 return originalKeyword.getSearchTerm();
551 synchronized public String getEscapedQueryString() {
552 if (
false == isEscaped()) {
575 public BlackboardArtifact createKeywordHitArtifact(Content content, Keyword foundKeyword, KeywordHit hit, String snippet, String listName) {
576 final String MODULE_NAME = KeywordSearchModuleFactory.getModuleName();
578 if (content == null) {
579 LOGGER.log(Level.WARNING,
"Error adding artifact for keyword hit to blackboard");
586 if (originalKeyword.getArtifactAttributeType() == ATTRIBUTE_TYPE.TSK_CARD_NUMBER) {
587 createCCNAccount(content, foundKeyword, hit, snippet, listName);
595 Collection<BlackboardAttribute> attributes =
new ArrayList<>();
597 attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_KEYWORD, MODULE_NAME, foundKeyword.getSearchTerm()));
598 attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_KEYWORD_REGEXP, MODULE_NAME, getQueryString()));
600 if (StringUtils.isNotBlank(listName)) {
601 attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_SET_NAME, MODULE_NAME, listName));
603 if (snippet != null) {
604 attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_KEYWORD_PREVIEW, MODULE_NAME, snippet));
607 hit.getArtifactID().ifPresent(artifactID
608 -> attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_ASSOCIATED_ARTIFACT, MODULE_NAME, artifactID))
611 if (originalKeyword.searchTermIsLiteral()) {
612 attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_KEYWORD_SEARCH_TYPE, MODULE_NAME, KeywordSearch.QueryType.SUBSTRING.ordinal()));
614 attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_KEYWORD_SEARCH_TYPE, MODULE_NAME, KeywordSearch.QueryType.REGEX.ordinal()));
618 return content.newAnalysisResult(
619 BlackboardArtifact.Type.TSK_KEYWORD_HIT, Score.SCORE_LIKELY_NOTABLE,
620 null, listName, null, attributes)
621 .getAnalysisResult();
622 }
catch (TskCoreException e) {
623 LOGGER.log(Level.SEVERE,
"Error adding bb attributes for terms search artifact", e);
628 private void createCCNAccount(Content content, Keyword foundKeyword, KeywordHit hit, String snippet, String listName) {
630 final String MODULE_NAME = KeywordSearchModuleFactory.getModuleName();
632 if (originalKeyword.getArtifactAttributeType() != ATTRIBUTE_TYPE.TSK_CARD_NUMBER) {
633 LOGGER.log(Level.SEVERE,
"Keyword hit is not a credit card number");
641 Collection<BlackboardAttribute> attributes =
new ArrayList<>();
643 Map<BlackboardAttribute.Type, BlackboardAttribute> parsedTrackAttributeMap =
new HashMap<>();
644 Matcher matcher = CREDIT_CARD_TRACK1_PATTERN.matcher(hit.getSnippet());
645 if (matcher.find()) {
646 parseTrack1Data(parsedTrackAttributeMap, matcher);
648 matcher = CREDIT_CARD_TRACK2_PATTERN.matcher(hit.getSnippet());
649 if (matcher.find()) {
650 parseTrack2Data(parsedTrackAttributeMap, matcher);
652 final BlackboardAttribute ccnAttribute = parsedTrackAttributeMap.get(
new BlackboardAttribute.Type(ATTRIBUTE_TYPE.TSK_CARD_NUMBER));
653 if (ccnAttribute == null || StringUtils.isBlank(ccnAttribute.getValueString())) {
655 if (hit.isArtifactHit()) {
656 LOGGER.log(Level.SEVERE, String.format(
"Failed to parse credit card account number for artifact keyword hit: term = %s, snippet = '%s', artifact id = %d", foundKeyword.getSearchTerm(), hit.getSnippet(), hit.getArtifactID().get()));
659 LOGGER.log(Level.SEVERE, String.format(
"Failed to parse credit card account number for content keyword hit: term = %s, snippet = '%s', object id = %d", foundKeyword.getSearchTerm(), hit.getSnippet(), hit.getContentID()));
660 }
catch (TskCoreException ex) {
661 LOGGER.log(Level.SEVERE, String.format(
"Failed to parse credit card account number for content keyword hit: term = %s, snippet = '%s' ", foundKeyword.getSearchTerm(), hit.getSnippet()));
662 LOGGER.log(Level.SEVERE,
"There was a error getting contentID for keyword hit.", ex);
667 attributes.addAll(parsedTrackAttributeMap.values());
673 final int bin = Integer.parseInt(ccnAttribute.getValueString().substring(0, 8));
674 CreditCards.BankIdentificationNumber binInfo = CreditCards.getBINInfo(bin);
675 if (binInfo != null) {
676 binInfo.getScheme().ifPresent(scheme
677 -> attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_CARD_SCHEME, MODULE_NAME, scheme)));
678 binInfo.getCardType().ifPresent(cardType
679 -> attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_CARD_TYPE, MODULE_NAME, cardType)));
680 binInfo.getBrand().ifPresent(brand
681 -> attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_BRAND_NAME, MODULE_NAME, brand)));
682 binInfo.getBankName().ifPresent(bankName
683 -> attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_BANK_NAME, MODULE_NAME, bankName)));
684 binInfo.getBankPhoneNumber().ifPresent(phoneNumber
685 -> attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_PHONE_NUMBER, MODULE_NAME, phoneNumber)));
686 binInfo.getBankURL().ifPresent(url
687 -> attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_URL, MODULE_NAME, url)));
688 binInfo.getCountry().ifPresent(country
689 -> attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_COUNTRY, MODULE_NAME, country)));
690 binInfo.getBankCity().ifPresent(city
691 -> attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_CITY, MODULE_NAME, city)));
698 if (content instanceof AbstractFile) {
699 AbstractFile file = (AbstractFile) content;
700 if (file.getType() == TskData.TSK_DB_FILES_TYPE_ENUM.UNUSED_BLOCKS
701 || file.getType() == TskData.TSK_DB_FILES_TYPE_ENUM.UNALLOC_BLOCKS) {
702 attributes.add(
new BlackboardAttribute(KEYWORD_SEARCH_DOCUMENT_ID, MODULE_NAME, hit.getSolrDocumentId()));
706 if (StringUtils.isNotBlank(listName)) {
707 attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_SET_NAME, MODULE_NAME, listName));
709 if (snippet != null) {
710 attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_KEYWORD_PREVIEW, MODULE_NAME, snippet));
713 hit.getArtifactID().ifPresent(artifactID
714 -> attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_ASSOCIATED_ARTIFACT, MODULE_NAME, artifactID))
717 attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_KEYWORD_SEARCH_TYPE, MODULE_NAME, KeywordSearch.QueryType.REGEX.ordinal()));
723 AccountFileInstance ccAccountInstance = Case.getCurrentCaseThrows().getSleuthkitCase().getCommunicationsManager().createAccountFileInstance(Account.Type.CREDIT_CARD, ccnAttribute.getValueString(), MODULE_NAME, content);
725 ccAccountInstance.addAttributes(attributes);
727 }
catch (TskCoreException | NoCurrentCaseException ex) {
728 LOGGER.log(Level.SEVERE,
"Error creating CCN account instance", ex);
742 static private void parseTrack2Data(Map<BlackboardAttribute.Type, BlackboardAttribute> attributesMap, Matcher matcher) {
743 addAttributeIfNotAlreadyCaptured(attributesMap, ATTRIBUTE_TYPE.TSK_CARD_NUMBER,
"accountNumber", matcher);
744 addAttributeIfNotAlreadyCaptured(attributesMap, ATTRIBUTE_TYPE.TSK_CARD_EXPIRATION,
"expiration", matcher);
745 addAttributeIfNotAlreadyCaptured(attributesMap, ATTRIBUTE_TYPE.TSK_CARD_SERVICE_CODE,
"serviceCode", matcher);
746 addAttributeIfNotAlreadyCaptured(attributesMap, ATTRIBUTE_TYPE.TSK_CARD_DISCRETIONARY,
"discretionary", matcher);
747 addAttributeIfNotAlreadyCaptured(attributesMap, ATTRIBUTE_TYPE.TSK_CARD_LRC,
"LRC", matcher);
759 static private void parseTrack1Data(Map<BlackboardAttribute.Type, BlackboardAttribute> attributeMap, Matcher matcher) {
760 parseTrack2Data(attributeMap, matcher);
761 addAttributeIfNotAlreadyCaptured(attributeMap, ATTRIBUTE_TYPE.TSK_NAME_PERSON,
"name", matcher);
776 static private void addAttributeIfNotAlreadyCaptured(Map<BlackboardAttribute.Type, BlackboardAttribute> attributeMap, ATTRIBUTE_TYPE attrType, String groupName, Matcher matcher) {
777 BlackboardAttribute.Type type =
new BlackboardAttribute.Type(attrType);
779 if (!attributeMap.containsKey(type)) {
780 String value = matcher.group(groupName);
781 if (attrType.equals(ATTRIBUTE_TYPE.TSK_CARD_NUMBER)) {
782 attributeMap.put(
new BlackboardAttribute.Type(ATTRIBUTE_TYPE.TSK_KEYWORD),
783 new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_KEYWORD, MODULE_NAME, value));
784 value = CharMatcher.anyOf(
" -").removeFrom(value);
787 if (StringUtils.isNotBlank(value)) {
788 attributeMap.put(type,
new BlackboardAttribute(attrType, MODULE_NAME, value));