Commit 3c76cb36 by Melvin Klimke Committed by Enkelmann

Added p_code_extractor to cwe checker (#86)

parent 4b6ca3be
......@@ -239,3 +239,30 @@ Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# Libraries for development
ghidra/p_code_extractor/lib/
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
\ No newline at end of file
import java.util.ArrayList;
import ghidra.program.model.listing.Instruction;
import ghidra.program.model.pcode.PcodeOp;
import term.Blk;
import term.Def;
import term.Term;
public final class PcodeBlockData {
// private constructor for non-instantiable classes
private PcodeBlockData() {
throw new UnsupportedOperationException();
}
/**
* The blocks array contains at least one block for a given Ghidra generated block.
* when the Ghidra Block is split due to branches inside it, new blocks are added to the blocks array.
* The blocks array only contains instructions from one Ghidra block.
*/
public static ArrayList<Term<Blk>> blocks;
/**
* The PcodeOp array contains the pcode operations for one assembly instruction
*/
public static PcodeOp[] ops;
/**
* The temporaryDefStorage contains definitions as long as the corresponding block is unknown.
* The block is unknown as splits can occur.
*/
public static ArrayList<Term<Def>> temporaryDefStorage;
/**
* Contains the currently analysed assembly instruction
*/
public static Instruction instruction;
/**
* Contains the index of the currently analysed assembly instruction in the current Ghidra block
*/
public static int instructionIndex;
/**
* Contains the number of assembly instructions in the current Ghidra block
*/
public static long numberOfInstructionsInBlock;
}
package bil;
public interface ExecutionType {
public String getType();
enum OtherType implements ExecutionType {
COPY("COPY"),
LOAD("LOAD"),
STORE("STORE"),
PIECE("PIECE"),
SUBPIECE("SUBPIECE");
private String type;
private OtherType(String type) {
this.type = type;
}
@Override
public String getType() {
return this.type;
}
}
enum BinOpType implements ExecutionType {
INT_EQUAL("INT_EQUAL"),
INT_NOTEQUAL("INT_NOTEQUAL"),
INT_LESS("INT_LESS"),
INT_SLESS("INT_SLESS"),
INT_LESSEQUAL("INT_LESSEQUAL"),
INT_SLESSEQUAL("INT_SLESSEQUAL"),
INT_ADD("INT_ADD"),
INT_SUB("INT_SUB"),
INT_CARRY("INT_CARRY"),
INT_SCARRY("INT_SCARRY"),
INT_SBORROW("INT_SBORROW"),
INT_XOR("INT_XOR"),
INT_AND("INT_AND"),
INT_OR("INT_OR"),
INT_LEFT("INT_LEFT"),
INT_RIGHT("INT_RIGHT"),
INT_SRIGHT("INT_SRIGHT"),
INT_MULT("INT_MULT"),
INT_DIV("INT_DIV"),
INT_REM("INT_REM"),
INT_SDIV("INT_SDIV"),
INT_SREM("INT_SREM"),
BOOL_XOR("BOOL_XOR"),
BOOL_AND("BOOL_AND"),
BOOL_OR("BOOL_OR"),
FLOAT_EQUAL("FLOAT_EQUAL"),
FLOAT_NOTEQUAL("FLOAT_NOTEQUAL"),
FLOAT_LESS("FLOAT_LESS"),
FLOAT_LESSEQUAL("FLOAT_LESSEQUAL"),
FLOAT_ADD("FLOAT_ADD"),
FLOAT_SUB("FLOAT_SUB"),
FLOAT_MULT("FLOAT_MULT"),
FLOAT_DIV("FLOAT_DIV");
private String type;
private BinOpType(String type) {
this.type = type;
}
@Override
public String getType() {
return this.type;
}
}
enum UnOpType implements ExecutionType {
INT_NEGATE("INT_NEGATE"),
INT_2COMP("INT_2COMP"),
BOOL_NEGATE("BOOL_NEGATE"),
FLOAT_NEG("FLOAT_NEG"),
FLOAT_ABS("FLOAT_ABS"),
FLOAT_SQRT("FLOAT_SQRT"),
FLOAT_CEIL("FLOAT_CEIL"),
FLOAT_FLOOR("FLOAT_FLOOR"),
FLOAT_ROUND("FLOAT_ROUND");
private String type;
private UnOpType(String type) {
this.type = type;
}
@Override
public String getType() {
return this.type;
}
}
enum CastType implements ExecutionType {
INT_ZEXT("INT_ZEXT"),
INT_SEXT("INT_SEXT"),
INT2FLOAT("INT2FLOAT"),
FLOAT2FLOAT("FLOAT2FLOAT"),
TRUNC("TRUNC"),
FLOAT_NAN("FLOAT_NAN");
private String type;
private CastType(String type) {
this.type = type;
}
@Override
public String getType() {
return this.type;
}
}
enum JmpType implements ExecutionType {
CALL("CALL"),
GOTO("GOTO"),
RETURN("RETURN");
private String type;
private JmpType(String type) {
this.type = type;
}
@Override
public String getType() {
return this.type;
}
}
}
package bil;
import com.google.gson.annotations.SerializedName;
public class Expression {
@SerializedName("mnemonic")
private String mnemonic;
@SerializedName("input0")
private Variable input0;
@SerializedName("input1")
private Variable input1;
@SerializedName("input2")
private Variable input2;
public Expression() {
}
public Expression(String mnemonic, Variable input0) {
this.setMnemonic(mnemonic);
this.setInput0(input0);
}
public Expression(String mnemonic, Variable input0, Variable input1) {
this.setMnemonic(mnemonic);
this.setInput0(input0);
this.setInput1(input1);
}
public Expression(String mnemonic, Variable input0, Variable input1, Variable input2) {
this.setMnemonic(mnemonic);
this.setInput0(input0);
this.setInput1(input1);
this.setInput2(input2);
}
public String getMnemonic() {
return mnemonic;
}
public void setMnemonic(String mnemonic) {
this.mnemonic = mnemonic;
}
public Variable getInput0() {
return input0;
}
public void setInput0(Variable input0) {
this.input0 = input0;
}
public Variable getInput1() {
return input1;
}
public void setInput1(Variable input1) {
this.input1 = input1;
}
public Variable getInput2() {
return input2;
}
public void setInput2(Variable input2) {
this.input2 = input2;
}
}
package bil;
import com.google.gson.annotations.SerializedName;
public class Variable {
@SerializedName("name")
private String name;
@SerializedName("value")
private String value;
@SerializedName("address")
private String address;
@SerializedName("size")
private long size;
@SerializedName("is_virtual")
private Boolean isVirtual;
public Variable() {
}
public Variable(String name, long size, Boolean is_virtual) {
this.setName(name);
this.setSize(size);
this.setIsVirtual(is_virtual);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
public Boolean getIsVirtual() {
return isVirtual;
}
public void setIsVirtual(Boolean is_virtual) {
this.isVirtual = is_virtual;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package serializer;
import java.io.FileWriter;
import java.io.IOException;
import com.google.gson.*;
import term.Project;
import term.Sub;
import term.Jmp;
import term.Def;
public class Serializer {
private Project project;
private String path;
public Serializer() {
}
public Serializer(Project project, String path) {
this.setProject(project);
this.setPath(path);
}
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public void serializeProject() {
ExclusionStrategy strategy = new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes field) {
if (field.getDeclaringClass() == Sub.class && field.getName().equals("addresses")) {
return true;
}
if (field.getDeclaringClass() == Jmp.class && (field.getName().equals("type") || field.getName().equals("pcodeIndex"))) {
return true;
}
if (field.getDeclaringClass() == Def.class && field.getName().equals("pcodeIndex")) {
return true;
}
return false;
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
};
Gson gson = new GsonBuilder().setPrettyPrinting().addSerializationExclusionStrategy(strategy).create();
try {
FileWriter writer = new FileWriter(path);
gson.toJson(project, writer);
writer.close();
} catch (JsonIOException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package symbol;
import java.util.ArrayList;
import com.google.gson.annotations.SerializedName;
import term.Arg;
import term.Tid;
public class ExternSymbol {
@SerializedName("tid")
private Tid tid;
@SerializedName("address")
private String address;
@SerializedName("name")
private String name;
@SerializedName("calling_convention")
private String callingConvention;
@SerializedName("arguments")
private ArrayList<Arg> arguments;
@SerializedName("no_return")
private Boolean noReturn;
public ExternSymbol() {
}
public ExternSymbol(Tid tid, String address, String name, String callingConvention, ArrayList<Arg> arguments, Boolean noReturn) {
this.setTid(tid);
this.setAddress(address);
this.setName(name);
this.setCallingConvention(callingConvention);
this.setArguments(arguments);
this.setNoReturn(noReturn);
}
public Tid getTid() {
return tid;
}
public void setTid(Tid tid) {
this.tid = tid;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCallingConvention() {
return callingConvention;
}
public void setCallingConvention(String callingConvention) {
this.callingConvention = callingConvention;
}
public ArrayList<Arg> getArguments() {
return arguments;
}
public void setArguments(ArrayList<Arg> arguments) {
this.arguments = arguments;
}
public Boolean getNoReturn() {
return noReturn;
}
public void setNoReturn(Boolean noReturn) {
this.noReturn = noReturn;
}
}
package term;
import bil.Expression;
import bil.Variable;
import com.google.gson.annotations.SerializedName;
public class Arg {
@SerializedName("var")
private Variable var;
@SerializedName("location")
private Expression location;
@SerializedName("intent")
private String intent;
public Arg() {
}
public Arg(Variable var, String intent) {
this.setVar(var);
this.setIntent(intent);
}
public Arg(Expression location, String intent) {
this.setLocation(location);
this.setIntent(intent);
}
public Variable getVar() {
return var;
}
public void setVar(Variable var) {
this.var = var;
}
public Expression getLocation() {
return location;
}
public void setLocation(Expression location) {
this.location = location;
}
public String getIntent() {
return intent;
}
public void setIntent(String intent) {
this.intent = intent;
}
}
package term;
import java.util.ArrayList;
import com.google.gson.annotations.SerializedName;
public class Blk {
@SerializedName("defs")
private ArrayList<Term<Def>> defs;
@SerializedName("jmps")
private ArrayList<Term<Jmp>> jmps;
public Blk() {
this.setDefs(new ArrayList<Term<Def>>());
this.setJmps(new ArrayList<Term<Jmp>>());
}
public Blk(ArrayList<Term<Def>> defs, ArrayList<Term<Jmp>> jmps) {
this.setDefs(defs);
this.setJmps(jmps);
}
public ArrayList<Term<Def>> getDefs() {
return defs;
}
public void setDefs(ArrayList<Term<Def>> defs) {
this.defs = defs;
}
public ArrayList<Term<Jmp>> getJmps() {
return jmps;
}
public void setJmps(ArrayList<Term<Jmp>> jmps) {
this.jmps = jmps;
}
public void addDef(Term<Def> def) {
this.defs.add(def);
}
public void addJmp(Term<Jmp> jmp) {
this.jmps.add(jmp);
}
public void addMultipleDefs(ArrayList<Term<Def>> defs) {
this.defs.addAll(defs);
}
}
package term;
import com.google.gson.annotations.SerializedName;
public class Call {
@SerializedName("target")
private Label target;
@SerializedName("return")
private Label return_;
@SerializedName("call_string")
private String callString;
public Call() {
}
public Call(Label target) {
this.setTarget(target);
}
public Call(Label target, Label return_) {
this.setTarget(target);
this.setReturn_(return_);
}
public Call(Label target, Label return_, String callString) {
this.setTarget(target);
this.setReturn_(return_);
this.setCallString(callString);
}
public Label getTarget() {
return target;
}
public void setTarget(Label target) {
this.target = target;
}
public Label getReturn_() {
return return_;
}
public void setReturn_(Label return_) {
this.return_ = return_;
}
public String getCallString() {
return callString;
}
public void setCallString(String callString) {
this.callString = callString;
}
}
package term;
import bil.Expression;
import bil.Variable;
import com.google.gson.annotations.SerializedName;
public class Def {
@SerializedName("lhs")
private Variable lhs;
@SerializedName("rhs")
private Expression rhs;
@SerializedName("pcode_index")
private int pcodeIndex;
public Def() {
}
public Def(Expression rhs, int pcodeIndex) {
this.setRhs(rhs);
this.setPcodeIndex(pcodeIndex);
}
public Def(Variable lhs, Expression rhs, int pcodeIndex) {
this.setLhs(lhs);
this.setRhs(rhs);
this.setPcodeIndex(pcodeIndex);
}
public Variable getLhs() {
return lhs;
}
public void setLhs(Variable lhs) {
this.lhs = lhs;
}
public Expression getRhs() {
return rhs;
}
public void setRhs(Expression rhs) {
this.rhs = rhs;
}
public int getPcodeIndex() {
return pcodeIndex;
}
public void setPcodeIndex(int pcodeIndex) {
this.pcodeIndex = pcodeIndex;
}
}
package term;
import bil.ExecutionType;
import bil.Variable;
import com.google.gson.annotations.SerializedName;
public class Jmp {
@SerializedName("type_")
private ExecutionType.JmpType type;
@SerializedName("mnemonic")
private String mnemonic;
@SerializedName("goto")
private Label goto_;
@SerializedName("call")
private Call call;
@SerializedName("condition")
private Variable condition;
@SerializedName("pcode_index")
private int pcodeIndex;
public Jmp() {
}
public Jmp(ExecutionType.JmpType type, String mnemonic, Label goto_, int pcodeIndex) {
this.setType(type);
this.setMnemonic(mnemonic);
this.setGoto_(goto_);
this.setPcodeIndex(pcodeIndex);
}
public Jmp(ExecutionType.JmpType type, String mnemonic, Call call, int pcodeIndex) {
this.setType(type);
this.setMnemonic(mnemonic);
this.setCall(call);
this.setPcodeIndex(pcodeIndex);
}
public Jmp(ExecutionType.JmpType type, String mnemonic, Label goto_, Variable condition, int pcodeIndex) {
this.setType(type);
this.setMnemonic(mnemonic);
this.setGoto_(goto_);
this.setCondition(condition);
this.setPcodeIndex(pcodeIndex);
}
public ExecutionType.JmpType getType() {
return type;
}
public void setType(ExecutionType.JmpType type) {
this.type = type;
}
public String getMnemonic() {
return mnemonic;
}
public void setMnemonic(String mnemonic) {
this.mnemonic = mnemonic;
}
public Variable getCondition() {
return condition;
}
public void setCondition(Variable condition) {
this.condition = condition;
}
public Call getCall() {
return call;
}
public void setCall(Call call) {
this.call = call;
}
public Label getGoto_() {
return goto_;
}
public void setGoto_(Label goto_) {
this.goto_ = goto_;
}
public int getPcodeIndex() {
return pcodeIndex;
}
public void setPcodeIndex(int pcodeIndex) {
this.pcodeIndex = pcodeIndex;
}
}
package term;
import bil.Variable;
import com.google.gson.annotations.SerializedName;
public class Label {
@SerializedName("Direct")
private Tid direct;
@SerializedName("Indirect")
private Variable indirect;
public Label(Tid tid) {
this.setDirect(tid);
this.setIndirect(null);
}
public Label(Variable variable) {
this.setDirect(null);
this.setIndirect(variable);
}
public Tid getDirect() {
return direct;
}
public void setDirect(Tid direct) {
this.direct = direct;
}
public Variable getIndirect() {
return indirect;
}
public void setIndirect(Variable indirect) {
this.indirect = indirect;
}
}
package term;
import java.util.ArrayList;
import com.google.gson.annotations.SerializedName;
import symbol.ExternSymbol;
public class Program {
@SerializedName("subs")
private ArrayList<Term<Sub>> subs;
@SerializedName("extern_symbols")
private ArrayList<ExternSymbol> externSymbols;
@SerializedName("entry_points")
private ArrayList<Tid> entryPoints;
public Program() {
}
public Program(ArrayList<Term<Sub>> subs) {
this.setSubs(subs);
}
public Program(ArrayList<Term<Sub>> subs, ArrayList<ExternSymbol> externSymbols, ArrayList<Tid> entryPoints) {
this.setSubs(subs);
this.setExternSymbols(externSymbols);
this.setEntryPoints(entryPoints);
}
public ArrayList<Term<Sub>> getSubs() {
return subs;
}
public void setSubs(ArrayList<Term<Sub>> subs) {
this.subs = subs;
}
public void addSub(Term<Sub> sub) {
this.subs.add(sub);
}
public ArrayList<ExternSymbol> getExternSymbols() {
return externSymbols;
}
public void setExternSymbols(ArrayList<ExternSymbol> extern_symbols) {
this.externSymbols = extern_symbols;
}
public ArrayList<Tid> getEntryPoints() {
return entryPoints;
}
public void setEntryPoints(ArrayList<Tid> entryPoints) {
this.entryPoints = entryPoints;
}
}
package term;
import bil.Variable;
import com.google.gson.annotations.SerializedName;
public class Project {
@SerializedName("program")
private Term<Program> program;
@SerializedName("stack_pointer_register")
private Variable stackPointerRegister;
@SerializedName("cpu_architecture")
private String cpuArch;
public Project() {
}
public Project(Term<Program> program, String cpuArch, Variable stackPointerRegister) {
this.setProgram(program);
this.setCpuArch(cpuArch);
this.setStackPointerRegister(stackPointerRegister);
}
public Term<Program> getProgram() {
return program;
}
public void setProgram(Term<Program> program) {
this.program = program;
}
public Variable getStackPointerRegister() {
return stackPointerRegister;
}
public void setStackPointerRegister(Variable stackPointerRegister) {
this.stackPointerRegister = stackPointerRegister;
}
public String getCpuArch() {
return cpuArch;
}
public void setCpuArch(String cpuArch) {
this.cpuArch = cpuArch;
}
}
package term;
import java.util.ArrayList;
import com.google.gson.annotations.SerializedName;
import ghidra.program.model.address.AddressSetView;
public class Sub {
@SerializedName("name")
private String name;
private AddressSetView addresses;
@SerializedName("blocks")
private ArrayList<Term<Blk>> blocks;
public Sub() {
}
public Sub(String name, AddressSetView addresses) {
this.setName(name);
this.setAddresses(addresses);
}
public Sub(String name, ArrayList<Term<Blk>> blocks, AddressSetView addresses) {
this.setName(name);
this.setBlocks(blocks);
this.setAddresses(addresses);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<Term<Blk>> getBlocks() {
return blocks;
}
public void setBlocks(ArrayList<Term<Blk>> blocks) {
this.blocks = blocks;
}
public void addBlock(Term<Blk> block) {
this.blocks.add(block);
}
public AddressSetView getAddresses() {
return addresses;
}
public void setAddresses(AddressSetView addresses) {
this.addresses = addresses;
}
}
package term;
import com.google.gson.annotations.SerializedName;
public class Term<T> {
@SerializedName("tid")
private Tid tid;
@SerializedName("term")
private T term;
public Term() {
}
public Term(Tid tid, T term) {
this.setTid(tid);
this.setTerm(term);
}
public Tid getTid() {
return tid;
}
public void setTid(Tid tid) {
this.tid = tid;
}
public T getTerm() {
return term;
}
public void setTerm(T term) {
this.term = term;
}
}
package term;
import com.google.gson.annotations.SerializedName;
public class Tid {
@SerializedName("id")
private String id;
@SerializedName("address")
private String address;
public Tid() {
}
public Tid(String id, String address) {
this.setId(id);
this.setAddress(address);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment