Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 186 additions & 0 deletions src/pe_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,180 @@
#include <map>
#include <sstream>

namespace {
uint32_t rotateRight(
uint32_t value,
uint32_t bits
) {
return (value >> bits) |
(value << (32 - bits));
}

std::string calculateSha256Hex(
const std::vector<uint8_t>& input
) {
static const uint32_t constants[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};

std::vector<uint8_t> message = input;

const uint64_t bitLength =
static_cast<uint64_t>(message.size()) * 8ULL;

message.push_back(0x80);

while ((message.size() % 64) != 56) {
message.push_back(0x00);
}

for (int shift = 56; shift >= 0; shift -= 8) {
message.push_back(
static_cast<uint8_t>(
(bitLength >> shift) & 0xFF
)
);
}

uint32_t hash[8] = {
0x6a09e667,
0xbb67ae85,
0x3c6ef372,
0xa54ff53a,
0x510e527f,
0x9b05688c,
0x1f83d9ab,
0x5be0cd19
};

for (std::size_t chunk = 0;
chunk < message.size();
chunk += 64) {
uint32_t words[64]{};

for (std::size_t index = 0;
index < 16;
++index) {
const std::size_t offset =
chunk + index * 4;

words[index] =
(static_cast<uint32_t>(message[offset]) << 24) |
(static_cast<uint32_t>(message[offset + 1]) << 16) |
(static_cast<uint32_t>(message[offset + 2]) << 8) |
static_cast<uint32_t>(message[offset + 3]);
}

for (std::size_t index = 16;
index < 64;
++index) {
const uint32_t smallSigma0 =
rotateRight(words[index - 15], 7) ^
rotateRight(words[index - 15], 18) ^
(words[index - 15] >> 3);

const uint32_t smallSigma1 =
rotateRight(words[index - 2], 17) ^
rotateRight(words[index - 2], 19) ^
(words[index - 2] >> 10);

words[index] =
words[index - 16] +
smallSigma0 +
words[index - 7] +
smallSigma1;
}

uint32_t a = hash[0];
uint32_t b = hash[1];
uint32_t c = hash[2];
uint32_t d = hash[3];
uint32_t e = hash[4];
uint32_t f = hash[5];
uint32_t g = hash[6];
uint32_t h = hash[7];

for (std::size_t index = 0;
index < 64;
++index) {
const uint32_t bigSigma1 =
rotateRight(e, 6) ^
rotateRight(e, 11) ^
rotateRight(e, 25);

const uint32_t choose =
(e & f) ^
((~e) & g);

const uint32_t temp1 =
h +
bigSigma1 +
choose +
constants[index] +
words[index];

const uint32_t bigSigma0 =
rotateRight(a, 2) ^
rotateRight(a, 13) ^
rotateRight(a, 22);

const uint32_t majority =
(a & b) ^
(a & c) ^
(b & c);

const uint32_t temp2 =
bigSigma0 +
majority;

h = g;
g = f;
f = e;
e = d + temp1;
d = c;
c = b;
b = a;
a = temp1 + temp2;
}

hash[0] += a;
hash[1] += b;
hash[2] += c;
hash[3] += d;
hash[4] += e;
hash[5] += f;
hash[6] += g;
hash[7] += h;
}

std::ostringstream output;
output << std::hex
<< std::setfill('0');

for (const uint32_t value : hash) {
output << std::setw(8)
<< value;
}

return output.str();
}
}

PEParser::PEParser(
const std::string& filePath,
const std::string& outputDirectory
Expand Down Expand Up @@ -720,6 +894,9 @@ bool PEParser::exportTextReport() const {
staticRiskScore
);

const std::string sha256Hash =
calculateSha256Hex(fileData);

report
<< "Static Analysis Report\n"
<< "Target: "
Expand All @@ -728,6 +905,9 @@ bool PEParser::exportTextReport() const {
<< "Target File Size: "
<< fileData.size()
<< " bytes\n"
<< "SHA-256: "
<< sha256Hash
<< "\n"
<< "Generated At: "
<< currentTimestamp()
<< "\n"
Expand Down Expand Up @@ -976,6 +1156,9 @@ bool PEParser::exportJsonReport() const {
staticRiskScore
);

const std::string sha256Hash =
calculateSha256Hex(fileData);

report
<< "{\n"
<< " \"target\": \""
Expand All @@ -984,6 +1167,9 @@ bool PEParser::exportJsonReport() const {
<< " \"targetFileSize\": "
<< fileData.size()
<< ",\n"
<< " \"sha256\": \""
<< escapeJsonString(sha256Hash)
<< "\",\n"
<< " \"generatedAt\": \""
<< escapeJsonString(currentTimestamp())
<< "\",\n"
Expand Down
Loading