UM Virus Detecting: Part 3 - Packed

Good morning! It's me, seb here with Part 3. We already covered import and signature scanning focused on Easy SuperInfector stubs. Now let’s talk about packing detection. It’s a basic but useful technique for spotting packed malware.


First, packing means compressing or encrypting the executable to hide its real code. Packed files have high randomness in their data, so we can detect that using entropy.


Here’s how I calculate entropy:

double calc_entropy(const BYTE* data, size_t size) {
    if (size == 0) return 0.0;

If the section size is zero, entropy is zero, nothing to scan here.

unsigned int freq[256] = { 0 };
    for (size_t i = 0; i < size; i++)
        freq[data[i]]++;

Count how many times each byte value (0-255) appears in the data. This frequency distribution is the basis for entropy calculation.


double ent = 0.0;
    for (int i = 0; i < 256; i++) {
        if (freq[i] == 0) continue;
        double p = (double)freq[i] / size;
        ent -= p * log2(p);
    }
    return ent;
}

Calculate entropy using Shannon’s formula. It sums up the randomness based on byte frequency. Higher entropy means more randomness, likely packing or encryption.


Next, I open the file and map it to memory:

No file, no scan.


Ignore tiny files, usually irrelevant or corrupted.


Memory mapping for faster access.


Then I grab the PE headers:

Without headers, no PE info, so no scanning.


Cap sections scanned to 96 max, enough for anything reasonable.


For each section:

Make sure section header is safe to read.


Validate raw section data pointer and size.


Calculate entropy:

High entropy means packed or compressed.


If any section is too random, flag file as packed.


Packing detection like this is rough but helpful. Most legit apps aren’t packed, so it filters suspicious files. Easy SuperInfector stubs usually aren’t packed (they did infact become packed later on to counter some of my methods), so this check just adds a safety net. I’ll improve this in future versions.

Last updated