Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Struct Support to Ghidra #316

Open
mahaloz opened this issue Aug 8, 2023 · 1 comment
Open

Add Struct Support to Ghidra #316

mahaloz opened this issue Aug 8, 2023 · 1 comment
Labels
enhancement New feature or request ghidra

Comments

@mahaloz
Copy link
Collaborator

mahaloz commented Aug 8, 2023

Description

Some work has been done by Shellphish members at DEFCON:

import ghidra.app.script.GhidraScript;
import ghidra.program.model.data.*;

public class binsync_structs extends GhidraScript {

    public void run() throws Exception {
        add_named_struct("my_struct");
        add_member_to_struct("my_struct", "my_field");
        expand_struct("my_struct", 0x4);
        retype_struct_member("my_struct", "my_field", IntegerDataType.dataType);
    }

    private Structure get_struct(String name){
        return (Structure)currentProgram.getDataTypeManager().getDataType("/"+name);
    }

    public void add_named_struct(String name) throws Exception{
        StructureDataType struct = new StructureDataType(name, 0);
        currentProgram.getDataTypeManager().addDataType(struct, DataTypeConflictHandler.DEFAULT_HANDLER);
    }

    public void add_member_to_struct(String struct_name, String member){
        Structure struct = get_struct(struct_name);
        struct.add(ByteDataType.dataType, 1, member, "");
    }

    public void retype_struct_member(String struct_name, String member, DataType type){
        Structure struct = get_struct(struct_name);
        int offset = 0;
        for (DataTypeComponent dtc : struct.getComponents()) {
            if (dtc.getFieldName().equals(member)){
                offset = dtc.getOffset();
                for(int i=offset; i<offset+type.getLength(); i++){
                    struct.clearAtOffset(i);
                }
                struct.replaceAtOffset(offset, type, 4, member,"");
                break;
            }
        }
    }

    public void expand_struct(String struct_name, int new_size){
        Structure struct = get_struct(struct_name);
        struct.growStructure(new_size-struct.getLength());
    }

}

Alternatives

No response

Additional context

No response

@mahaloz mahaloz added enhancement New feature or request ghidra labels Aug 8, 2023
@mahaloz
Copy link
Collaborator Author

mahaloz commented Oct 1, 2023

This is also some great reference code for when the lister is implemented:
mahaloz/decomp2dbg#75

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request ghidra
Projects
None yet
Development

No branches or pull requests

1 participant