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 221 222 223 224 225 226 227 228 229 230 231 232 233 234
| json = require "cjson.safe" md5 = require "md5" redis = require "redis"
login_url = "/login"
success_code = 0
event_name = "login_audit"
event_type = "lua"
name = "login_audit.json"
proto = "TCP"
host = "127.0.0.1" port = 6379
http_common_mapping = '{"accept":"accept","accept-charset":"accept_charset","accept-encoding":"accept_encoding","accept-language":"accept_language","user-agent":"user_agent"}' common_mapping_table = json.decode(http_common_mapping)
function md5Encode(args) m = md5.new() m:update(args) return md5.tohex(m:finish()) end
function formatStr(args) t = {} ios = string.match(args, 'canon') if ios ~= nil then mail = 'email"%s+(.-)%s' t['email'] = string.match(args, mail) else data = string.split(args, '&') for n, v in ipairs(data) do d = string.split(v, '=') t[d[1]] = d[2] end end return t end
function string.split(s, p) rt = {} string.gsub(s, '[^'..p..']+', function(w) table.insert(rt, w) end ) return rt end
function init (args) local needs = {} needs["protocol"] = "http" return needs end
function setup (args) filename = SCLogPath() .. "/" .. name file = assert(io.open(filename, "a")) SCLogInfo("app_login_audit filename: " .. filename) http = 0 SCLogInfo("Connect Redis Server...") client = redis.connect(host, port) response = client:ping() if response then SCLogInfo("Redis Server connection succeeded.") end end
function log(args) http_table = {}
ti = { tags = {} }
score = 50
http_hostname = HttpGetRequestHost() http_url = HttpGetRequestUriNormalized() rl = HttpGetRequestLine() if rl then http_method = string.match(rl, "%w+") if http_method then http_table["method"] = http_method end end if http_url == login_url and http_method == "POST" then http_table["hostname"] = http_hostname http_table["url"] = http_url http_table["url_path"] = http_url rsl = HttpGetResponseLine() if rsl then status_code = string.match(rsl, "%s(%d+)%s") http_table["status"] = tonumber(status_code)
http_protocol = string.match(rsl, "(.-)%s") http_table["protocol"] = http_protocol end
a, o, e = HttpGetResponseBody() if a then for n, v in ipairs(a) do body = json.decode(v) results_code = tonumber(body["code"]) if results_code == success_code then http_table["results"] = "success" else http_table["results"] = "failed" end end http_table["results_code"] = results_code end
a, o, e = HttpGetRequestBody() if a then for n, v in ipairs(a) do res = formatStr(v) if res["email"] then black_ioc = client:get(res["email"]) if black_ioc then ti["provider"] = "Canon" ti["producer"] = "NTA" table.insert(ti["tags"], "account in blacklist") score = score + 10 end end end end
rh = HttpGetRequestHeaders() if rh then for k, v in pairs(rh) do key = string.lower(k) request_var = request_mapping_table[key] if request_var then http_table[request_var] = v end end end
rsh = HttpGetResponseHeaders() if rsh then for k, v in pairs(rsh) do key = string.lower(k) response_var = response_mapping_table[key] if response_var then http_table[response_var] = v end end end
sec, usec = SCPacketTimestamp() timestring = os.date("!%Y-%m-%dT%T", sec) .. '.' .. usec .. '+0000' ip_version, src_ip, dst_ip, protocol, src_port, dst_port = SCFlowTuple()
id = SCFlowId() flow_id = string.format("%.0f", id) flow_id = tonumber(flow_id)
true_client_ip = HttpGetRequestHeader("True-Client-IP") if true_client_ip ~= nil then src_ip = true_client_ip end
tetrad = src_ip .. src_port .. dst_ip .. dst_port session_id = md5Encode(tetrad)
raw_data = { timestamp = timestring, flow_id = flow_id, session_id = session_id, src_ip = src_ip, src_port = src_port, proto = proto, dest_ip = dst_ip, dest_port = dst_port, event_name = event_name, event_type = event_type, app_type = app_type, http = http_table, ti = ti, score = score }
data = json.encode(raw_data)
file:write(data .. "\n") file:flush()
http = http + 1 end
end
function deinit (args) SCLogInfo ("app_login_audit transactions logged: " .. http); file:close(file) end
|