rex-struct2 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 96e25ba1fb9934b67130e91323a69ca07d3df0d4
4
+ data.tar.gz: f3c1068604210bc216406e9b668110d9cde4bb28
5
+ SHA512:
6
+ metadata.gz: f17d67bf75872e8e37f8ff4d6669dc106a4705196e400b6166388b695e195aa017446026dc63f4b4323b7b5c24a3d277c47bd313825a7bb875dfe42cc4353d33
7
+ data.tar.gz: 1810db97a584af22b4b9819dadb5d101570aa799a49738b6e45aa8bd79840e5944ccc267343d575f488997f58f21101ab783c1813b1c291b6fc342534fe148a8
checksums.yaml.gz.sig ADDED
Binary file
data.tar.gz.sig ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,52 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This Code of Conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting the project maintainers at msfdev@metasploit.com. If
39
+ the incident involves a committer, you may report directly to
40
+ egypt@metasploit.com or todb@metasploit.com.
41
+
42
+ All complaints will be reviewed and investigated and will result in a
43
+ response that is deemed necessary and appropriate to the circumstances.
44
+ Maintainers are obligated to maintain confidentiality with regard to the
45
+ reporter of an incident.
46
+
47
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
48
+ version 1.3.0, available at
49
+ [http://contributor-covenant.org/version/1/3/0/][version]
50
+
51
+ [homepage]: http://contributor-covenant.org
52
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rex-struct2.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ Copyright (C) 2012-2013, Rapid7, Inc.
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification,
5
+ are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+
10
+ * Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+
14
+ * Neither the name of Rapid7 LLC nor the names of its contributors
15
+ may be used to endorse or promote products derived from this software
16
+ without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # Rex::Struct2
2
+
3
+ Ruby Exploitation(Rex) Library for creating and using C-like structures. Useful for various protocol and binary analysis.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rex-struct2'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rex-struct2
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rex-struct2. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
34
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rex/struct2"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,8 @@
1
+ require "rex/struct2/version"
2
+ require 'rex/struct2/c_struct_template'
3
+
4
+ module Rex
5
+ module Struct2
6
+ # Your code goes here...
7
+ end
8
+ end
@@ -0,0 +1,181 @@
1
+ # -*- coding: binary -*-
2
+
3
+ # Rex::Struct2
4
+ module Rex
5
+ module Struct2
6
+
7
+ require 'rex/struct2/s_struct'
8
+
9
+ class CStruct_Values
10
+
11
+ def initialize(obj)
12
+ @obj = obj
13
+ end
14
+
15
+ def [](*args)
16
+ o = @obj[*args]
17
+ return if !o
18
+ return o.value
19
+ end
20
+
21
+ def []=(*args)
22
+ o = @obj[*args[0 .. -2]]
23
+ return if !o
24
+ o.value = args[-1]
25
+ end
26
+
27
+ # this one is for HD, the whiniest girl around...
28
+ # allow for like v.field = whatever
29
+ def method_missing(sym, *args)
30
+ if sym.to_s[-1] == "="[0]
31
+ return self[sym.to_s[0 .. -2]] = args[0]
32
+ else
33
+ return self[sym.to_s]
34
+ end
35
+ end
36
+ end
37
+
38
+ class CStruct < SStruct
39
+
40
+ require 'rex/struct2/element'
41
+ require 'rex/struct2/generic'
42
+ require 'rex/struct2/s_string'
43
+ require 'rex/struct2/c_struct_template'
44
+ require 'rex/struct2/restraint'
45
+
46
+ include Rex::Struct2::Element
47
+
48
+ attr_reader :v
49
+
50
+ @@dt_table = {
51
+ 'int8' => proc { |*a| Rex::Struct2::Generic.new('C', true, *a) },
52
+ 'uint8' => proc { |*a| Rex::Struct2::Generic.new('C', false, *a) },
53
+ 'int16v' => proc { |*a| Rex::Struct2::Generic.new('v', true, *a) },
54
+ 'uint16v' => proc { |*a| Rex::Struct2::Generic.new('v', false, *a) },
55
+ 'int32v' => proc { |*a| Rex::Struct2::Generic.new('V', true, *a) },
56
+ 'uint32v' => proc { |*a| Rex::Struct2::Generic.new('V', false, *a) },
57
+ 'int64v' => proc { |*a| Rex::Struct2::Generic.new('q', true, *a) },
58
+ 'uint64v' => proc { |*a| Rex::Struct2::Generic.new('Q', false, *a) },
59
+ 'int16n' => proc { |*a| Rex::Struct2::Generic.new('n', true, *a) },
60
+ 'uint16n' => proc { |*a| Rex::Struct2::Generic.new('n', false, *a) },
61
+ 'int32n' => proc { |*a| Rex::Struct2::Generic.new('N', true, *a) },
62
+ 'uint32n' => proc { |*a| Rex::Struct2::Generic.new('N', false, *a) },
63
+ 'string' => proc { |*a| Rex::Struct2::SString.new(*a) },
64
+ 'sstruct' => proc { |*a| Rex::Struct2::SStruct.new(*a) },
65
+ 'object' => proc { |o| o },
66
+ 'template' => proc { |o| o.make_struct },
67
+ }
68
+
69
+ # CStruct.typedef(name, factory, ... )
70
+ def CStruct.typedef(*args)
71
+ while args.length >= 2
72
+ name = args.shift
73
+ factory = args.shift
74
+ @@dt_table[name] = factory
75
+ end
76
+ end
77
+
78
+ def initialize(*dts)
79
+ super()
80
+ @name_table = [ ]
81
+ @v = Rex::Struct2::CStruct_Values.new(self)
82
+
83
+ return self.add_from_dt(*dts)
84
+ end
85
+
86
+ def add_from_dt(*dts)
87
+ dts.each { | dt |
88
+ return if !dt.kind_of?(Array) || dt.length < 2
89
+
90
+ type = dt[0]
91
+ name = dt[1]
92
+
93
+ factory = @@dt_table[type]
94
+
95
+ return if !factory
96
+
97
+ # call with the arguments passed in
98
+ obj = factory.call(*(dt[2 .. -1]))
99
+
100
+ self.add_object(name, obj)
101
+ }
102
+
103
+ return dts.length
104
+ end
105
+
106
+ def add_object(*objs)
107
+ while objs.length >= 2
108
+ @name_table << objs.shift
109
+ self << objs.shift
110
+ end
111
+ end
112
+ # apply_restraint( name, restraint, name2, restraint2 ... )
113
+ def apply_restraint(*ress)
114
+ while ress.length >= 2
115
+ name = ress.shift
116
+ res = ress.shift
117
+ self[name].restraint = res
118
+
119
+ # update the restrainted object, so it will update the value
120
+ # of the restrainter, with the initial size. If you don't
121
+ # want this behavior, um, you'll have to be careful with what
122
+ # you supply as default values...
123
+ self[name].update_restraint
124
+ end
125
+ return self
126
+ end
127
+
128
+ # create_restraints( [ name, stuff_to_restraint_constructor ] ... )
129
+ def create_restraints(*ress)
130
+ ress.each { |r|
131
+ # make a copy before we modify...
132
+ r = r.dup
133
+ # resolve names into objects
134
+ r[1] = self[r[1]] if r[1]
135
+ r[2] = self[r[2]] if r[2]
136
+
137
+ # build and apply the restraint
138
+ self.apply_restraint(r[0], Rex::Struct2::Restraint.new(*r[1 .. -1]))
139
+ }
140
+
141
+ return self
142
+ end
143
+
144
+ # ya ya, I know, these are weird. I'm not sure why I even bothered
145
+ # to inherit from array...
146
+ def [](index, *other)
147
+ if index.kind_of?(String)
148
+ i = @name_table.index(index)
149
+ return if !i
150
+ return super(i)
151
+ else
152
+ return super(index, *other)
153
+ end
154
+ end
155
+
156
+ def []=(index, *other)
157
+ if index.kind_of?(String)
158
+ i = @name_table.index(index)
159
+ return if !i
160
+ return super(i, *other)
161
+ else
162
+ return super(index, *other)
163
+ end
164
+ end
165
+
166
+ # Produce a list of field names
167
+ def keys
168
+ @name_table
169
+ end
170
+
171
+ # Iterate through all fields and values
172
+ def each_pair(&block)
173
+ @name_table.each do |k|
174
+ block.call(k, self.v[k])
175
+ end
176
+ end
177
+ end
178
+
179
+ # end Rex::Struct2
180
+ end
181
+ end
@@ -0,0 +1,39 @@
1
+ # -*- coding: binary -*-
2
+
3
+ # Rex::Struct2
4
+ module Rex
5
+ module Struct2
6
+
7
+ class CStructTemplate
8
+
9
+ require 'rex/struct2/c_struct'
10
+
11
+ attr_reader :template, :template_create_restraints, :template_apply_restraint
12
+ attr_writer :template, :template_create_restraints, :template_apply_restraint
13
+
14
+ def initialize(*tem)
15
+ self.template = tem
16
+ self.template_create_restraints = [ ]
17
+ self.template_apply_restraint = [ ]
18
+ end
19
+
20
+ def create_restraints(*ress)
21
+ self.template_create_restraints = ress
22
+ return self
23
+ end
24
+
25
+ def apply_restraint(*ress)
26
+ self.template_apply_restraint = ress
27
+ return self
28
+ end
29
+
30
+ def make_struct
31
+ Rex::Struct2::CStruct.new(*self.template).
32
+ create_restraints(*self.template_create_restraints).
33
+ apply_restraint(*self.template_apply_restraint)
34
+ end
35
+ end
36
+
37
+ # end Rex::Struct2
38
+ end
39
+ end
@@ -0,0 +1,26 @@
1
+ # -*- coding: binary -*-
2
+
3
+ # Rex::Struct2
4
+ module Rex
5
+ module Struct2
6
+
7
+ # this is a "constant" element. It's not actually constant, you can set it
8
+ # via the constructor and value. It doesn't do from_s/to_s, etc.
9
+
10
+ # what use is it? Well it's useful for doing constant restraints (like fix
11
+ # sized arrays), and probably not a ton more.
12
+
13
+ class Constant
14
+
15
+ require 'rex/struct2/element'
16
+ include Rex::Struct2::Element
17
+
18
+ def initialize(value)
19
+ self.value = value
20
+ end
21
+
22
+ end
23
+
24
+ # end Rex::Struct2
25
+ end
26
+ end
@@ -0,0 +1,44 @@
1
+ # -*- coding: binary -*-
2
+
3
+ # Rex::Struct2
4
+ module Rex
5
+ module Struct2
6
+
7
+ module Element
8
+
9
+ # elements should have to_s, but we don't define it here because
10
+ # it will just overlap with inheritence and cause issues
11
+
12
+ attr_reader :value, :restraint, :container
13
+ attr_writer :restraint, :container
14
+
15
+ # update the restraints on any value change
16
+ def value=(newval)
17
+ @value = newval
18
+ self.update_restraint
19
+ end
20
+
21
+ # avoid conflicting with normal namespace length()
22
+ def slength
23
+ to_s().length()
24
+ end
25
+
26
+ def update_restraint
27
+ if self.restraint
28
+ # Sort of a hack, but remove the restraint before we update, so we aren't using
29
+ # the old restraint during calculating the restraint update value
30
+ old_restraint, self.restraint = self.restraint, nil
31
+ old_restraint.update(self.slength)
32
+ self.restraint = old_restraint
33
+ end
34
+
35
+ if self.container
36
+ self.container.update_restraint
37
+ end
38
+ end
39
+
40
+ end
41
+
42
+ # end Rex::Struct2
43
+ end
44
+ end
@@ -0,0 +1,73 @@
1
+ # -*- coding: binary -*-
2
+
3
+ # Rex::Struct2
4
+ module Rex
5
+ module Struct2
6
+
7
+ class Generic
8
+
9
+ require 'rex/struct2/element'
10
+ include Rex::Struct2::Element
11
+
12
+ attr_reader :default
13
+ attr_writer :default
14
+
15
+ attr_accessor :mask, :check_mask
16
+
17
+ def initialize(packspec, signed=false, default=nil)
18
+ @packspec = packspec
19
+ @default = default
20
+
21
+ bytelen = [ -1 ].pack(@packspec).length
22
+ self.mask = (1 << (8 * bytelen)) - 1
23
+
24
+ if signed
25
+ self.check_mask = 1 << (8 * bytelen - 1)
26
+ else
27
+ self.check_mask = 0
28
+ end
29
+
30
+ reset()
31
+ end
32
+
33
+ def reset
34
+ self.value = @default
35
+ end
36
+
37
+ def to_s
38
+ # I realize this will bomb out if this isn't an integer, for
39
+ # example if it is nil. That should only happen for a user
40
+ # error so that's what I want it to do...
41
+ string = [ @value ].pack(@packspec)
42
+
43
+ if restraint && restraint.max
44
+ return string.slice(0, restraint.max)
45
+ else
46
+ return string
47
+ end
48
+ # what to do for restraint.min?!?
49
+ end
50
+
51
+ def from_s(bytes)
52
+ value = bytes.unpack(@packspec)[0]
53
+ # return nil on unpack error
54
+ return if !value
55
+ len = slength()
56
+ # error on any restraint issues
57
+ return if restraint && restraint.max && len > restraint.max
58
+ return if restraint && restraint.min && len < restraint.min
59
+ # else set our value and return length used for this element
60
+
61
+ if (value & check_mask) != 0
62
+ value = -((~value & mask) + 1)
63
+ end
64
+
65
+ self.value = value
66
+ return(len)
67
+ end
68
+
69
+ end
70
+
71
+ # end Rex::Struct2
72
+ end
73
+ end
@@ -0,0 +1,54 @@
1
+ # -*- coding: binary -*-
2
+
3
+ # Rex::Struct2
4
+ module Rex
5
+ module Struct2
6
+
7
+ class Restraint
8
+
9
+ attr_reader :max_object, :min_object, :should_update,
10
+ :max_transform, :min_transform, :max_inv_transform, :min_inv_transform
11
+ attr_writer :max_object, :min_object, :should_update,
12
+ :max_transform, :min_transform, :max_inv_transform, :min_inv_transform
13
+
14
+
15
+ def initialize(
16
+ max_object=nil, min_object=nil, should_update=false,
17
+ max_transform=nil, min_transform=nil,
18
+ max_inv_transform=nil, min_inv_transform=nil
19
+ )
20
+ @max_object = max_object
21
+ @min_object = min_object
22
+ @should_update = should_update
23
+
24
+ def_trans = proc {|i| i}
25
+
26
+ @max_transform = max_transform == nil ? def_trans : max_transform
27
+ @min_transform = min_transform == nil ? def_trans : min_transform
28
+ @max_inv_transform = max_inv_transform == nil ? def_trans : max_inv_transform
29
+ @min_inv_transform = min_inv_transform == nil ? def_trans : min_inv_transform
30
+ end
31
+
32
+ def min
33
+ return if !min_object
34
+ return min_object.value
35
+ end
36
+
37
+ def max
38
+ return if !max_object
39
+ return max_object.value
40
+ end
41
+
42
+ # update values if request (ie string set field to its length)
43
+ def update(value)
44
+ return if !@should_update
45
+
46
+ max_object.value = max_inv_transform.call(value) if max_object
47
+ min_object.value = min_inv_transform.call(value) if min_object
48
+ end
49
+
50
+ end
51
+
52
+ # end Rex::Struct2
53
+ end
54
+ end
@@ -0,0 +1,72 @@
1
+ # -*- coding: binary -*-
2
+
3
+ # Rex::Struct2
4
+ module Rex
5
+ module Struct2
6
+
7
+ class SString
8
+
9
+ require 'rex/struct2/element'
10
+ require 'rex/struct2/constant'
11
+ include Rex::Struct2::Element
12
+
13
+ attr_reader :size, :default, :pad
14
+ attr_writer :default, :pad
15
+
16
+ def initialize(size=nil, default=nil, pad=nil)
17
+ self.size = size
18
+ @default = default
19
+ @pad = pad
20
+ reset()
21
+ end
22
+
23
+ def size=(newsize)
24
+ if !newsize
25
+ self.restraint = nil
26
+ else
27
+ res = Rex::Struct2::Constant.new(newsize)
28
+ self.restraint = Rex::Struct2::Restraint.new(res, res, false)
29
+ end
30
+ end
31
+
32
+ def reset
33
+ self.value = @default
34
+ end
35
+
36
+ def to_s
37
+ string = self.value
38
+
39
+ return if !string
40
+
41
+ # pad if short
42
+ if restraint && restraint.min && self.pad && restraint.min > string.length
43
+ string += self.pad * (restraint.min - string.length)
44
+ end
45
+ # truncate if long
46
+ if restraint && restraint.max
47
+ string = string.slice(0, restraint.max)
48
+ end
49
+
50
+ return string
51
+ end
52
+
53
+ def from_s(bytes)
54
+ # we don't have enough bytes to satisfy our minimum
55
+ if restraint && restraint.min && bytes.length < restraint.min
56
+ return
57
+ end
58
+
59
+ if restraint && restraint.max
60
+ self.value = bytes.slice(0, restraint.max)
61
+ else
62
+ self.value = bytes.dup
63
+ end
64
+
65
+
66
+ return(self.slength)
67
+ end
68
+ end
69
+
70
+ # end Rex::Struct2
71
+ end
72
+ end
@@ -0,0 +1,111 @@
1
+ # -*- coding: binary -*-
2
+
3
+ # Rex::Struct2
4
+ module Rex
5
+ module Struct2
6
+
7
+ class SStruct
8
+
9
+ require 'rex/struct2/element'
10
+ include Rex::Struct2::Element
11
+
12
+ attr_reader :leftover, :elements
13
+ attr_writer :leftover, :elements
14
+
15
+ private :elements, :elements=
16
+
17
+ # watch out!, leftover returns our copy of the string! so don't do
18
+ # anything stupid like struct.leftover.slice! !!
19
+
20
+ def initialize(*opts)
21
+ self.elements = [ ]
22
+ self.add_element(*opts)
23
+ end
24
+
25
+
26
+ def reset
27
+ elements.each {|e| e.reset}
28
+ return self
29
+ end
30
+
31
+ def add_element(*objs)
32
+ objs.each { |o|
33
+ elements << o
34
+ o.container = self
35
+ }
36
+ return self
37
+ end
38
+
39
+ def <<(obj)
40
+ self.add_element(obj)
41
+ end
42
+
43
+ def to_s
44
+ # !!! what do we do on mix restraint issues? just fail?
45
+ # maybe throw an exception, because that is most likely
46
+ # a usage error
47
+
48
+ buff = ""
49
+ elements.each do |e|
50
+ buff << e.to_s
51
+ end
52
+
53
+ if restraint && restraint.max
54
+ return buff.slice(0, restraint.max)
55
+ else
56
+ return buff
57
+ end
58
+ end
59
+
60
+ def length
61
+ return elements.length
62
+ end
63
+
64
+ def [](obj)
65
+ return elements[obj]
66
+ end
67
+
68
+ def each(&block)
69
+ return elements.each(&block)
70
+ end
71
+
72
+ def from_s(obytes)
73
+ # make my own copy so I can chop it up
74
+ bytes = obytes.dup
75
+ length = 0
76
+
77
+ # I don't think we should call update_restraint here, but
78
+ # I could have mis thought or something
79
+
80
+ # if we have a restraint (and if there is a val) truncate
81
+ if restraint
82
+ max = restraint.max
83
+ bytes = bytes.slice(0, max) if max
84
+ end
85
+
86
+ elements.each { |e|
87
+ used = e.from_s(bytes)
88
+ return if !used
89
+ bytes.slice!(0, used)
90
+ length += used
91
+ }
92
+
93
+ # make sure we matched out min restraint, else return failure
94
+ if restraint
95
+ min = restraint.min
96
+ return if min && length < min
97
+ end
98
+
99
+ # I guess this is me getting "set", so I should have a value
100
+ # and I should update my restraints on set
101
+ self.value = obytes.slice(0, length)
102
+
103
+ self.leftover = bytes
104
+ return(length)
105
+ end
106
+
107
+ end
108
+
109
+ # end Rex::Struct2
110
+ end
111
+ end
@@ -0,0 +1,5 @@
1
+ module Rex
2
+ module Struct2
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rex/struct2/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rex-struct2"
8
+ spec.version = Rex::Struct2::VERSION
9
+ spec.authors = ["David 'thelightcosine' Maloney"]
10
+ spec.email = ["DMaloney@rapid7.com"]
11
+
12
+ spec.summary = %q{Rex C-Struct Library}
13
+ spec.description = %q{Ruby Exploitation(Rex) library for generating/manipulating C-Style structs}
14
+ spec.homepage = "https://github.com/rapid7/rex-struct2"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.12"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ end
metadata ADDED
@@ -0,0 +1,183 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rex-struct2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David 'thelightcosine' Maloney
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDdTCCAl2gAwIBAgILBAAAAAABFUtaw5QwDQYJKoZIhvcNAQEFBQAwVzELMAkG
14
+ A1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNVBAsTB1Jv
15
+ b3QgQ0ExGzAZBgNVBAMTEkdsb2JhbFNpZ24gUm9vdCBDQTAeFw05ODA5MDExMjAw
16
+ MDBaFw0yODAxMjgxMjAwMDBaMFcxCzAJBgNVBAYTAkJFMRkwFwYDVQQKExBHbG9i
17
+ YWxT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kqaYnoYgf2L+7L5zKYT4h/NE/P7kj7EbduHgy/v/aAIrNl
85
+ 2SpuQH+SWteq3NXkAmFEEqvLJQ4sbptZt8OP8ghL3pVAvZNFmww/YVszSkShSzcg
86
+ QdihYCSEL2drS2cFd50jBeq71sxUtxbv82DUa2b+
87
+ -----END CERTIFICATE-----
88
+ date: 2016-07-15 00:00:00.000000000 Z
89
+ dependencies:
90
+ - !ruby/object:Gem::Dependency
91
+ name: bundler
92
+ requirement: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.12'
97
+ type: :development
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.12'
104
+ - !ruby/object:Gem::Dependency
105
+ name: rake
106
+ requirement: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '10.0'
111
+ type: :development
112
+ prerelease: false
113
+ version_requirements: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '10.0'
118
+ - !ruby/object:Gem::Dependency
119
+ name: rspec
120
+ requirement: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.0'
125
+ type: :development
126
+ prerelease: false
127
+ version_requirements: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.0'
132
+ description: Ruby Exploitation(Rex) library for generating/manipulating C-Style structs
133
+ email:
134
+ - DMaloney@rapid7.com
135
+ executables: []
136
+ extensions: []
137
+ extra_rdoc_files: []
138
+ files:
139
+ - ".gitignore"
140
+ - ".rspec"
141
+ - ".travis.yml"
142
+ - CODE_OF_CONDUCT.md
143
+ - Gemfile
144
+ - LICENSE
145
+ - README.md
146
+ - Rakefile
147
+ - bin/console
148
+ - bin/setup
149
+ - lib/rex/struct2.rb
150
+ - lib/rex/struct2/c_struct.rb
151
+ - lib/rex/struct2/c_struct_template.rb
152
+ - lib/rex/struct2/constant.rb
153
+ - lib/rex/struct2/element.rb
154
+ - lib/rex/struct2/generic.rb
155
+ - lib/rex/struct2/restraint.rb
156
+ - lib/rex/struct2/s_string.rb
157
+ - lib/rex/struct2/s_struct.rb
158
+ - lib/rex/struct2/version.rb
159
+ - rex-struct2.gemspec
160
+ homepage: https://github.com/rapid7/rex-struct2
161
+ licenses: []
162
+ metadata: {}
163
+ post_install_message:
164
+ rdoc_options: []
165
+ require_paths:
166
+ - lib
167
+ required_ruby_version: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - ">="
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
172
+ required_rubygems_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ requirements: []
178
+ rubyforge_project:
179
+ rubygems_version: 2.4.8
180
+ signing_key:
181
+ specification_version: 4
182
+ summary: Rex C-Struct Library
183
+ test_files: []
metadata.gz.sig ADDED
Binary file