flat assembler
Message board for the users of flat assembler.

Index > Windows > reading sim card using windows api?

Author
Thread Post new topic Reply to topic
sleepsleep



Joined: 05 Oct 2006
Posts: 9191
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
hi there,
i bought the cheap china made SUN-500 (sim card clone + reader)
but the software...... Crying or Very sad

so, i intend to code one, to read the SMS and phone contact.

you guys got any info / reference / guide?

the sim card reader is connected using USB (but the driver fake it to COM)

thanks.
Post 16 Aug 2008, 04:57
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u
Post 16 Aug 2008, 16:15
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7802
Location: Kraków, Poland
Tomasz Grysztar
Doesn't your driver provide PC/SC interface?
I have some small programs written in fasm that I once used to copy SMS archive from one sim card to another - using the USB smart card reader through PC/SC API.
Post 17 Aug 2008, 20:49
View user's profile Send private message Visit poster's website Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9191
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
the driver, A-Men Technologies, USB to serial comes in one install shield .exe

after unzip it. i got.
Code:
Setup.exe (install shield)
data1.cab
data2.cab
Setup.ini
ikernel.ex_
data1.hdr
setup.inx
layout.bin
    


i tried the python sim reader through dex link. but to no avail.

the only dll that reside with the sim utility software folder that came with the sim reader CD is ISO7816.dll

using depencency walker, all functions that reside on it are here,
http://pastebin.com/m1ae2f8c

if i use the depecency walker on the sim editor exe, the function that gets called (on iso7816.dll) are below.
Code:
@DecodingItem$qpuc
@STKPassword$qpuc
@IsSTKCard$qv
@IsAutoMenuOn$qv
@EndDebug$qv
@BeginDebug$qv
@UpdateRecord$qpucucuci
@ReadRecord$qpucucuci
@UpdateBinary$qpucii
@ReadBinary$qpucii
@GetResponse$qpuci
@SelectFile$qus
@Verify$qucpuc
@IncomeingAPDU$qpuc
@OutgoingAPDU$qpuct1
@Disconnect$qv
@ConnectCard$qi
    


i dont' know if this the helper dll you mentioned? btw, did you wrote it based on dll that shipped with win os only? like no 3rd party dll?

thanks in advance.
Post 17 Aug 2008, 22:40
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7802
Location: Kraków, Poland
Tomasz Grysztar
Yes, PC/SC is shipped with OS, at least with Windows 2000 or higher. The only thing you need is a PC/SC-compliant driver for your reader.
Post 18 Aug 2008, 06:44
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7802
Location: Kraków, Poland
Tomasz Grysztar
Please try to use this tool to determine whether you have a PC/SC interface for you reader working.


Description: PC/SC diagnostic tool
Download
Filename: PCSCDiag.zip
Filesize: 313.38 KB
Downloaded: 90 Time(s)

Post 18 Aug 2008, 08:42
View user's profile Send private message Visit poster's website Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9191
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
thanks,
but i think i am out of luck.

Image
Post 18 Aug 2008, 23:11
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7802
Location: Kraków, Poland
Tomasz Grysztar
Maybe you haven't got the driver installed correctly. What reader model is it? The FAQ on the A-MEN official page states that their readers are compatible with PC/SC.
Post 18 Aug 2008, 23:31
View user's profile Send private message Visit poster's website Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9191
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
the reader is actually not from A-Men, it is from "china SUN-500", cheap SIM reader & writer

regarding the A-Men technologies,
USB to serial Comm Port.
because i saw it on the below image.
[img=http://img208.imageshack.us/img208/7775/15959914sm6.th.png]

the driver imho, probably provide the functionality to fake USB into Comm port, so the device could read from COM3, but without PC/SC functionality to R/W the SIM card.

btw, their SIM software could functions (at the moment i run the pcscdiag.exe, like reading my SIM sms/contacts)
Post 19 Aug 2008, 00:06
View user's profile Send private message Reply with quote
Alphonso



Joined: 16 Jan 2007
Posts: 294
Alphonso
sleepsleep, I bought something very similar with the idea of using multiple sims on one sim card (maxis and digi prepaid come to mind) Wink but never did get the blank. You can get software to read/write your sim here (free). MagicSim V22

It comes with both serial and smart card drivers and is a biggish download (38M) but most of that is help files (chinese / eng). It's been a long time since I looked at this but I think the serial device is just a straight 232 + level converter so it should just be fairly easy to program.
Post 19 Aug 2008, 04:32
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7802
Location: Kraków, Poland
Tomasz Grysztar
Still, just in case someone visites here with a similar problem (and a PC/SC standard reader), here are the sources for my small programs I used to transfer SMS archive from one card to another.

Note that write.asm was assembler after executing the first program, as it uses FILE directive to include the SMS archive from file created by it the read.exe. Also there's no error messaging, all error checking is just "jc exit". This is because these programs was written for my single personal use.


Description: Read/write SMS archive from/to SIM card using PC/SC API
Download
Filename: pcsc_sim.zip
Filesize: 5.26 KB
Downloaded: 98 Time(s)

Post 19 Aug 2008, 08:25
View user's profile Send private message Visit poster's website Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9191
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
thanks tomasz for the help Smile
btw, alphonso, are you somehow located in malaysia?

the software that came with the sim r/w device is something like magicsim, but they couldn't read more than 30 sms...
they even gave me some rectagular fonts if the message is crossing 2 pages. and without time/date info Sad

maybe i could do something with the DLL ..... let see.... waiting for holiday Smile
Post 19 Aug 2008, 22:20
View user's profile Send private message Reply with quote
Alphonso



Joined: 16 Jan 2007
Posts: 294
Alphonso
No, not located in Malaysia but sometimes work there.

If your device is similar to mine (very probable) the RST is used to reset the SIM so if you try something like the attached you may get an ATR response, Answer To Reset I think. It will probably start with'3B' if it works.

At least then you can at least establish comm's. After that it's a case of finding the right command codes to do what you want.
Code:
format PE GUI 4.0

include 'win32a.inc'

   RBufferLength = 32

   SETXOFF       = 1
   SETXON        = 2
   SETRTS        = 3
   CLRRTS        = 4
   SETDTR        = 5
   CLRDTR        = 6
   RESETDEV      = 7
   SETBREAK      = 8
   CLRBREAK      = 9

   PURGE_TXABORT = 00001h
   PURGE_RXABORT = 00002h
   PURGE_TXCLEAR = 00004h
   PURGE_RXCLEAR = 00008h
   PURGE_ALL     = 0000Fh

struc DCB {
  .DCBlength                      dd 28
  .BaudRate                       dd 9600
  .Flags                          dd 11100000h
  .wReserved                      dw 0
  .XonLim                         dw 100h
  .XoffLim                        dw 40h
  .ByteSize                       db 8            ;8 bits
  .Parity                         db 2            ;Even
  .StopBits                       db 2            ;2 stop bits
  .XonChar                        db 11h
  .XoffChar                       db 13h
  .ErrorChar                      db 0
  .EofChar                        db 0
  .EvtChar                        db 0
  .wReserved1                     dw 0
}
struc COMMTIMEOUTS {
  .ReadIntervalTimeout            dd 0
  .ReadTotalTimeoutMultiplier     dd 0
  .ReadTotalTimeoutConstant       dd 10           ;seems to be 10ms even if set to 1
  .WriteTotalTimeoutMultiplier    dd 1
  .WriteTotalTimeoutConstant      dd 50
}

section '.code' code readable executable

        invoke CreateFile,CPName,GENERIC_WRITE+GENERIC_READ,0,0,OPEN_EXISTING,0,0  ;Open Comm Port
        mov [CPHandle],eax
        cmp eax,-1
        je NoComms

        invoke GetCommTimeouts,[CPHandle],OriginalCOMMTIMEOUTS  ;Save original state
        cmp eax,0                                               ;
        je exit                                                 ;
                                                                ;
        invoke GetCommState,[CPHandle],OriginalDCB              ;
        cmp eax,0
        je exit

        invoke SetCommState,[CPHandle],SimDCB
        invoke SetCommTimeouts,[CPHandle],SimCOMMTIMEOUTS
        invoke EscapeCommFunction,[CPHandle],SETDTR              ;probably not required
        invoke EscapeCommFunction,[CPHandle],SETRTS              ;Reset SIM
        invoke Sleep,10
        invoke PurgeComm,[CPHandle],PURGE_ALL                    ;Clear buffers

        invoke GetTickCount                                      ;Start timing
        mov    [StartTime],eax
        invoke EscapeCommFunction,[CPHandle],CLRRTS              ;Clear Reset (SIM RST High)

again:
        invoke ReadFile,[CPHandle],[RBufferP],RBufferLength,ActualBytes,0
        cmp eax,0
        je ReadError

        inc    [Counts]
        mov    eax,[ActualBytes]
        cmp    [Counts],50                                       ;50x 10ms, 500ms should be enough
        jg     TimeOut                                           ;longer than 0.5s
        cmp    eax,0
        jz     again                                             ;no bytes received

        add    [RBufferP],eax
        cmp    [RBufferP],RBuffer+RBufferLength
        jge    BufferFull
        cmp    [RBufferP],RBuffer+2
        jl     again                                             ;Get a least the first two bytes

        cmp    [DataLength],2
        jne    CheckLength

        mov    al,byte [RBuffer+1]                               ;Work out number of expected bytes
        mov    ebx,BitCount                                      ;number of bits per byte
        mov    dl,al
        shr    al,4
        xlatb                                                    ;al = number of high nibble bits set
        and    dl,0fh
        add    dl,al
        add    byte [DataLength],dl                              ;expected data length

CheckLength:
        mov    eax,[RBufferP]
        sub    eax,RBuffer
        cmp    eax,[DataLength]
        jl     again                                             ;Have all bytes been received?

        invoke GetTickCount
        sub    eax,[StartTime]                                   ;How long it took to get an ATR

        invoke wsprintf,AccessTime,wsformat,eax

BytesReceived:
        mov    ecx,[DataLength]
        mov    esi,RBuffer                                      ;Hex...
        mov    edi,Result                                       ;converted to Ascii
Conv:   lodsb
        mov    dl,al
        shr    al,4
        cmp    al,9
        jle    Dec1
        add    al,7
Dec1:
        add    al,30h
        stosb
        mov    al,dl
        and    al,0fh
        cmp    al,9
        jle     Dec2
        add    al,7
Dec2:
        add    al,30h
        stosb
        dec    ecx
        jnz    Conv
        xor    al,al
        stosb                                                   ;end string with a zero

        invoke MessageBox,NULL,AccessTime,CPName,MB_OK          ;Display ATR result,
        jmp    ReInstate                                        ;no checks for legitimacy

BufferFull:
        invoke MessageBox,NULL,TooMuchData,CPName,MB_OK
        jmp    ReInstate

TimeOut:
        invoke MessageBox,NULL,TooLong,CPName,MB_OK

ReInstate:
        invoke SetCommState,[CPHandle],OriginalDCB              ;Restore original state
        invoke SetCommTimeouts,[CPHandle],OriginalCOMMTIMEOUTS  ;

exit:   invoke  CloseHandle,[CPHandle]
        invoke  ExitProcess,0
NoComms:
        invoke MessageBox,NULL,CommFailure,CPName,MB_OK
        invoke  ExitProcess,0

section '.data' data readable writeable

CPHandle                dd 0
ActualBytes             dd 0
Counts                  dd 0
StartTime               dd 0
DataLength              dd 2
RBufferP                dd RBuffer
BitCount                db 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4
SimDCB                  DCB
OriginalDCB             DCB
SimCOMMTIMEOUTS         COMMTIMEOUTS
OriginalCOMMTIMEOUTS    COMMTIMEOUTS

wsformat                db 'ATR reponse time = %4u ms.   ',0dh,0dh,0
Lenwsformat=$-wsformat
CPName                  db 'COM4',0                             ;Comport SIM card is on
TooLong                 db 'Timed out >500ms',0
CommFailure             db 'Unable to open port',0
TooMuchData             db 'Too much data',0
ReadError               db 'Error reading data',0

AccessTime              rb Lenwsformat
Result                  rb RBufferLength*2
                        rb 1
RBuffer                 rb RBufferLength*2

section '.idata' import data readable writeable

 library kernel32,'KERNEL32.DLL',\
         user32,'USER32.DLL'

 import kernel32,\
        Sleep,'Sleep',\
        GetTickCount,'GetTickCount',\
        CreateFile,'CreateFileA',\
        ReadFile,'ReadFile',\
        CloseHandle,'CloseHandle',\
        GetCommState,'GetCommState',\
        GetCommTimeouts,'GetCommTimeouts',\
        SetCommTimeouts,'SetCommTimeouts',\
        SetCommState,'SetCommState',\
        PurgeComm,'PurgeComm',\
        EscapeCommFunction,'EscapeCommFunction',\
        ExitProcess,'ExitProcess'

 import user32,\
        wsprintf,'wsprintfA',\
        MessageBox,'MessageBoxA'     
Edit: Just tried a few different sim cards and the ATR response times vary from 20ms to nearly 300ms, so have updated the code a little, adjusted ecx count.


Last edited by Alphonso on 22 Aug 2008, 13:31; edited 1 time in total
Post 20 Aug 2008, 14:33
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9191
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
thanks alphonso, (btw, if you somehow reach malaysia, let me know Smile damn hard to find a fasm member in malaysia. as far as i knew, 2 guys only probably or me alone? lol.

your code, i will try it later, today got client problem with..... virus.
Post 20 Aug 2008, 15:08
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.