Skip to content

Instantly share code, notes, and snippets.

@nstarke
Last active January 20, 2021 21:31
Show Gist options
  • Save nstarke/ea83d6e8aba9a8b028a94cc14f5ff00d to your computer and use it in GitHub Desktop.
Save nstarke/ea83d6e8aba9a8b028a94cc14f5ff00d to your computer and use it in GitHub Desktop.
Ghidra Script: Count Referenced Strings
/* ###
* IP: GHIDRA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//This script counts the references to existing strings.
//@category Analysis
import java.util.*;
import ghidra.app.script.GhidraScript;
import ghidra.program.model.address.Address;
import ghidra.program.model.mem.Memory;
import ghidra.program.util.DefinedDataIterator;
import ghidra.program.model.listing.Data;
public class CountReferencedStrings extends GhidraScript {
@Override
public void run() throws Exception {
monitor.setMessage("Finding Strings with References");
int referencedCount = 0;
int totalCount = 0;
for (Data nextData: DefinedDataIterator.definedStrings(currentProgram) ) {
Address strAddr = nextData.getMinAddress();
int refCount = currentProgram.getReferenceManager().getReferenceCountTo(strAddr);
totalCount++;
if ( refCount > 0 ) {
referencedCount++;
}
}
println("Number of referenced strings found: " + referencedCount);
println("Total number of strings found: " + totalCount);
}
}
@dev747368
Copy link

better way to iterate strings:

for(Data data: DefinedDataIterator.definedStrings(currentProgram) ) { .... }

@nstarke
Copy link
Author

nstarke commented Jan 20, 2021

@dev747368 - Thank you, this is very helpful! I will get the script updated shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment