rex-exploitation 0.1.42 → 0.1.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e6c3c655a61711289a496a809789adaedf0f252e96a3a60f30840627b3bc03cb
4
- data.tar.gz: 91cdd049b97c784f989516329ba98c12252330dd2a4abce52cec0a5ae3cd9db4
3
+ metadata.gz: d57ee0a86e51281389fe57ae6bdef8d5a05973ddf9159ab084d61a494e9f525f
4
+ data.tar.gz: e77944b1b5b69f1fe584923a31005957af5f8e773f4c9335616fd288f1825524
5
5
  SHA512:
6
- metadata.gz: e14c26a91cf648d4cabfea522a6cf9b871f30bb575bc482e8a06e153432dcd39c5153da72b92dd4b396516ca60e58c3006a988ab0e8ac1de98421d430de8f4c0
7
- data.tar.gz: 2168efd5ad5afe44af7eb4246f33d781203dedc9251362ddd610e13aa19bce0059f66272049084f7bcb3f302739cff6a0b7ccb84c752f31a4b86d75b1d215814
6
+ metadata.gz: c479681a255e5faab29066c3fe06ebabc3d3b868572a0e99aa9c5400b50c691ef451a1c1d6cf3de5d188fcaa03b2b67276b015d9bbb4b8381f5209083f0ff66b
7
+ data.tar.gz: 798d4dd5d42818fa41d86d6a835716f7631516d8dd56605eae15125e6b50a9de7aac35a085d02a9f8d4a9443f2878a5de889c3ed266b81fea25c7b07caa30bc5
@@ -0,0 +1,141 @@
1
+ # -*- coding: binary -*-
2
+
3
+ module Rex
4
+ module Exploitation
5
+ #
6
+ # VBScript obfuscation library
7
+ #
8
+ class VBSObfuscate
9
+ # The VBScript code that this obfuscator will transform
10
+ attr_accessor :code
11
+
12
+ # Saves +code+ for later obfuscation with #obfuscate!
13
+ #
14
+ # @param code [#to_s] the code to obfuscate
15
+ # @param opts [Hash] an options hash
16
+ def initialize(code = nil, _opts = {})
17
+ self.code = code
18
+ end
19
+
20
+ # @return [String] the (possibly obfuscated) code
21
+ def to_s
22
+ @code
23
+ end
24
+
25
+ # Append +str+ to the (possibly obfuscated) code
26
+ def <<(str)
27
+ @code << str
28
+ end
29
+
30
+ # Obfuscate VBScript code.
31
+ #
32
+ # @option iterations [Integer] number of times to run the obfuscator on this code (1)
33
+ # @option normalize_whitespace [Boolean] normalize line endings and strip leading/trailing whitespace from each line (true)
34
+ # @option dynamic_execution [Boolean] dynamically execute obfuscated code with Execute (true)
35
+ #
36
+ # @return [self]
37
+ def obfuscate!(iterations: 1, normalize_whitespace: true, dynamic_execution: true)
38
+ raise(ArgumentError, 'code must be present') if @code.nil?
39
+ raise(ArgumentError, 'iterations must be a positive integer') unless iterations.integer? && iterations.positive?
40
+
41
+ obfuscated = @code.dup
42
+
43
+ iterations.times do
44
+ # Normalize line endings and strip leading/trailing whitespace
45
+ if normalize_whitespace
46
+ obfuscated.gsub!(/\r\n/, "\n")
47
+ obfuscated = obfuscated.lines.map(&:strip).reject(&:empty?).join("\n")
48
+ end
49
+
50
+ # Convert all VBScript to a string to be dynamically executed with Execute()
51
+ if dynamic_execution
52
+ obfuscated = 'Execute ' + vbscript_string_for_execute(obfuscated)
53
+ end
54
+
55
+ # Obfuscate strings
56
+ obfuscated = chunk_vbscript_strings(obfuscated)
57
+ obfuscated.gsub!(/"((?:[^"]|"")*)"/) do
58
+ raw = ::Regexp.last_match(1).gsub('""', '"')
59
+ raw.chars.map { |c| "chr(#{generate_number_expression(c.ord)})" }.join('&')
60
+ end
61
+
62
+ # Obfuscate integers
63
+ obfuscated.gsub!(/\b\d+\b/) do |num|
64
+ generate_number_expression(num.to_i)
65
+ end
66
+ end
67
+
68
+ @code = obfuscated
69
+
70
+ self
71
+ end
72
+
73
+ private
74
+
75
+ # Converts all VBScript in +vbscript+ to a string for dynamic execution
76
+ # with Execute().
77
+ #
78
+ # @param vbscript [String] VBScript code
79
+ #
80
+ # @return [String] obfuscated VBScript code for use with Execute()
81
+ def vbscript_string_for_execute(vbscript)
82
+ lines = vbscript.lines.map(&:chomp).map do |line|
83
+ escaped_line = line.gsub('"', '""')
84
+ "\"#{escaped_line}\""
85
+ end
86
+ lines.join('&vbCrLf&')
87
+ end
88
+
89
+ # Returns a random math expression evaluating to input +int+
90
+ #
91
+ # @param int [Integer] input integer
92
+ #
93
+ # @return [String] math expression evaluating to input +int+
94
+ def generate_number_expression(int)
95
+ case rand(4)
96
+ when 0 # Sum
97
+ a = rand(0..int)
98
+ b = int - a
99
+ "(#{a}+#{b})"
100
+ when 1 # Difference
101
+ r1 = int + rand(1..10)
102
+ r2 = r1 - int
103
+ "(#{r1}-#{r2})"
104
+ when 2 # Product (only if divisible)
105
+ divisors = (1..int).select { |d| (int % d).zero? }
106
+ if divisors.size > 1
107
+ d = divisors.sample
108
+ "(#{d}*#{int / d})"
109
+ else
110
+ "(#{int}+0)"
111
+ end
112
+ when 3 # Quotient
113
+ r2 = rand(1..10)
114
+ r1 = int * r2
115
+ "(#{r1}/#{r2})"
116
+ end
117
+ end
118
+
119
+ # Return VBScript code with all strings split into chunks and concatenated
120
+ #
121
+ # @param vbscript [String] VBScript code
122
+ #
123
+ # @return [String] VBScript code with chunked strings
124
+ def chunk_vbscript_strings(vbscript)
125
+ vbscript.gsub(/"([^"]+)"/) do
126
+ original = Regexp.last_match(1)
127
+ chunks = []
128
+
129
+ i = 0
130
+ while i < original.length
131
+ chunk_size = rand(1..5)
132
+ chunks << "\"#{original[i, chunk_size]}\""
133
+ i += chunk_size
134
+ end
135
+
136
+ chunks.join('&')
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
@@ -1,5 +1,5 @@
1
1
  module Rex
2
2
  module Exploitation
3
- VERSION = "0.1.42"
3
+ VERSION = "0.1.43"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rex-exploitation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.42
4
+ version: 0.1.43
5
5
  platform: ruby
6
6
  authors:
7
7
  - Metasploit Hackers
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-05-02 00:00:00.000000000 Z
11
+ date: 2025-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -229,6 +229,7 @@ files:
229
229
  - lib/rex/exploitation/opcodedb.rb
230
230
  - lib/rex/exploitation/ropdb.rb
231
231
  - lib/rex/exploitation/seh.rb
232
+ - lib/rex/exploitation/vbsobfuscate.rb
232
233
  - lib/rex/exploitation/version.rb
233
234
  - rex-exploitation.gemspec
234
235
  homepage: https://github.com/rapid7/rex-exploitation