metasploit-model 0.27.3 → 0.27.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (127) hide show
  1. checksums.yaml +8 -8
  2. data/.rspec +1 -1
  3. data/app/models/metasploit/model/association/reflection.rb +0 -3
  4. data/app/models/metasploit/model/module/ancestor/spec/template.rb +0 -3
  5. data/app/models/metasploit/model/module/class/spec/template.rb +0 -4
  6. data/app/models/metasploit/model/module/instance/spec/template.rb +0 -4
  7. data/app/models/metasploit/model/search/group/base.rb +0 -3
  8. data/app/models/metasploit/model/search/group/intersection.rb +0 -3
  9. data/app/models/metasploit/model/search/group/union.rb +0 -3
  10. data/app/models/metasploit/model/search/operation/association.rb +0 -2
  11. data/app/models/metasploit/model/search/operation/base.rb +0 -3
  12. data/app/models/metasploit/model/search/operation/boolean.rb +0 -2
  13. data/app/models/metasploit/model/search/operation/date.rb +0 -2
  14. data/app/models/metasploit/model/search/operation/group/base.rb +0 -2
  15. data/app/models/metasploit/model/search/operation/group/intersection.rb +0 -2
  16. data/app/models/metasploit/model/search/operation/group/union.rb +0 -2
  17. data/app/models/metasploit/model/search/operation/integer.rb +1 -5
  18. data/app/models/metasploit/model/search/operation/null.rb +0 -2
  19. data/app/models/metasploit/model/search/operation/set.rb +5 -2
  20. data/app/models/metasploit/model/search/operation/set/integer.rb +1 -3
  21. data/app/models/metasploit/model/search/operation/set/string.rb +1 -3
  22. data/app/models/metasploit/model/search/operation/string.rb +1 -5
  23. data/app/models/metasploit/model/search/operator/association.rb +0 -2
  24. data/app/models/metasploit/model/search/operator/attribute.rb +0 -2
  25. data/app/models/metasploit/model/search/operator/base.rb +0 -4
  26. data/app/models/metasploit/model/search/operator/delegation.rb +0 -2
  27. data/app/models/metasploit/model/search/operator/deprecated/app.rb +0 -2
  28. data/app/models/metasploit/model/search/operator/deprecated/author.rb +0 -2
  29. data/app/models/metasploit/model/search/operator/deprecated/authority.rb +0 -2
  30. data/app/models/metasploit/model/search/operator/deprecated/platform.rb +0 -2
  31. data/app/models/metasploit/model/search/operator/deprecated/ref.rb +0 -2
  32. data/app/models/metasploit/model/search/operator/deprecated/text.rb +0 -2
  33. data/app/models/metasploit/model/search/operator/group/base.rb +0 -2
  34. data/app/models/metasploit/model/search/operator/group/intersection.rb +0 -2
  35. data/app/models/metasploit/model/search/operator/group/union.rb +0 -2
  36. data/app/models/metasploit/model/search/operator/null.rb +0 -2
  37. data/app/models/metasploit/model/search/operator/single.rb +0 -2
  38. data/app/models/metasploit/model/search/query.rb +0 -3
  39. data/app/models/metasploit/model/spec/template.rb +0 -3
  40. data/app/models/metasploit/model/visitation/visitor.rb +0 -3
  41. data/lib/metasploit/model.rb +14 -8
  42. data/lib/metasploit/model/architecture.rb +320 -326
  43. data/lib/metasploit/model/association.rb +43 -46
  44. data/lib/metasploit/model/association/error.rb +29 -33
  45. data/lib/metasploit/model/association/tree.rb +119 -125
  46. data/lib/metasploit/model/author.rb +45 -51
  47. data/lib/metasploit/model/authority.rb +139 -146
  48. data/lib/metasploit/model/authority/bid.rb +0 -2
  49. data/lib/metasploit/model/authority/cve.rb +0 -2
  50. data/lib/metasploit/model/authority/msb.rb +0 -2
  51. data/lib/metasploit/model/authority/osvdb.rb +0 -2
  52. data/lib/metasploit/model/authority/pmasa.rb +0 -2
  53. data/lib/metasploit/model/authority/secunia.rb +0 -2
  54. data/lib/metasploit/model/authority/us_cert_vu.rb +0 -2
  55. data/lib/metasploit/model/authority/waraxe.rb +0 -2
  56. data/lib/metasploit/model/authority/zdi.rb +0 -2
  57. data/lib/metasploit/model/base.rb +0 -2
  58. data/lib/metasploit/model/derivation.rb +95 -99
  59. data/lib/metasploit/model/derivation/full_name.rb +16 -22
  60. data/lib/metasploit/model/email_address.rb +122 -128
  61. data/lib/metasploit/model/engine.rb +26 -21
  62. data/lib/metasploit/model/error.rb +3 -7
  63. data/lib/metasploit/model/file.rb +0 -2
  64. data/lib/metasploit/model/invalid.rb +11 -17
  65. data/lib/metasploit/model/login.rb +0 -3
  66. data/lib/metasploit/model/login/status.rb +0 -2
  67. data/lib/metasploit/model/module.rb +19 -23
  68. data/lib/metasploit/model/module/action.rb +50 -58
  69. data/lib/metasploit/model/module/ancestor.rb +456 -465
  70. data/lib/metasploit/model/module/ancestor/spec.rb +3 -2
  71. data/lib/metasploit/model/module/architecture.rb +27 -35
  72. data/lib/metasploit/model/module/author.rb +38 -47
  73. data/lib/metasploit/model/module/class.rb +358 -366
  74. data/lib/metasploit/model/module/class/spec.rb +3 -2
  75. data/lib/metasploit/model/module/handler.rb +28 -34
  76. data/lib/metasploit/model/module/instance.rb +586 -596
  77. data/lib/metasploit/model/module/instance/spec.rb +3 -2
  78. data/lib/metasploit/model/module/path.rb +157 -166
  79. data/lib/metasploit/model/module/platform.rb +25 -33
  80. data/lib/metasploit/model/module/rank.rb +71 -79
  81. data/lib/metasploit/model/module/reference.rb +25 -33
  82. data/lib/metasploit/model/module/stance.rb +15 -21
  83. data/lib/metasploit/model/module/target.rb +76 -84
  84. data/lib/metasploit/model/module/target/architecture.rb +27 -37
  85. data/lib/metasploit/model/module/target/platform.rb +27 -37
  86. data/lib/metasploit/model/module/type.rb +35 -41
  87. data/lib/metasploit/model/nilify_blanks.rb +39 -43
  88. data/lib/metasploit/model/platform.rb +231 -237
  89. data/lib/metasploit/model/real_pathname.rb +12 -16
  90. data/lib/metasploit/model/realm.rb +0 -2
  91. data/lib/metasploit/model/realm/key.rb +0 -2
  92. data/lib/metasploit/model/reference.rb +102 -108
  93. data/lib/metasploit/model/search.rb +94 -97
  94. data/lib/metasploit/model/search/association.rb +163 -169
  95. data/lib/metasploit/model/search/attribute.rb +131 -139
  96. data/lib/metasploit/model/search/group.rb +5 -2
  97. data/lib/metasploit/model/search/operation.rb +32 -29
  98. data/lib/metasploit/model/search/operation/group.rb +5 -2
  99. data/lib/metasploit/model/search/operation/value.rb +7 -0
  100. data/lib/metasploit/model/search/operation/{integer/value.rb → value/integer.rb} +1 -1
  101. data/lib/metasploit/model/search/operation/{string/value.rb → value/string.rb} +1 -1
  102. data/lib/metasploit/model/search/operator.rb +65 -65
  103. data/lib/metasploit/model/search/operator/deprecated.rb +8 -2
  104. data/lib/metasploit/model/search/operator/group.rb +5 -2
  105. data/lib/metasploit/model/search/operator/help.rb +71 -79
  106. data/lib/metasploit/model/search/with.rb +72 -78
  107. data/lib/metasploit/model/spec.rb +133 -136
  108. data/lib/metasploit/model/spec/error.rb +3 -9
  109. data/lib/metasploit/model/spec/i18n_exception_handler.rb +0 -2
  110. data/lib/metasploit/model/spec/pathname_collision.rb +19 -27
  111. data/lib/metasploit/model/spec/template/write.rb +0 -2
  112. data/lib/metasploit/model/spec/temporary_pathname.rb +47 -56
  113. data/lib/metasploit/model/translation.rb +0 -2
  114. data/lib/metasploit/model/version.rb +1 -1
  115. data/lib/metasploit/model/visitation.rb +7 -10
  116. data/lib/metasploit/model/visitation/visit.rb +79 -85
  117. data/metasploit-model.gemspec +1 -1
  118. data/spec/app/models/metasploit/model/search/operation/integer_spec.rb +1 -1
  119. data/spec/app/models/metasploit/model/search/operation/set/integer_spec.rb +1 -1
  120. data/spec/app/models/metasploit/model/search/operation/set/string_spec.rb +1 -1
  121. data/spec/app/models/metasploit/model/search/operation/string_spec.rb +1 -1
  122. data/spec/dummy/config/application.rb +1 -0
  123. data/spec/lib/metasploit/model/search/operation/{integer/value_spec.rb → value/integer_spec.rb} +2 -2
  124. data/spec/lib/metasploit/model/search/operation/{string/value_spec.rb → value/string_spec.rb} +2 -2
  125. data/spec/support/shared/examples/metasploit/model/search/operation/{integer/value.rb → value/integer.rb} +2 -2
  126. data/spec/support/shared/examples/metasploit/model/search/operation/{string/value.rb → value/string.rb} +2 -2
  127. metadata +16 -13
@@ -1,60 +1,52 @@
1
- require 'metasploit/model/translation'
2
-
3
- module Metasploit
4
- module Model
5
- module Module
6
- # Code shared between `Mdm::Module::Action` and `Metasploit::Framework::Module::Action`.
7
- module Action
8
- extend ActiveModel::Naming
9
- extend ActiveSupport::Concern
10
-
11
- include Metasploit::Model::Translation
12
-
13
- included do
14
- include ActiveModel::MassAssignmentSecurity
15
- include ActiveModel::Validations
16
- include Metasploit::Model::Search
17
-
18
- #
19
- # Mass Assignment Security
20
- #
21
-
22
- attr_accessible :name
23
-
24
- #
25
- # Search Attributes
26
- #
27
-
28
- search_attribute :name, :type => :string
29
-
30
- #
31
- # Validations
32
- #
33
-
34
- validates :module_instance,
35
- :presence => true
36
- validates :name,
37
- :presence => true
38
- end
39
-
40
- #
41
- # Associations
42
- #
43
-
44
- # @!attribute [rw] module_instance
45
- # Module that has this action.
46
- #
47
- # @return [Metasploit::Model::Module::Instance]
48
-
49
- #
50
- # Attributes
51
- #
52
-
53
- # @!attribute [rw] name
54
- # The name of this action.
55
- #
56
- # @return [String]
57
- end
58
- end
1
+ # Code shared between `Mdm::Module::Action` and `Metasploit::Framework::Module::Action`.
2
+ module Metasploit::Model::Module::Action
3
+ extend ActiveModel::Naming
4
+ extend ActiveSupport::Concern
5
+
6
+ include Metasploit::Model::Translation
7
+
8
+ included do
9
+ include ActiveModel::MassAssignmentSecurity
10
+ include ActiveModel::Validations
11
+ include Metasploit::Model::Search
12
+
13
+ #
14
+ # Mass Assignment Security
15
+ #
16
+
17
+ attr_accessible :name
18
+
19
+ #
20
+ # Search Attributes
21
+ #
22
+
23
+ search_attribute :name, :type => :string
24
+
25
+ #
26
+ # Validations
27
+ #
28
+
29
+ validates :module_instance,
30
+ :presence => true
31
+ validates :name,
32
+ :presence => true
59
33
  end
34
+
35
+ #
36
+ # Associations
37
+ #
38
+
39
+ # @!attribute [rw] module_instance
40
+ # Module that has this action.
41
+ #
42
+ # @return [Metasploit::Model::Module::Instance]
43
+
44
+ #
45
+ # Attributes
46
+ #
47
+
48
+ # @!attribute [rw] name
49
+ # The name of this action.
50
+ #
51
+ # @return [String]
60
52
  end
@@ -1,500 +1,491 @@
1
- require 'metasploit/model/module/type'
2
- require 'metasploit/model/translation'
3
-
4
- module Metasploit
5
- module Model
6
- module Module
7
- # Code shared between `Mdm::Module::Ancestor` and `Metasploit::Framework::Module::Ancestor`.
8
- module Ancestor
9
- extend ActiveModel::Naming
10
- extend ActiveSupport::Autoload
11
- extend ActiveSupport::Concern
12
-
13
- include Metasploit::Model::Translation
14
-
15
- autoload :Spec
16
-
17
- #
18
- # CONSTANTS
19
- #
20
-
21
- # The directory for a given {#module_type} is a not always the pluralization of #module_type, so this maps the
22
- # #module_type to the type directory that is used to generate the #real_path from the #module_type and
23
- # #reference_name.
24
- DIRECTORY_BY_MODULE_TYPE = {
25
- Metasploit::Model::Module::Type::AUX => Metasploit::Model::Module::Type::AUX,
26
- Metasploit::Model::Module::Type::ENCODER => Metasploit::Model::Module::Type::ENCODER.pluralize,
27
- Metasploit::Model::Module::Type::EXPLOIT => Metasploit::Model::Module::Type::EXPLOIT.pluralize,
28
- Metasploit::Model::Module::Type::NOP => Metasploit::Model::Module::Type::NOP.pluralize,
29
- Metasploit::Model::Module::Type::PAYLOAD => Metasploit::Model::Module::Type::PAYLOAD.pluralize,
30
- Metasploit::Model::Module::Type::POST => Metasploit::Model::Module::Type::POST
31
- }
32
-
33
- # File extension used for metasploit modules.
34
- EXTENSION = '.rb'
35
-
36
- # The {#payload_type payload types} that require {#handler_type}.
37
- HANDLED_TYPES = [
38
- 'single',
39
- 'stager'
40
- ]
41
-
42
- # Maps directory to {#module_type} for converting a {#real_path} into a {#module_type} and {#reference_name}
43
- MODULE_TYPE_BY_DIRECTORY = DIRECTORY_BY_MODULE_TYPE.invert
44
-
45
- # Valid values for {#payload_type} if {#payload?} is `true`.
46
- PAYLOAD_TYPES = [
47
- 'single',
48
- 'stage',
49
- 'stager'
50
- ]
51
-
52
- # Regexp to keep '\' out of reference names
53
- REFERENCE_NAME_REGEXP = /\A[\-0-9A-Z_a-z]+(?:\/[\-0-9A-Z_a-z]+)*\Z/
54
-
55
- # Separator used to join names in {#reference_name}. It is always '/', even on Windows, where '\' is a valid
56
- # file separator.
57
- REFERENCE_NAME_SEPARATOR = '/'
58
-
59
- # Regular expression matching a full SHA-1 hex digest.
60
- SHA1_HEX_DIGEST_REGEXP = /\A[0-9a-z]{40}\Z/
61
-
62
- included do
63
- include ActiveModel::MassAssignmentSecurity
64
- include ActiveModel::Validations
65
- include ActiveModel::Validations::Callbacks
66
- include Metasploit::Model::Derivation
67
- include Metasploit::Model::Derivation::FullName
68
- include Metasploit::Model::RealPathname
69
-
70
- #
71
- # Derivations
72
- #
73
-
74
- #
75
- # Module Cache Construction derivation of {#module_type} and {#reference_name} from {#real_path} and
76
- # {Metasploit::Model::Module::Path#real_path}.
77
- #
78
-
79
- derives :module_type, :validate => false
80
- derives :reference_name, :validate => false
81
-
82
- #
83
- # Normal derivation from setting {#module_type} and {#reference_name}
84
- #
85
-
86
- derives :full_name, :validate => true
87
- derives :payload_type, :validate => true
88
- derives :real_path, :validate => true
89
-
90
- # Don't validate attributes that require accessing file system to derive value
91
- derives :real_path_modified_at, :validate => false
92
- derives :real_path_sha1_hex_digest, :validate => false
93
-
94
- #
95
- # Mass Assignment Security
96
- #
97
-
98
- # full_name is NOT accessible since it's derived and must match {#derived_full_name} so there's no reason for a
99
- # user to set it.
100
- # handler_type is accessible because it's needed to derive {Metasploit::Model::Module::Class#reference_name}.
101
- attr_accessible :handler_type
102
- # module_type is accessible because it's needed to derive {#full_name} and {#real_path}.
103
- attr_accessible :module_type
104
- # payload_type is NOT accessible since it's derived and must match {#derived_payload_type}.
105
- # reference_name is accessible because it's needed to derive {#full_name} and {#real_path}.
106
- attr_accessible :reference_name
107
- # real_path is accessible since {#module_type} and {#reference_name} can be derived from real_path.
108
- attr_accessible :real_path
109
- # real_path_modified_at is NOT accessible since it's derived
110
- # real_path_sha1_hex_digest is NOT accessible since it's derived
111
-
112
- #
113
- # Validations
114
- #
115
-
116
- validates :handler_type,
117
- :unless => :loading_context?,
118
- :nil => {
119
- :unless => :handled?
120
- },
121
- :presence => {
122
- :if => :handled?
123
- }
124
- validates :module_type,
125
- :inclusion => {
126
- :in => Metasploit::Model::Module::Type::ALL
127
- }
128
- validates :parent_path,
129
- :presence => true
130
- validates :payload_type,
131
- :inclusion => {
132
- :if => :payload?,
133
- :in => PAYLOAD_TYPES
134
- },
135
- :nil => {
136
- :unless => :payload?
137
- }
138
- validates :real_path_modified_at,
139
- :presence => true
140
- validates :real_path_sha1_hex_digest,
141
- :format => {
142
- :with => SHA1_HEX_DIGEST_REGEXP
143
- }
144
- validates :reference_name,
145
- :format => {
146
- :with => REFERENCE_NAME_REGEXP
147
- }
148
- end
149
-
150
- # Defines class methods added to classes that include {Metasploit::Model::Module::Ancestor}.
151
- module ClassMethods
152
- # Returns whether {#handler_type} is required or must be `nil` for the given payload_type.
153
- #
154
- # @param options [Hash{Symbol => String,nil}]
155
- # @option options [String, nil] module_type (nil) `nil` or an element of
156
- # `Metasploit::Model::Module::Ancestor::MODULE_TYPES`.
157
- # @option options [String, nil] payload_type (nil) `nil` or an element of {PAYLOAD_TYPES}.
158
- # @return [true] if {#handler_type} must be present.
159
- # @return [false] if {#handler_type} must be `nil`.
160
- def handled?(options={})
161
- options.assert_valid_keys(:module_type, :payload_type)
162
-
163
- handled = false
164
- module_type = options[:module_type]
165
- payload_type = options[:payload_type]
166
-
167
- if module_type == 'payload' and HANDLED_TYPES.include? payload_type
168
- handled = true
169
- end
170
-
171
- handled
172
- end
173
- end
1
+ # Code shared between `Mdm::Module::Ancestor` and `Metasploit::Framework::Module::Ancestor`.
2
+ module Metasploit::Model::Module::Ancestor
3
+ extend ActiveModel::Naming
4
+ extend ActiveSupport::Autoload
5
+ extend ActiveSupport::Concern
6
+
7
+ include Metasploit::Model::Translation
8
+
9
+ autoload :Spec
10
+
11
+ #
12
+ # CONSTANTS
13
+ #
14
+
15
+ # The directory for a given {#module_type} is a not always the pluralization of #module_type, so this maps the
16
+ # #module_type to the type directory that is used to generate the #real_path from the #module_type and
17
+ # #reference_name.
18
+ DIRECTORY_BY_MODULE_TYPE = {
19
+ Metasploit::Model::Module::Type::AUX => Metasploit::Model::Module::Type::AUX,
20
+ Metasploit::Model::Module::Type::ENCODER => Metasploit::Model::Module::Type::ENCODER.pluralize,
21
+ Metasploit::Model::Module::Type::EXPLOIT => Metasploit::Model::Module::Type::EXPLOIT.pluralize,
22
+ Metasploit::Model::Module::Type::NOP => Metasploit::Model::Module::Type::NOP.pluralize,
23
+ Metasploit::Model::Module::Type::PAYLOAD => Metasploit::Model::Module::Type::PAYLOAD.pluralize,
24
+ Metasploit::Model::Module::Type::POST => Metasploit::Model::Module::Type::POST
25
+ }
26
+
27
+ # File extension used for metasploit modules.
28
+ EXTENSION = '.rb'
29
+
30
+ # The {#payload_type payload types} that require {#handler_type}.
31
+ HANDLED_TYPES = [
32
+ 'single',
33
+ 'stager'
34
+ ]
35
+
36
+ # Maps directory to {#module_type} for converting a {#real_path} into a {#module_type} and {#reference_name}
37
+ MODULE_TYPE_BY_DIRECTORY = DIRECTORY_BY_MODULE_TYPE.invert
38
+
39
+ # Valid values for {#payload_type} if {#payload?} is `true`.
40
+ PAYLOAD_TYPES = [
41
+ 'single',
42
+ 'stage',
43
+ 'stager'
44
+ ]
45
+
46
+ # Regexp to keep '\' out of reference names
47
+ REFERENCE_NAME_REGEXP = /\A[\-0-9A-Z_a-z]+(?:\/[\-0-9A-Z_a-z]+)*\Z/
48
+
49
+ # Separator used to join names in {#reference_name}. It is always '/', even on Windows, where '\' is a valid
50
+ # file separator.
51
+ REFERENCE_NAME_SEPARATOR = '/'
52
+
53
+ # Regular expression matching a full SHA-1 hex digest.
54
+ SHA1_HEX_DIGEST_REGEXP = /\A[0-9a-z]{40}\Z/
55
+
56
+ included do
57
+ include ActiveModel::MassAssignmentSecurity
58
+ include ActiveModel::Validations
59
+ include ActiveModel::Validations::Callbacks
60
+ include Metasploit::Model::Derivation
61
+ include Metasploit::Model::Derivation::FullName
62
+ include Metasploit::Model::RealPathname
63
+
64
+ #
65
+ # Derivations
66
+ #
67
+
68
+ #
69
+ # Module Cache Construction derivation of {#module_type} and {#reference_name} from {#real_path} and
70
+ # {Metasploit::Model::Module::Path#real_path}.
71
+ #
72
+
73
+ derives :module_type, :validate => false
74
+ derives :reference_name, :validate => false
75
+
76
+ #
77
+ # Normal derivation from setting {#module_type} and {#reference_name}
78
+ #
79
+
80
+ derives :full_name, :validate => true
81
+ derives :payload_type, :validate => true
82
+ derives :real_path, :validate => true
83
+
84
+ # Don't validate attributes that require accessing file system to derive value
85
+ derives :real_path_modified_at, :validate => false
86
+ derives :real_path_sha1_hex_digest, :validate => false
87
+
88
+ #
89
+ # Mass Assignment Security
90
+ #
91
+
92
+ # full_name is NOT accessible since it's derived and must match {#derived_full_name} so there's no reason for a
93
+ # user to set it.
94
+ # handler_type is accessible because it's needed to derive {Metasploit::Model::Module::Class#reference_name}.
95
+ attr_accessible :handler_type
96
+ # module_type is accessible because it's needed to derive {#full_name} and {#real_path}.
97
+ attr_accessible :module_type
98
+ # payload_type is NOT accessible since it's derived and must match {#derived_payload_type}.
99
+ # reference_name is accessible because it's needed to derive {#full_name} and {#real_path}.
100
+ attr_accessible :reference_name
101
+ # real_path is accessible since {#module_type} and {#reference_name} can be derived from real_path.
102
+ attr_accessible :real_path
103
+ # real_path_modified_at is NOT accessible since it's derived
104
+ # real_path_sha1_hex_digest is NOT accessible since it's derived
105
+
106
+ #
107
+ # Validations
108
+ #
109
+
110
+ validates :handler_type,
111
+ :unless => :loading_context?,
112
+ :nil => {
113
+ :unless => :handled?
114
+ },
115
+ :presence => {
116
+ :if => :handled?
117
+ }
118
+ validates :module_type,
119
+ :inclusion => {
120
+ :in => Metasploit::Model::Module::Type::ALL
121
+ }
122
+ validates :parent_path,
123
+ :presence => true
124
+ validates :payload_type,
125
+ :inclusion => {
126
+ :if => :payload?,
127
+ :in => PAYLOAD_TYPES
128
+ },
129
+ :nil => {
130
+ :unless => :payload?
131
+ }
132
+ validates :real_path_modified_at,
133
+ :presence => true
134
+ validates :real_path_sha1_hex_digest,
135
+ :format => {
136
+ :with => SHA1_HEX_DIGEST_REGEXP
137
+ }
138
+ validates :reference_name,
139
+ :format => {
140
+ :with => REFERENCE_NAME_REGEXP
141
+ }
142
+ end
174
143
 
175
- #
176
- # Associations
177
- #
178
-
179
- # @!attribute [rw] parent_path
180
- # Path under which this ancestor exists on-disk.
181
- #
182
- # @return [Metasploit::Model::Module::Path]
183
-
184
- #
185
- # Attributes
186
- #
187
-
188
- # @!attribute [rw] full_name
189
- # The full name of the module. The full name is `"#{module_type}/#{reference_name}"`.
190
- #
191
- # @return [String]
192
-
193
- # @!attribute [rw] handler_type
194
- # The handler type (in the case of singles) or (in the case of stagers) the handler type alias. Handler type is
195
- # appended to the end of the single's or stage's {#reference_name} to get the
196
- # {Metasploit::Model::Module::Class#reference_name}.
197
- #
198
- # @return [String] if {Metasploit::Model::Module::Ancestor#handled?} is `true`.
199
- # @return [nil] if {Metasploit::Model::Module::Ancestor#handled?} is `false`.
200
-
201
- # @!attribute [rw] module_type
202
- # The type of the module. This would be called #type, but #type is reserved for ActiveRecord's single table
203
- # inheritance.
204
- #
205
- # @return [String] key in {Metasploit::Model::Module::Ancestor::DIRECTORY_BY_MODULE_TYPE}.
206
-
207
- # @!attribute [rw] payload_type
208
- # For payload modules, the type of payload, either 'single', 'stage', or 'stager'.
209
- #
210
- # @return ['single', 'stage', 'stager'] if `Metasploit::Model::Module::Ancestor#payload?` is `true`.
211
- # @return [nil] if `Metasploit::Model::Module::Ancestor#payload?` is `false`
212
- # @see Metasploit::Model::Module::Ancestor::PAYLOAD_TYPES
213
-
214
- # @!attribute [rw] real_path
215
- # The real (absolute) path to module file on-disk.
216
- #
217
- # @return [String]
218
-
219
- # @!attribute [rw] real_path_modified_at
220
- # The modification time of the module {#real_path file on-disk}.
221
- #
222
- # @return [DateTime]
223
-
224
- # @!attribute [rw] real_path_sha1_hex_digest
225
- # The SHA1 hexadecimal digest of contents of the file at {#real_path}. Stored as a string because postgres does not
226
- # have support for a 160 bit numerical type and the hexdigest format is more recognizable when using SQL directly.
227
- #
228
- # @see Digest::SHA1#hexdigest
229
- # @return [String]
230
-
231
- # @!attribute [rw] reference_name
232
- # The reference name of the module. The name of the module under its {#module_type type}.
233
- #
234
- # @return [String]
235
-
236
- #
237
- # Instance Methods
238
- #
239
-
240
- # The contents of {#real_path}.
241
- #
242
- # @return [String] contents of file at {#real_path}.
243
- # @return [nil] if {#real_path} is `nil`.
244
- # @return [nil] if {#real_path} does not exist on-disk.
245
- def contents
246
- contents = nil
247
-
248
- if real_path
249
- # rescue around both File calls since file could be deleted before size or after size and before read
250
- begin
251
- size = File.size(real_path)
252
- # Specify full size of file for faster read on Windows (less chance of context switching mid-read).
253
- # Open in binary mode in Windows to handle non-text content embedded in file.
254
- contents = File.read(real_path, size, 0, mode: 'rb')
255
- rescue Errno::ENOENT
256
- contents = nil
257
- end
258
- end
144
+ # Defines class methods added to classes that include {Metasploit::Model::Module::Ancestor}.
145
+ module ClassMethods
146
+ # Returns whether {#handler_type} is required or must be `nil` for the given payload_type.
147
+ #
148
+ # @param options [Hash{Symbol => String,nil}]
149
+ # @option options [String, nil] module_type (nil) `nil` or an element of
150
+ # `Metasploit::Model::Module::Ancestor::MODULE_TYPES`.
151
+ # @option options [String, nil] payload_type (nil) `nil` or an element of {PAYLOAD_TYPES}.
152
+ # @return [true] if {#handler_type} must be present.
153
+ # @return [false] if {#handler_type} must be `nil`.
154
+ def handled?(options={})
155
+ options.assert_valid_keys(:module_type, :payload_type)
156
+
157
+ handled = false
158
+ module_type = options[:module_type]
159
+ payload_type = options[:payload_type]
160
+
161
+ if module_type == 'payload' and HANDLED_TYPES.include? payload_type
162
+ handled = true
163
+ end
259
164
 
260
- contents
261
- end
165
+ handled
166
+ end
167
+ end
262
168
 
263
- # Derives {#module_type} from {#real_path} and {Metasploit::Model::Module::Path#real_path}.
264
- #
265
- # @return [String]
266
- # @return [nil] if {#real_path} is `nil`
267
- # @return [nil] if {#relative_file_names} does not start with a module type directory.
268
- def derived_module_type
269
- module_type_directory = relative_file_names.first
270
- derived = MODULE_TYPE_BY_DIRECTORY[module_type_directory]
169
+ #
170
+ # Associations
171
+ #
172
+
173
+ # @!attribute [rw] parent_path
174
+ # Path under which this ancestor exists on-disk.
175
+ #
176
+ # @return [Metasploit::Model::Module::Path]
177
+
178
+ #
179
+ # Attributes
180
+ #
181
+
182
+ # @!attribute [rw] full_name
183
+ # The full name of the module. The full name is `"#{module_type}/#{reference_name}"`.
184
+ #
185
+ # @return [String]
186
+
187
+ # @!attribute [rw] handler_type
188
+ # The handler type (in the case of singles) or (in the case of stagers) the handler type alias. Handler type is
189
+ # appended to the end of the single's or stage's {#reference_name} to get the
190
+ # {Metasploit::Model::Module::Class#reference_name}.
191
+ #
192
+ # @return [String] if {Metasploit::Model::Module::Ancestor#handled?} is `true`.
193
+ # @return [nil] if {Metasploit::Model::Module::Ancestor#handled?} is `false`.
194
+
195
+ # @!attribute [rw] module_type
196
+ # The type of the module. This would be called #type, but #type is reserved for ActiveRecord's single table
197
+ # inheritance.
198
+ #
199
+ # @return [String] key in {Metasploit::Model::Module::Ancestor::DIRECTORY_BY_MODULE_TYPE}.
200
+
201
+ # @!attribute [rw] payload_type
202
+ # For payload modules, the type of payload, either 'single', 'stage', or 'stager'.
203
+ #
204
+ # @return ['single', 'stage', 'stager'] if `Metasploit::Model::Module::Ancestor#payload?` is `true`.
205
+ # @return [nil] if `Metasploit::Model::Module::Ancestor#payload?` is `false`
206
+ # @see Metasploit::Model::Module::Ancestor::PAYLOAD_TYPES
207
+
208
+ # @!attribute [rw] real_path
209
+ # The real (absolute) path to module file on-disk.
210
+ #
211
+ # @return [String]
212
+
213
+ # @!attribute [rw] real_path_modified_at
214
+ # The modification time of the module {#real_path file on-disk}.
215
+ #
216
+ # @return [DateTime]
217
+
218
+ # @!attribute [rw] real_path_sha1_hex_digest
219
+ # The SHA1 hexadecimal digest of contents of the file at {#real_path}. Stored as a string because postgres does not
220
+ # have support for a 160 bit numerical type and the hexdigest format is more recognizable when using SQL directly.
221
+ #
222
+ # @see Digest::SHA1#hexdigest
223
+ # @return [String]
224
+
225
+ # @!attribute [rw] reference_name
226
+ # The reference name of the module. The name of the module under its {#module_type type}.
227
+ #
228
+ # @return [String]
229
+
230
+ #
231
+ # Instance Methods
232
+ #
233
+
234
+ # The contents of {#real_path}.
235
+ #
236
+ # @return [String] contents of file at {#real_path}.
237
+ # @return [nil] if {#real_path} is `nil`.
238
+ # @return [nil] if {#real_path} does not exist on-disk.
239
+ def contents
240
+ contents = nil
241
+
242
+ if real_path
243
+ # rescue around both File calls since file could be deleted before size or after size and before read
244
+ begin
245
+ size = File.size(real_path)
246
+ # Specify full size of file for faster read on Windows (less chance of context switching mid-read).
247
+ # Open in binary mode in Windows to handle non-text content embedded in file.
248
+ contents = File.read(real_path, size, 0, mode: 'rb')
249
+ rescue Errno::ENOENT
250
+ contents = nil
251
+ end
252
+ end
271
253
 
272
- derived
273
- end
254
+ contents
255
+ end
274
256
 
275
- # Derives {#payload_type} from {#reference_name}.
276
- #
277
- # @return [String]
278
- # @return [nil] if {#payload_type_directory} is `nil`
279
- def derived_payload_type
280
- derived = nil
281
- directory = payload_type_directory
257
+ # Derives {#module_type} from {#real_path} and {Metasploit::Model::Module::Path#real_path}.
258
+ #
259
+ # @return [String]
260
+ # @return [nil] if {#real_path} is `nil`
261
+ # @return [nil] if {#relative_file_names} does not start with a module type directory.
262
+ def derived_module_type
263
+ module_type_directory = relative_file_names.first
264
+ derived = MODULE_TYPE_BY_DIRECTORY[module_type_directory]
282
265
 
283
- if directory
284
- derived = directory.singularize
285
- end
266
+ derived
267
+ end
286
268
 
287
- derived
288
- end
269
+ # Derives {#payload_type} from {#reference_name}.
270
+ #
271
+ # @return [String]
272
+ # @return [nil] if {#payload_type_directory} is `nil`
273
+ def derived_payload_type
274
+ derived = nil
275
+ directory = payload_type_directory
289
276
 
290
- # Derives {#real_path} by combining {Metasploit::Model::Module::Path#real_path parent_path.real_path},
291
- # {#module_type_directory}, and {#reference_path} in the same way the module loader does in
292
- # metasploit-framework.
293
- #
294
- # @return [String] the real path to the file holding the ruby Module or ruby Class represented by this ancestor.
295
- # @return [nil] if {#parent_path} is `nil`.
296
- # @return [nil] if {Metasploit::Model::Module::Path#real_path parent_path.real_path} is `nil`.
297
- # @return [nil] if {#module_type_directory} is `nil`.
298
- # @return [nil] if {#reference_name} is `nil`.
299
- def derived_real_path
300
- derived_real_path = nil
301
-
302
- if parent_path and parent_path.real_path and module_type_directory and reference_path
303
- derived_real_path = File.join(
304
- parent_path.real_path,
305
- module_type_directory,
306
- reference_path
307
- )
308
- end
277
+ if directory
278
+ derived = directory.singularize
279
+ end
309
280
 
310
- derived_real_path
311
- end
281
+ derived
282
+ end
312
283
 
313
- # Derives {#real_path_modified_at} by getting the modification time of the file on-disk.
314
- #
315
- # @return [Time] modification time of {#real_path} if {#real_path} exists on disk and modification time can be
316
- # queried by user.
317
- # @return [nil] if {#real_path} does not exist or user cannot query the file's modification time.
318
- def derived_real_path_modified_at
319
- real_path_string = real_path.to_s
320
-
321
- begin
322
- mtime = File.mtime(real_path_string)
323
- rescue Errno::ENOENT
324
- nil
325
- else
326
- mtime.utc
327
- end
328
- end
284
+ # Derives {#real_path} by combining {Metasploit::Model::Module::Path#real_path parent_path.real_path},
285
+ # {#module_type_directory}, and {#reference_path} in the same way the module loader does in
286
+ # metasploit-framework.
287
+ #
288
+ # @return [String] the real path to the file holding the ruby Module or ruby Class represented by this ancestor.
289
+ # @return [nil] if {#parent_path} is `nil`.
290
+ # @return [nil] if {Metasploit::Model::Module::Path#real_path parent_path.real_path} is `nil`.
291
+ # @return [nil] if {#module_type_directory} is `nil`.
292
+ # @return [nil] if {#reference_name} is `nil`.
293
+ def derived_real_path
294
+ derived_real_path = nil
295
+
296
+ if parent_path and parent_path.real_path and module_type_directory and reference_path
297
+ derived_real_path = File.join(
298
+ parent_path.real_path,
299
+ module_type_directory,
300
+ reference_path
301
+ )
302
+ end
329
303
 
330
- # Derives {#real_path_sha1_hex_digest} by running the contents of {#real_path} through Digest::SHA1.hexdigest.
331
- #
332
- # @return [String] 40 character SHA1 hex digest if {#real_path} can be read.
333
- # @return [nil] if {#real_path} cannot be read.
334
- def derived_real_path_sha1_hex_digest
335
- begin
336
- sha1 = Digest::SHA1.file(real_path.to_s)
337
- rescue Errno::ENOENT
338
- hex_digest = nil
339
- else
340
- hex_digest = sha1.hexdigest
341
- end
304
+ derived_real_path
305
+ end
342
306
 
343
- hex_digest
344
- end
307
+ # Derives {#real_path_modified_at} by getting the modification time of the file on-disk.
308
+ #
309
+ # @return [Time] modification time of {#real_path} if {#real_path} exists on disk and modification time can be
310
+ # queried by user.
311
+ # @return [nil] if {#real_path} does not exist or user cannot query the file's modification time.
312
+ def derived_real_path_modified_at
313
+ real_path_string = real_path.to_s
314
+
315
+ begin
316
+ mtime = File.mtime(real_path_string)
317
+ rescue Errno::ENOENT
318
+ nil
319
+ else
320
+ mtime.utc
321
+ end
322
+ end
345
323
 
346
- # Derives {#reference_name} from {#real_path} and {Metasploit::Model::Module::Path#real_path}.
347
- #
348
- # @return [String]
349
- # @return [nil] if {#real_path} is `nil`.
350
- def derived_reference_name
351
- derived = nil
352
- reference_name_file_names = relative_file_names.drop(1)
353
- reference_name_base_name = reference_name_file_names[-1]
354
-
355
- if reference_name_base_name
356
- if File.extname(reference_name_base_name) == EXTENSION
357
- reference_name_file_names[-1] = File.basename(reference_name_base_name, EXTENSION)
358
- derived = reference_name_file_names.join(REFERENCE_NAME_SEPARATOR)
359
- end
360
- end
324
+ # Derives {#real_path_sha1_hex_digest} by running the contents of {#real_path} through Digest::SHA1.hexdigest.
325
+ #
326
+ # @return [String] 40 character SHA1 hex digest if {#real_path} can be read.
327
+ # @return [nil] if {#real_path} cannot be read.
328
+ def derived_real_path_sha1_hex_digest
329
+ begin
330
+ sha1 = Digest::SHA1.file(real_path.to_s)
331
+ rescue Errno::ENOENT
332
+ hex_digest = nil
333
+ else
334
+ hex_digest = sha1.hexdigest
335
+ end
361
336
 
362
- derived
363
- end
337
+ hex_digest
338
+ end
364
339
 
365
- # Returns whether {#handler_type} is required or must be `nil`.
366
- #
367
- # @return (see handled?)
368
- # @see handled?
369
- def handled?
370
- self.class.handled?(
371
- :module_type => module_type,
372
- :payload_type => payload_type
373
- )
374
- end
340
+ # Derives {#reference_name} from {#real_path} and {Metasploit::Model::Module::Path#real_path}.
341
+ #
342
+ # @return [String]
343
+ # @return [nil] if {#real_path} is `nil`.
344
+ def derived_reference_name
345
+ derived = nil
346
+ reference_name_file_names = relative_file_names.drop(1)
347
+ reference_name_base_name = reference_name_file_names[-1]
348
+
349
+ if reference_name_base_name
350
+ if File.extname(reference_name_base_name) == EXTENSION
351
+ reference_name_file_names[-1] = File.basename(reference_name_base_name, EXTENSION)
352
+ derived = reference_name_file_names.join(REFERENCE_NAME_SEPARATOR)
353
+ end
354
+ end
375
355
 
376
- # The directory for {#module_type} under {Metasploit::Model::Module::Path parent_path.real_path}.
377
- #
378
- # @return [String]
379
- # @see Metasploit::Model::Module::Ancestor::DIRECTORY_BY_MODULE_TYPE
380
- def module_type_directory
381
- Metasploit::Model::Module::Ancestor::DIRECTORY_BY_MODULE_TYPE[module_type]
382
- end
356
+ derived
357
+ end
383
358
 
384
- # Return whether this forms part of a payload (either a single, stage, or stager).
385
- #
386
- # @return [true] if {#module_type} == 'payload'
387
- # @return [false] if {#module_type} != 'payload'
388
- def payload?
389
- if module_type == Metasploit::Model::Module::Type::PAYLOAD
390
- true
391
- else
392
- false
393
- end
394
- end
359
+ # Returns whether {#handler_type} is required or must be `nil`.
360
+ #
361
+ # @return (see handled?)
362
+ # @see handled?
363
+ def handled?
364
+ self.class.handled?(
365
+ :module_type => module_type,
366
+ :payload_type => payload_type
367
+ )
368
+ end
395
369
 
396
- # The name used to forming the {Metasploit::Model::Module::Class#reference_name} for payloads.
397
- #
398
- # @return [String] The {#reference_name} without the {#payload_type_directory} if {#payload_type} is `'single'`
399
- # or `'stage'`
400
- # @return [String] The {#handler_type} if {#payload_type} is `'stager'`
401
- # @return [nil] if {#module_type} is not `'payload'`
402
- def payload_name
403
- payload_name = nil
404
-
405
- if module_type == Metasploit::Model::Module::Type::PAYLOAD
406
- case payload_type
407
- when 'single', 'stage'
408
- if reference_name && payload_type_directory
409
- escaped_payload_type_directory = Regexp.escape(payload_type_directory)
410
- payload_type_directory_regexp = /^#{escaped_payload_type_directory}\//
411
- payload_name = reference_name.gsub(payload_type_directory_regexp, '')
412
- end
413
- when 'stager'
414
- payload_name = handler_type
415
- end
416
- end
370
+ # The directory for {#module_type} under {Metasploit::Model::Module::Path parent_path.real_path}.
371
+ #
372
+ # @return [String]
373
+ # @see Metasploit::Model::Module::Ancestor::DIRECTORY_BY_MODULE_TYPE
374
+ def module_type_directory
375
+ Metasploit::Model::Module::Ancestor::DIRECTORY_BY_MODULE_TYPE[module_type]
376
+ end
417
377
 
418
- payload_name
419
- end
378
+ # Return whether this forms part of a payload (either a single, stage, or stager).
379
+ #
380
+ # @return [true] if {#module_type} == 'payload'
381
+ # @return [false] if {#module_type} != 'payload'
382
+ def payload?
383
+ if module_type == Metasploit::Model::Module::Type::PAYLOAD
384
+ true
385
+ else
386
+ false
387
+ end
388
+ end
420
389
 
421
- # The directory for {#payload_type} under {#module_type_directory} in {#real_path}.
422
- #
423
- # @return [String] first directory in reference_name
424
- # @return [nil] if {#payload?} is `false`.
425
- # @return [nil] if {#reference_name} is `nil`.
426
- def payload_type_directory
427
- directory = nil
428
-
429
- if payload? and reference_name
430
- head, _tail = reference_name.split(REFERENCE_NAME_SEPARATOR, 2)
431
- directory = head
390
+ # The name used to forming the {Metasploit::Model::Module::Class#reference_name} for payloads.
391
+ #
392
+ # @return [String] The {#reference_name} without the {#payload_type_directory} if {#payload_type} is `'single'`
393
+ # or `'stage'`
394
+ # @return [String] The {#handler_type} if {#payload_type} is `'stager'`
395
+ # @return [nil] if {#module_type} is not `'payload'`
396
+ def payload_name
397
+ payload_name = nil
398
+
399
+ if module_type == Metasploit::Model::Module::Type::PAYLOAD
400
+ case payload_type
401
+ when 'single', 'stage'
402
+ if reference_name && payload_type_directory
403
+ escaped_payload_type_directory = Regexp.escape(payload_type_directory)
404
+ payload_type_directory_regexp = /^#{escaped_payload_type_directory}\//
405
+ payload_name = reference_name.gsub(payload_type_directory_regexp, '')
432
406
  end
407
+ when 'stager'
408
+ payload_name = handler_type
409
+ end
410
+ end
433
411
 
434
- directory
435
- end
412
+ payload_name
413
+ end
436
414
 
437
- # File names on {#relative_pathname}.
438
- #
439
- # @return [Enumerator<String>]
440
- def relative_file_names
441
- relative_pathname = self.relative_pathname
442
-
443
- if relative_pathname
444
- relative_pathname.each_filename
445
- else
446
- # empty enumerator
447
- Enumerator.new { }
448
- end
449
- end
415
+ # The directory for {#payload_type} under {#module_type_directory} in {#real_path}.
416
+ #
417
+ # @return [String] first directory in reference_name
418
+ # @return [nil] if {#payload?} is `false`.
419
+ # @return [nil] if {#reference_name} is `nil`.
420
+ def payload_type_directory
421
+ directory = nil
422
+
423
+ if payload? and reference_name
424
+ head, _tail = reference_name.split(REFERENCE_NAME_SEPARATOR, 2)
425
+ directory = head
426
+ end
450
427
 
451
- # {#real_path} relative to {Metasploit::Model::Module::Path#real_path}
452
- #
453
- # @return [Pathname]
454
- def relative_pathname
455
- relative_pathname = nil
456
- real_pathname = self.real_pathname
428
+ directory
429
+ end
457
430
 
458
- if real_pathname
459
- parent_path = self.parent_path
431
+ # File names on {#relative_pathname}.
432
+ #
433
+ # @return [Enumerator<String>]
434
+ def relative_file_names
435
+ relative_pathname = self.relative_pathname
436
+
437
+ if relative_pathname
438
+ relative_pathname.each_filename
439
+ else
440
+ # empty enumerator
441
+ Enumerator.new { }
442
+ end
443
+ end
460
444
 
461
- if parent_path
462
- parent_path_real_pathname = parent_path.real_pathname
445
+ # {#real_path} relative to {Metasploit::Model::Module::Path#real_path}
446
+ #
447
+ # @return [Pathname]
448
+ def relative_pathname
449
+ relative_pathname = nil
450
+ real_pathname = self.real_pathname
463
451
 
464
- if parent_path_real_pathname
465
- relative_pathname = real_pathname.relative_path_from parent_path_real_pathname
466
- end
467
- end
468
- end
452
+ if real_pathname
453
+ parent_path = self.parent_path
469
454
 
470
- relative_pathname
455
+ if parent_path
456
+ parent_path_real_pathname = parent_path.real_pathname
457
+
458
+ if parent_path_real_pathname
459
+ relative_pathname = real_pathname.relative_path_from parent_path_real_pathname
471
460
  end
461
+ end
462
+ end
472
463
 
473
- # The path relative to the {#module_type_directory} under the {Metasploit::Model::Module::Path
474
- # parent_path.real_path}, including the file {EXTENSION extension}.
475
- #
476
- # @return [String] {#reference_name} + {EXTENSION}
477
- # @return [nil] if {#reference_name} is `nil`.
478
- def reference_path
479
- path = nil
464
+ relative_pathname
465
+ end
480
466
 
481
- if reference_name
482
- path = "#{reference_name}#{EXTENSION}"
483
- end
467
+ # The path relative to the {#module_type_directory} under the {Metasploit::Model::Module::Path
468
+ # parent_path.real_path}, including the file {EXTENSION extension}.
469
+ #
470
+ # @return [String] {#reference_name} + {EXTENSION}
471
+ # @return [nil] if {#reference_name} is `nil`.
472
+ def reference_path
473
+ path = nil
484
474
 
485
- path
486
- end
475
+ if reference_name
476
+ path = "#{reference_name}#{EXTENSION}"
477
+ end
478
+
479
+ path
480
+ end
487
481
 
488
- private
482
+ private
489
483
 
490
- # Whether this ancestor is being validated for loading.
491
- #
492
- # @return [true] if `#validation_context` is `:loading`
493
- # @return [false] otherwise
494
- def loading_context?
495
- validation_context == :loading
496
- end
497
- end
498
- end
484
+ # Whether this ancestor is being validated for loading.
485
+ #
486
+ # @return [true] if `#validation_context` is `:loading`
487
+ # @return [false] otherwise
488
+ def loading_context?
489
+ validation_context == :loading
499
490
  end
500
- end
491
+ end