ronin-sql 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. data/COPYING.txt +339 -0
  2. data/History.txt +7 -0
  3. data/Manifest.txt +45 -0
  4. data/README.txt +66 -0
  5. data/Rakefile +14 -0
  6. data/lib/ronin/code/sql.rb +24 -0
  7. data/lib/ronin/code/sql/between.rb +62 -0
  8. data/lib/ronin/code/sql/binary_expr.rb +46 -0
  9. data/lib/ronin/code/sql/builder.rb +61 -0
  10. data/lib/ronin/code/sql/code.rb +35 -0
  11. data/lib/ronin/code/sql/common_dialect.rb +62 -0
  12. data/lib/ronin/code/sql/create_index.rb +76 -0
  13. data/lib/ronin/code/sql/create_table.rb +93 -0
  14. data/lib/ronin/code/sql/create_view.rb +65 -0
  15. data/lib/ronin/code/sql/delete.rb +64 -0
  16. data/lib/ronin/code/sql/dialect.rb +162 -0
  17. data/lib/ronin/code/sql/drop_table.rb +51 -0
  18. data/lib/ronin/code/sql/exceptions.rb +24 -0
  19. data/lib/ronin/code/sql/exceptions/unknown_dialect.rb +31 -0
  20. data/lib/ronin/code/sql/expr.rb +193 -0
  21. data/lib/ronin/code/sql/field.rb +86 -0
  22. data/lib/ronin/code/sql/function.rb +52 -0
  23. data/lib/ronin/code/sql/in.rb +49 -0
  24. data/lib/ronin/code/sql/injection.rb +39 -0
  25. data/lib/ronin/code/sql/injection_builder.rb +137 -0
  26. data/lib/ronin/code/sql/injection_style.rb +79 -0
  27. data/lib/ronin/code/sql/insert.rb +86 -0
  28. data/lib/ronin/code/sql/keyword.rb +48 -0
  29. data/lib/ronin/code/sql/like_expr.rb +87 -0
  30. data/lib/ronin/code/sql/program.rb +79 -0
  31. data/lib/ronin/code/sql/replace.rb +58 -0
  32. data/lib/ronin/code/sql/select.rb +187 -0
  33. data/lib/ronin/code/sql/statement.rb +112 -0
  34. data/lib/ronin/code/sql/style.rb +170 -0
  35. data/lib/ronin/code/sql/unary_expr.rb +45 -0
  36. data/lib/ronin/code/sql/update.rb +75 -0
  37. data/lib/ronin/sql.rb +28 -0
  38. data/lib/ronin/sql/error.rb +52 -0
  39. data/lib/ronin/sql/extensions.rb +24 -0
  40. data/lib/ronin/sql/extensions/uri.rb +24 -0
  41. data/lib/ronin/sql/extensions/uri/http.rb +69 -0
  42. data/lib/ronin/sql/sql.rb +83 -0
  43. data/lib/ronin/sql/version.rb +29 -0
  44. data/spec/spec_helper.rb +5 -0
  45. data/tasks/spec.rb +7 -0
  46. metadata +121 -0
@@ -0,0 +1,86 @@
1
+ #
2
+ #--
3
+ # Ronin SQL - A Ronin library providing support for SQL related security
4
+ # tasks.
5
+ #
6
+ # Copyright (c) 2007-2008 Hal Brodigan (postmodern.mod3 at gmail.com)
7
+ #
8
+ # This program is free software; you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation; either version 2 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # This program is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with this program; if not, write to the Free Software
20
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
+ #++
22
+ #
23
+
24
+ require 'ronin/code/sql/expr'
25
+ require 'ronin/code/sql/between'
26
+
27
+ module Ronin
28
+ module Code
29
+ module SQL
30
+ class Field < Expr
31
+
32
+ def initialize(style,name,prefix=nil)
33
+ super(style)
34
+
35
+ @prefix = prefix
36
+ @name = name
37
+ end
38
+
39
+ def *
40
+ field_cache['*'.to_sym]
41
+ end
42
+
43
+ def id
44
+ field_cache[:id]
45
+ end
46
+
47
+ def between(start,stop)
48
+ Between.new(self,start,stop)
49
+ end
50
+
51
+ def <=>(range)
52
+ between(range.begin,range.end)
53
+ end
54
+
55
+ def compile
56
+ if @prefix
57
+ return "#{@prefix}.#{@name}"
58
+ else
59
+ return @name.to_s
60
+ end
61
+ end
62
+
63
+ def to_sym
64
+ compile.to_sym
65
+ end
66
+
67
+ protected
68
+
69
+ def method_missing(sym,*args)
70
+ if (args.length==0 && @prefix.nil?)
71
+ return field_cache[sym]
72
+ end
73
+
74
+ raise(NoMethodError,sym.id2name)
75
+ end
76
+
77
+ private
78
+
79
+ def field_cache
80
+ @field_cache ||= Hash.new { |hash,key| hash[key] = Field.new(@style,key,self) }
81
+ end
82
+
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,52 @@
1
+ #
2
+ #--
3
+ # Ronin SQL - A Ronin library providing support for SQL related security
4
+ # tasks.
5
+ #
6
+ # Copyright (c) 2007-2008 Hal Brodigan (postmodern.mod3 at gmail.com)
7
+ #
8
+ # This program is free software; you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation; either version 2 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # This program is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with this program; if not, write to the Free Software
20
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
+ #++
22
+ #
23
+
24
+ require 'ronin/code/sql/expr'
25
+
26
+ module Ronin
27
+ module Code
28
+ module SQL
29
+ class Function < Expr
30
+
31
+ def initialize(style,func,*fields)
32
+ super(style)
33
+
34
+ @style = style
35
+ @func = keyword(func)
36
+ @fields = fields
37
+ end
38
+
39
+ def compile
40
+ "#{@func}(#{fields?})"
41
+ end
42
+
43
+ protected
44
+
45
+ def fields?
46
+ return compile_list(@fields) unless @fields.empty?
47
+ end
48
+
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,49 @@
1
+ #
2
+ #--
3
+ # Ronin SQL - A Ronin library providing support for SQL related security
4
+ # tasks.
5
+ #
6
+ # Copyright (c) 2007-2008 Hal Brodigan (postmodern.mod3 at gmail.com)
7
+ #
8
+ # This program is free software; you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation; either version 2 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # This program is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with this program; if not, write to the Free Software
20
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
+ #++
22
+ #
23
+
24
+ require 'ronin/code/sql/expr'
25
+
26
+ module Ronin
27
+ module Code
28
+ module SQL
29
+ class In < Expr
30
+
31
+ def initialize(style,field,*range)
32
+ super(style)
33
+
34
+ @field = field
35
+ @range = range
36
+ end
37
+
38
+ def compile
39
+ compile_expr(@field,keyword_in,compile_datalist(@range))
40
+ end
41
+
42
+ protected
43
+
44
+ keyword :in
45
+
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,39 @@
1
+ #
2
+ # Ronin SQL - A Ronin library providing support for SQL related security
3
+ # tasks.
4
+ #
5
+ # Copyright (c) 2007-2008 Hal Brodigan (postmodern.mod3 at gmail.com)
6
+ #
7
+ # This program is free software; you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation; either version 2 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program; if not, write to the Free Software
19
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'ronin/code/sql/program'
23
+ require 'ronin/code/sql/injection_style'
24
+ require 'ronin/code/sql/injection_builder'
25
+ require 'ronin/extensions/string'
26
+
27
+ module Ronin
28
+ module Code
29
+ module SQL
30
+ class Injection < Program
31
+
32
+ def initialize(options={},&block)
33
+ @builder = InjectionBuilder.new(InjectionStyle.new(options),&block)
34
+ end
35
+
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,137 @@
1
+ #
2
+ #--
3
+ # Ronin SQL - A Ronin library providing support for SQL related security
4
+ # tasks.
5
+ #
6
+ # Copyright (c) 2007-2008 Hal Brodigan (postmodern.mod3 at gmail.com)
7
+ #
8
+ # This program is free software; you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation; either version 2 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # This program is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with this program; if not, write to the Free Software
20
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
+ #++
22
+ #
23
+
24
+ require 'ronin/code/sql/statement'
25
+ require 'ronin/code/sql/injection_style'
26
+
27
+ module Ronin
28
+ module Code
29
+ module SQL
30
+ class InjectionBuilder < Statement
31
+
32
+ def initialize(style,&block)
33
+ @escape = nil
34
+ @escape_data = nil
35
+ @expressions = []
36
+ @program = nil
37
+
38
+ super(style,&block)
39
+ end
40
+
41
+ def escape(var=1,&block)
42
+ @escape = nil
43
+ @escape_data = var
44
+
45
+ block.call if block
46
+ return self
47
+ end
48
+
49
+ def inject(*expr)
50
+ @expressions += expr
51
+ return self
52
+ end
53
+
54
+ def inject_and(expr)
55
+ inject(keyword_and, expr)
56
+ end
57
+
58
+ def inject_or(expr)
59
+ inject(keyword_or, expr)
60
+ end
61
+
62
+ def inject_sql(options={},&block)
63
+ @program = Program.new(@style,options,&block)
64
+ end
65
+
66
+ def all_rows(var=1)
67
+ inject_or(BinaryExpr.new(@style,'=',var,var))
68
+ end
69
+
70
+ def exact_rows(var=1)
71
+ inject_and(BinaryExpr.new(@style,'=',var,var))
72
+ end
73
+
74
+ def has_field?(name)
75
+ inject_or(field(name).is_not?(null))
76
+ end
77
+
78
+ def has_table?(table)
79
+ inject_and(select_from(table,:fields => count(all), :from => table)==1)
80
+ end
81
+
82
+ def uses_table?(table)
83
+ inject_or(table.is_not?(null))
84
+ end
85
+
86
+ def compile
87
+ injection_expr = lambda {
88
+ compile_expr("#{@escape_data}#{@escape}",*(@expressions))
89
+ }
90
+
91
+ append_comment = lambda { |str|
92
+ compile_expr(str,'--')
93
+ }
94
+
95
+ if @program
96
+ return compile_statements(injection_expr.call,append_comment.call(@program))
97
+ else
98
+ injection = injection_expr.call
99
+
100
+ if (@escape && injection =~ /#{@escape}\s*$/)
101
+ return injection.rstrip.chop
102
+ else
103
+ return append_comment.call(injection)
104
+ end
105
+ end
106
+ end
107
+
108
+ protected
109
+
110
+ keyword :or
111
+ keyword :and
112
+
113
+ def self.escape(name,char)
114
+ name = name.to_s.downcase.to_sym
115
+ char = char.to_s
116
+
117
+ class_eval %{
118
+ def escape_#{name}(var=nil,&block)
119
+ @escape = #{char.dump}
120
+ @escape_data = var
121
+
122
+ block.call if block
123
+ return self
124
+ end
125
+ }
126
+
127
+ return self
128
+ end
129
+
130
+ escape :string, "'"
131
+ escape :parenthesis, ')'
132
+ escape :statement, ';'
133
+
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,79 @@
1
+ #
2
+ #--
3
+ # Ronin SQL - A Ronin library providing support for SQL related security
4
+ # tasks.
5
+ #
6
+ # Copyright (c) 2007-2008 Hal Brodigan (postmodern.mod3 at gmail.com)
7
+ #
8
+ # This program is free software; you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation; either version 2 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # This program is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with this program; if not, write to the Free Software
20
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
+ #++
22
+ #
23
+
24
+ require 'ronin/code/sql/style'
25
+
26
+ module Ronin
27
+ module Code
28
+ module SQL
29
+ class InjectionStyle < Style
30
+
31
+ # Comment-Obfusticate all keywords
32
+ attr_accessor :comment_evasion
33
+
34
+ # Swapcase-Obfusciate all keywords
35
+ attr_accessor :case_evasion
36
+
37
+ def initialize(options={})
38
+ super(options)
39
+
40
+ if options[:comment_evasion].nil?
41
+ @comment_evasion = false
42
+ else
43
+ @comment_evasion = options[:comment_evasion]
44
+ end
45
+
46
+ if options[:case_evasion].nil?
47
+ @case_evasion = false
48
+ else
49
+ @case_evasion = options[:case_evasion]
50
+ end
51
+ end
52
+
53
+ def compile_space
54
+ if @comment_evasion
55
+ return '/**/'
56
+ else
57
+ return super
58
+ end
59
+ end
60
+
61
+ def compile_keyword(name)
62
+ name = name.to_s
63
+
64
+ if @case_evasion
65
+ (rand(name.length)+1).times do
66
+ i = rand(name.length-1).to_i
67
+ name[i] = name[i..i].swapcase
68
+ end
69
+
70
+ return name
71
+ else
72
+ return super(name)
73
+ end
74
+ end
75
+
76
+ end
77
+ end
78
+ end
79
+ end