forked from FladeX/homakov.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobfuscator.rb
86 lines (70 loc) · 1.73 KB
/
obfuscator.rb
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
class Rack::Obfuscator
def initialize(app)
@app = app
@obfuscate = [
['GET','/signin']
]
@protect = [
['POST','/auth/identity/callback']
]
@dict = {}
end
def call(env)
pair = [env['REQUEST_METHOD'],env['REQUEST_PATH']]
if @protect.include? pair
invalid_request
elsif env['REQUEST_PATH'].length == 21
puts "checking #{env['REQUEST_PATH']} #{@dict}"
if new_path = @dict.delete(env['REQUEST_PATH'][1..-1])
puts "Setting new path #{new_path}"
#env['REQUEST_URI'] = new_path
#env['REQUEST_PATH'] = new_path
env['PATH_INFO'] = new_path
input = deobfuscate(env['rack.input'].read)
env['rack.input'] = StringIO.new(input)
env['QUERY_STRING'] = deobfuscate(env['QUERY_STRING'])
end
end
response = @app.call(env)
if @obfuscate.include? pair
[response[0], response[1], [obfuscate(response[2][0])]]
else
response
end
end
def invalid_request
[500,{},["invalid request"]]
end
def obfuscate(text)
r = /<form([^>]*?)action="([^"]+)"/m
text = text.gsub(r) do |m|
m =~ r
hash = gen
@dict[hash] = $2
"<form #{$1} action=\"/#{hash}\""
end
r = /<input([^>]+)name="([^"]*?)"/m
text = text.gsub(r) do |m|
m =~ r
hash = gen
@dict[hash] = $2
"<input#{$1}name=\"#{hash}\""
end
end
def deobfuscate(string)
return string if string.length < 10
hash = Rack::Utils.parse_query(string)
new_hash = {}
hash.each{|k,v|
if new_k = @dict.delete(k)
new_hash[new_k] = v
else
new_hash[k] = v
end
}
Rack::Utils.build_query(new_hash)
end
def gen
SecureRandom.hex(10)
end
end