-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor smb_lookupsid module to use RubySMB
- Loading branch information
1 parent
1b9f242
commit 8e616e0
Showing
7 changed files
with
292 additions
and
326 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
### | ||
# | ||
# This mixin provides a method to connect to an IPC share on the remote SMB server. | ||
# | ||
# -*- coding: binary -*- | ||
|
||
module Msf | ||
|
||
module Exploit::Remote::MsIpc | ||
|
||
include Msf::Exploit::Remote::SMB::Client::Authenticated | ||
include Msf::Auxiliary::Report | ||
|
||
class MsIpcError < StandardError; end | ||
class MsIpcConnectionError < MsIpcError; end | ||
class MsIpcAuthenticationError < MsIpcError; end | ||
|
||
module_function | ||
|
||
def connect_ipc | ||
begin | ||
if session | ||
self.simple = session.simple_client | ||
ipc_tree = simple.client.tree_connect("\\\\#{simple.peerhost}\\IPC$") | ||
else | ||
connect | ||
# smb_login does a tree_connect to the IPC share already. | ||
ipc_tree = smb_login | ||
end | ||
rescue Rex::ConnectionError => e | ||
raise MsIpcConnectionError, e.message | ||
rescue Rex::Proto::SMB::Exceptions::Error, RubySMB::Error::RubySMBError => e | ||
raise MsIpcAuthenticationError, "Unable to authenticate ([#{e.class}] #{e})." | ||
end | ||
|
||
report_service( | ||
host: simple.peerhost, | ||
port: simple.peerport, | ||
host_name: simple.client.default_name, | ||
proto: 'tcp', | ||
name: 'smb', | ||
info: "Module: #{fullname}, last negotiated version: SMBv#{simple.client.negotiated_smb_version} (dialect = #{simple.client.dialect})" | ||
) | ||
|
||
ipc_tree | ||
end | ||
|
||
def disconnect_ipc(ipc_tree) | ||
ipc_tree.disconnect! if ipc_tree | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
### | ||
# | ||
# This mixin provides methods to open, and close policy handles, and to query policy info on the remote SMB server. | ||
# | ||
# -*- coding: binary -*- | ||
|
||
module Msf | ||
|
||
module Exploit::Remote::MsLsarpc | ||
|
||
include Msf::Exploit::Remote::MsIpc | ||
|
||
class MsLsarpcError < StandardError; end | ||
class MsLsarpcConnectionError < MsLsarpcError; end | ||
class MsLsarpcAuthenticationError < MsLsarpcError; end | ||
class MsLsarpcUnexpectedReplyError < MsLsarpcError; end | ||
|
||
LSA_UUID = '12345778-1234-abcd-ef00-0123456789ab'.freeze | ||
LSA_VERS = '0.0'.freeze | ||
LSARPC_ENDPOINT = RubySMB::Dcerpc::Lsarpc.freeze | ||
|
||
# The currently connected LSARPC pipe | ||
attr_reader :lsarpc_pipe | ||
|
||
def map_security_principal_to_string(security_principal) | ||
case security_principal | ||
when 1 | ||
'User' | ||
when 2 | ||
'Group' | ||
when 3 | ||
'Domain' | ||
when 4 | ||
'Alias' | ||
when 5 | ||
'Well-Known Group' | ||
when 6 | ||
'Deleted Account' | ||
when 7 | ||
'Invalid' | ||
when 8 | ||
'Unknown' | ||
when '9' | ||
'Computer' | ||
when 10 | ||
'Label' | ||
else | ||
'Unknown - Not a valid Security Principal' | ||
end | ||
end | ||
|
||
def open_policy_2(impersonation_level, security_context_tracking_mode, access_mask) | ||
object_attributes = LSARPC_ENDPOINT::LsaprObjectAttributes.new( | ||
{ | ||
len: 24, | ||
root_directory: nil, | ||
object_name: nil, | ||
attributes: 0, | ||
security_descriptor: nil, | ||
security_quality_of_service: { | ||
len: 12, | ||
impersonation_level: impersonation_level, | ||
security_context_tracking_mode: security_context_tracking_mode, | ||
effective_only: 0 | ||
} | ||
} | ||
) | ||
|
||
self.lsarpc_pipe.lsar_open_policy_2( | ||
system_name: simple.peerhost, | ||
object_attributes: object_attributes, | ||
access_mask: access_mask | ||
) | ||
end | ||
|
||
def query_information_policy(policy_handle, information_class) | ||
self.lsarpc_pipe.lsar_query_information_policy( | ||
policy_handle: policy_handle, | ||
information_class: information_class | ||
) | ||
end | ||
|
||
def lookup_sids(policy_handle, sids_buffer, lookup_level) | ||
self.lsarpc_pipe.lsar_lookup_sids( | ||
policy_handle: policy_handle, | ||
sid_enum_buffer: { | ||
num_entries: sids_buffer.count, | ||
sid_info: sids_buffer | ||
}, | ||
lookup_level: lookup_level | ||
) | ||
end | ||
|
||
def close_policy(policy_handle) | ||
self.lsarpc_pipe.lsar_close_handle( | ||
policy_handle: policy_handle | ||
) if (self.lsarpc_pipe && policy_handle) | ||
end | ||
|
||
def disconnect_lsarpc | ||
begin | ||
self.lsarpc_pipe.close if self.lsarpc_pipe&.is_connected? | ||
rescue RubySMB::Error::UnexpectedStatusCode => _e | ||
# noop - Encountered when trying to close LSARPC pipe vs. Samba | ||
end | ||
end | ||
|
||
module_function | ||
|
||
def connect_lsarpc(tree) | ||
begin | ||
vprint_status('Connecting to LSARPC') | ||
self.lsarpc_pipe = tree.open_file(filename: 'LSARPC', write: true, read: true) | ||
|
||
raise MsLsarpcConnectionError.new('Could not open LSARPC pipe on remote SMB server.') unless lsarpc_pipe | ||
|
||
self.lsarpc_pipe.extend(LSARPC_ENDPOINT) unless lsarpc_pipe.is_a?(LSARPC_ENDPOINT) | ||
|
||
vprint_status('Binding to \\LSARPC...') | ||
self.lsarpc_pipe.bind(endpoint: LSARPC_ENDPOINT) | ||
vprint_good('Bound to \\LSARPC') | ||
|
||
self.lsarpc_pipe | ||
rescue RubySMB::Dcerpc::Error::FaultError => e | ||
elog(e.message, error: e) | ||
raise MsLsarpcUnexpectedReplyError, "Connection failed (DCERPC fault: #{e.status_name})" | ||
end | ||
end | ||
|
||
protected | ||
|
||
attr_writer :lsarpc_pipe | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.