aboutsummaryrefslogtreecommitdiffstats
path: root/src/demo_client.rb
blob: c8c8911e653e6dde00616afe64c5d06909847a4d (plain)
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
#!/usr/bin/env ruby
# encoding: UTF-8
=begin
This file is part of softSIM.

softSIM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

softSIM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with sofSIM.  If not, see <http://www.gnu.org/licenses/>.

Copyright (C) 2011 Kevin "tsaitgaist" Redon kevredon@mail.tsaitgaist.info
=end
# this programm will create a client which can be used to test servers
require_relative 'sap/client'
require_relative 'lib/apdu'
require_relative 'info_client'
require_relative 'copy_client'

#=============
#== default ==
#=============

# client use (demo,info,copy)
@type = "demo"
# which IO to use (tcp,unix,bt)
@socket = "tcp"
# tcp port
@port = 1337
# tcp host
@host = "localhost"
# unix socket
@unix = "/tmp/sap.socket"
# file were to save the copy
@file = "sim.xml"
# bluetooth rfcomm serial port
@bt = nil
# the verbosity (from common)
$verbosity = 0

#=============
#== methods ==
#=============

include APDU

# tell APDU methods how to send
def transmit_apdu(apdu)
  return @client.apdu(apdu)
end

# show help
def print_help
  puts "demo_client.rb [options]"
  puts ""
  puts "demonstration SAP client connecting to an indicated SAP server"
  puts "it executes some common commands"
  puts ""
  puts "options :"
  puts " --help,-h\t\tprint this help"
  puts " --type,-t client\tclient type: demo,info,copy (default #{@type})"
  puts " --socket,-s type\tsocket type: tcp,unix,bt (default #{@socket})"
  puts " --tcp,-p port\t\ttcp port (default #{@port})"
  puts " --host,-l host\t\ttcp host (default #{@host})"
  puts " --unix,-u file\t\tunix socket (default #{@unix})"
  puts " --file,-f path\t\tfile where to save the sim copy (default #{@file})"
  puts " --bluetooth,-s rfcomm\tbluetooth rfcomm serial port (default #{@bt ? @bt : 'self discovery'})"
  puts " --verbosity,-v\t\tdebug verbosity 0..5 (default #{$verbosity})"
end

#==========
#== main ==
#==========

# parse CLI arguments
while arg=ARGV.shift do
  
  case arg
  when "--help","-h"
    print_help
    exit 0
  when "--type","-t"
    param = ARGV.shift
    @type = param if param
  when "--socket","-s"
    param = ARGV.shift
    @socket = param if param
  when "--port","-p"
    param = ARGV.shift.to_i
    @port = param if param
  when "--host","-l"
    param = ARGV.shift
    @host = param if param
  when "--unix","-u"
    param = ARGV.shift
    @unix = param if param
  when "--file","-f"
    param = ARGV.shift
    @file = param if param
  when "--bluetooth","-s"
    param = ARGV.shift
    @bt = param if param
  when "--verbosity","-v"
    param = ARGV.shift.to_i
    $verbosity = param if param
  else
    puts "unknown argument #{arg}"
    exit 0
  end
end

# create IO
case @socket
when "tcp"
  require 'socket'
  io = TCPSocket.open(@host,@port)
when "unix"
  require 'socket'
  io = UNIXSocket.open(@unix)
when "bt"
  #sudo gem install serialport (http://rubygems.org/gems/serialport)
  require 'rubygems'
  require 'serialport'
  if @bt then
    io = SerialPort.new(@bt)
  else
    require_relative 'tools/bluetooth_sap_serial'
    bt = BluetoothSAPSerial.new
    # using SerialPort because reading the File does not work (have to find right stty options)
    io = SerialPort.new(bt.connect)
  end
else
  raise "please defined which socket to use"
end

case @type
when "demo"
  # create client
  @client = Client.new(io)
  @client.start
  @client.connect

  # get ATR
  atr = @client.atr
  puts atr ? "ATR: #{atr.to_hex_disp}" :  "could not get ATR"

  # get IMSI
  imsi = read_ef([MF,DF_GSM,EF_IMSI])
  # byte 1 is the length of the IMSI
  imsi_length = imsi[0]
  imsi = imsi[1,imsi_length]
  # first nibble is for parity check (not done)
  imsi = imsi.nibble_str[1..-1]
  puts "IMSI : "+imsi

  # run A38 algo
  # the rands
  rands = []
  4.times do |i|
    rands << [(i<<4)+i]*16
  end
  # the results
  puts "some KCs (RAND SRES Kc):"
  rands.each do |r|
    response = a38(r)
    puts "  - #{r.to_hex_disp.gsub(' ','')} #{response[0,4].to_hex_disp.gsub(' ','')} #{response[4..-1].to_hex_disp.gsub(' ','')}"
  end

  @client.disconnect
when "info"
  @client = Info.new(io)
  @client.display
  @client.close
when "copy"
  @client = Copy.new(io)
  @client.copy(@file)
  @client.close
else
  raise "please defined which type to use"
end

# close client_io
io.close
bt.close if @socket=="bt" and !@bt