-
Notifications
You must be signed in to change notification settings - Fork 786
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
99% done with RCR, not yet been tested.
- Loading branch information
Showing
18 changed files
with
371 additions
and
426 deletions.
There are no files selected for viewing
Empty file.
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,83 @@ | ||
import wmi | ||
import win32com | ||
|
||
__author__ = 'maor.rayzin' | ||
|
||
|
||
class MongoUtils: | ||
|
||
def __init__(self): | ||
# Static class | ||
pass | ||
|
||
@staticmethod | ||
def fix_obj_for_mongo(o): | ||
if type(o) == dict: | ||
return dict([(k, MongoUtils.fix_obj_for_mongo(v)) for k, v in o.iteritems()]) | ||
|
||
elif type(o) in (list, tuple): | ||
return [MongoUtils.fix_obj_for_mongo(i) for i in o] | ||
|
||
elif type(o) in (int, float, bool): | ||
return o | ||
|
||
elif type(o) in (str, unicode): | ||
# mongo dosn't like unprintable chars, so we use repr :/ | ||
return repr(o) | ||
|
||
elif hasattr(o, "__class__") and o.__class__ == wmi._wmi_object: | ||
return MongoUtils.fix_wmi_obj_for_mongo(o) | ||
|
||
elif hasattr(o, "__class__") and o.__class__ == win32com.client.CDispatch: | ||
try: | ||
# objectSid property of ds_user is problematic and need thie special treatment. | ||
# ISWbemObjectEx interface. Class Uint8Array ? | ||
if str(o._oleobj_.GetTypeInfo().GetTypeAttr().iid) == "{269AD56A-8A67-4129-BC8C-0506DCFE9880}": | ||
return o.Value | ||
except: | ||
pass | ||
|
||
try: | ||
return o.GetObjectText_() | ||
except: | ||
pass | ||
|
||
return repr(o) | ||
|
||
else: | ||
return repr(o) | ||
|
||
@staticmethod | ||
def fix_wmi_obj_for_mongo(o): | ||
row = {} | ||
|
||
for prop in o.properties: | ||
try: | ||
value = getattr(o, prop) | ||
except wmi.x_wmi: | ||
# This happens in Win32_GroupUser when the user is a domain user. | ||
# For some reason, the wmi query for PartComponent fails. This table | ||
# is actually contains references to Win32_UserAccount and Win32_Group. | ||
# so instead of reading the content to the Win32_UserAccount, we store | ||
# only the id of the row in that table, and get all the other information | ||
# from that table while analyzing the data. | ||
value = o.properties[prop].value | ||
|
||
row[prop] = MongoUtils.fix_obj_for_mongo(value) | ||
|
||
for method_name in o.methods: | ||
if not method_name.startswith("GetOwner"): | ||
continue | ||
|
||
method = getattr(o, method_name) | ||
|
||
try: | ||
value = method() | ||
value = MongoUtils.fix_obj_for_mongo(value) | ||
row[method_name[3:]] = value | ||
|
||
except wmi.x_wmi: | ||
continue | ||
|
||
return row | ||
|
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,25 @@ | ||
import _winreg | ||
|
||
from common.utils.mongo_utils import MongoUtils | ||
|
||
__author__ = 'maor.rayzin' | ||
|
||
|
||
class RegUtils: | ||
|
||
def __init__(self): | ||
# Static class | ||
pass | ||
|
||
@staticmethod | ||
def get_reg_key(subkey_path, store=_winreg.HKEY_LOCAL_MACHINE): | ||
key = _winreg.ConnectRegistry(None, store) | ||
subkey = _winreg.OpenKey(key, subkey_path) | ||
|
||
d = dict([_winreg.EnumValue(subkey, i)[:2] for i in xrange(_winreg.QueryInfoKey(subkey)[0])]) | ||
d = MongoUtils.fix_obj_for_mongo(d) | ||
|
||
subkey.Close() | ||
key.Close() | ||
|
||
return d |
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,27 @@ | ||
import wmi | ||
|
||
from mongo_utils import MongoUtils | ||
|
||
__author__ = 'maor.rayzin' | ||
|
||
|
||
class WMIUtils: | ||
|
||
def __init__(self): | ||
# Static class | ||
pass | ||
|
||
@staticmethod | ||
def get_wmi_class(class_name, moniker="//./root/cimv2", properties=None): | ||
_wmi = wmi.WMI(moniker=moniker) | ||
|
||
try: | ||
if not properties: | ||
wmi_class = getattr(_wmi, class_name)() | ||
else: | ||
wmi_class = getattr(_wmi, class_name)(properties) | ||
|
||
except wmi.x_wmi: | ||
return | ||
|
||
return MongoUtils.fix_obj_for_mongo(wmi_class) |
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
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,32 @@ | ||
WMI_CLASSES = {"Win32_OperatingSystem", "Win32_ComputerSystem", "Win32_LoggedOnUser", "Win32_UserAccount", | ||
"Win32_UserProfile", "Win32_Group", "Win32_GroupUser", "Win32_Product", "Win32_Service", | ||
"Win32_OptionalFeature"} | ||
|
||
# These wmi queries are able to return data about all the users & machines in the domain. | ||
# For these queries to work, the monkey should be run on a domain machine and | ||
# | ||
# monkey should run as *** SYSTEM *** !!! | ||
# | ||
WMI_LDAP_CLASSES = {"ds_user": ("DS_sAMAccountName", "DS_userPrincipalName", | ||
"DS_sAMAccountType", "ADSIPath", "DS_userAccountControl", | ||
"DS_objectSid", "DS_objectClass", "DS_memberOf", | ||
"DS_primaryGroupID", "DS_pwdLastSet", "DS_badPasswordTime", | ||
"DS_badPwdCount", "DS_lastLogon", "DS_lastLogonTimestamp", | ||
"DS_lastLogoff", "DS_logonCount", "DS_accountExpires"), | ||
|
||
"ds_group": ("DS_whenChanged", "DS_whenCreated", "DS_sAMAccountName", | ||
"DS_sAMAccountType", "DS_objectSid", "DS_objectClass", | ||
"DS_name", "DS_memberOf", "DS_member", "DS_instanceType", | ||
"DS_cn", "DS_description", "DS_distinguishedName", "ADSIPath"), | ||
|
||
"ds_computer": ("DS_dNSHostName", "ADSIPath", "DS_accountExpires", | ||
"DS_adminDisplayName", "DS_badPasswordTime", | ||
"DS_badPwdCount", "DS_cn", "DS_distinguishedName", | ||
"DS_instanceType", "DS_lastLogoff", "DS_lastLogon", | ||
"DS_lastLogonTimestamp", "DS_logonCount", "DS_objectClass", | ||
"DS_objectSid", "DS_operatingSystem", "DS_operatingSystemVersion", | ||
"DS_primaryGroupID", "DS_pwdLastSet", "DS_sAMAccountName", | ||
"DS_sAMAccountType", "DS_servicePrincipalName", "DS_userAccountControl", | ||
"DS_whenChanged", "DS_whenCreated"), | ||
} | ||
|
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.