From 0592bd3395c6cac9141700194a157189b355df25 Mon Sep 17 00:00:00 2001 From: iLightThings Date: Fri, 9 Sep 2022 17:04:14 +0000 Subject: [PATCH 1/5] Added export shares to cmedb --- cme/cmedb.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/cme/cmedb.py b/cme/cmedb.py index 7469facd..059a25b0 100755 --- a/cme/cmedb.py +++ b/cme/cmedb.py @@ -84,6 +84,65 @@ class DatabaseNavigator(cmd.Cmd): export_file.write('{},{},{},{},{},{}\n'.format(hostid,ipaddress,hostname,domain,opsys,dc)) print('[+] hosts exported') + elif line[0].lower() == 'shares': + if len(line) < 3: + print("[-] invalid arguments, export shares ") + return + + if line[1].lower() == 'simple': + shares = self.db.get_shares() + with open(os.path.expanduser(line[2]), 'w') as export_file: + export_file.write('id,computerid,userid,name,remark,read,write\n') + #id|computerid|userid|name|remark|read|write + for share in shares: + shareid,hostid,userid,sharename,shareremark,read,write = share + export_file.write('{},{},{},{},{},{},{}\n'.format(shareid,hostid,userid,sharename,shareremark,read,write)) + print('[+] shares exported') + elif line[1].lower() == 'detailed': #Detailed view gets hostsname, and usernames, and true false statement + shares = self.db.get_shares() + #id|computerid|userid|name|remark|read|write + + users = self.db.get_users() + #id|domain|username|password|credtype|pillaged_from_computerid + + hosts = self.db.get_computers() + #id|ip|hostname|domain|os|dc|smbv1|signing + + with open(os.path.expanduser(line[2]), 'w') as export_file: + export_file.write('id,computerid,userid,name,remark,read,write\n') + for share in shares: + shareid,hostid,userid,sharename,shareremark,read,write = share + export_file.write('{},{},{},{},{},{},{}\n'.format(shareid,self.get_host(hostid),self.get_user(userid),sharename,shareremark,bool(read),bool(write))) + print('[+] shares exported') + + else: + print("[-] invalid arguments, export shares ") + return + + else: + print('[-] invalid argument, specify creds, hosts or shares') + + # Return username from ID + def get_user(self,req_id: int) -> str: + users = self.db.get_users() + #id|domain|username|password|credtype|pillaged_from_computerid + for u in users: + id,domain,username,password,credtype,pillaged_from_computerid = u + if req_id == id: + return '{}\\{}'.format(domain,username) + return "USER NOT FOUND" + + # Return hostname from ID + def get_host(self,req_id: int) -> str: + hosts = self.db.get_computers() + #id|ip|hostname|domain|os|dc|smbv1|signing + for h in hosts: + id,ip,hostname,domain,os,dc,smbv1,signing = h + if req_id == id: + return hostname + return "HOST NOT FOUND" + + else: print('[-] invalid argument, specify creds or hosts') From 715ab6fa59f5a300d548f37efaccbe0565e42ff9 Mon Sep 17 00:00:00 2001 From: iLightThings Date: Fri, 9 Sep 2022 17:11:50 +0000 Subject: [PATCH 2/5] Small fix --- cme/cmedb.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/cme/cmedb.py b/cme/cmedb.py index 059a25b0..34fffc1b 100755 --- a/cme/cmedb.py +++ b/cme/cmedb.py @@ -143,8 +143,6 @@ class DatabaseNavigator(cmd.Cmd): return "HOST NOT FOUND" - else: - print('[-] invalid argument, specify creds or hosts') def do_import(self, line): From 2099d850cd85a44d72d13460377e5d0801ea7367 Mon Sep 17 00:00:00 2001 From: iLightThings Date: Fri, 9 Sep 2022 17:31:00 +0000 Subject: [PATCH 3/5] refactor --- cme/cmedb.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/cme/cmedb.py b/cme/cmedb.py index 34fffc1b..5b6125af 100755 --- a/cme/cmedb.py +++ b/cme/cmedb.py @@ -98,16 +98,10 @@ class DatabaseNavigator(cmd.Cmd): shareid,hostid,userid,sharename,shareremark,read,write = share export_file.write('{},{},{},{},{},{},{}\n'.format(shareid,hostid,userid,sharename,shareremark,read,write)) print('[+] shares exported') + elif line[1].lower() == 'detailed': #Detailed view gets hostsname, and usernames, and true false statement shares = self.db.get_shares() #id|computerid|userid|name|remark|read|write - - users = self.db.get_users() - #id|domain|username|password|credtype|pillaged_from_computerid - - hosts = self.db.get_computers() - #id|ip|hostname|domain|os|dc|smbv1|signing - with open(os.path.expanduser(line[2]), 'w') as export_file: export_file.write('id,computerid,userid,name,remark,read,write\n') for share in shares: From 67a358b1de207506aac3dc466d61c7f0ba54e403 Mon Sep 17 00:00:00 2001 From: iLightThings Date: Mon, 12 Sep 2022 12:12:57 +0000 Subject: [PATCH 4/5] Using CSV module to write CSV file. --- cme/cmedb.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/cme/cmedb.py b/cme/cmedb.py index 5b6125af..bdc3c682 100755 --- a/cme/cmedb.py +++ b/cme/cmedb.py @@ -11,6 +11,7 @@ from terminaltables import AsciiTable import configparser from cme.loaders.protocol_loader import protocol_loader from requests import ConnectionError +import csv # The following disables the InsecureRequests warning and the 'Starting new HTTPS connection' log message from requests.packages.urllib3.exceptions import InsecureRequestWarning @@ -92,21 +93,25 @@ class DatabaseNavigator(cmd.Cmd): if line[1].lower() == 'simple': shares = self.db.get_shares() with open(os.path.expanduser(line[2]), 'w') as export_file: - export_file.write('id,computerid,userid,name,remark,read,write\n') + shareCSV = csv.writer(export_file, delimiter=";", quoting=csv.QUOTE_ALL, lineterminator='\n') + csv_header = ["id","computerid","userid","name","remark","read","write"] + shareCSV.writerow(csv_header) #id|computerid|userid|name|remark|read|write for share in shares: shareid,hostid,userid,sharename,shareremark,read,write = share - export_file.write('{},{},{},{},{},{},{}\n'.format(shareid,hostid,userid,sharename,shareremark,read,write)) + shareCSV.writerow([shareid,hostid,userid,sharename,shareremark,read,write]) print('[+] shares exported') elif line[1].lower() == 'detailed': #Detailed view gets hostsname, and usernames, and true false statement shares = self.db.get_shares() #id|computerid|userid|name|remark|read|write with open(os.path.expanduser(line[2]), 'w') as export_file: - export_file.write('id,computerid,userid,name,remark,read,write\n') + shareCSV = csv.writer(export_file, delimiter=";", quoting=csv.QUOTE_ALL, lineterminator='\n') + csv_header = ["id","computerid","userid","name","remark","read","write"] + shareCSV.writerow(csv_header) for share in shares: shareid,hostid,userid,sharename,shareremark,read,write = share - export_file.write('{},{},{},{},{},{},{}\n'.format(shareid,self.get_host(hostid),self.get_user(userid),sharename,shareremark,bool(read),bool(write))) + shareCSV.writerow([shareid,self.get_host(hostid),self.get_user(userid),sharename,shareremark,bool(read),bool(write)]) print('[+] shares exported') else: From 561d6b0a2224f94e8accddf392dd0a89ee355c13 Mon Sep 17 00:00:00 2001 From: iLightThings Date: Tue, 20 Sep 2022 08:24:14 -0400 Subject: [PATCH 5/5] Refactored using existing functions. --- cme/cmedb.py | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/cme/cmedb.py b/cme/cmedb.py index bdc3c682..76963c8b 100755 --- a/cme/cmedb.py +++ b/cme/cmedb.py @@ -111,7 +111,14 @@ class DatabaseNavigator(cmd.Cmd): shareCSV.writerow(csv_header) for share in shares: shareid,hostid,userid,sharename,shareremark,read,write = share - shareCSV.writerow([shareid,self.get_host(hostid),self.get_user(userid),sharename,shareremark,bool(read),bool(write)]) + + #Format is domain\user + prettyuser = f"{self.db.get_users(userid)[0][1]}\{self.db.get_users(userid)[0][2]}" + + #Format is hostname + prettyhost = f"{self.db.get_computers(hostid)[0][2]}" + + shareCSV.writerow([shareid,prettyhost,prettyuser,sharename,shareremark,bool(read),bool(write)]) print('[+] shares exported') else: @@ -121,28 +128,6 @@ class DatabaseNavigator(cmd.Cmd): else: print('[-] invalid argument, specify creds, hosts or shares') - # Return username from ID - def get_user(self,req_id: int) -> str: - users = self.db.get_users() - #id|domain|username|password|credtype|pillaged_from_computerid - for u in users: - id,domain,username,password,credtype,pillaged_from_computerid = u - if req_id == id: - return '{}\\{}'.format(domain,username) - return "USER NOT FOUND" - - # Return hostname from ID - def get_host(self,req_id: int) -> str: - hosts = self.db.get_computers() - #id|ip|hostname|domain|os|dc|smbv1|signing - for h in hosts: - id,ip,hostname,domain,os,dc,smbv1,signing = h - if req_id == id: - return hostname - return "HOST NOT FOUND" - - - def do_import(self, line): if not line: