This repository has been archived by the owner on Jun 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsize.py
128 lines (100 loc) · 4.12 KB
/
size.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# -*- coding: utf-8 -*-
# This file is part of Viper - https://github.com/viper-framework/viper
# See the file 'LICENSE' for copying permission.
import os
from viper.common.out import bold
from viper.common.abstracts import Module
from viper.core.session import __sessions__
from viper.core.database import Database
from viper.core.storage import get_sample_path
class SIZE(Module):
cmd = 'size'
description = 'Size command to show/scan/cluster files'
authors = ['emdel']
def __init__(self):
super(SIZE, self).__init__()
self.parser.add_argument('-a', '--all', action='store_true', help='Show all')
self.parser.add_argument('-c', '--cluster', action='store_true', help='Cluster')
self.parser.add_argument('-s', '--scan', action='store_true', help='Scan')
self.file_size = 0
def __check_session(self):
if not __sessions__.is_set() and (not self.args.all and not self.args.cluster):
self.log('error', "No open session. This command expects a file to be open.")
return False
return True
def size_all(self):
db = Database()
samples = db.find(key='all')
rows = []
for sample in samples:
sample_path = get_sample_path(sample.sha256)
if not os.path.exists(sample_path):
continue
try:
cur_size = os.path.getsize(sample_path)
except Exception as e:
self.log('error', "Error {0} for sample {1}".format(e, sample.sha256))
continue
rows.append([sample.md5, sample.name, cur_size])
self.log('table', dict(header=['MD5', 'Name', 'Size (B)'], rows=rows))
return
def size_cluster(self):
db = Database()
samples = db.find(key='all')
cluster = {}
for sample in samples:
sample_path = get_sample_path(sample.sha256)
if not os.path.exists(sample_path):
continue
try:
cur_size = os.path.getsize(sample_path)
except Exception as e:
self.log('error', "Error {0} for sample {1}".format(e, sample.sha256))
continue
if cur_size not in cluster:
cluster[cur_size] = []
cluster[cur_size].append([sample.md5, sample.name])
for cluster_name, cluster_members in cluster.items():
# Skipping clusters with only one entry.
if len(cluster_members) == 1:
continue
self.log('info', "Cluster size {0} with {1} elements".format(bold(cluster_name), len(cluster_members)))
self.log('table', dict(header=['MD5', 'Name'], rows=cluster_members))
def size_scan(self):
db = Database()
samples = db.find(key='all')
rows = []
for sample in samples:
if sample.sha256 == __sessions__.current.file.sha256:
continue
sample_path = get_sample_path(sample.sha256)
if not os.path.exists(sample_path):
continue
try:
cur_size = os.path.getsize(sample_path)
except Exception:
continue
if self.file_size == cur_size:
rows.append([sample.md5, sample.name])
if len(rows) > 0:
self.log('info', "Following are samples with size {0}".format(bold(self.file_size)))
self.log('table', dict(header=['MD5', 'Name'], rows=rows))
def run(self):
super(SIZE, self).run()
if self.args is None:
return
if not self.__check_session():
self.log('error', 'At least one of the parameters is required')
self.usage()
return
if self.args.all:
self.size_all()
elif self.args.cluster:
self.size_cluster()
elif self.args.scan:
self.file_size = __sessions__.current.file.size
self.log("info", "Size: {0} B".format(self.file_size))
self.size_scan()
else:
self.log('error', 'At least one of the parameters is required')
self.usage()