Class: Pentest::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/pentest/endpoint.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(route, app_path, hooks) ⇒ Endpoint

Returns a new instance of Endpoint.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pentest/endpoint.rb', line 7

def initialize(route, app_path, hooks)
  @route = route
  @app_path = app_path
  @hooks = hooks

  @controller = route.defaults[:controller]
  @action = route.defaults[:action]

  return if @controller.nil? || @action.nil?

  @controller_name = ::ActiveSupport::Inflector.camelize(@controller) + "Controller"
  @controller_class = ::ActiveSupport::Inflector.constantize(@controller_name)

  @error_patterns = Set.new
end

Instance Attribute Details

#app_pathObject (readonly)

Returns the value of attribute app_path.



5
6
7
# File 'lib/pentest/endpoint.rb', line 5

def app_path
  @app_path
end

#routeObject (readonly)

Returns the value of attribute route.



5
6
7
# File 'lib/pentest/endpoint.rb', line 5

def route
  @route
end

Instance Method Details

#dispatch(payload) ⇒ Object



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
99
100
101
102
103
104
105
106
# File 'lib/pentest/endpoint.rb', line 60

def dispatch(payload)
  request = ActionDispatch::TestRequest.create
  request.request_method = @route.verb
  request.path = path(payload.params_hash)

  @hooks[:before_attacks].each do |before_attack_proc|
    before_attack_proc.call(request)
  end

  request.path_parameters = {
    controller: @controller,
    action: @action,
  }

  payload.params_hash.each do |param_parts, value|
    if param_parts.size == 1
      param, = param_parts
      if @route.required_parts.include? param
        request.path_parameters[param] = value
      else
        request.query_parameters[param] = value
      end
    elsif param_parts.size == 2
      request.query_parameters[param_parts[0]] ||= {}
      request.query_parameters[param_parts[0]][param_parts[1]] = value
    end
  end

  request.path_parameters.each do |param, value|
    request.update_param(param, value)
  end

  request.query_parameters.each do |param, value|
    request.update_param(param, value)
  end

  response = ActionDispatch::TestResponse.create

  err = nil
  begin
    @controller_class.new.dispatch(@action.to_sym, request, response)
  rescue => e
    err = e
  end

  [request, response, err]
end

#scan!(ingredients) ⇒ Object



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
# File 'lib/pentest/endpoint.rb', line 27

def scan!(ingredients)
  params = get_params

  Logger.info "#{@route.verb} #{path}"
  Logger.debug "Attacking #{@controller_class.inspect}##{@action}...", timestamp: false
  Logger.debug "Detected Parameters: #{params.to_a.inspect}", timestamp: false

  error_patterns = Set.new
  penetrated_payloads = []

  Logger.start_progress

  Checkers.run_checkers(self, params) do |checker|
    params.each_with_index do |param, injection_point|
      penetrated_payload, errors = checker.attack(param, injection_point, ingredients)

      accumulate_errors(errors)

      unless penetrated_payload.nil?
        penetrated_payloads << penetrated_payload
      end
    end
  end

  Logger.end_progress

  @error_patterns.each do |error_pattern|
    Logger.warn("Error: #{error_pattern}", timestamp: false)
  end

  penetrated_payloads
end

#valid?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/pentest/endpoint.rb', line 23

def valid?
  !@controller.nil? && !@action.nil? && @controller_class.method_defined?(@action.to_sym)
end