ronin-sql 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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,112 @@
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/field'
26
+ require 'ronin/code/sql/binary_expr'
27
+ require 'ronin/code/sql/unary_expr'
28
+ require 'ronin/code/sql/like_expr'
29
+ require 'ronin/code/sql/in'
30
+ require 'ronin/extensions/meta'
31
+
32
+ module Ronin
33
+ module Code
34
+ module SQL
35
+ class Statement < Expr
36
+
37
+ def initialize(style,&block)
38
+ super(style)
39
+
40
+ instance_eval(&block) if block
41
+ end
42
+
43
+ protected
44
+
45
+ def self.option(name,value=nil)
46
+ class_eval %{
47
+ def #{name}(&block)
48
+ instance_variable_set("@#{name}",true)
49
+
50
+ instance_eval(&block) if block
51
+ return self
52
+ end
53
+ }
54
+
55
+ class_def("#{name}?") do
56
+ if value
57
+ keyword(value.to_s) if instance_variable_get("@#{name}")
58
+ else
59
+ instance_variable_get("@#{name}")
60
+ end
61
+ end
62
+ end
63
+
64
+ def self.option_list(name,values=[])
65
+ values.each do |opt|
66
+ class_eval %{
67
+ def #{opt}_#{name}(&block)
68
+ instance_variable_set("@#{name}",'#{opt.to_s.upcase}')
69
+
70
+ instance_eval(&block) if block
71
+ return self
72
+ end
73
+ }
74
+ end
75
+
76
+ class_def("#{name}?") do
77
+ opt = instance_variable_get("@#{name}")
78
+
79
+ return keyword(opt) if opt
80
+ return nil
81
+ end
82
+ end
83
+
84
+ def all
85
+ field_cache['*'.to_sym]
86
+ end
87
+
88
+ def id
89
+ field_cache[:id]
90
+ end
91
+
92
+ def method_missing(sym,*args,&block)
93
+ if @style.dialect.expresses?(sym)
94
+ return @style.dialect.express(sym,*args,&block)
95
+ end
96
+
97
+ # return a field
98
+ return @style.dialect.field(sym) if args.empty?
99
+
100
+ return super(sym,*args,&block)
101
+ end
102
+
103
+ private
104
+
105
+ def field_cache
106
+ @field_cache ||= Hash.new { |hash,key| hash[key] = Field.new(@style,key) }
107
+ end
108
+
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,170 @@
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/dialect'
25
+ require 'ronin/code/sql/common_dialect'
26
+
27
+ module Ronin
28
+ module Code
29
+ module SQL
30
+ class Style
31
+
32
+ # The dialect of SQL
33
+ attr_reader :dialect
34
+
35
+ # Use single-line or multi-line style
36
+ attr_accessor :multiline
37
+
38
+ # Use lowercase style
39
+ attr_accessor :lowercase
40
+
41
+ # Compile with less parenthesis
42
+ attr_accessor :less_parenthesis
43
+
44
+ # Space string
45
+ attr_accessor :space
46
+
47
+ # New-line string
48
+ attr_accessor :newline
49
+
50
+ def initialize(options={})
51
+ @dialect = Dialect.get_dialect(options[:dialect] || :common).new(self)
52
+
53
+ if options[:multiline].nil?
54
+ @multiline = true
55
+ else
56
+ @multiline = options[:multiline]
57
+ end
58
+
59
+ if options[:lowercase].nil?
60
+ @lowercase = false
61
+ else
62
+ @lowercase = options[:lowercase]
63
+ end
64
+
65
+ if options[:less_parenthesis].nil?
66
+ @less_parenthesis = false
67
+ else
68
+ @less_parenthesis = options[:less_parenthesis]
69
+ end
70
+
71
+ @space = (options[:space] || ' ')
72
+ @newline = (options[:newline] || "\n")
73
+ end
74
+
75
+ def compile_space
76
+ if @space.kind_of?(Array)
77
+ return @space[rand(@space.length)].to_s
78
+ else
79
+ return @space.to_s
80
+ end
81
+ end
82
+
83
+ def preappend_space(str)
84
+ compile_space + str.to_s
85
+ end
86
+
87
+ def append_space(str)
88
+ str.to_s + compile_space
89
+ end
90
+
91
+ def compile_newline
92
+ return compile_space unless @multiline
93
+
94
+ if @newline.kind_of?(Array)
95
+ return @newline[@newline.length * rand].to_s
96
+ else
97
+ return @newline.to_s
98
+ end
99
+ end
100
+
101
+ def quote_string(data)
102
+ "'" + data.to_s.sub("'","''") + "'"
103
+ end
104
+
105
+ def compile_keyword(name)
106
+ name = name.to_s
107
+
108
+ if @lowercase
109
+ return name.downcase
110
+ else
111
+ return name.upcase
112
+ end
113
+ end
114
+
115
+ def compile_list(*exprs)
116
+ exprs = exprs.flatten
117
+
118
+ unless @less_parenthesis
119
+ return exprs.compact.join(append_space(','))
120
+ else
121
+ return exprs.compact.join(',')
122
+ end
123
+ end
124
+
125
+ def compile_datalist(*exprs)
126
+ compile_row( exprs.flatten.map { |expr| compile_data(value) } )
127
+ end
128
+
129
+ def compile_row(*exprs)
130
+ exprs = exprs.flatten
131
+
132
+ unless exprs.length==1
133
+ unless @less_parenthesis
134
+ return "(#{compile_list(exprs)})"
135
+ else
136
+ return compile_list(exprs)
137
+ end
138
+ else
139
+ return exprs[0].to_s
140
+ end
141
+ end
142
+
143
+ def compile_data(data)
144
+ if data.kind_of?(Statement)
145
+ return "(#{data})"
146
+ elsif data.kind_of?(Array)
147
+ return compile_datalist(data)
148
+ elsif data.kind_of?(String)
149
+ return quote_string(data)
150
+ else
151
+ return data.to_s
152
+ end
153
+ end
154
+
155
+ def compile_expr(*expr)
156
+ expr.compact.join(compile_space).strip
157
+ end
158
+
159
+ def compile_statements(statements,separator=compile_newline)
160
+ if @multiline
161
+ return statements.join(compile_newline)
162
+ else
163
+ return statements.join(append_space(';'))
164
+ end
165
+ end
166
+
167
+ end
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,45 @@
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 UnaryExpr < Expr
30
+
31
+ def initialize(style,op,expr)
32
+ super(style)
33
+
34
+ @op = op
35
+ @expr = expr
36
+ end
37
+
38
+ def compile
39
+ compile_expr(compile_keyword(@op),@expr)
40
+ end
41
+
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,75 @@
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 Update < Statement
30
+
31
+ def initialize(style,table=nil,set_data={},where_expr=nil,&block)
32
+ @table = table
33
+ @set_data = set_data
34
+ @where_expr = where_expr
35
+
36
+ super(style,&block)
37
+ end
38
+
39
+ def table(value)
40
+ @table = value
41
+ return self
42
+ end
43
+
44
+ def set(data)
45
+ @set_data = data
46
+ return self
47
+ end
48
+
49
+ def where(expr)
50
+ @where_expr = expr
51
+ return self
52
+ end
53
+
54
+ def compile
55
+ set_values = "#{keyword_set} "+@set_data.map { |name,value|
56
+ "#{name} = #{quote_string(value)}"
57
+ }.join(', ')
58
+
59
+ return compile_expr(keyword_update,@table,set_values,where?)
60
+ end
61
+
62
+ protected
63
+
64
+ keyword :update
65
+ keyword :where
66
+ keyword :set
67
+
68
+ def where?
69
+ compile_expr(keyword_where,@where_expr) if @where_expr
70
+ end
71
+
72
+ end
73
+ end
74
+ end
75
+ end
data/lib/ronin/sql.rb ADDED
@@ -0,0 +1,28 @@
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'
25
+ require 'ronin/sql/extensions'
26
+ require 'ronin/sql/error'
27
+ require 'ronin/sql/sql'
28
+ require 'ronin/sql/version'
@@ -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
+ module Ronin
25
+ module SQL
26
+ class Error
27
+
28
+ # SQL error type
29
+ attr_reader :type
30
+
31
+ # SQL error message
32
+ attr_reader :message
33
+
34
+ #
35
+ # Creates a new SQL Error object with the specified _type_ and
36
+ # _message_.
37
+ #
38
+ def initialize(type,message)
39
+ @type = type
40
+ @message = message
41
+ end
42
+
43
+ #
44
+ # Returns the message in String form.
45
+ #
46
+ def to_s
47
+ @message.to_s
48
+ end
49
+
50
+ end
51
+ end
52
+ end