xref: /aosp_15_r20/external/AFLplusplus/utils/afl_untracer/ghidra_get_patchpoints.java (revision 08b48e0b10e97b33e7b60c5b6e2243bd915777f2)
1 /* ###
2  * IP: GHIDRA
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 // Find patch points for untracer tools (e.g. afl++ utils/afl_untracer)
17 //
18 //   Copy to ..../Ghidra/Features/Search/ghidra_scripts/
19 //   Writes the results to ~/Desktop/patches.txt
20 //
21 //   This is my very first Ghidra script. I am sure this could be done better.
22 //
23 //@category Search
24 
25 import ghidra.app.script.GhidraScript;
26 import ghidra.program.model.address.*;
27 import ghidra.program.model.block.*;
28 import ghidra.program.model.listing.*;
29 import ghidra.program.model.symbol.*;
30 import ghidra.program.model.mem.*;
31 
32 import java.io.*;
33 
34 public class ghidra_get_patchpoints extends GhidraScript {
35 
36 	@Override
run()37 	public void run() throws Exception {
38 
39 		long segment_start = 0;
40 		Memory memory = currentProgram.getMemory();
41 		MultEntSubModel model = new MultEntSubModel(currentProgram);
42 		CodeBlockIterator subIter = model.getCodeBlocks(monitor);
43 		BufferedWriter out = new BufferedWriter(new FileWriter(System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "patches.txt"));
44 
45 		while (subIter.hasNext()) {
46 
47 			CodeBlock multiEntryBlock = subIter.next();
48 			SimpleBlockModel basicBlockModel = new SimpleBlockModel(currentProgram);
49 			CodeBlockIterator bbIter = basicBlockModel.getCodeBlocksContaining(multiEntryBlock, monitor);
50 
51 			while (bbIter.hasNext()) {
52 
53 				CodeBlock basicBlock = bbIter.next();
54 
55 				if (segment_start == 0) {
56 
57 					Address firstAddr = basicBlock.getFirstStartAddress();
58 					long firstBlockAddr = firstAddr.getAddressableWordOffset();
59 					MemoryBlock mb = memory.getBlock(firstAddr);
60 					Address startAddr = mb.getStart();
61 					Address endAddr = mb.getEnd();
62 					segment_start = startAddr.getAddressableWordOffset();
63 					if ((firstBlockAddr - segment_start) >= 0x1000)
64 					  segment_start += 0x1000;
65 					long segment_end = endAddr.getAddressableWordOffset();
66 					long segment_size = segment_end - segment_start;
67 					if ((segment_size % 0x1000) > 0)
68 					  segment_size = (((segment_size / 0x1000) + 1) * 0x1000);
69 					out.write(currentProgram.getName() + ":0x" + Long.toHexString(segment_size) + "\n");
70 					//println("Start: " + Long.toHexString(segment_start));
71 					//println("End: " + Long.toHexString(segment_end));
72 
73 				}
74 
75  	   		        if (basicBlock.getFirstStartAddress().getAddressableWordOffset() - segment_start > 0)
76  	   		        	out.write("0x" + Long.toHexString(basicBlock.getFirstStartAddress().getAddressableWordOffset() - segment_start) + "\n");
77 
78 			}
79 		}
80 
81 		out.close();
82 
83 	}
84 }
85