Skip to content

Commit

Permalink
Improved log file for smbclient.py
Browse files Browse the repository at this point in the history
  • Loading branch information
RazzburyPi committed Dec 2, 2022
1 parent 33d7bd7 commit 324d451
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions impacket/examples/smbclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ def emptyline(self):

def precmd(self,line):
# switch to unicode
f = open(self.outputfile, 'a')
f.write('> ' + line + "\n")
f.close()
if self.outputfile is not None:
f = open(self.outputfile, 'a')
f.write('> ' + line + "\n")
f.close()
if PY2:
return line.decode('utf-8')
return line
Expand Down Expand Up @@ -327,11 +328,14 @@ def do_shares(self, line):
LOG.error("Not logged in")
return
resp = self.smb.listShares()
f = open(self.outputfile, 'a')
if self.outputfile is not None:
f = open(self.outputfile, 'a')
for i in range(len(resp)):
f.write(resp[i]['shi1_netname'][:-1] + '\n')
if self.outputfile:
f.write(resp[i]['shi1_netname'][:-1] + '\n')
print(resp[i]['shi1_netname'][:-1])
f.close()
if self.outputfile:
f.close()

def do_use(self,line):
if self.loggedIn is False:
Expand Down Expand Up @@ -376,9 +380,10 @@ def do_pwd(self,line):
if self.loggedIn is False:
LOG.error("Not logged in")
return
f = open(self.outputfile, 'a')
f.write(self.pwd)
f.close()
if self.outputfile is not None:
f = open(self.outputfile, 'a')
f.write(self.pwd)
f.close()
print(self.pwd)

def do_ls(self, wildcard, display = True):
Expand All @@ -395,18 +400,21 @@ def do_ls(self, wildcard, display = True):
self.completion = []
pwd = pwd.replace('/','\\')
pwd = ntpath.normpath(pwd)
of = open(self.outputfile, 'a')
if self.outputfile is not None:
of = open(self.outputfile, 'a')
for f in self.smb.listPath(self.share, pwd):
if display is True:
of.write("%crw-rw-rw- %10d %s %s" % (
'd' if f.is_directory() > 0 else '-', f.get_filesize(), time.ctime(float(f.get_mtime_epoch())),
f.get_longname()) + "\n")
if self.outputfile:
of.write("%crw-rw-rw- %10d %s %s" % (
'd' if f.is_directory() > 0 else '-', f.get_filesize(), time.ctime(float(f.get_mtime_epoch())),
f.get_longname()) + "\n")

print("%crw-rw-rw- %10d %s %s" % (
'd' if f.is_directory() > 0 else '-', f.get_filesize(), time.ctime(float(f.get_mtime_epoch())),
f.get_longname()))
self.completion.append((f.get_longname(), f.is_directory()))
of.close()
if self.outputfile:
of.close()


def do_rm(self, filename):
Expand Down Expand Up @@ -523,23 +531,27 @@ def do_cat(self, filename):
output = fh.getvalue()
encoding = chardet.detect(output)["encoding"]
error_msg = "[-] Output cannot be correctly decoded, are you sure the text is readable ?"
f = open(self.outputfile, 'a')
if self.outputfile is not None:
f = open(self.outputfile, 'a')
if encoding:
try:
f.write(output.decode(encoding) + '\n')
if self.outputfile:
f.write(output.decode(encoding) + '\n')
f.close()
print(output.decode(encoding))
f.close()
except:
f.write(error_msg + '\n')
if self.outputfile:
f.write(error_msg + '\n')
f.close()
print(error_msg)
f.close()
finally:
fh.close()
else:
f.write(error_msg + '\n')
if self.outpufile:
f.write(error_msg + '\n')
f.close()
print(error_msg)
fh.close()
f.close()

def do_close(self, line):
self.do_logoff(line)
Expand Down

0 comments on commit 324d451

Please sign in to comment.