-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathShellSweep.lua
161 lines (142 loc) · 6.06 KB
/
ShellSweep.lua
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
-- Entropy thresholds and operations for each file extension using a Lua table
-- Each file extension maps to a nested Lua table with 'operation' and 'value' fields.
-- Adjust the values based on your requirements.
-- ASCII art
local asciiArt = [[
██████ ██░ ██ ▓█████ ██▓ ██▓ ██████ █ █░▓█████ ▓█████ ██▓███
▒██ ▒ ▓██░ ██▒▓█ ▀ ▓██▒ ▓██▒ ▒██ ▒ ▓█░ █ ░█░▓█ ▀ ▓█ ▀ ▓██░ ██▒
░ ▓██▄ ▒██▀▀██░▒███ ▒██░ ▒██░ ░ ▓██▄ ▒█░ █ ░█ ▒███ ▒███ ▓██░ ██▓▒
▒ ██▒░▓█ ░██ ▒▓█ ▄ ▒██░ ▒██░ ▒ ██▒░█░ █ ░█ ▒▓█ ▄ ▒▓█ ▄ ▒██▄█▓▒ ▒
▒██████▒▒░▓█▒░██▓░▒████▒░██████▒░██████▒▒██████▒▒░░██▒██▓ ░▒████▒░▒████▒▒██▒ ░ ░
▒ ▒▓▒ ▒ ░ ▒ ░░▒░▒░░ ▒░ ░░ ▒░▓ ░░ ▒░▓ ░▒ ▒▓▒ ▒ ░░ ▓░▒ ▒ ░░ ▒░ ░░░ ▒░ ░▒▓▒░ ░ ░
░ ░▒ ░ ░ ▒ ░▒░ ░ ░ ░ ░░ ░ ▒ ░░ ░ ▒ ░░ ░▒ ░ ░ ▒ ░ ░ ░ ░ ░ ░ ░ ░░▒ ░
░ ░ ░ ░ ░░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░░
░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░
]]
print(asciiArt)
local fileExtensions = {
['.asp'] = {
{ operation = 'lt', value = 0.805376867704514 },
{ operation = 'gt', value = 5.51268104400858 }
},
['.ashx'] = {
{ operation = 'gt', value = 3.75840459657413 }
},
['.asax'] = {
{ operation = 'gt', value = 3.7288741494524 }
},
['.jspx'] = {
{ operation = 'gt', value = 4.87651397975203 }
},
['.html'] = {
{ operation = 'gt', value = 4.8738392644771 }
},
['.aspx'] = {
{ operation = 'lt', value = 0.805376867704514 },
{ operation = 'gt', value = 4.15186444439319 }
},
['.php'] = {
{ operation = 'gt', value = 4.23015141285636 }
},
['.jsp'] = {
{ operation = 'gt', value = 4.40958415652662 }
},
['.js'] = {
{ operation = 'gt', value = 4.25868439013462 }
}
}
-- Calculate the entropy of a given string
local function getEntropy(str)
local length = #str
local symbolFrequency = {}
for i = 1, length do
local symbol = str:sub(i, i)
if symbolFrequency[symbol] then
symbolFrequency[symbol] = symbolFrequency[symbol] + 1
else
symbolFrequency[symbol] = 1
end
end
local entropy = 0
for _, frequency in pairs(symbolFrequency) do
local freq = frequency / length
entropy = entropy - (freq * math.log(freq, 2))
end
return entropy
end
-- Directories to scan
local directoryPaths = {
'/opt/webshells'
}
-- Directories to exclude
local excludePaths = {
'/path/to/exclude1',
'/path/to/exclude2',
'/path/to/exclude3'
}
-- File hashes to ignore
local ignoreHashes = {
'FE3F0B4326FF9754CB8B61AA3CEFB465A5308658064EE51C41B0A8B50027728D',
'B6675117A7B174C3AA2510DDDEFF4221BA6E31005333F47C7239ED5D055BBBDD',
'54EFA324203B762A03033879057F8A9DB0F7B45C83C8E1A40529CAFF1EB18004',
'71FE41C6CCB0023576483A1C89929255480A4F5F0F07CFF9A8D2030ECF70E7AE'
}
-- Read the hashes from the file into an array (if needed)
local ignoreHashesFilePath = 'path_to_your_file.txt'
local file = io.open(ignoreHashesFilePath, 'r')
if file then
ignoreHashes = {}
for line in file:lines() do
table.insert(ignoreHashes, line)
end
file:close()
end
local webshellFound = false
-- Walk through each directory and flag files with high/low entropy
for _, directoryPath in ipairs(directoryPaths) do
for file in io.popen('find "'..directoryPath..'" -type f'):lines() do
local exclude = false
for _, excludePath in ipairs(excludePaths) do
if file:find('^'..excludePath) then
exclude = true
break
end
end
local extension = file:match('^.+(%..+)$')
if extension and fileExtensions[extension] and not exclude then
local f = io.open(file, 'r')
if f then
local content = f:read('*all')
f:close()
local entropy = getEntropy(content)
local hash = io.popen('sha256sum "'..file..'"'):read():match('^([%w%d]+)')
for _, condition in ipairs(fileExtensions[extension]) do
local operation = condition.operation
local value = condition.value
local metCondition = false
if operation == 'gt' then
if entropy > value then
metCondition = true
end
elseif operation == 'lt' then
if entropy < value then
metCondition = true
end
elseif operation == 'eq' then
if entropy == value then
metCondition = true
end
end
if metCondition and not ignoreHashes[hash] then
print('Possible webshell found: '..file..', Entropy: '..entropy..', Hash: '..hash)
webshellFound = true
end
end
end
end
end
end
-- If no webshells were found
if not webshellFound then
print('No evil identified today.')
end