Module: Injector

Defined in:
lib/bscan/modules/injector.rb

Overview

Copyright © 2015, Oleg Gryb

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Constant Summary collapse

COMMENT_START =
'# '

Instance Method Summary collapse

Instance Method Details

#inject_to_body(msg, msg_hdrs) ⇒ Object



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
# File 'lib/bscan/modules/injector.rb', line 116

def inject_to_body msg,msg_hdrs
  scanf = false
  Log 2, "#{@mid}inject_to_body req: #{msg.getRequest()}"
  msg_hdrs.each do |a|
   Log 2, "#{@mid}inject_to_body hdr: #{a[0]} #{a[1]}"
   if a.size > 1 and a[0] =~ /content-type/i and a[1] =~ /application\/x-www-form-urlencoded/i
      scanf = true
      break
    end
  end  
  return if not scanf
  m=msg.getRequest().match(/\r?\n\r?\n/)
  return if m.size < 1
  start_pos = m.end(0)

  begin
    injs = open_in_path(@config[prop('file')])
    injs.each_line do |l|
      l.chomp!
      next if (l =~ /^#{COMMENT_START}/ or l.length < 1)
      Log 2, "#{@mid}inject_to_body injecting: #{l}"
      pos=start_pos
      while (m=msg.getRequest().match(/([^=]+)=([^=]+)/,pos)) 
        req = msg.getRequest()[0..m.begin(2)-1] + l + msg.getRequest()[m.end(2)..-1]
        req.sub!(/content-length\s*:\s*\d+/i, "Content-Length: "+(req.length-start_pos).to_s)
        Log 2, "#{@mid}inject_to_body #{pos} #{req}"
        @activity[0]=true
        send_req req, msg.getProtocol, l
        pos=m.end(1)+1
      end
    end
    injs.close
  rescue Exception => e
    Log 0, "#{@mid}inject_to_body Exception: #{e.message}"
    Log 0, e.backtrace.join("\n")
  end
end

#inject_to_patternObject



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
# File 'lib/bscan/modules/injector.rb', line 74

def inject_to_pattern
  param = @config[prop('inject_instead_of')]
  a=[]
  if (not param or (a=param.split(':',3)).size < 3)
      Log 0, "#{@mid}inject_to_pattern: 'inject_instead_of' parameter is not valid #{param}"
      return
  end
  Log 2, "#{@mid}inject_to_pattern input: #{a.join('|')}"
  begin
    p,f,proto = Regexp.escape(a[0]), a[1], a[2]
    file = open_in_path(f)
    req = file.read
    req.gsub!(/\^M\n/,"\r\n")
    replace_params(req)
    file.close

    injs = open_in_path(@config[prop('file')])
    injs.each_line do |l|
      l.chomp!
      next if (l =~ /^#{COMMENT_START}/ or l.length < 1)
      Log 2, "#{@mid}inject_to_pattern injecting: #{l}"

      pos = 0
      while (m=req.match(/(#{p}).*?(#{p})/,pos)) 
        r = (req[0..m.begin(1)-1] + l + req[m.end(2)..-1]).gsub /#{p}(.*?)#{p}/,'\1'

        Log 2, "#{@mid}inject_to_pattern new req:\n#{r}"
        set_len r
        @activity[0]=true
        send_req r,proto,l 
        pos=m.end(1)
      end
    end
    injs.close
    
  rescue Exception => e
      Log 0, "#{@mid}inject_to_pattern Exception: #{e.message}"
      Log 0, e.backtrace.join("\n")
  end
  
end

#run(*args) ⇒ Object



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
# File 'lib/bscan/modules/injector.rb', line 17

def run *args

  @config ||= @bscan_config
  @activity[0]=true

  @prop_pref = 'bscan.injector.'
  @prop_pref += args[2] + '.' if args[2] && args[2].length > 0
  @mid = args[2]?"Injector.#{args[2]}.":'Injector.' 
  msg = args[1]
  
  if not msg
    inject_to_pattern
    return
  end
  
  msg_info = @burp_cb.getHelpers().analyzeRequest(msg)
  msg_body = msg.getRequest()[msg_info.getBodyOffset()..-1]
  msg_hdrs = msg_info.getHeaders()
  msg_url = msg_info.getUrl().toString()
  
  url = msg_url.dup.to_s
  Log 2, "#{@mid}run for #{url}"
  begin
    if (url =~ /([^?]+)\?(.+)/)
      beg = "#{$1}?"
      params = $2
      Log 2, "#{@mid}run BEG: #{beg} PARAMS: #{params} FILE: #{@config[prop('file')]}"
      injs = open_in_path(@config[prop('file')])
      injs.each_line do |l|
        l.chomp!
        next if (l =~ /^#{COMMENT_START}/ or l.length < 1)
        Log 2, "#{@mid}run injecting: #{l}"
       
       @activity[0]=true

        do_scan(msg, beg + l, l)       # in parameter name
        do_scan(msg, beg.chop + l, l)  # in  URL
        
        pos=0
        while (m=params.match(/([^&]+)=([^&]+)/,pos)) 
          trg = beg + params[0..m.begin(2)-1] + l + params[m.end(2)..-1]
          @activity[0]=true
          do_scan(msg, trg, l)
          pos=m.end(1)+1
        end
      end
      injs.close
    end
    
    inject_to_body(msg, msg_hdrs) if @config['bscan.injector.one.inject_to_body'] == 'true'
    
  rescue Exception => e
    Log 0, "#{@mid}run Exception: #{e.message}"
    Log 0, e.backtrace.join("\n")
 end
end