File size: 1,366 Bytes
dce6da2
 
 
 
461e792
 
dce6da2
461e792
 
 
dce6da2
461e792
 
 
dce6da2
461e792
 
 
 
 
dce6da2
461e792
 
dce6da2
461e792
 
dce6da2
461e792
 
 
 
 
dce6da2
 
461e792
 
 
 
 
dce6da2
461e792
 
dce6da2
461e792
 
 
 
 
 
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
"""
Contains the CNN model for malware classification.
"""

import torch.nn as nn


class MalwareCNN(nn.Module):
    """
    Convolutional Neural Network for classifying malware families based on byte images.

    Args:
        num_classes (int): Number of unique malware families to classify.
    """

    def __init__(self, num_classes=24):
        super(MalwareCNN, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(1, 32, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(2, 2),  # 128 -> 64
            nn.Conv2d(32, 64, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(2, 2),  # 64 -> 32
            nn.Conv2d(64, 128, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(2, 2),  # 32 -> 16
        )
        self.classifier = nn.Sequential(
            nn.Flatten(),
            nn.Linear(128 * 16 * 16, 512),
            nn.ReLU(),
            nn.Dropout(0.6),  # Increased dropout to prevent overfitting
            nn.Linear(512, num_classes),
        )

    def forward(self, x):
        """
        Forward pass through the CNN.

        Args:
            x (torch.Tensor): Input batch of images.

        Returns:
            torch.Tensor: Logits for each class.
        """
        x = self.features(x)
        x = self.classifier(x)
        return x