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.HashSet;
26 import java.util.List;
29 import java.util.logging.Level;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32 import org.apache.commons.lang.StringUtils;
33 import org.apache.commons.validator.routines.checkdigit.LuhnCheckDigit;
34 import org.apache.solr.client.solrj.SolrQuery;
35 import org.apache.solr.client.solrj.response.TermsResponse.Term;
54 final class TermsComponentQuery
implements KeywordSearchQuery {
56 private static final Logger LOGGER = Logger.getLogger(TermsComponentQuery.class.getName());
57 private static final String MODULE_NAME = KeywordSearchModuleFactory.getModuleName();
58 private static final String SEARCH_HANDLER =
"/terms";
59 private static final String SEARCH_FIELD = Server.Schema.TEXT.toString();
60 private static final int TERMS_SEARCH_TIMEOUT = 90 * 1000;
61 private static final String CASE_INSENSITIVE =
"case_insensitive";
62 private static final boolean DEBUG_FLAG = Version.Type.DEVELOPMENT.equals(Version.getBuildType());
63 private static final int MAX_TERMS_QUERY_RESULTS = 20000;
64 private final KeywordList keywordList;
65 private final Keyword originalKeyword;
66 private String searchTerm;
67 private boolean searchTermIsEscaped;
68 private final List<KeywordQueryFilter> filters =
new ArrayList<>();
75 static final Pattern CREDIT_CARD_NUM_PATTERN = Pattern.compile(
"(?<ccn>[3-6]([ -]?[0-9]){11,18})");
76 static final LuhnCheckDigit CREDIT_CARD_NUM_LUHN_CHECK =
new LuhnCheckDigit();
77 static final Pattern CREDIT_CARD_TRACK1_PATTERN = Pattern.compile(
89 +
"(?<accountNumber>[3-6]([ -]?[0-9]){11,18})"
91 +
"(?<name>[^^]{2,26})"
93 +
"(?:(?:\\^|(?<expiration>\\d{4}))"
94 +
"(?:(?:\\^|(?<serviceCode>\\d{3}))"
95 +
"(?:(?<discretionary>[^?]*)"
99 static final Pattern CREDIT_CARD_TRACK2_PATTERN = Pattern.compile(
110 +
"(?<accountNumber>[3-6]([ -]?[0-9]){11,18})"
112 +
"(?:(?<expiration>\\d{4})"
113 +
"(?:(?<serviceCode>\\d{3})"
114 +
"(?:(?<discretionary>[^:;<=>?]*)"
118 static final BlackboardAttribute.Type KEYWORD_SEARCH_DOCUMENT_ID =
new BlackboardAttribute.Type(ATTRIBUTE_TYPE.TSK_KEYWORD_SEARCH_DOCUMENT_ID);
136 TermsComponentQuery(KeywordList keywordList, Keyword keyword) {
137 this.keywordList = keywordList;
138 this.originalKeyword = keyword;
139 this.searchTerm = keyword.getSearchTerm();
149 public KeywordList getKeywordList() {
160 public String getQueryString() {
161 return originalKeyword.getSearchTerm();
172 public boolean isLiteral() {
181 public void setSubstringQuery() {
182 searchTerm =
".*" + searchTerm +
".*";
189 public void escape() {
190 searchTerm = Pattern.quote(originalKeyword.getSearchTerm());
191 searchTermIsEscaped =
true;
200 public boolean isEscaped() {
201 return searchTermIsEscaped;
211 public String getEscapedQueryString() {
212 return this.searchTerm;
221 public boolean validate() {
222 if (searchTerm.isEmpty()) {
226 Pattern.compile(searchTerm);
228 }
catch (IllegalArgumentException ex) {
240 public void setField(String field) {
250 public void addFilter(KeywordQueryFilter filter) {
251 this.filters.add(filter);
265 public QueryResults performQuery() throws KeywordSearchModuleException, NoOpenCoreException {
270 final SolrQuery termsQuery =
new SolrQuery();
271 termsQuery.setRequestHandler(SEARCH_HANDLER);
272 termsQuery.setTerms(
true);
273 termsQuery.setTermsRegexFlag(CASE_INSENSITIVE);
274 termsQuery.setTermsRegex(searchTerm);
275 termsQuery.addTermsField(SEARCH_FIELD);
276 termsQuery.setTimeAllowed(TERMS_SEARCH_TIMEOUT);
277 termsQuery.setShowDebugInfo(DEBUG_FLAG);
278 termsQuery.setTermsLimit(MAX_TERMS_QUERY_RESULTS);
279 List<Term> terms = KeywordSearch.getServer().queryTerms(termsQuery).getTerms(SEARCH_FIELD);
283 QueryResults results =
new QueryResults(
this);
284 for (Term term : terms) {
289 if (originalKeyword.getArtifactAttributeType() == ATTRIBUTE_TYPE.TSK_CARD_NUMBER) {
290 Matcher matcher = CREDIT_CARD_NUM_PATTERN.matcher(term.getTerm());
292 final String ccn = CharMatcher.anyOf(
" -").removeFrom(matcher.group(
"ccn"));
293 if (
false == CREDIT_CARD_NUM_LUHN_CHECK.isValid(ccn)) {
308 String escapedTerm = KeywordSearchUtil.escapeLuceneQuery(term.getTerm());
309 LuceneQuery termQuery =
new LuceneQuery(keywordList,
new Keyword(escapedTerm,
true,
true));
310 filters.forEach(termQuery::addFilter);
311 QueryResults termQueryResult = termQuery.performQuery();
312 Set<KeywordHit> termHits =
new HashSet<>();
313 for (Keyword word : termQueryResult.getKeywords()) {
314 termHits.addAll(termQueryResult.getResults(word));
316 results.addResult(
new Keyword(term.getTerm(),
false,
true, originalKeyword.getListName(), originalKeyword.getOriginalTerm()),
new ArrayList<>(termHits));
338 public KeywordCachedArtifact writeSingleFileHitsToBlackBoard(Keyword foundKeyword, KeywordHit hit, String snippet, String listName) {
345 BlackboardArtifact newArtifact;
346 Collection<BlackboardAttribute> attributes =
new ArrayList<>();
347 if (originalKeyword.getArtifactAttributeType() != ATTRIBUTE_TYPE.TSK_CARD_NUMBER) {
348 attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_KEYWORD, MODULE_NAME, foundKeyword.getSearchTerm()));
349 attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_KEYWORD_REGEXP, MODULE_NAME, originalKeyword.getSearchTerm()));
352 newArtifact = hit.getContent().newArtifact(ARTIFACT_TYPE.TSK_KEYWORD_HIT);
354 }
catch (TskCoreException ex) {
355 LOGGER.log(Level.SEVERE,
"Error adding artifact for keyword hit to blackboard", ex);
363 attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_ACCOUNT_TYPE, MODULE_NAME, Account.Type.CREDIT_CARD.name()));
364 Map<BlackboardAttribute.Type, BlackboardAttribute> parsedTrackAttributeMap =
new HashMap<>();
365 Matcher matcher = CREDIT_CARD_TRACK1_PATTERN.matcher(hit.getSnippet());
366 if (matcher.find()) {
367 parseTrack1Data(parsedTrackAttributeMap, matcher);
369 matcher = CREDIT_CARD_TRACK2_PATTERN.matcher(hit.getSnippet());
370 if (matcher.find()) {
371 parseTrack2Data(parsedTrackAttributeMap, matcher);
373 final BlackboardAttribute ccnAttribute = parsedTrackAttributeMap.get(
new BlackboardAttribute.Type(ATTRIBUTE_TYPE.TSK_CARD_NUMBER));
374 if (ccnAttribute == null || StringUtils.isBlank(ccnAttribute.getValueString())) {
375 if (hit.isArtifactHit()) {
376 LOGGER.log(Level.SEVERE, String.format(
"Failed to parse credit card account number for artifact keyword hit: term = %s, snippet = '%s', artifact id = %d", searchTerm, hit.getSnippet(), hit.getArtifact().getArtifactID()));
378 LOGGER.log(Level.SEVERE, String.format(
"Failed to parse credit card account number for content keyword hit: term = %s, snippet = '%s', object id = %d", searchTerm, hit.getSnippet(), hit.getContent().getId()));
382 attributes.addAll(parsedTrackAttributeMap.values());
388 final int bin = Integer.parseInt(ccnAttribute.getValueString().substring(0, 8));
389 CreditCards.BankIdentificationNumber binInfo = CreditCards.getBINInfo(bin);
390 if (binInfo != null) {
391 binInfo.getScheme().ifPresent(scheme
392 -> attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_CARD_SCHEME, MODULE_NAME, scheme)));
393 binInfo.getCardType().ifPresent(cardType
394 -> attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_CARD_TYPE, MODULE_NAME, cardType)));
395 binInfo.getBrand().ifPresent(brand
396 -> attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_BRAND_NAME, MODULE_NAME, brand)));
397 binInfo.getBankName().ifPresent(bankName
398 -> attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_BANK_NAME, MODULE_NAME, bankName)));
399 binInfo.getBankPhoneNumber().ifPresent(phoneNumber
400 -> attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_PHONE_NUMBER, MODULE_NAME, phoneNumber)));
401 binInfo.getBankURL().ifPresent(url
402 -> attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_URL, MODULE_NAME, url)));
403 binInfo.getCountry().ifPresent(country
404 -> attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_COUNTRY, MODULE_NAME, country)));
405 binInfo.getBankCity().ifPresent(city
406 -> attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_CITY, MODULE_NAME, city)));
414 if (hit.getContent() instanceof AbstractFile) {
415 AbstractFile file = (AbstractFile) hit.getContent();
416 if (file.getType() == TskData.TSK_DB_FILES_TYPE_ENUM.UNUSED_BLOCKS
417 || file.getType() == TskData.TSK_DB_FILES_TYPE_ENUM.UNALLOC_BLOCKS) {
418 attributes.add(
new BlackboardAttribute(KEYWORD_SEARCH_DOCUMENT_ID, MODULE_NAME, hit.getSolrDocumentId()));
426 newArtifact = hit.getContent().newArtifact(ARTIFACT_TYPE.TSK_ACCOUNT);
427 }
catch (TskCoreException ex) {
428 LOGGER.log(Level.SEVERE,
"Error adding artifact for account to blackboard", ex);
433 if (StringUtils.isNotBlank(listName)) {
434 attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_SET_NAME, MODULE_NAME, listName));
436 if (snippet != null) {
437 attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_KEYWORD_PREVIEW, MODULE_NAME, snippet));
439 if (hit.isArtifactHit()) {
440 attributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_ASSOCIATED_ARTIFACT, MODULE_NAME, hit.getArtifact().getArtifactID()));
444 attributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD_SEARCH_TYPE, MODULE_NAME, KeywordSearch.QueryType.SUBSTRING.ordinal()));
447 newArtifact.addAttributes(attributes);
448 KeywordCachedArtifact writeResult =
new KeywordCachedArtifact(newArtifact);
449 writeResult.add(attributes);
451 }
catch (TskCoreException e) {
452 LOGGER.log(Level.SEVERE,
"Error adding bb attributes for terms search artifact", e);
465 static private void parseTrack2Data(Map<BlackboardAttribute.Type, BlackboardAttribute> attributesMap, Matcher matcher) {
466 addAttributeIfNotAlreadyCaptured(attributesMap, ATTRIBUTE_TYPE.TSK_CARD_NUMBER,
"accountNumber", matcher);
467 addAttributeIfNotAlreadyCaptured(attributesMap, ATTRIBUTE_TYPE.TSK_CARD_EXPIRATION,
"expiration", matcher);
468 addAttributeIfNotAlreadyCaptured(attributesMap, ATTRIBUTE_TYPE.TSK_CARD_SERVICE_CODE,
"serviceCode", matcher);
469 addAttributeIfNotAlreadyCaptured(attributesMap, ATTRIBUTE_TYPE.TSK_CARD_DISCRETIONARY,
"discretionary", matcher);
470 addAttributeIfNotAlreadyCaptured(attributesMap, ATTRIBUTE_TYPE.TSK_CARD_LRC,
"LRC", matcher);
482 static private void parseTrack1Data(Map<BlackboardAttribute.Type, BlackboardAttribute> attributeMap, Matcher matcher) {
483 parseTrack2Data(attributeMap, matcher);
484 addAttributeIfNotAlreadyCaptured(attributeMap, ATTRIBUTE_TYPE.TSK_NAME_PERSON,
"name", matcher);
498 static private void addAttributeIfNotAlreadyCaptured(Map<BlackboardAttribute.Type, BlackboardAttribute> attributeMap, ATTRIBUTE_TYPE attrType, String groupName, Matcher matcher) {
499 BlackboardAttribute.Type type =
new BlackboardAttribute.Type(attrType);
500 attributeMap.computeIfAbsent(type, (BlackboardAttribute.Type t) -> {
501 String value = matcher.group(groupName);
502 if (attrType.equals(ATTRIBUTE_TYPE.TSK_CARD_NUMBER)) {
503 value = CharMatcher.anyOf(
" -").removeFrom(value);
505 if (StringUtils.isNotBlank(value)) {
506 return new BlackboardAttribute(attrType, MODULE_NAME, value);