mirror of
https://github.com/siemens-mobile-hacks/pattern89.git
synced 2026-11-25 21:47:18 +00:00
First working version of pattern89 code
This commit is contained in:
parent
391b82d9c2
commit
8eff2d5c79
5 changed files with 138 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
cmd/.DS_Store
|
||||
3
README.md
Normal file
3
README.md
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# Pattern89 -- pattern search tool
|
||||
|
||||
Pattern format follows the one used by Smelter (c) avkiev.
|
||||
75
cmd/pattern89/pattern89.go
Normal file
75
cmd/pattern89/pattern89.go
Normal file
|
|
@ -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 <file-path> -pattern <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")
|
||||
}
|
||||
3
go.mod
Normal file
3
go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
module github.com/siemens-mobile-hacks/pattern89
|
||||
|
||||
go 1.21.1
|
||||
56
pkg/pattern89/pattern89.go
Normal file
56
pkg/pattern89/pattern89.go
Normal file
|
|
@ -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
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue