diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ded759e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +cmd/.DS_Store diff --git a/README.md b/README.md new file mode 100644 index 0000000..39ee4e1 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Pattern89 -- pattern search tool + +Pattern format follows the one used by Smelter (c) avkiev. \ No newline at end of file diff --git a/cmd/pattern89/pattern89.go b/cmd/pattern89/pattern89.go new file mode 100644 index 0000000..19e8001 --- /dev/null +++ b/cmd/pattern89/pattern89.go @@ -0,0 +1,75 @@ +package main + +import ( + "flag" + "fmt" + "io" + "os" + + "github.com/siemens-mobile-hacks/pattern89/pkg/pattern89" +) + +var ( + fullFlash = flag.String("fullflash", "", "Path to the fullflash file.") + patternStr = flag.String("pattern", "", "Pattern to look for.") +) + +func main() { + flag.Parse() + + // Check if the file path is provided. + if *fullFlash == "" || *patternStr == "" { + fmt.Println("Usage: pattern89 -fullflash -pattern ") + return + } + + // Convert the pattern to a byte slice with wildcards represented as 0xFF. + pattern, err := pattern89.ParsePattern(*patternStr) + + if err != nil { + fmt.Println("Error opening file:", err) + return + } + + // Open the file. + file, err := os.Open(*fullFlash) + if err != nil { + fmt.Println("Error opening file:", err) + return + } + defer file.Close() + + // Create a buffer to read chunks of the file + const chunkSize = 4096 + buffer := make([]byte, chunkSize) + offset := int64(0) + + for { + // Read a chunk of the file. + bytesRead, err := file.Read(buffer) + if err != nil && err != io.EOF { + fmt.Println("Error reading file:", err) + return + } + + // Search for the pattern in the chunk. + if pos := pattern89.FindPattern(buffer[:bytesRead], pattern); pos != -1 { + fmt.Printf("Pattern found at offset: %08X\n", offset+int64(pos)) + return + } + + // Move the offset to the beginning of the next chunk + // (keeping some overlap to account for patterns that might be split across chunks). + offset += int64(bytesRead) - int64(pattern.Length()) + if bytesRead < chunkSize { + break + } + _, err = file.Seek(offset, io.SeekStart) + if err != nil { + fmt.Println("Error seeking file:", err) + return + } + } + + fmt.Println("Pattern not found") +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..aa2116c --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/siemens-mobile-hacks/pattern89 + +go 1.21.1 diff --git a/pkg/pattern89/pattern89.go b/pkg/pattern89/pattern89.go new file mode 100644 index 0000000..0f6de52 --- /dev/null +++ b/pkg/pattern89/pattern89.go @@ -0,0 +1,56 @@ +package pattern89 + +import ( + "encoding/hex" + "fmt" + "strings" +) + +type Pattern struct { + patBytes []byte + patWildcards []bool +} + +func (p Pattern) Length() int { + return len(p.patBytes) +} + +func ParsePattern(pattern string) (Pattern, error) { + // Remove commas so that we have just a byte stream. + pattern = strings.ReplaceAll(pattern, ",", "") + // Remove whitespace as well. + pattern = strings.ReplaceAll(pattern, " ", "") + if len(pattern)%2 != 0 { + return Pattern{}, fmt.Errorf("invalid pattern") + } + patternBytes := make([]byte, len(pattern)/2) + wildcards := make([]bool, len(pattern)/2) + for i := 0; i < len(pattern); i += 2 { + part := pattern[i : i+2] + if part == "??" { + patternBytes[i/2] = 0xFF + wildcards[i/2] = true + } else { + b, _ := hex.DecodeString(part) + patternBytes[i/2] = b[0] + wildcards[i/2] = false + } + } + return Pattern{patBytes: patternBytes, patWildcards: wildcards}, nil +} + +func FindPattern(data []byte, pattern Pattern) int { + for i := 0; i <= len(data)-len(pattern.patBytes); i++ { + match := true + for j := 0; j < len(pattern.patBytes); j++ { + if !pattern.patWildcards[j] && data[i+j] != pattern.patBytes[j] { + match = false + break + } + } + if match { + return i + } + } + return -1 +}