Skip to content
Snippets Groups Projects
Commit e4f1d1a8 authored by Juan Diego Campo's avatar Juan Diego Campo
Browse files

First version of json input generation

parent 1ddfa74f
No related branches found
No related tags found
No related merge requests found
*.o
*.class
*.jar
*.log
modsec_ml
......@@ -2,12 +2,15 @@ CPPFLAGS=-g -pthread -I/usr/local/modsecurity/include
LDFLAGS=-g
LDLIBS=-L/usr/local/modsecurity/lib -lmodsecurity -lstdc++
all: modsec_ml test.class
all: modsec_ml test.jar
modsec_ml: modsec_ml.o
modsec_ml.o: modsec_ml.cpp
test.class: test.java
javac test.java
test.jar: test.class
jar cfe test.jar test test.class
clean:
rm -f modsec_ml modsec_ml.o test.class
SecRuleEngine On
SecRequestBodyAccess On
SecDebugLog ./debug.log
SecDebugLog /etc/nginx/debug.log
SecDebugLogLevel 9
SecAuditEngine RelevantOnly
SecAuditLog ./audit.log
SecRuleScript ml.lua "phase:2,deny,status:403,log"
# SecRule ARGS n "id:15,phase:2,deny,status:403,log"
SecRuleScript /home/jdcampo/gsi/modsec/modsec-ml/ml.lua "phase:2,deny,status:403,log"
function split(str, pat)
local t = {}
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
function main()
local input = 'testing....\
testing2...'
local json = require("json")
local input = "{\n"
input = input.."\t\"unique_id\": "..json.encode(m.getvar("UNIQUE_ID"))..",\n"
input = input.."\t\"method\": "..json.encode(m.getvar("REQUEST_METHOD"))..",\n"
input = input.."\t\"protocol\": "..json.encode(m.getvar("REQUEST_PROTOCOL"))..",\n"
input = input.."\t\"uri\": "..json.encode(m.getvar("REQUEST_URI"))..",\n"
if m.getvar("REQUEST_BODY") then
input = input.."\t\"body\": "..json.encode(m.getvar("REQUEST_BODY"))..",\n"
else
input = input.."\t\"body\": \"\",\n"
end
-- FIXME: These var don't work: ARGS_GET, ARGS_POST, REQUEST_HEADERS
input = input.."\t\"argsGet\": {\n"
-- local args_get = m.getvars("ARGS_GET")
local args_get = split(m.getvar("REQUEST_URI"),"?")
args_get = split(args_get[#args_get],"&")
for i = 1, #args_get do
arg = split(args_get[i],"=")
input = input.."\t\t"..json.encode(arg[1])..": "
input = input..json.encode(arg[2])..",\n"
end
input = input.."\t},\n"
input = input.."\t\"headers\": {\n"
-- local headers = m.getvars("REQUEST_HEADERS")
local headers = split(m.getvar("FULL_REQUEST"),"\n\n")[1]
headers = split(headers,"\n")
for i = 1, #headers do
header = split(headers[i],": ")
input = input.."\t\t"..json.encode(header[1])..": "
input = input..json.encode(header[2])..",\n"
end
input = input.."\t},\n"
input = input.."\t\"argsPost\": {\n"
-- local args_post = m.getvars("ARGS_POST")
local args_post = {}
for i = 1, #args_post do
input = input.."\t\t"..json.encode(args_post[i].name)..": "
input = input..json.encode(args_post[i].value)..",\n"
end
input = input.."\t},\n"
input = input.."}\n"
m.log(3, "ML - input: \n"..input)
-- As far as I can see, external program input can only be done
-- through a tmp file
local tmpfile = './lua_pipe'
local tmpfile = '/tmp/lua_pipe'
local f = io.open(tmpfile, 'w')
f:write(input)
f:close()
cmd = 'java test <'..tmpfile
cmd = 'java -jar /usr/local/nginx/modules/test.jar <'..tmpfile
local p = io.popen(cmd, 'r')
local output = p:read('*a')
local _, exit, status = p:close()
local output = p:lines()
m.log(3, "ML - res:\n")
for line in output do
m.log(3, line)
end
p:close()
os.remove(tmpfile)
print("exit: "..exit)
print("status: "..status)
print(output)
return nil;
end
import java.util.Scanner;
public class test {
class test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
while (input.hasNextLine()) {
String line = input.nextLine();
line = line.replaceAll("testing", "tested");
System.out.println(line);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment