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,65 @@
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/select'
26
+
27
+ module Ronin
28
+ module Code
29
+ module SQL
30
+ class CreateView < Statement
31
+
32
+ option :temp, "TEMP"
33
+ option :if_not_exists, "IF NOT EXISTS"
34
+
35
+ def initialize(style,view=nil,query=nil,&block)
36
+ @view = view
37
+ @query = query
38
+
39
+ super(style,&block)
40
+ end
41
+
42
+ def view(field)
43
+ @view = field
44
+ return self
45
+ end
46
+
47
+ def query(table=nil,opts={:fields => nil, :where => nil},&block)
48
+ @query = Select.new(@style,table,opts,&block)
49
+ return self
50
+ end
51
+
52
+ def compile
53
+ compile_expr(keyword_create,temp?,keyword_view,if_not_exists?,@view,keyword_as,@query)
54
+ end
55
+
56
+ protected
57
+
58
+ keyword :create
59
+ keyword :view
60
+ keyword :as
61
+
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,64 @@
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
+
26
+ module Ronin
27
+ module Code
28
+ module SQL
29
+ class Delete < Statement
30
+
31
+ def initialize(style,table=nil,where_expr=nil,&block)
32
+ @table = table || everything
33
+ @where = where_expr
34
+
35
+ super(style,&block)
36
+ end
37
+
38
+ def from(table)
39
+ @table = table
40
+ return self
41
+ end
42
+
43
+ def where(expr)
44
+ @where = expr
45
+ return self
46
+ end
47
+
48
+ def compile
49
+ compile_expr(keyword_delete,@table,where?)
50
+ end
51
+
52
+ protected
53
+
54
+ keyword :delete, 'DELETE FROM'
55
+ keyword :where
56
+
57
+ def where?
58
+ compile_expr(keyword_where,@where) if @where
59
+ end
60
+
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,162 @@
1
+ #
2
+ #--
3
+ # Ronin SQL - A Ronin library providing support for SQL related security
4
+ # tasks.
5
+ #
6
+ # Copyright (c) 2007 Hal Brodigan (postmodern at users.sourceforge.net)
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/exceptions/unknown_dialect'
25
+ require 'ronin/code/sql/function'
26
+ require 'ronin/extensions/meta'
27
+
28
+ module Ronin
29
+ module Code
30
+ module SQL
31
+ class Dialect
32
+
33
+ # The style to use
34
+ attr_reader :style
35
+
36
+ def initialize(style)
37
+ @style = style
38
+ end
39
+
40
+ def Dialect.dialects
41
+ @@dialects ||= {}
42
+ end
43
+
44
+ def Dialect.has_dialect?(name)
45
+ Dialect.dialects.has_key?(name.to_sym)
46
+ end
47
+
48
+ def Dialect.get_dialect(name)
49
+ name = name.to_sym
50
+
51
+ unless Dialect.has_dialect?(name)
52
+ raise(UnknownDialect,"unknown dialect #{name.dump}",caller)
53
+ end
54
+
55
+ return Dialect.dialects[name]
56
+ end
57
+
58
+ def expresses?(name)
59
+ public_methods.include?(name.to_s)
60
+ end
61
+
62
+ def express(name,*args,&block)
63
+ unless expresses?(name)
64
+ raise(NameError,"undefined method '#{name}' for #{self}",caller)
65
+ end
66
+
67
+ return send(name,*args,&block)
68
+ end
69
+
70
+ def field(name)
71
+ field_cache[name.to_sym]
72
+ end
73
+
74
+ protected
75
+
76
+ def self.dialect(name)
77
+ name = name.to_sym
78
+
79
+ class_def(:name) { name }
80
+
81
+ Dialect.dialects[name] = self
82
+ return self
83
+ end
84
+
85
+ def self.keyword(name,value=name.to_s.upcase)
86
+ name = name.to_s.downcase
87
+
88
+ class_def("keyword_#{name}") { keyword(value) }
89
+ return self
90
+ end
91
+
92
+ def self.primitives(*names)
93
+ names.each do |name|
94
+ name = name.to_s.downcase
95
+
96
+ class_def(name) { keyword(name) }
97
+ end
98
+
99
+ return self
100
+ end
101
+
102
+ def self.data_type(name,options={})
103
+ name = name.to_s.downcase
104
+ type_name = name.upcase.to_sym
105
+
106
+ if options[:length]==true
107
+ class_def(name) do |length|
108
+ if length
109
+ "#{type_name}(#{length})"
110
+ else
111
+ type_name
112
+ end
113
+ end
114
+ else
115
+ class_def(name) { type_name }
116
+ end
117
+
118
+ return self
119
+ end
120
+
121
+ def self.function(*names)
122
+ names.each do |name|
123
+ class_def(name) do |field|
124
+ Function.new(@style,name,field)
125
+ end
126
+ end
127
+
128
+ return self
129
+ end
130
+
131
+ def self.aggregators(*names)
132
+ function(*names)
133
+ end
134
+
135
+ def self.command(name,base)
136
+ class_eval %{
137
+ def #{name}(*args,&block)
138
+ #{base}.new(@style,*args,&block)
139
+ end
140
+ }
141
+
142
+ return self
143
+ end
144
+
145
+ def keyword(value)
146
+ keyword_cache[value.to_sym]
147
+ end
148
+
149
+ private
150
+
151
+ def keyword_cache
152
+ @keyword_cache ||= Hash.new { |hash,key| hash[key] = Keyword.new(@style,key) }
153
+ end
154
+
155
+ def field_cache
156
+ @field_cache ||= Hash.new { |hash,key| hash[key] = Field.new(@style,key) }
157
+ end
158
+
159
+ end
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,51 @@
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
+
26
+ module Ronin
27
+ module Code
28
+ module SQL
29
+ class DropTable < Statement
30
+
31
+ option :if_exists, "IF EXISTS"
32
+
33
+ def initialize(style,table=nil,&block)
34
+ @table = table
35
+ @exists = false
36
+
37
+ super(style,&block)
38
+ end
39
+
40
+ def compile
41
+ compile_expr(keyword_drop,if_exists?,@table)
42
+ end
43
+
44
+ protected
45
+
46
+ keyword :drop, 'DROP TABLE'
47
+
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,24 @@
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/exceptions/unknown_dialect'
@@ -0,0 +1,31 @@
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
+ module Ronin
25
+ module Code
26
+ module SQL
27
+ class DialectNotFound < RuntimeError
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,193 @@
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/keyword'
25
+ require 'ronin/extensions/meta'
26
+
27
+ module Ronin
28
+ module Code
29
+ module SQL
30
+ class Expr
31
+
32
+ # The style to use
33
+ attr_reader :style
34
+
35
+ def initialize(style)
36
+ @style = style
37
+ end
38
+
39
+ def in?(*range)
40
+ In.new(@style,self,*range)
41
+ end
42
+
43
+ def ===(*range)
44
+ in?(*range)
45
+ end
46
+
47
+ def not_in?(*range)
48
+ in?(*range).not!
49
+ end
50
+
51
+ def compile
52
+ # place holder
53
+ end
54
+
55
+ def to_s
56
+ compile
57
+ end
58
+
59
+ protected
60
+
61
+ def keyword(value)
62
+ keyword_cache[value.to_sym]
63
+ end
64
+
65
+ def keywords(*values)
66
+ values.map { |value| keyword(value) }
67
+ end
68
+
69
+ def self.keyword(name,value=name.to_s.upcase)
70
+ name = name.to_s.downcase
71
+
72
+ class_def("keyword_#{name}") do
73
+ keyword(value)
74
+ end
75
+
76
+ return self
77
+ end
78
+
79
+ def self.binary_op(op,*names)
80
+ names.each do |name|
81
+ class_def(name) do |expr|
82
+ BinaryExpr.new(@style,op,self,expr)
83
+ end
84
+ end
85
+
86
+ return self
87
+ end
88
+
89
+ binary_op '=', '==', :equals?
90
+ binary_op '!=', :not_equals?
91
+ binary_op '<>', '<=>', :different?
92
+ binary_op '>', '>', :greater?
93
+ binary_op '>=', '>=', :greater_equal?
94
+ binary_op '<', '<', :less?
95
+ binary_op '<=', '<=', :less_equal?
96
+ binary_op 'IS', :is?
97
+ binary_op 'IS NOT', :is_not?
98
+ binary_op 'AS', :as
99
+ binary_op 'CAST', :cast
100
+ binary_op 'OR', :or
101
+ binary_op 'XOR', :xor
102
+ binary_op 'AND', :and
103
+
104
+ def self.like_op(op,*names)
105
+ names.each do |name|
106
+ class_def(name) do |expr,escape|
107
+ LikeExpr.new(@style,op,self,expr,escape)
108
+ end
109
+ end
110
+
111
+ return self
112
+ end
113
+
114
+ like_op 'LIKE', :like
115
+ like_op 'GLOB', :glob
116
+ like_op 'REGEXP', :regexp
117
+ like_op 'MATCH', :match
118
+
119
+ def self.unary_op(op,*names)
120
+ names.each do |name|
121
+ class_def(name) do
122
+ UnaryExpr.new(@style,op,self)
123
+ end
124
+ end
125
+
126
+ return self
127
+ end
128
+
129
+ unary_op 'NOT', :not!
130
+ unary_op 'EXISTS', :exists?
131
+
132
+ def compile_space
133
+ @style.compile_space
134
+ end
135
+
136
+ def preappend_space(str)
137
+ @style.preappend_space(str)
138
+ end
139
+
140
+ def append_space(str)
141
+ @style.append_space(str)
142
+ end
143
+
144
+ def space(*str)
145
+ @style.space(*str)
146
+ end
147
+
148
+ def compile_newline
149
+ @style.compile_newline
150
+ end
151
+
152
+ def quote_string(data)
153
+ @style.quote_string(data)
154
+ end
155
+
156
+ def compile_keyword(name)
157
+ @style.compile_keyword(name)
158
+ end
159
+
160
+ def compile_list(*expr)
161
+ @style.compile_list(*expr)
162
+ end
163
+
164
+ def compile_datalist(*expr)
165
+ @style.compile_list(*expr)
166
+ end
167
+
168
+ def compile_row(*expr)
169
+ @style.compile_row(*expr)
170
+ end
171
+
172
+ def compile_data(data)
173
+ @style.compile_data(data)
174
+ end
175
+
176
+ def compile_expr(*expr)
177
+ @style.compile_expr(*expr)
178
+ end
179
+
180
+ def compile_statements(*statements)
181
+ @style.compile_statements(*statements)
182
+ end
183
+
184
+ private
185
+
186
+ def keyword_cache
187
+ @keyword_cache ||= Hash.new { |hash,key| hash[key] = Keyword.new(@style,key) }
188
+ end
189
+
190
+ end
191
+ end
192
+ end
193
+ end