Module: Pentest::AstUtils

Defined in:
lib/pentest/ast_utils.rb

Class Method Summary collapse

Class Method Details

.get_params_key(exp) ⇒ Object

Match “params” and return :hoge



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pentest/ast_utils.rb', line 21

def get_params_key(exp)
  return nil unless Sexp === exp

  if exp[0] == :call
    type, callee, method, arg = exp
    if is_params?(callee) && method == :[]
      if Sexp === arg && arg[0] == :lit
        return arg[1]
      end
      if Sexp === arg && arg[0] == :str
        return arg[1].to_sym
      end
    end
  end
  
  nil
end

.is_params?(exp) ⇒ Boolean

Match “params”

Returns:

  • (Boolean)


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

def is_params?(exp)
  return false unless Sexp === exp
  
  if exp[0] == :call
    type, callee, method = exp
    if callee.nil? && method == :params
      true
    else
      false
    end
  end
end

.search_for_params(exp) ⇒ Object



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

def search_for_params(exp)
  return Set.new unless Sexp === exp

  ret = Set.new

  params_key = get_params_key(exp)

  unless params_key.nil?
    ret << [params_key, nil, nil]
  end

  if exp[0] == :call
    type, callee, method, arg = exp

    callee_params = get_params_key(callee)
    if callee_params.nil?
      ret.merge search_for_params callee
    else
      ret << [callee_params, :callee, method, arg]
    end

    arg_params = get_params_key(arg)
    if arg_params.nil?
      ret.merge search_for_params arg
    else
      ret << [arg_params, :call_arg, callee, method]
    end
  else
    exp.each do |child|
      ret.merge search_for_params child
    end
  end

  ret
end