Path: blob/master/modules/auxiliary/scanner/misc/ibm_mq_login.rb
28052 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Auxiliary67include Msf::Exploit::Remote::Tcp8include Msf::Auxiliary::Scanner9include Msf::Auxiliary::Report1011def initialize12super(13'Name' => 'IBM WebSphere MQ Login Check',14'Description' => 'This module can be used to bruteforce usernames that can be used to connect to a queue manager. The name of a valid server-connection channel without SSL configured is required, as well as a list of usernames to try.',15'Author' => 'Petros Koutroumpis',16'License' => MSF_LICENSE17)18register_options([19Opt::RPORT(1414),20OptInt.new('TIMEOUT', [true, "The socket connect timeout in seconds", 5]),21OptInt.new('CONCURRENCY', [true, "The number of usernames to check concurrently", 10]),22OptString.new('QUEUE_MANAGER', [true, "Queue Manager name to use", ""]),23OptString.new('CHANNEL', [true, "Channel to use", "SYSTEM.ADMIN.SVRCONN"]),24OptString.new('PASSWORD', [false, "Optional password to attempt with login"]),25OptPath.new('USERNAMES_FILE',26[ true, "The file that contains a list of usernames. UserIDs are case insensitive!"])27])28# deregister_options('THREADS')29end3031def run_host(ip)32@usernames = []33if datastore['CHANNEL'].length.to_i > 2034print_error("Channel name cannot be more that 20 characters.")35exit36end37if datastore['QUEUE_MANAGER'].length.to_i > 4838print_error("Queue Manager name cannot be more that 48 characters.")39exit40end41begin42username_list43rescue ::Rex::ConnectionError44rescue ::Exception => e45print_error("#{e} #{e.backtrace}")46end47print_line48if (@usernames.empty?)49print_status("#{ip}:#{rport} No valid users found.")50else51print_good("#{ip}:#{rport} Valid usernames found: #{@usernames}")52report_note(53:host => rhost,54:port => rport,55:type => 'mq.usernames'56)57print_line58end59end6061def first_packet(channel, qm_name)62init1 = "\x54\x53\x48\x20" + # StructId63"\x00\x00\x01\x0c" + # MQSegmLen64"\x01" + # ByteOrder65"\x01" + # SegmType66"\x31" + # CtlFlag167"\x00" + # CtlFlag268"\x00\x00\x00\x00\x00\x00\x00\x00" + # LUW Ident69"\x00\x00\x01\x11" + # Encoding70"\x04\xb8" + # CCSID71"\x00\x00" + # Reserved72"\x49\x44\x20\x20" + # StructId73"\x0d" + # FAPLevel74"\x26" + # CapFlag175"\x00" + # ECapFlag176"\x00" + # InierrFlg177"\x00\x00" + # ReserveD78"\x00\x00" + # MaxMsgBtch79"\x00\x00\x7f\xec" + # MaxTrSize80"\x06\x40\x00\x00" + # MaxMsgSize81"\x00\x00\x00\x00" + # SeqWrapVal82channel + # Channel Name83"\x51" + # CapFlag284"\x00" + # ECapFlag285"\x04\xb8" + # ccsid86qm_name + # Queue Manager Name87"\x00\x00\x00\x01" + # HBInterval88"\x00\x8a" + # EFLLength89"\x00" + # IniErrFlg290"\x00" + # Reserved191"\x00\xff" + # HdrCprsLst92"\x00\xff\xff\xff\xff\xff\xff\xff" + # MsgCprsLst193"\xff\xff\xff\xff\xff\xff\xff\xff" + # MsgCprsLst294"\x00\x00" + # Reserved295"\x00\x00\x00\x00" + # SSLKeyRst96"\x00\x00\x00\x0a" + # ConvBySkt97"\x08" + # CapFlag398"\x00" + # ECapFlag399"\x00\x00" + # Reserved3100"\x00\x00\x00\x00" + # ProcessId101"\x00\x00\x00\x00" + # ThreadId102"\x00\x00\x00\x1b" + # TraceId103"MQMM09000000" + # ProdId104"MQMID" + "\x20" * 43 + # MQM ID105"\x00\x01\x00\x00\xff\xff\xff\xff" + # Unknown1106"\xff\xff\xff\xff\xff\xff\xff\xff" + # Unknown2107"\xff\xff\xff\xff\xf1\x18\xa6\x93" + # Unknown3108"\x2b\x8a\x44\x3c\x67\x53\x73\x08" # Unknown4109end110111def second_packet(channel, qm_name)112init2 = "\x54\x53\x48\x4d" + # StructId113"\x00\x00\x00\xf4" + # MQSegmLen114"\x00\x00\x00\x01" + # Convers Id115"\x00\x00\x00\x00" + # Request Id116"\x02" + # ByteOrder117"\x01" + # SegmType118"\x31" + # CtlFlag1119"\x00" + # CtlFlag2120"\x00\x00\x00\x00\x00\x00\x00\x00" + # LUW Ident121"\x11\x01\x00\x00" + # Encoding122"\xb5\x01" + # CCSID123"\x00\x00" + # Reserved124"\x49\x44\x20\x20" + # StructId125"\x0c" + # FAPLevel126"\x26" + # CapFlag1127"\x00" + # ECapFlag1128"\x00" + # IniErrFlg1129"\x00\x00" + # Reserved130"\x00\x00" + # MaxMsgBtch131"\xec\x7f\x00\x00" + # MaxTrSize132"\x00\x00\x40\x00" + # MaxMsgSize133"\x00\x00\x00\x00" + # SeqWrapVal134channel + # Channel Name135"\x51" + # CapFlag2136"\x00" + # ECapFlag2137"\xb5\x01" + # ccsid138qm_name + # Queue Manager Name139"\x2c\x01\x00\x00" + # HBInterval140"\x8a\x00" + # EFLLength141"\x00" + # IniErrFlg2142"\x00" + # Reserved1143"\x00\xff" + # HdrCprsLst144"\x00\xff\xff\xff\xff\xff\xff" + # MsgCprsLst1145"\xff\xff\xff\xff\xff\xff\xff" + # MsgCprsLst2146"\xff\xff" + # MsgCprsLst3147"\x00\x00" + # Reserved2148"\x00\x00\x00\x00" + # SSLKeyRst149"\x0a\x00\x00\x00" + # ConvBySkt150"\x00" + # CapFlag3151"\x00" + # ECapFlag3152"\x00\x00" + # Reserved3153"\x00\x00\x00\x00" + # ProcessId154"\x00\x00\x00\x00" + # ThreadId155"\x1b\x00\x00\x00" + # TraceId156"MQMM09000000" + # ProdId157"MQMID" + "\x20" * 43 # MQM ID158end159160def send_userid(userid, uname)161if datastore['PASSWORD'].nil?162password = "\x00" * 12163else164password = datastore['PASSWORD']165if (password.length > 12)166print_warning("Passwords greater than 12 characters are unsupported. Truncating...")167password = password[0..12]168end169password = password + ("\x00" * (12 - password.length))170end171vprint_status("Using password: '#{password}' (Length: #{password.length})")172173send_userid = "\x54\x53\x48\x4d" + # StructId174"\x00\x00\x00\xa8" + # MQSegmLen175"\x00\x00\x00\x01" + # Convers ID176"\x00\x00\x00\x00" + # Request ID177"\x02" + # Byte Order178"\x08" + # SegmType179"\x30" + # CtlFlag1180"\x00" + # CtlFlag2181"\x00\x00\x00\x00\x00\x00\x00\x00" + # LUW Ident182"\x11\x01\x00\x00" + # Encoding183"\xb5\x01" + # CCSID184"\x00\x00" + # Reserved185"\x55\x49\x44\x20" + # StructId186userid + # UserId - Doesnt affect anything187password + # Password188uname + # Long UID - This matters!189"\x00" + # SID Len190"\x00" * 39 # Unknown191end192193def start_conn(qm_name)194start_conn = "\x54\x53\x48\x4d" + # StructId195"\x00\x00\x01\x38" + # MQSegmLen196"\x00\x00\x00\x01" + # Convers ID197"\x00\x00\x00\x00" + # Request ID198"\x02" + # Byte Order199"\x81" + # SegmType200"\x30" + # CtlFlag1201"\x00" + # CtlFlag2202"\x00\x00\x00\x00\x00\x00\x00\x00" + # LUW Ident203"\x11\x01\x00\x00" + # Encoding204"\xb5\x01" + # CCSID205"\x00\x00" + # Reserved206"\x00\x00\x01\x38" + # Reply Len207"\x00\x00\x00\x00" + # Compl Code208"\x00\x00\x00\x00" + # Reason Code209"\x00\x00\x00\x00" + # Object Hdl210qm_name + # Queue Manager Name211"\x4d\x51\x20\x45\x78\x70\x6c" + # Appl Name212"\x6f\x72\x65\x72\x20\x39\x2e" + # Appl Name213"\x30\x2e\x30\x20\x20\x20\x20" + # Appl Name214"\x20\x20\x20\x20\x20\x20\x20" + # Appl Name215"\x1c\x00\x00\x00" + # ApplType216"\x00" * 32 + # AccntTok217"\x03\x00\x00\x00" + # MQCONNX218"\x00\x00\x00\x00" + # Options219"\x46\x43\x4e\x4f" + # Struct ID220"\x02\x00\x00\x00" + # Version221"\x00\x00\x00\x00" + # Option222"\x4d\x51\x4a\x42\x30\x39\x30" + # msgid223"\x30\x30\x30\x30\x34" + # msgid224"MQM" + "\x20" * 45 + # MqmId225"\x00" * 68 # Unknown226end227228def username_list229username_data = get_usernames230while (username_data.length > 0)231t = []232r = []233begin2341.upto(datastore['CONCURRENCY']) do235this_username = username_data.shift236if this_username.nil?237next238end239240t << framework.threads.spawn("Module(#{self.refname})-#{rhost}:#{rport}", false, this_username) do |username|241connect242vprint_status "#{rhost}:#{rport} - Sending request for #{username}..."243channel = datastore['CHANNEL']244if channel.length > 20245print_error("Channel name must be less than 20 characters.")246next247end248channel += "\x20" * (20 - channel.length.to_i) # max channel name length is 20249qm_name = datastore['QUEUE_MANAGER']250if qm_name.length > 48251print_error("Queue Manager name must be less than 48 characters.")252next253end254qm_name += "\x20" * (48 - qm_name.length.to_i) # max queue manager name length is 48255if username.length > 12256print_error("Username must be less than 12 characters.")257next258end259uname = username + "\x20" * (64 - username.length.to_i)260userid = username + "\x20" * (12 - username.length.to_i) # this doesnt make a difference261timeout = datastore['TIMEOUT'].to_i262s = connect(false,263{264'RPORT' => rport,265'RHOST' => rhost,266})267s.put(first_packet(channel, qm_name))268first_response = s.get_once(-1, timeout)269if first_response[-4..-1] == "\x00\x00\x00\x02" # CHANNEL_WRONG_TYPE code270print_error("Channel needs to be MQI type!")271next272end273s.put(second_packet(channel, qm_name))274second_response = s.get_once(-1, timeout)275s.put(send_userid(userid, uname))276s.put(start_conn(qm_name))277data = s.get_once(-1, timeout)278if data[41..44] == "\x00\x00\x00\x00"279print_status("Found username: #{username}")280@usernames << username281end282disconnect283end284end285t.each { |x| x.join }286end287end288end289290def get_usernames291if (!@common)292File.open(datastore['USERNAMES_FILE'], "rb") do |fd|293data = fd.read(fd.stat.size)294@common = data.split(/\n/).compact.uniq295end296end297@common298end299300end301302303