ronin-support 0.1.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (127) hide show
  1. data/.document +4 -0
  2. data/.rspec +1 -0
  3. data/.yardopts +1 -0
  4. data/COPYING.txt +504 -0
  5. data/ChangeLog.md +4 -0
  6. data/Gemfile +23 -0
  7. data/README.md +98 -0
  8. data/Rakefile +29 -0
  9. data/gemspec.yml +20 -0
  10. data/lib/ronin/extensions.rb +28 -0
  11. data/lib/ronin/extensions/file.rb +63 -0
  12. data/lib/ronin/extensions/ip_addr.rb +220 -0
  13. data/lib/ronin/extensions/kernel.rb +45 -0
  14. data/lib/ronin/extensions/meta.rb +22 -0
  15. data/lib/ronin/extensions/meta/object.rb +24 -0
  16. data/lib/ronin/extensions/string.rb +200 -0
  17. data/lib/ronin/formatting.rb +26 -0
  18. data/lib/ronin/formatting/binary.rb +22 -0
  19. data/lib/ronin/formatting/digest.rb +22 -0
  20. data/lib/ronin/formatting/extensions.rb +25 -0
  21. data/lib/ronin/formatting/extensions/binary.rb +24 -0
  22. data/lib/ronin/formatting/extensions/binary/file.rb +35 -0
  23. data/lib/ronin/formatting/extensions/binary/integer.rb +147 -0
  24. data/lib/ronin/formatting/extensions/binary/string.rb +363 -0
  25. data/lib/ronin/formatting/extensions/digest.rb +23 -0
  26. data/lib/ronin/formatting/extensions/digest/file.rb +117 -0
  27. data/lib/ronin/formatting/extensions/digest/string.rb +80 -0
  28. data/lib/ronin/formatting/extensions/http.rb +23 -0
  29. data/lib/ronin/formatting/extensions/http/integer.rb +57 -0
  30. data/lib/ronin/formatting/extensions/http/string.rb +102 -0
  31. data/lib/ronin/formatting/extensions/text.rb +23 -0
  32. data/lib/ronin/formatting/extensions/text/array.rb +125 -0
  33. data/lib/ronin/formatting/extensions/text/string.rb +206 -0
  34. data/lib/ronin/formatting/http.rb +22 -0
  35. data/lib/ronin/formatting/text.rb +22 -0
  36. data/lib/ronin/network.rb +29 -0
  37. data/lib/ronin/network/esmtp.rb +22 -0
  38. data/lib/ronin/network/extensions.rb +29 -0
  39. data/lib/ronin/network/extensions/esmtp.rb +22 -0
  40. data/lib/ronin/network/extensions/esmtp/net.rb +100 -0
  41. data/lib/ronin/network/extensions/http.rb +22 -0
  42. data/lib/ronin/network/extensions/http/net.rb +661 -0
  43. data/lib/ronin/network/extensions/imap.rb +22 -0
  44. data/lib/ronin/network/extensions/imap/net.rb +124 -0
  45. data/lib/ronin/network/extensions/pop3.rb +22 -0
  46. data/lib/ronin/network/extensions/pop3/net.rb +92 -0
  47. data/lib/ronin/network/extensions/smtp.rb +22 -0
  48. data/lib/ronin/network/extensions/smtp/net.rb +110 -0
  49. data/lib/ronin/network/extensions/ssl.rb +22 -0
  50. data/lib/ronin/network/extensions/ssl/net.rb +147 -0
  51. data/lib/ronin/network/extensions/tcp.rb +22 -0
  52. data/lib/ronin/network/extensions/tcp/net.rb +304 -0
  53. data/lib/ronin/network/extensions/telnet.rb +22 -0
  54. data/lib/ronin/network/extensions/telnet/net.rb +156 -0
  55. data/lib/ronin/network/extensions/udp.rb +22 -0
  56. data/lib/ronin/network/extensions/udp/net.rb +226 -0
  57. data/lib/ronin/network/http.rb +24 -0
  58. data/lib/ronin/network/http/exceptions.rb +22 -0
  59. data/lib/ronin/network/http/exceptions/unknown_request.rb +29 -0
  60. data/lib/ronin/network/http/http.rb +290 -0
  61. data/lib/ronin/network/http/proxy.rb +307 -0
  62. data/lib/ronin/network/imap.rb +49 -0
  63. data/lib/ronin/network/network.rb +41 -0
  64. data/lib/ronin/network/pop3.rb +49 -0
  65. data/lib/ronin/network/smtp.rb +24 -0
  66. data/lib/ronin/network/smtp/email.rb +143 -0
  67. data/lib/ronin/network/smtp/smtp.rb +68 -0
  68. data/lib/ronin/network/ssl.rb +47 -0
  69. data/lib/ronin/network/tcp.rb +22 -0
  70. data/lib/ronin/network/telnet.rb +109 -0
  71. data/lib/ronin/network/udp.rb +22 -0
  72. data/lib/ronin/path.rb +132 -0
  73. data/lib/ronin/support.rb +28 -0
  74. data/lib/ronin/support/inflector.rb +40 -0
  75. data/lib/ronin/support/version.rb +27 -0
  76. data/lib/ronin/templates.rb +23 -0
  77. data/lib/ronin/templates/erb.rb +75 -0
  78. data/lib/ronin/templates/template.rb +161 -0
  79. data/ronin-support.gemspec +10 -0
  80. data/spec/extensions/file_spec.rb +24 -0
  81. data/spec/extensions/ip_addr_spec.rb +171 -0
  82. data/spec/extensions/kernel_spec.rb +30 -0
  83. data/spec/extensions/string_spec.rb +177 -0
  84. data/spec/formatting/binary/helpers/hexdumps.rb +16 -0
  85. data/spec/formatting/binary/helpers/hexdumps/ascii.bin +0 -0
  86. data/spec/formatting/binary/helpers/hexdumps/hexdump_decimal_shorts.txt +17 -0
  87. data/spec/formatting/binary/helpers/hexdumps/hexdump_hex_bytes.txt +17 -0
  88. data/spec/formatting/binary/helpers/hexdumps/hexdump_hex_shorts.txt +17 -0
  89. data/spec/formatting/binary/helpers/hexdumps/hexdump_octal_bytes.txt +17 -0
  90. data/spec/formatting/binary/helpers/hexdumps/hexdump_octal_shorts.txt +17 -0
  91. data/spec/formatting/binary/helpers/hexdumps/hexdump_repeated.txt +6 -0
  92. data/spec/formatting/binary/helpers/hexdumps/od_decimal_bytes.txt +17 -0
  93. data/spec/formatting/binary/helpers/hexdumps/od_decimal_ints.txt +17 -0
  94. data/spec/formatting/binary/helpers/hexdumps/od_decimal_quads.txt +17 -0
  95. data/spec/formatting/binary/helpers/hexdumps/od_decimal_shorts.txt +17 -0
  96. data/spec/formatting/binary/helpers/hexdumps/od_hex_bytes.txt +17 -0
  97. data/spec/formatting/binary/helpers/hexdumps/od_hex_ints.txt +17 -0
  98. data/spec/formatting/binary/helpers/hexdumps/od_hex_quads.txt +17 -0
  99. data/spec/formatting/binary/helpers/hexdumps/od_hex_shorts.txt +17 -0
  100. data/spec/formatting/binary/helpers/hexdumps/od_octal_bytes.txt +17 -0
  101. data/spec/formatting/binary/helpers/hexdumps/od_octal_ints.txt +17 -0
  102. data/spec/formatting/binary/helpers/hexdumps/od_octal_quads.txt +17 -0
  103. data/spec/formatting/binary/helpers/hexdumps/od_octal_shorts.txt +17 -0
  104. data/spec/formatting/binary/helpers/hexdumps/od_repeated.txt +6 -0
  105. data/spec/formatting/binary/helpers/hexdumps/repeated.bin +1 -0
  106. data/spec/formatting/binary/integer_spec.rb +140 -0
  107. data/spec/formatting/binary/string_spec.rb +306 -0
  108. data/spec/formatting/digest/string_spec.rb +82 -0
  109. data/spec/formatting/http/integer_spec.rb +42 -0
  110. data/spec/formatting/http/string_spec.rb +76 -0
  111. data/spec/formatting/text/array_spec.rb +105 -0
  112. data/spec/formatting/text/string_spec.rb +180 -0
  113. data/spec/network/http/http_spec.rb +280 -0
  114. data/spec/network/http/proxy_spec.rb +150 -0
  115. data/spec/network/network_spec.rb +8 -0
  116. data/spec/network/ssl_spec.rb +14 -0
  117. data/spec/path_spec.rb +84 -0
  118. data/spec/spec_helper.rb +4 -0
  119. data/spec/support_spec.rb +8 -0
  120. data/spec/templates/classes/example_erb.rb +11 -0
  121. data/spec/templates/classes/example_template.rb +35 -0
  122. data/spec/templates/erb_spec.rb +21 -0
  123. data/spec/templates/helpers/data.rb +9 -0
  124. data/spec/templates/helpers/data/includes/_relative.erb +1 -0
  125. data/spec/templates/helpers/data/templates/example.erb +1 -0
  126. data/spec/templates/template_spec.rb +54 -0
  127. metadata +286 -0
@@ -0,0 +1,24 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'ronin/network/smtp/email'
23
+ require 'ronin/network/smtp/smtp'
24
+ require 'ronin/network/extensions/smtp'
@@ -0,0 +1,143 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ module Ronin
23
+ module Network
24
+ module SMTP
25
+ class Email
26
+
27
+ # Sender of the email
28
+ attr_accessor :from
29
+
30
+ # Recipient of the email
31
+ attr_accessor :to
32
+
33
+ # Subject of the email
34
+ attr_accessor :subject
35
+
36
+ # Date of the email
37
+ attr_accessor :date
38
+
39
+ # Unique message-id string
40
+ attr_accessor :message_id
41
+
42
+ # Body of the email
43
+ attr_accessor :body
44
+
45
+ #
46
+ # Creates a new Email object.
47
+ #
48
+ # @param [Hash] options
49
+ # Additional options.
50
+ #
51
+ # @option options [String] :from
52
+ # The address the email is from.
53
+ #
54
+ # @option options [String] :to
55
+ # The address that the email should be sent to.
56
+ #
57
+ # @option options [String] :subject
58
+ # The subject of the email.
59
+ #
60
+ # @option options [String] :message_id
61
+ # Message-ID of the email.
62
+ #
63
+ # @option options [String, Time] :date (Time.now)
64
+ # The date the email was sent on.
65
+ #
66
+ # @option options [String, Array<String>] :body
67
+ # The body of the email.
68
+ #
69
+ # @yield [email]
70
+ # If a block is given, it will be passed the newly created email
71
+ # object.
72
+ #
73
+ # @yieldparam [Email] email
74
+ # The newly created email object.
75
+ #
76
+ def initialize(options={})
77
+ @from = options[:from]
78
+ @to = options[:to]
79
+ @subject = options[:subject]
80
+ @date = options[:date] || Time.now
81
+ @message_id = options[:message_id]
82
+ @body = []
83
+
84
+ if options[:body].kind_of?(Array)
85
+ @body += options[:body]
86
+ else
87
+ @body << options[:body]
88
+ end
89
+
90
+ yield self if block_given?
91
+ end
92
+
93
+ #
94
+ # Formats the email into a SMTP message.
95
+ #
96
+ # @return [String]
97
+ # Properly formatted SMTP message.
98
+ #
99
+ # @see http://www.ruby-doc.org/stdlib/libdoc/net/smtp/rdoc/classes/Net/SMTP.html
100
+ #
101
+ def to_s
102
+ address = lambda { |info|
103
+ if info.kind_of?(Array)
104
+ return "#{info[0]} <#{info[1]}>"
105
+ elsif info.kind_of?(Hash)
106
+ return "#{info[:name]} <#{info[:email]}>"
107
+ else
108
+ return info
109
+ end
110
+ }
111
+
112
+ message = []
113
+
114
+ if @from
115
+ message << "From: #{address.call(@from)}"
116
+ end
117
+
118
+ if @to
119
+ message << "To: #{address.call(@to)}"
120
+ end
121
+
122
+ if @subject
123
+ message << "Subject: #{@subject}"
124
+ end
125
+
126
+ if @date
127
+ message << "Date: #{@date}"
128
+ end
129
+
130
+ if @message_id
131
+ message << "Message-Id: <#{@message_id}>"
132
+ end
133
+
134
+ message << ''
135
+ message += @body
136
+
137
+ return message.join("\n")
138
+ end
139
+
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,68 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'ronin/network/smtp/email'
23
+
24
+ module Ronin
25
+ module Network
26
+ module SMTP
27
+ # Default smtp port
28
+ DEFAULT_PORT = 25
29
+
30
+ #
31
+ # @return [Integer]
32
+ # The default Ronin SMTP port.
33
+ #
34
+ def SMTP.default_port
35
+ @default_port ||= DEFAULT_PORT
36
+ end
37
+
38
+ #
39
+ # Sets the default Ronin SMTP port.
40
+ #
41
+ # @param [Integer] port
42
+ # The new default Ronin SMTP port.
43
+ #
44
+ def SMTP.default_port=(port)
45
+ @default_port = port
46
+ end
47
+
48
+ #
49
+ # Creates a properly formatted email.
50
+ #
51
+ # @yield [email]
52
+ # If a block is given, it will be passed the newly created Email
53
+ # object.
54
+ #
55
+ # @yieldparam [Ronin::Network::Email::SMTP] email
56
+ # The new Email object.
57
+ #
58
+ # @return [String]
59
+ # Formatted SMTP email.
60
+ #
61
+ # @see Ronin::Network::SMTP::Email
62
+ #
63
+ def SMTP.message(options={},&block)
64
+ Email.new(options,&block).to_s
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,47 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'ronin/network/extensions/ssl'
23
+
24
+ module Ronin
25
+ module Network
26
+ module SSL
27
+ #
28
+ # Returns the OpenSSL verify mode.
29
+ #
30
+ # @param [Symbol, String] mode
31
+ # The name of the verify mode.
32
+ #
33
+ # @return [Integer]
34
+ # The verify mode number used by OpenSSL.
35
+ #
36
+ def SSL.verify(mode=nil)
37
+ verify_mode = 'VERIFY_' + (mode || :none).to_s.upcase
38
+
39
+ unless OpenSSL::SSL.const_defined?(verify_mode)
40
+ raise(RuntimeError,"unknown verify mode #{mode}")
41
+ end
42
+
43
+ return OpenSSL::SSL.const_get(verify_mode)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,22 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'ronin/network/extensions/tcp'
@@ -0,0 +1,109 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'ronin/network/extensions/telnet'
23
+
24
+ module Ronin
25
+ module Network
26
+ module Telnet
27
+ # Default telnet port
28
+ DEFAULT_PORT = 23
29
+
30
+ # The default prompt regular expression
31
+ DEFAULT_PROMPT = /[$%#>] \z/n
32
+
33
+ # The default timeout
34
+ DEFAULT_TIMEOUT = 10
35
+
36
+ #
37
+ # @return [Integer]
38
+ # The default Ronin Telnet port.
39
+ #
40
+ def Telnet.default_port
41
+ @default_port ||= DEFAULT_PORT
42
+ end
43
+
44
+ #
45
+ # Sets the default Ronin Telnet port.
46
+ #
47
+ # @param [Integer] port
48
+ # The new default Ronin Telnet port.
49
+ #
50
+ def Telnet.default_port=(port)
51
+ @default_port = port
52
+ end
53
+
54
+ #
55
+ # @return [Regexp]
56
+ # The default Ronin Telnet prompt pattern.
57
+ #
58
+ def Telnet.default_prompt
59
+ @default_prompt ||= DEFAULT_PROMPT
60
+ end
61
+
62
+ #
63
+ # Sets the default Ronin Telnet prompt pattern.
64
+ #
65
+ # @param [Regexp] prompt
66
+ # The new default Ronin Telnet prompt pattern.
67
+ #
68
+ def Telnet.default_prompt=(prompt)
69
+ @default_prompt = prompt
70
+ end
71
+
72
+ #
73
+ # @return [Integer]
74
+ # The default Ronin Telnet timeout.
75
+ #
76
+ def Telnet.default_timeout
77
+ @default_timeout ||= DEFAULT_TIMEOUT
78
+ end
79
+
80
+ #
81
+ # Sets the default Ronin Telnet timeout.
82
+ #
83
+ # @param [Integer] timeout
84
+ # The new default Ronin Telnet timeout.
85
+ #
86
+ def Telnet.default_timeout=(timeout)
87
+ @default_timeout = timeout
88
+ end
89
+
90
+ #
91
+ # @return [Telnet, IO, nil]
92
+ # The Ronin Telnet proxy.
93
+ #
94
+ def Telnet.proxy
95
+ @proxy ||= nil
96
+ end
97
+
98
+ #
99
+ # Sets the Ronin Telnet proxy.
100
+ #
101
+ # @param [Telnet, IO, nil] new_proxy
102
+ # The new Ronin Telnet proxy.
103
+ #
104
+ def Telnet.proxy=(new_proxy)
105
+ @proxy = new_proxy
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,22 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'ronin/network/extensions/udp'
data/lib/ronin/path.rb ADDED
@@ -0,0 +1,132 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'pathname'
23
+
24
+ module Ronin
25
+ #
26
+ # The {Path} class extends `Pathname` to allow representing directory
27
+ # traversal paths.
28
+ #
29
+ class Path < Pathname
30
+
31
+ # The separator to join paths together with
32
+ attr_accessor :separator
33
+
34
+ def initialize(path)
35
+ @separator = File::SEPARATOR
36
+
37
+ super(path)
38
+ end
39
+
40
+ #
41
+ # The root path.
42
+ #
43
+ # @return [Path]
44
+ # The root path.
45
+ #
46
+ def Path.root
47
+ Path.new('/')
48
+ end
49
+
50
+ #
51
+ # Creates a new path object for upward directory traversal.
52
+ #
53
+ # @param [Integer, Array, Range] n
54
+ # The number of directories to go up.
55
+ #
56
+ # @param [String] separator
57
+ # Path separator.
58
+ #
59
+ # @return [Path]
60
+ # The new path object.
61
+ #
62
+ # @raise [ArgumentError]
63
+ # A negative number was given as the first argument.
64
+ #
65
+ # @example Generate a relative path that goes up 7 directories.
66
+ # Path.up(7)
67
+ # # => #<Ronin::Path:../../../../../../..>
68
+ #
69
+ # @example Generate multiple relative paths, going up 1 to 3 directories.
70
+ # Path.up(1..3)
71
+ # # => [#<Ronin::Path:..>, #<Ronin::Path:../..>,
72
+ # #<Ronin::Path:../../..>]
73
+ #
74
+ def self.up(n,separator=File::SEPARATOR)
75
+ if n.kind_of?(Integer)
76
+ if n == 0
77
+ return separator
78
+ elsif n < 0
79
+ raise(ArgumentError,"negative argument")
80
+ end
81
+
82
+ path = self.new('..')
83
+ path.separator = separator
84
+
85
+ dirs = (['..'] * (n-1))
86
+
87
+ return Path.new(path.join(*dirs))
88
+ elsif n.kind_of?(Enumerable)
89
+ return n.map { |i| self.up(i) }
90
+ else
91
+ raise(ArgumentError,"The first argument of Path.up must be either an Integer or Enumerable")
92
+ end
93
+ end
94
+
95
+ #
96
+ # Joins directory names together with the path, but does not resolve
97
+ # the resulting path.
98
+ #
99
+ # @param [Array] names
100
+ # The names to join together.
101
+ #
102
+ # @return [Path]
103
+ # The joined path.
104
+ #
105
+ # @example
106
+ # Path.up(7).join('etc/passwd')
107
+ # # => #<Ronin::Path:../../../../../../../etc/passwd>
108
+ #
109
+ def join(*names)
110
+ sub_dirs = names.map { |name| name.to_s }
111
+
112
+ # filter out errant directory separators
113
+ sub_dirs.reject! { |dir| dir == @separator }
114
+
115
+ # join the path
116
+ sub_path = sub_dirs.join(@separator)
117
+
118
+ path = if self.root?
119
+ # prefix the root dir
120
+ self.to_s + sub_path
121
+ else
122
+ # join the path with the sub-path
123
+ [self.to_s, sub_path].join(@separator)
124
+ end
125
+
126
+ return self.class.new(path)
127
+ end
128
+
129
+ alias / join
130
+
131
+ end
132
+ end