2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2009-01-11 06:09:02 +00:00
|
|
|
module Msf
|
|
|
|
|
|
|
|
module Exploit::Remote::Dialup
|
|
|
|
|
|
|
|
def initialize(info = {})
|
|
|
|
super
|
|
|
|
|
|
|
|
register_options(
|
|
|
|
[
|
2009-01-11 17:30:26 +00:00
|
|
|
OptInt.new( 'BAUDRATE', [true, 'Baud Rate', 19200]),
|
|
|
|
OptEnum.new( 'DATABITS', [true, 'Data Bits (4 is Windows Only)', '8', ['4', '5', '6', '7', '8'], '8']),
|
|
|
|
OptString.new('DIALPREFIX', [true, 'Dial Prefix', 'ATDT *67, *70,']),
|
|
|
|
OptString.new('DIALSUFFIX', [false, 'Dial Suffix', nil]),
|
|
|
|
OptInt.new( 'DIALTIMEOUT', [true, 'Dial Timeout in seconds', 60]),
|
|
|
|
OptBool.new( 'DISPLAYMODEM', [true, 'Displays modem commands and responses on the console', false]),
|
|
|
|
OptEnum.new( 'FLOWCONTROL', [true, 'Flow Control', 'None', ['None', 'Hardware', 'Software', 'Both'], 'None']),
|
|
|
|
OptString.new('INITSTRING', [true, 'Initialization String', 'AT X6 S11=80']),
|
|
|
|
OptString.new('NUMBER', [true, 'Number to Dial (e.g. 1.800.950.9955, (202) 358-1234, 358.1234 etc.)', nil]),
|
|
|
|
OptEnum.new( 'PARITY', [true, 'Parity (Mark & Space are Windows Only)', 'None', ['None', 'Even', 'Odd', 'Mark', 'Space'], 'None']),
|
|
|
|
OptString.new('SERIALPORT', [true, 'Serial Port (e.g. 0 (COM1), 1 (COM2), /dev/ttyS0, etc.)', '/dev/ttyS0']),
|
|
|
|
OptEnum.new( 'STOPBITS', [true, 'Stop Bits', '1', ['1', '2'], '1']),
|
2009-01-11 06:09:02 +00:00
|
|
|
], self.class)
|
|
|
|
|
|
|
|
deregister_options('RHOST')
|
2010-09-26 21:02:00 +00:00
|
|
|
|
2009-01-11 17:30:26 +00:00
|
|
|
begin
|
|
|
|
require 'telephony'
|
|
|
|
@telephony_loaded = true
|
|
|
|
rescue ::Exception => e
|
|
|
|
@telephony_loaded = false
|
|
|
|
@telephony_error = e
|
2010-09-26 21:02:00 +00:00
|
|
|
end
|
2009-01-11 06:09:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Opens the modem connection
|
|
|
|
def connect_dialup(global = true, opts={})
|
2010-09-26 21:02:00 +00:00
|
|
|
|
2009-01-11 17:30:26 +00:00
|
|
|
if (not @telephony_loaded)
|
|
|
|
print_status("The serialport module is not available: #{telephony_error}")
|
|
|
|
raise RuntimeError, "Telephony not available"
|
|
|
|
end
|
2010-09-26 21:02:00 +00:00
|
|
|
|
2009-01-11 06:09:02 +00:00
|
|
|
serialport = datastore['SERIALPORT']
|
|
|
|
baud = datastore['BAUDRATE'].to_i
|
|
|
|
data_bits = datastore['DATABITS'].to_i
|
|
|
|
stop_bits = datastore['STOPBITS'].to_i
|
|
|
|
parity = case datastore['PARITY']
|
2009-03-07 18:18:38 +00:00
|
|
|
when 'Even' ; Telephony::Modem::EVEN
|
|
|
|
when 'Odd' ; Telephony::Modem::ODD
|
|
|
|
when 'Mark' ; Telephony::Modem::MARK
|
|
|
|
when 'Space'; Telephony::Modem::SPACE
|
2009-01-11 07:45:08 +00:00
|
|
|
else Telephony::Modem::NONE
|
2009-01-11 07:23:43 +00:00
|
|
|
end
|
|
|
|
flowcontrol = case datastore['FLOWCONTROL']
|
2009-03-07 18:18:38 +00:00
|
|
|
when 'Hardware' ; Telephony::Modem::HARD
|
|
|
|
when 'Software' ; Telephony::Modem::SOFT
|
|
|
|
when 'Both' ; Telephony::Modem::HARD | Telephony::Modem::SOFT
|
2009-01-11 07:45:08 +00:00
|
|
|
else Telephony::Modem::NONE
|
2009-01-11 06:09:02 +00:00
|
|
|
end
|
2010-09-26 21:02:00 +00:00
|
|
|
|
2009-01-11 06:09:02 +00:00
|
|
|
initstring = datastore['INITSTRING']
|
|
|
|
dialprefix = datastore['DIALPREFIX']
|
|
|
|
dialsuffix = datastore['DIALSUFFIX']
|
|
|
|
dialtimeout = datastore['DIALTIMEOUT'].to_i
|
2009-01-11 17:30:26 +00:00
|
|
|
number = datastore['NUMBER'].tr(' ', '')
|
2009-01-11 06:09:02 +00:00
|
|
|
|
|
|
|
modem = Telephony::Modem.new(serialport)
|
|
|
|
modem.params = {
|
|
|
|
'baud' => baud,
|
|
|
|
'data_bits' => data_bits,
|
|
|
|
'parity' => parity,
|
|
|
|
'stop_bits' => stop_bits
|
|
|
|
}
|
2009-01-11 07:23:43 +00:00
|
|
|
modem.flow_control = flowcontrol
|
2009-01-11 06:09:02 +00:00
|
|
|
modem.display = datastore['DISPLAYMODEM']
|
|
|
|
|
|
|
|
print_status("Initializing Modem")
|
|
|
|
result = modem.put_command('ATZ', 3)
|
|
|
|
if result != 'OK'
|
|
|
|
print_error("Error resetting modem")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
result = modem.put_command(initstring, 3)
|
|
|
|
if result != 'OK'
|
|
|
|
print_error("Error initializing modem")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
print_status("Dialing: #{number} (#{dialtimeout} sec. timeout)")
|
|
|
|
dialstring = dialprefix + ' ' + number
|
|
|
|
dialstring += (' ' + dialsuffix) if dialsuffix
|
|
|
|
|
|
|
|
time = Time.now
|
|
|
|
result = modem.put_command(dialstring, dialtimeout)
|
|
|
|
while result =~ /RINGING/i
|
|
|
|
result = modem.get_response(dialtimeout-(Time.now-time))
|
|
|
|
end
|
|
|
|
|
|
|
|
case result
|
2009-03-07 18:18:38 +00:00
|
|
|
when /CONNECT/i
|
2009-01-11 06:09:02 +00:00
|
|
|
print_status("Carrier: #{result}" )
|
2009-01-13 07:08:40 +00:00
|
|
|
self.modem = modem if global
|
2009-01-11 06:09:02 +00:00
|
|
|
return modem
|
|
|
|
else
|
2009-06-30 00:59:58 +00:00
|
|
|
print_error("No Carrier")
|
2009-01-11 06:09:02 +00:00
|
|
|
disconnect_dialup(modem)
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-26 21:02:00 +00:00
|
|
|
# Closes the modem connection
|
2009-01-13 07:08:40 +00:00
|
|
|
def disconnect_dialup(nmodem = self.modem)
|
|
|
|
if(nmodem)
|
2009-06-30 00:59:58 +00:00
|
|
|
nmodem.flush
|
2009-01-13 07:08:40 +00:00
|
|
|
nmodem.hangup
|
|
|
|
nmodem.close
|
|
|
|
end
|
2009-01-11 06:09:02 +00:00
|
|
|
end
|
|
|
|
|
2009-06-30 00:59:58 +00:00
|
|
|
# Reads until timeout looking for regexp
|
2009-01-28 06:13:47 +00:00
|
|
|
def dialup_expect(regexp, timeout)
|
2009-06-30 00:59:58 +00:00
|
|
|
res = {
|
|
|
|
:match => false,
|
|
|
|
:buffer => nil,
|
|
|
|
}
|
|
|
|
return res if ! self.modem
|
|
|
|
|
|
|
|
res[:buffer] = ''
|
2009-01-28 06:13:47 +00:00
|
|
|
|
|
|
|
time = Time.now
|
|
|
|
while Time.now < time + timeout
|
|
|
|
c = self.modem.getc
|
2009-06-30 00:59:58 +00:00
|
|
|
res[:buffer] += c.chr if c
|
|
|
|
if res[:buffer].match(regexp) != nil
|
|
|
|
res[:match] = true
|
|
|
|
while c
|
|
|
|
c = self.modem.getc
|
|
|
|
res[:buffer] += c.chr if c
|
2010-09-26 21:02:00 +00:00
|
|
|
end
|
2009-06-30 00:59:58 +00:00
|
|
|
return res
|
|
|
|
end
|
2009-01-28 06:13:47 +00:00
|
|
|
end
|
2009-06-30 00:59:58 +00:00
|
|
|
return res
|
|
|
|
end
|
2009-01-28 06:13:47 +00:00
|
|
|
|
2009-06-30 00:59:58 +00:00
|
|
|
def dialup_getc
|
|
|
|
return false if ! self.modem
|
|
|
|
return self.modem.getc
|
|
|
|
end
|
|
|
|
|
|
|
|
def dialup_gets
|
|
|
|
return false if ! self.modem
|
|
|
|
buffer = ''
|
|
|
|
c = self.modem.getc
|
|
|
|
while c != 0x0a
|
|
|
|
buffer += c
|
|
|
|
c = self.modem.getc
|
|
|
|
end
|
|
|
|
buffer += c
|
|
|
|
return buffer
|
|
|
|
end
|
|
|
|
|
|
|
|
def dialup_putc(c)
|
|
|
|
return false if ! self.modem
|
2009-07-08 20:51:47 +00:00
|
|
|
return self.modem.putc(c)
|
2009-01-28 06:13:47 +00:00
|
|
|
end
|
|
|
|
|
2009-06-30 00:59:58 +00:00
|
|
|
def dialup_puts(string)
|
2009-01-28 06:13:47 +00:00
|
|
|
return false if ! self.modem
|
2009-07-08 20:51:47 +00:00
|
|
|
return self.modem.puts(string)
|
2009-01-28 06:13:47 +00:00
|
|
|
end
|
|
|
|
|
2009-01-13 07:08:40 +00:00
|
|
|
def handler(nmodem = self.modem)
|
|
|
|
# If the handler claims the modem, then we don't want it to get closed
|
|
|
|
# during cleanup
|
|
|
|
if ((rv = super) == Handler::Claimed)
|
|
|
|
if (nmodem == self.modem)
|
|
|
|
self.modem = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return rv
|
|
|
|
end
|
2010-09-26 21:02:00 +00:00
|
|
|
|
2009-01-13 07:08:40 +00:00
|
|
|
attr_accessor :modem
|
|
|
|
|
2009-01-11 06:09:02 +00:00
|
|
|
end
|
|
|
|
end
|