mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2025-10-04 10:19:23 +02:00
Add SHA256 hash to Program
interface
Implements the first part of #291 Signed-off-by: Christian Blichmann <cblichmann@google.com>
This commit is contained in:
parent
49c2010b63
commit
f23fcf81a9
5 changed files with 64 additions and 7 deletions
|
@ -41,6 +41,7 @@ import ghidra.program.util.DefaultLanguageService;
|
||||||
import ghidra.program.util.GhidraProgramUtilities;
|
import ghidra.program.util.GhidraProgramUtilities;
|
||||||
import ghidra.util.InvalidNameException;
|
import ghidra.util.InvalidNameException;
|
||||||
import ghidra.util.MD5Utilities;
|
import ghidra.util.MD5Utilities;
|
||||||
|
import ghidra.util.HashUtilities;
|
||||||
import ghidra.util.exception.*;
|
import ghidra.util.exception.*;
|
||||||
import ghidra.util.task.TaskMonitor;
|
import ghidra.util.task.TaskMonitor;
|
||||||
|
|
||||||
|
@ -285,6 +286,8 @@ public abstract class AbstractProgramLoader implements Loader {
|
||||||
}
|
}
|
||||||
String md5 = computeBinaryMD5(provider);
|
String md5 = computeBinaryMD5(provider);
|
||||||
prog.setExecutableMD5(md5);
|
prog.setExecutableMD5(md5);
|
||||||
|
String sha256 = computeBinarySHA256(provider);
|
||||||
|
prog.setExecutableSHA256(sha256);
|
||||||
|
|
||||||
if (shouldSetImageBase(prog, imageBase)) {
|
if (shouldSetImageBase(prog, imageBase)) {
|
||||||
try {
|
try {
|
||||||
|
@ -465,6 +468,12 @@ public abstract class AbstractProgramLoader implements Loader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String computeBinarySHA256(ByteProvider provider) throws IOException {
|
||||||
|
try (InputStream in = provider.getInputStream(0)) {
|
||||||
|
return HashUtilities.getHash(HashUtilities.SHA256_ALGORITHM, in);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private boolean shouldSetImageBase(Program prog, Address imageBase) {
|
private boolean shouldSetImageBase(Program prog, Address imageBase) {
|
||||||
if (imageBase == null || imageBase instanceof SegmentedAddress) {
|
if (imageBase == null || imageBase instanceof SegmentedAddress) {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -128,6 +128,7 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
|
||||||
private static final String EXECUTABLE_PATH = "Executable Location";
|
private static final String EXECUTABLE_PATH = "Executable Location";
|
||||||
private static final String EXECUTABLE_FORMAT = "Executable Format";
|
private static final String EXECUTABLE_FORMAT = "Executable Format";
|
||||||
private static final String EXECUTABLE_MD5 = "Executable MD5";
|
private static final String EXECUTABLE_MD5 = "Executable MD5";
|
||||||
|
private static final String EXECUTABLE_SHA256 = "Executable SHA256";
|
||||||
private static final String TABLE_NAME = "Program";
|
private static final String TABLE_NAME = "Program";
|
||||||
private static final String EXECUTE_PATH = "Execute Path";
|
private static final String EXECUTE_PATH = "Execute Path";
|
||||||
private static final String EXECUTE_FORMAT = "Execute Format";
|
private static final String EXECUTE_FORMAT = "Execute Format";
|
||||||
|
@ -716,7 +717,6 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @see ghidra.program.model.listing.Program#setExecutableFormat(java.lang.String)
|
* @see ghidra.program.model.listing.Program#setExecutableFormat(java.lang.String)
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
@ -742,7 +742,6 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @see ghidra.program.model.listing.Program#setExecutableMD5(java.lang.String)
|
* @see ghidra.program.model.listing.Program#setExecutableMD5(java.lang.String)
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
@ -752,6 +751,31 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
|
||||||
changed = true;
|
changed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see ghidra.program.model.listing.Program#getExecutableSHA256()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String getExecutableSHA256() {
|
||||||
|
String format = null;
|
||||||
|
try {
|
||||||
|
Options pl = getOptions(PROGRAM_INFO);
|
||||||
|
format = pl.getString(EXECUTABLE_SHA256, (String) null);
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
}
|
||||||
|
return format == null ? UNKNOWN : format;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see ghidra.program.model.listing.Program#setExecutableSHA256(java.lang.String)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void setExecutableSHA256(String sha256) {
|
||||||
|
Options pl = getOptions(PROGRAM_INFO);
|
||||||
|
pl.setString(EXECUTABLE_SHA256, sha256);
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see ghidra.program.model.listing.Program#getCreationDate()
|
* @see ghidra.program.model.listing.Program#getCreationDate()
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -190,6 +190,18 @@ public interface Program extends DataTypeManagerDomainObject {
|
||||||
*/
|
*/
|
||||||
public void setExecutableMD5(String md5);
|
public void setExecutableMD5(String md5);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value corresponding to the original binary file SHA256 hash.
|
||||||
|
* @param sha256 SHA256 binary file hash
|
||||||
|
*/
|
||||||
|
public void setExecutableSHA256(String sha256);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a value corresponding to the original binary file SHA256 hash.
|
||||||
|
* May be null if program source did not correspond to a binary file.
|
||||||
|
*/
|
||||||
|
public String getExecutableSHA256();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the creation date of this program.
|
* Returns the creation date of this program.
|
||||||
* If the program was created before this property
|
* If the program was created before this property
|
||||||
|
|
|
@ -406,6 +406,16 @@ public class ProgramTestDouble implements Program {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getExecutableSHA256() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setExecutableSHA256(String sha256) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Date getCreationDate() {
|
public Date getCreationDate() {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
|
|
|
@ -786,7 +786,7 @@ public class ServerTestUtil {
|
||||||
funcAddr += 2;
|
funcAddr += 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
setProgramMd5(program);
|
setProgramHashes(program);
|
||||||
|
|
||||||
ContentHandler contentHandler = DomainObjectAdapter.getContentHandler(program);
|
ContentHandler contentHandler = DomainObjectAdapter.getContentHandler(program);
|
||||||
long checkoutId = contentHandler.createFile(repoFilesystem, null, folderPath, name,
|
long checkoutId = contentHandler.createFile(repoFilesystem, null, folderPath, name,
|
||||||
|
@ -856,15 +856,17 @@ public class ServerTestUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets a dummy MD5 value for the given program.
|
* Sets dummy hash values for the given program.
|
||||||
*
|
*
|
||||||
* @param program the current program
|
* @param program the current program
|
||||||
*/
|
*/
|
||||||
private static void setProgramMd5(Program program) {
|
private static void setProgramHashes(Program program) {
|
||||||
int id = program.startTransaction("setmd5");
|
int id = program.startTransaction("sethashes");
|
||||||
try {
|
try {
|
||||||
String md5 = RandomStringUtils.randomNumeric(32);
|
String md5 = RandomStringUtils.randomNumeric(32);
|
||||||
program.setExecutableMD5(md5);
|
program.setExecutableMD5(md5);
|
||||||
|
String sha256 = RandomStringUtils.randomNumeric(64);
|
||||||
|
program.setExecutableSHA256(sha256);
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
program.endTransaction(id, true);
|
program.endTransaction(id, true);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue