summaryrefslogtreecommitdiffstats
path: root/snort-model.lua
blob: 1746b1cd329aca2a9d340944eb65758884206ee9 (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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
-- acf model for displaying logfiles recusivly 
module (..., package.seeall)

-- no initializer in model - use controller.init for that

require("posix")
require("fs")

-- START SORT ################################################################################
--[[
function __genOrderedIndex( t )
    local orderedIndex = {}
    for key in pairs(t) do
        table.insert( orderedIndex, key )
    end
    table.sort( orderedIndex )
    return orderedIndex
end

function orderedNext(t, state)
    -- Equivalent of the next function, but returns the keys in the alphabetic
    -- order. We use a temporary ordered key table that is stored in the
    -- table being iterated.

    --print("orderedNext: state = "..tostring(state) )
    if state == nil then
        -- the first time, generate the index
        t.__orderedIndex = __genOrderedIndex( t )
        key = t.__orderedIndex[1]
        return key, t[key]
    end
    -- fetch the next value
    key = nil
    for i = 1,table.getn(t.__orderedIndex) do
        if t.__orderedIndex[i] == state then
            key = t.__orderedIndex[i+1]
        end
    end

    if key then
        return key, t[key]
    end

    -- no more value to return, cleanup
    t.__orderedIndex = nil
    return
end

function orderedPairs(t)
    -- Equivalent of the pairs() function on tables. Allows to iterate
    -- in order
    return orderedNext, t, nil
end
--]]
-- END SORT ################################################################################


local function get_version()
	local cmd = "snort -V 2>&1 | grep Version | sed 's/.*ersion\ /snort-/'"
	local cmd_output = io.popen( cmd )
	local cmd_output_result = cmd_output:read("*a") or ""
	cmd_output:close()
	return cmd_output_result
end

local is_running = function( process )
	local statusreport = nil
	local cmdoutput = {}
	local cmd, error = io.popen("pidof " .. process ,r)
	local cmdoutput = string.gsub(cmd:read("*a"), "%s", "")
	cmd:close()
	if (cmdoutput ~= "") then
		statusreport = "Running"
	else
		statusreport = "Stopped"
	end
	return statusreport
end

-- ################################################################################
-- PUBLIC FUNCTIONS

getstatus = function (self)
	local status = {}
	local version = get_version()
	status.version = version
	local isrunning = is_running("snort")
	status.status = isrunning
	return status
end

service_control = function ( self, srvcmd ) 
	local srvcmd = string.lower(srvcmd)
	local retval = ""
	local line = ""
	if (srvcmd == "start") or (srvcmd == "stop") or (srvcmd == "restart") then
		local file = io.popen( "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin /etc/init.d/snort " .. srvcmd .. " 2>&1" )
		if file ~= nil then
			line = file:read( "*l" )
			while line ~= nil do
				retval = retval .. "\n" .. line
				line = file:read( "*l" )
			end
			file:close()
		end
	else
		retval = "Unknown command!"
	end
	return retval
end

xxxread_alert = function ()
	local alertfile = "/var/log/snort/alert"
	local alerts = ""
	local fileresult = {}
	local fileresultcnt = ""
	local presentation = {}
	local presentationtable = {}
	local liboutput = fs.read_file_as_array(alertfile)
	if (liboutput) then
		for k,v in ipairs(liboutput) do
			local generator,signature,revision = string.match(v, "^.*%[%*%*%]%s*%[(%d*):(%d*):(%d*).*")
			if (generator) and (signature) and (revision) then
			if not (fileresult[generator..":"..signature..":"..revision]) then
				fileresult[generator..":"..signature..":"..revision]={}
			end
				table.insert (fileresult[generator..":"..signature..":"..revision], v)
				local tablemax = table.maxn(fileresult[generator..":"..signature..":"..revision])
				fileresult[generator..":"..signature..":"..revision][tablemax]={}
				fileresult[generator..":"..signature..":"..revision][tablemax]["classification"]=string.match(liboutput[k+1],"^.*%[.*lassification:%s*(.*)%]%s*%[") or "Classification: unknown"
				fileresult[generator..":"..signature..":"..revision][tablemax]["priority"]=string.match(liboutput[k+1],"^.*%[.*lassification:%s*.*%]%s*%[(.*)%]") or "Priority: unknown"
				fileresult[generator..":"..signature..":"..revision][tablemax]["count"]=tablemax
				for i=0, 6 do
					if liboutput[k+i] == "" then break end
					if (liboutput[k+i-1]) then
						if not (string.match(liboutput[k+i],"^%[Classification.*")) then
							table.insert(fileresult[generator..":"..signature..":"..revision][tablemax],liboutput[k+i])
						end
					end
				end
			end
		end
		for k,v in pairs(fileresult) do
			table.insert(presentation,v)
		end
		for i = 1, table.maxn(presentation) do
			local maxn = table.maxn(presentation[i])
			presentationtable[i] = presentation[i][maxn]
		end
		alerts = table.maxn(presentationtable)
	else
		alerts = "0"
	end
	return alerts,presentationtable
end

read_alert = function ()
	local alertfile = "/var/log/snort/alert"
	local alertcount = 0
	local alertpriority = {}
	local alertprioritytmp = ""
	local priority = ""
	local classification = ""
	local currid = ""
	local prevrid = ""
	local count = {}
	local liboutput = fs.read_file_as_array(alertfile)
	if (liboutput) then
		for k,v in ipairs(liboutput) do
			--DEBUG
--			if (k == 1) then break end
			currid = string.match(v, "^.*%[%*%*%]%s*%[(%d+:%d+:%d+)%].*")
			if (currid) then
				local priority = string.match(liboutput[k+1],"^.*%[.*lassification:%s*.*%]%s*%[(.*)%]") or "Priority: Unknown"
				local classification = string.match(liboutput[k+1],"^.*%[.*lassification:%s*(.*)%]%s*%[") or "Classification: Unknown"
				if (alertpriority[priority] == nil) then
					alertpriority[priority] = {}
				end
				if (alertpriority[priority][classification] == nil) then
					alertpriority[priority][classification] = {}
				end
				alertpriority[priority][classification][currid] = {}
				if (alertpriority[priority][classification][currid]["value"] == nil) then
					alertpriority[priority][classification][currid]["value"] = {}
				end
				-- COUNTER
				if not (count[priority..classification..currid]) then
					count[priority..classification..currid] = 0
				end
				count[priority..classification..currid] = count[priority..classification..currid] + 1
				alertpriority[priority][classification][currid]["count"] = count[priority..classification..currid]
				for i=0, 10 do
					local rowvalue = liboutput[k+i]
					if (rowvalue == "") then
						break 
					end
					if (rowvalue) then
						table.insert(alertpriority[priority][classification][currid]["value"],rowvalue)
					end
				end
				alertcount = alertcount + 1
			end
		end
	end
--[[
t = {
    ['a'] = 'xxx',
    ['b'] = 'xxx',
    ['c'] = 'xxx',
    ['d'] = 'xxx',
    ['e'] = 'xxx',
}

	for key, val in orderedNext(t) do
		t=key
	end	
--]]
	return alertcount,alertpriority
end