-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterGoStrings.java
More file actions
69 lines (60 loc) · 2.67 KB
/
FilterGoStrings.java
File metadata and controls
69 lines (60 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//Find the Go packages using the string "go:itab.*github.com" delimiter
//@author
//@category Strings
//@keybinding
//@menupath
//@toolbar
import ghidra.app.script.GhidraScript;
import ghidra.program.model.address.Address;
import ghidra.program.model.mem.Memory;
import ghidra.program.model.mem.MemoryAccessException;
import ghidra.program.model.mem.MemoryBlock;
import ghidra.program.model.listing.CodeUnit;
import ghidra.program.model.listing.Listing;
import ghidra.util.Msg;
public class FilterGoStrings extends GhidraScript {
private static final String SEARCH_PATTERN = "go:itab.*github.com"; // Define the pattern to search for
protected void run() throws Exception {
// Retrieve the Listing object for accessing code units (strings) in the program
Listing listing = currentProgram.getListing();
// Iterate over all code units in the program
for (CodeUnit codeUnit : listing.getCodeUnits(true)) {
// Check if the code unit is a string
String str = codeUnit.getComment(CodeUnit.EOL_COMMENT); // Get the string from the code unit comment
if (str != null && str.contains(SEARCH_PATTERN)) {
Address address = codeUnit.getMinAddress();
println("Pattern found at address: " + address + " - " + str);
}
}
// Check memory blocks
Memory memory = currentProgram.getMemory();
for (MemoryBlock block : memory.getBlocks()) {
Address start = block.getStart();
Address end = block.getEnd();
Address address = start;
// Read and check bytes within the memory block
while (address.compareTo(end) <= 0) {
String str = getStringAt(address);
if (str != null && str.contains(SEARCH_PATTERN)) {
println("Pattern found in memory block at address: " + address + " - " + str);
}
address = address.add(1000); // Move forward by 1000 bytes
}
}
}
private String getStringAt(Address address) {
try {
// Read bytes from memory; adjust size if needed
byte[] bytes = new byte[1000]; // Buffer to store bytes
int length = currentProgram.getMemory().getBytes(address, bytes); // Read bytes into buffer
if (length <= 0) {
return null;
}
return new String(bytes, 0, length).trim(); // Convert bytes to string
} catch (MemoryAccessException e) {
// Handle memory access exceptions
Msg.error(this, "Failed to read memory at " + address + ": " + e.getMessage());
return null;
}
}
}