· 8 years ago · Apr 18, 2018, 11:34 AM
1module Rack
2 #
3 # The LieServer is a simple Rack middleware app which allows one to spoof
4 # the +Server+ header in responses for every request, requests to certain
5 # sub-directories or paths which match a regular expression.
6 #
7 # Be deceitful to would be attackers, tell them your running IIS 3.0.
8 #
9 # MIT License - Hal Brodigan (postmodern.mod3 at gmail.com)
10 #
11 class LieServer
12
13 #
14 # Initializes the lie server.
15 #
16 # @param [#call] app
17 # The Rack app to lie for.
18 #
19 # @param [Hash{Regexp,String => String}] options
20 # Additional lie options.
21 #
22 # @example
23 # use Rack::LieServer, '/' => 'IIS 3.0'
24 #
25 # @example
26 # use Rack::LieServer, /\.asp$/ => 'Apache',
27 # '/' => 'Nginx'
28 #
29 def initialize(app,options={})
30 @app = app
31
32 patterns = []
33 paths = {}
34
35 options.each do |pattern,lie|
36 if pattern.kind_of?(Regexp)
37 patterns << [pattern, lie]
38 else
39 paths[pattern] = lie
40 end
41 end
42
43 @routes = patterns + paths.sort.reverse
44 end
45
46 def call(env)
47 code, headers, body = @app.call(env)
48 path = env['PATH_INFO']
49
50 pattern, lie = @routes.find do |pattern,lie|
51 if pattern.kind_of?(Regexp)
52 path =~ pattern
53 else
54 path[0,pattern.length] == pattern
55 end
56 end
57
58 headers['Server'] = lie if lie
59
60 [code, headers, body]
61 end
62
63 end
64end