summaryrefslogtreecommitdiff
path: root/modules/entropy.py
blob: 48b29240fb3a7e44c68ebc9d21731c1b9071e3f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
''' This module uses shannon's Entropy to detect packed malwares
'''
import os
import math
import logging
logging.basicConfig(level=logging.DEBUG)

import scanmodule

def main():
    return Entropy()

class Entropy(scanmodule.ScanModule):
    name = 'entropy'
    def populate(self, path):
        pass
    def load(self, path):
        pass
    def save(self, path):
        pass

    def __compute_score(self, path):
        return (self.__entropy(path) - 5) * 100

    def is_malware(self, path):
        score = self.__compute_score(path)
        logging.info('Entropy score for ' + path + ' : ' + str(score))
        return  score > 75

    def evaluate(self, path):
        ''' Computes an arbitraty score for the given path
        @ret A sorted list of the form [name, match_in_percent_superior_to_zero]
        '''
        score = self.__compute_score(path)
        if score > 0:
            return [['MALWARE', score],]
        return None

    def __entropy(self, path):
        ''' Computes shannon's entropy for the given file
        @param path Path to the file
        '''
        # Computes the frequency of each byte in the file
        fsize = max(float(os.path.getsize(path)), 1.0)

        freq = [0] * 256
        with open(path, 'rb') as f:
            for c in f.read():
                freq[ord(c)] += 1

        entropy = 0.0
        for f in freq:
            if f:
                f /= fsize
                entropy += f * math.log(f, 2)
        return -entropy