Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimise yarahunter #89

Merged
merged 4 commits into from
Jun 24, 2024
Merged
Changes from 2 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
18 changes: 12 additions & 6 deletions pkg/scan/process_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func ScanFile(s *Scanner, f *os.File, iocs *[]output.IOCFound, layer string) err
}
err = yrScanner.ScanFileDescriptor(f.Fd())
if err != nil {
fmt.Println("Scan File Descriptor error, trying alternative", err)
logrus.Errorf("yara: %s: Error scanning file, error=%s, trying alternative", fileName, err.Error())
var buf []byte
if buf, err = io.ReadAll(f); err != nil {
logrus.Errorf("yara: %s: Error reading file, error=%s",
Expand All @@ -271,21 +271,27 @@ func ScanFile(s *Scanner, f *os.File, iocs *[]output.IOCFound, layer string) err
}
err = yrScanner.ScanMem(buf)
if err != nil {
fmt.Println("Scan File Mmory Error", err)
logrus.Errorf("yara: %s: Error scanning file, error=%s", fileName, err.Error())
return filepath.SkipDir
}

}
var iocsFound []output.IOCFound
totalMatchesStringData := make([]string, 0)
for _, m := range matches {
matchesStringData := make([]string, len(m.Strings))
matchesStringData := make(map[string]struct{})
for _, str := range m.Strings {
if !strings.Contains(strings.Join(matchesStringData, " "), string(str.Data)) {
matchesStringData = append(matchesStringData, string(str.Data))
if _, exists := matchesStringData[string(str.Data)]; !exists {
matchesStringData[string(str.Data)] = struct{}{}
totalMatchesStringData = append(totalMatchesStringData, string(str.Data))
}
}
ibreakthecloud marked this conversation as resolved.
Show resolved Hide resolved
// Convert map keys to slice since the rest of your code expects a slice
matchesStringDataSlice := make([]string, 0, len(matchesStringData))
for key := range matchesStringData {
matchesStringDataSlice = append(matchesStringDataSlice, key)
}

matchesMetaData := make([]string, len(m.Metas))
for _, strMeta := range m.Metas {
matchesMetaData = append(matchesMetaData, fmt.Sprintf("%v : %v \n", strMeta.Identifier, strMeta.Value))
Expand All @@ -294,7 +300,7 @@ func ScanFile(s *Scanner, f *os.File, iocs *[]output.IOCFound, layer string) err
iocsFound = append(iocsFound, output.IOCFound{
RuleName: m.Rule,
CategoryName: m.Tags,
StringsToMatch: matchesStringData,
StringsToMatch: matchesStringDataSlice,
Meta: matchesMetaData,
CompleteFilename: fileName,
})
Expand Down
Loading