nasl 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,50 @@
1
+ ################################################################################
2
+ # Copyright (c) 2011-2018, Tenable Network Security
3
+ # All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # 1. Redistributions of source code must retain the above copyright notice, this
9
+ # list of conditions and the following disclaimer.
10
+ #
11
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ # this list of conditions and the following disclaimer in the documentation
13
+ # and/or other materials provided with the distribution.
14
+ #
15
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+ ################################################################################
26
+
27
+ require 'nasl/parser/node'
28
+
29
+ module Nasl
30
+ class Namespace < Node
31
+ attr_reader :name, :body
32
+
33
+ include Enumerable
34
+
35
+ def initialize(tree, *tokens)
36
+ super
37
+
38
+ @body = if @tokens.length == 5 then @tokens[3] else [] end
39
+ @name = @tokens[1]
40
+
41
+ @children << :name
42
+ @children << :body
43
+ end
44
+
45
+ def each
46
+ @body.each{ |stmt| yield stmt }
47
+ end
48
+
49
+ end
50
+ end
@@ -39,15 +39,24 @@ module Nasl
39
39
  # Store all of the tokens that made up this node.
40
40
  @tokens = tokens
41
41
 
42
+ # handle empty token set
43
+
42
44
  # Extract the context object from the first token.
43
- @ctx = @tokens.first.ctx
45
+ if(!@tokens.nil? and @tokens.length>0 and !@tokens.first.nil?)
46
+ @ctx = @tokens.first.ctx
47
+ end
44
48
  end
45
49
 
46
50
  def context(*args)
47
- @ctx.context(region, *args)
51
+ if(!@ctx.nil?)
52
+ @ctx.context(region, *args)
53
+ end
48
54
  end
49
55
 
50
56
  def region
57
+ if(@tokens.flatten.first.nil?)
58
+ return []
59
+ end
51
60
  @tokens.flatten.first.region.begin..@tokens.flatten.last.region.end
52
61
  end
53
62
 
@@ -0,0 +1,41 @@
1
+ ################################################################################
2
+ # Copyright (c) 2011-2018, Tenable Network Security
3
+ # All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # 1. Redistributions of source code must retain the above copyright notice, this
9
+ # list of conditions and the following disclaimer.
10
+ #
11
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ # this list of conditions and the following disclaimer in the documentation
13
+ # and/or other materials provided with the distribution.
14
+ #
15
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+ ################################################################################
26
+
27
+ require 'nasl/parser/node'
28
+
29
+ module Nasl
30
+ class ObjVar < Node
31
+ attr_reader :idents
32
+
33
+ def initialize(tree, *tokens)
34
+ super
35
+
36
+ @idents = @tokens[1]
37
+
38
+ @children << :idents
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,50 @@
1
+ ################################################################################
2
+ # Copyright (c) 2011-2018, Tenable Network Security
3
+ # All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # 1. Redistributions of source code must retain the above copyright notice, this
9
+ # list of conditions and the following disclaimer.
10
+ #
11
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ # this list of conditions and the following disclaimer in the documentation
13
+ # and/or other materials provided with the distribution.
14
+ #
15
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+ ################################################################################
26
+
27
+ require 'nasl/parser/node'
28
+
29
+ module Nasl
30
+ class Object < Node
31
+ attr_reader :name, :body
32
+
33
+ include Enumerable
34
+
35
+ def initialize(tree, *tokens)
36
+ super
37
+
38
+ @body = if @tokens.length == 5 then @tokens[3] else [] end
39
+ @name = @tokens[1]
40
+
41
+ @children << :name
42
+ @children << :body
43
+ end
44
+
45
+ def each
46
+ @body.each{ |stmt| yield stmt }
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,51 @@
1
+ ################################################################################
2
+ # Copyright (c) 2011-2018, Tenable Network Security
3
+ # All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # 1. Redistributions of source code must retain the above copyright notice, this
9
+ # list of conditions and the following disclaimer.
10
+ #
11
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ # this list of conditions and the following disclaimer in the documentation
13
+ # and/or other materials provided with the distribution.
14
+ #
15
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+ ################################################################################
26
+
27
+ require 'nasl/parser/node'
28
+
29
+ module Nasl
30
+ class Switch < Node
31
+ attr_reader :switch_expr, :switch_op, :body
32
+
33
+ def initialize(tree, *tokens)
34
+ super
35
+
36
+ if @tokens.length == 5
37
+ @switch_op = nil
38
+ @switch_expr = @tokens[2]
39
+ @body = @tokens[4]
40
+ else
41
+ @switch_op = @tokens[2]
42
+ @switch_expr = @tokens[5]
43
+ @body = @tokens[7]
44
+ end
45
+
46
+ @children << :switch_op
47
+ @children << :switch_expr
48
+ @children << :body
49
+ end
50
+ end
51
+ end
@@ -43,7 +43,7 @@ module Nasl
43
43
  case @type
44
44
  when *[:BREAK, :CONTINUE, :ELSE, :EXPORT, :FOR, :FOREACH, :FUNCTION,
45
45
  :GLOBAL, :IF, :IMPORT, :INCLUDE, :LOCAL, :REPEAT, :RETURN, :UNTIL,
46
- :REP, :WHILE]
46
+ :REP, :WHILE, :NAMESPACE, :OBJECT, :VAR, :PUBLIC, :PRIVATE, :CASE, :SWITCH, :DEFAULT]
47
47
  "a keyword"
48
48
  when :UNDEF
49
49
  "an undefined constant"
@@ -1,5 +1,5 @@
1
1
  ################################################################################
2
- # Copyright (c) 2011-2014, Tenable Network Security
2
+ # Copyright (c) 2011-2016, Tenable Network Security
3
3
  # All rights reserved.
4
4
  #
5
5
  # Redistribution and use in source and binary forms, with or without
@@ -50,12 +50,25 @@ module Nasl
50
50
  'until' => :UNTIL,
51
51
  'x' => :REP,
52
52
  'while' => :WHILE,
53
+ 'namespace' => :NAMESPACE,
54
+ 'object' => :OBJECT,
55
+ # 'new' => :NEW,
56
+ 'var' => :VAR,
57
+ 'public' => :PUBLIC,
58
+ 'private' => :PRIVATE,
59
+ 'switch' => :SWITCH,
60
+ 'case' => :CASE,
61
+ 'default' => :DEFAULT,
53
62
 
54
63
  'FALSE' => :FALSE,
55
64
  'NULL' => :UNDEF,
56
65
  'TRUE' => :TRUE
57
66
  }
58
67
 
68
+ @@operator_lengths = []
69
+
70
+ # These are all of the operators defined in NASL. Their order is vitally
71
+ # important.
59
72
  @@operators = [
60
73
  ["><", :SUBSTR_EQ],
61
74
  [">!<", :SUBSTR_NE],
@@ -122,16 +135,23 @@ module Nasl
122
135
  @@annotated = [
123
136
  :EXPORT,
124
137
  :FUNCTION,
125
- :GLOBAL
138
+ :GLOBAL,
139
+ :PUBLIC,
140
+ :PRIVATE
126
141
  ]
127
142
 
128
143
  def initialize!
129
144
  return if @@initialized
130
145
 
131
- # Convert the operators into a regex-compatible form.
132
- @@operators = @@operators.map do |op, type|
133
- [Regexp.new("^#{Regexp.escape(op)}"), op, type]
146
+ @@operator_lengths = @@operators.map { |op, type| op.length }.uniq
147
+
148
+ # Convert the operators into a form that's fast to access.
149
+ tmp = {}
150
+ @@operators.each_with_index do |op_and_type, index|
151
+ op, type = op_and_type
152
+ tmp[op] = [op, type, index]
134
153
  end
154
+ @@operators = tmp
135
155
 
136
156
  @@initialized = true
137
157
  end
@@ -199,7 +219,8 @@ module Nasl
199
219
 
200
220
  def get_identifier
201
221
  # Identifiers are composed of letters, digits, and underscores.
202
- ident = @line[/^[_a-z][_a-z0-9]*/i]
222
+ #ident = @line[/^[_a-z][_a-z0-9]*/i]
223
+ ident = @line[/^[_a-z]([_a-z0-9]*::[_a-z0-9]+)*[_a-z0-9]*/i]
203
224
  consume(ident.length)
204
225
 
205
226
  # Assume that we've got an identifier until proven otherwise.
@@ -308,16 +329,30 @@ module Nasl
308
329
  return [:COMMENT, block.join("\n")]
309
330
  end
310
331
 
311
- def get_operator
312
- # These are all of the operators defined in NASL. Their order is vitally
313
- # important.
314
- @@operators.each do |regex, op, type|
315
- next if @line !~ regex
316
- consume(op.length)
317
- return [type, op]
332
+ def get_comment_c_style
333
+ if @code[@point+1] == '/'
334
+ comment = @line[/^\/\/.*$/]
335
+ # Multi-line: /* comment here */
336
+ else
337
+ newline = @code[@point..-1]
338
+ comment = newline[/^\/\*.*?\*\//m]
339
+ die("Unterminated multiline comment") if comment.nil?
318
340
  end
319
341
 
320
- return nil
342
+ consume(comment.length)
343
+ skip
344
+
345
+ return [:COMMENT, comment]
346
+ end
347
+
348
+ def get_operator
349
+ line_prefixes = @@operator_lengths.map { |len| @line[0, len] }
350
+ operators_that_matched = line_prefixes.map { |prefix| @@operators[prefix] }
351
+ operators_that_matched.reject!(&:nil?)
352
+ return nil if operators_that_matched.empty?
353
+ op, type = operators_that_matched.sort { |a, b| a[2] <=> b[2] }.first
354
+ consume(op.length)
355
+ return [type, op]
321
356
  end
322
357
 
323
358
  def get_token
@@ -344,6 +379,8 @@ module Nasl
344
379
  get_integer
345
380
  elsif @char == '#'
346
381
  get_comment
382
+ elsif (@char == '/') && ["/", "*"].include?(@code[@point+1])
383
+ get_comment_c_style
347
384
  else
348
385
  get_operator
349
386
  end
@@ -25,5 +25,5 @@
25
25
  ################################################################################
26
26
 
27
27
  module Nasl
28
- VERSION = '0.3.0'
28
+ VERSION = '0.4.0'
29
29
  end
@@ -48,9 +48,10 @@ Gem::Specification.new do |s|
48
48
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
49
49
  s.require_paths = ['lib']
50
50
 
51
- s.add_development_dependency 'racc', '~>1.4'
52
- s.add_development_dependency 'rake', '~>10.1'
51
+ s.add_development_dependency 'racc', '~> 1.4'
52
+ s.add_development_dependency 'rake', '~> 10.3'
53
53
 
54
- s.add_runtime_dependency 'builder', '~> 3.1'
54
+ s.add_runtime_dependency 'builder', '~> 3.2'
55
55
  s.add_runtime_dependency 'rainbow', '~> 2.0'
56
+ s.add_runtime_dependency 'test-unit-minitest'
56
57
  end
@@ -0,0 +1,45 @@
1
+ ################################################################################
2
+ # Copyright (c) 2011-2018, Tenable Network Security
3
+ # All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # 1. Redistributions of source code must retain the above copyright notice, this
9
+ # list of conditions and the following disclaimer.
10
+ #
11
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ # this list of conditions and the following disclaimer in the documentation
13
+ # and/or other materials provided with the distribution.
14
+ #
15
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+ ################################################################################
26
+
27
+ class TestNamespace < Test::Unit::TestCase
28
+ include Nasl::Test
29
+
30
+ def test_empty
31
+ same("namespace foo {}", "<tree><namespace><identifier name=\"foo\"/></namespace></tree>")
32
+ end
33
+
34
+ def test_inner_fn
35
+ same("namespace foo { function fn(){} }", "<tree><namespace><identifier name=\"foo\"/><function><identifier name=\"fn\"/><block></block><fn_type>normal</fn_type></function></namespace></tree>")
36
+ end
37
+
38
+ def test_nested_namespace
39
+ same("namespace foo { namespace bar {} }", "<tree><namespace><identifier name=\"foo\"/><namespace><identifier name=\"bar\"/></namespace></namespace></tree>")
40
+ end
41
+
42
+ def test_namespace_indent
43
+ same("foo::bob();", "<tree><call><lvalue><identifier name=\"foo::bob\"/></lvalue></call></tree>");
44
+ end
45
+ end
@@ -0,0 +1,75 @@
1
+ ################################################################################
2
+ # Copyright (c) 2011-2018, Tenable Network Security
3
+ # All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # 1. Redistributions of source code must retain the above copyright notice, this
9
+ # list of conditions and the following disclaimer.
10
+ #
11
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ # this list of conditions and the following disclaimer in the documentation
13
+ # and/or other materials provided with the distribution.
14
+ #
15
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+ ################################################################################
26
+
27
+ class TestObject < Test::Unit::TestCase
28
+ include Nasl::Test
29
+
30
+ def test_each
31
+ tree = parse(
32
+ <<-EOF
33
+ namespace test {
34
+ namespace test_inner {
35
+ object foo {
36
+ var bob = 1;
37
+ var a1,b1;
38
+ # foo
39
+ public function bar_pub() {}
40
+ # foo
41
+ function bar_priv_default() { a = 1; return a; }
42
+ private function bar_priv() {}
43
+ # foo
44
+ function test_var()
45
+ {
46
+ var test = 'a';
47
+ var x,y,z,t;
48
+ }
49
+ }
50
+ }
51
+ # foo!
52
+ function foo(){}
53
+ }
54
+ # foo!
55
+ function foo(){}
56
+ EOF
57
+ )
58
+ assert_not_nil(tree)
59
+
60
+ objects = tree.all(:Object)
61
+ assert_equal(1, objects.length)
62
+ assert_equal(objects[0].name.name, "foo")
63
+
64
+ functions = tree.all(:Function)
65
+ assert_equal(6, functions.length)
66
+
67
+ assert_equal(functions[0].tokens[0].type.to_s, "PUBLIC")
68
+ assert_equal(functions[1].tokens[0], nil)
69
+ assert_equal(functions[2].tokens[0].type.to_s, "PRIVATE")
70
+
71
+ obj_vars = tree.all(:ObjVar)
72
+ assert_equal(2, obj_vars.length)
73
+ end
74
+
75
+ end