Module: Injector

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

Constant Summary collapse

COMMENT_START =
'# '

Instance Method Summary collapse

Instance Method Details

#inject_to_body(msg) ⇒ Object



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

def inject_to_body msg
  scanf = false
  Log 2, "#{@mid}inject_to_body req: #{msg.req_str}"
  msg.request_headers.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.req_str.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.req_str.match(/([^=]+)=([^=]+)/,pos)) 
        req = msg.req_str[0..m.begin(2)-1] + l + msg.req_str[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



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

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")
    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



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

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
  
  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 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