# Get some info about the current user we're running under
raiseModuleException("Binwalk extraction uses many third party utilities, which may not be secure.\nIf you wish to have extraction utilities executed as the current user, use --run-as=<user name>.")
user_info=pwd.getpwuid(os.getuid())
self.runas_uid=os.getuid()
# Don't run as root, unless explicitly instructed to
self.runas_gid=os.getgid()
ifuser_info.pw_uid==0:
raiseModuleException("Binwalk extraction uses many third party utilities, which may not be secure.\nIf you wish to have extraction utilities executed as the current user, use --run-as=%s (binwalk itself must be run as root)."%user_info.pw_name)
# Run external applications as the current user
self.runas_uid=user_info.pw_uid
self.runas_gid=user_info.pw_gid
else:
else:
# Run external applications as the specified user
user_info=pwd.getpwnam(self.runas_user)
user_info=pwd.getpwnam(self.runas_user)
self.runas_uid=user_info.pw_uid
self.runas_uid=user_info.pw_uid
self.runas_gid=user_info.pw_gid
self.runas_gid=user_info.pw_gid
# Make sure we'll have permissions to switch to the different user
ifself.runas_uid!=os.getuid()andos.getuid()!=0:
raiseModuleException("In order to execute third party applications as %s, binwalk must be run with root privileges."%self.runas_user)
# Holds a list of extraction rules loaded either from a file or when
# Holds a list of extraction rules loaded either from a file or when
# manually specified.
# manually specified.
self.extract_rules=[]
self.extract_rules=[]
...
@@ -858,7 +868,7 @@ class Extractor(Module):
...
@@ -858,7 +868,7 @@ class Extractor(Module):
fdout.close()
fdout.close()
fdin.close()
fdin.close()
# Make sure runasileged user can access this file
# Make sure run-as user can access this file
os.chown(fname,self.runas_uid,self.runas_gid)
os.chown(fname,self.runas_uid,self.runas_gid)
exceptKeyboardInterruptase:
exceptKeyboardInterruptase:
raisee
raisee
...
@@ -965,6 +975,6 @@ class Extractor(Module):
...
@@ -965,6 +975,6 @@ class Extractor(Module):
defsymlink_repair(self,symlink):
defsymlink_repair(self,symlink):
linktarget=os.path.realpath(symlink)
linktarget=os.path.realpath(symlink)
ifnotlinktarget.startswith(self.directory):
ifnotlinktarget.startswith(self.directory):
binwalk.core.common.warning("Symlink points outside of the extraction directory: %s -> %s; for security, changing link target to %s."%(symlink,linktarget,os.devnull))
os.remove(symlink)
os.remove(symlink)
os.symlink(os.devnull,symlink)
os.symlink(os.devnull,symlink)
binwalk.core.common.warning("Symlink points outside of the extraction directory: %s -> %s"%(symlink,linktarget))