charlysan / pyrfidhid

Python library to control Chinese USB HID 125Khz RFID Reader/Writer
MIT License
142 stars 36 forks source link

Maybe compatible hardware #1

Closed globalcitizen closed 6 years ago

globalcitizen commented 6 years ago

No idea why I get busy errors.

[22393.205161] usb 1-1: new low-speed USB device number 19 using xhci_hcd
[22393.344665] usb 1-1: New USB device found, idVendor=0108, idProduct=0100
[22393.344666] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[22393.344667] usb 1-1: Product: USB Smart Card Reader
[22393.344668] usb 1-1: Manufacturer: JX-SCI_001
[22393.344668] usb 1-1: SerialNumber: V1.00
[22393.350046] hid-generic 0003:0108:0100.000C: hiddev96,hidraw1: USB HID v1.10 Device [JX-SCI_001 USB Smart Card Reader] on usb-0000:00:14.0-1/input0

Made this test to use the device.

# lsusb -vd 0108:0100

Bus 001 Device 020: ID 0108:0100  
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               1.10
  bDeviceClass            0 
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0         8
  idVendor           0x0108 
  idProduct          0x0100 
  bcdDevice            0.00
  iManufacturer           1 JX-SCI_001
  iProduct                2 USB Smart Card Reader
  iSerial                 3 V1.00
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength           34
    bNumInterfaces          1
    bConfigurationValue     1
    iConfiguration          0 
    bmAttributes         0xe0
      Self Powered
      Remote Wakeup
    MaxPower              200mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           1
      bInterfaceClass         3 Human Interface Device
      bInterfaceSubClass      0 
      bInterfaceProtocol      0 
      iInterface              0 
        HID Device Descriptor:
          bLength                 9
          bDescriptorType        33
          bcdHID               1.10
          bCountryCode           51 Unknown
          bNumDescriptors         1
          bDescriptorType        34 Report
          wDescriptorLength      29
         Report Descriptors: 
           ** UNAVAILABLE **
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0008  1x 8 bytes
        bInterval               5
Device Status:     0x0000
  (Bus Powered)
# cat test
from rfidhid.core import RfidHid

try:
    # Try to open RFID device using default vid:pid (ffff:0035)
    rfid = RfidHid(0x0108,0x0100)
except Exception as e:
    print(e)
    exit()

payload_response = rfid.read_tag()
uid = payload_response.get_tag_uid()

rfid.beep()
print(uid)
# python test
Traceback (most recent call last):
  File "test", line 10, in <module>
    payload_response = rfid.read_tag()
  File "/root/code/pyrfidhid/rfidhid/core.py", line 122, in read_tag
    response = self.dev.ctrl_transfer(0x21, self.SET_REPORT, 0x0301, 0, buff)
  File "/root/.local/lib64/python3.5/site-packages/usb/core.py", line 1034, in ctrl_transfer
    self._ctx.managed_claim_interface(self, interface_number)
  File "/root/.local/lib64/python3.5/site-packages/usb/core.py", line 102, in wrapper
    return f(self, *args, **kwargs)
  File "/root/.local/lib64/python3.5/site-packages/usb/core.py", line 167, in managed_claim_interface
    self.backend.claim_interface(self.handle, i)
  File "/root/.local/lib64/python3.5/site-packages/usb/backend/libusb1.py", line 811, in claim_interface
    _check(self.lib.libusb_claim_interface(dev_handle.handle, intf))
  File "/root/.local/lib64/python3.5/site-packages/usb/backend/libusb1.py", line 595, in _check
    raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 16] Resource busy

I did notice that the thing gives errors after being plugged in awhile.

# lsusb -vd 0108:0100

Bus 001 Device 020: ID 0108:0100  
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               1.10
  bDeviceClass            0 
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0         8
  idVendor           0x0108 
  idProduct          0x0100 
  bcdDevice            0.00
  iManufacturer           1 (error)
  iProduct                2 (error)
  iSerial                 3 V1.00
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength           34
    bNumInterfaces          1
    bConfigurationValue     1
    iConfiguration          0 
    bmAttributes         0xe0
      Self Powered
      Remote Wakeup
    MaxPower              200mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           1
      bInterfaceClass         3 Human Interface Device
      bInterfaceSubClass      0 
      bInterfaceProtocol      0 
      iInterface              0 
        HID Device Descriptor:
          bLength                 9
          bDescriptorType        33
          bcdHID               1.10
          bCountryCode           51 Unknown
          bNumDescriptors         1
          bDescriptorType        34 Report
          wDescriptorLength      29
         Report Descriptors: 
           ** UNAVAILABLE **
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0008  1x 8 bytes
        bInterval               5
Device Status:     0x0000
  (Bus Powered)
tree ~/code/pyrfidhid # 
charlysan commented 6 years ago

1 - Are you sure the device is working properly?

2 - Is it compatible with IDRWv3 Windows Tool?

2 - Is that a MIFARE compatible device? From the usb information you provided it looks like a MIFARE reader:

 New USB device strings: Mfr=1, Product=2, SerialNumber=3

If that's the case, then the commands should be totally different.

3 - Have you tried to reverse it following the wiki document?

globalcitizen commented 6 years ago
  1. It's new so I guess so.

  2. I never received their software. This is the device: https://item.taobao.com/item.htm?id=523056712683

  3. It will read a whole bunch of stuff. "13.56Mhz IC卡: 支持S20/S50/S70卡,Ultralight/UltralightC, Ntag203系列,Mifare Plus , 支持Mifare Desfire ,FM1208系列等RFID". Most readers on the market seem to read a whole bunch of different standards. Many of them seem to use the ALPAR protocol. Apparently there are two major interfaces presented by low end USB readers on the market, serial via UART (often using Prolific chipsets) and HID. I came to your project because this is a HID based product.

  4. Not yet.

I did however notice some comments online suggesting some of the HID based solutions don't actually follow the USB spec and hence don't work properly with standard Linux or OSX USB stacks, requiring kernel patches. This would explain the iProduct and iManufacturer returning as (error) after a few seconds of being plugged in. I have sort of written off getting this to work now, based on reading that, and won't be putting any further time in to it.

PS. Although the manufacturer claimed Linux support on the sales page they retracted this claim when contacted and invited me to ship the product back for a refund.

globalcitizen commented 6 years ago

FYI - got the software from them.

I have since dumped many successful reads for MifareS50 and will try to analyze these dumps over the next few days and compare with your code / data. Here is the basic flow and the final value being read.

usb-first-packet usb-2 usb-3

charlysan commented 6 years ago

That sounds great @globalcitizen. Feel free to open a new issue for your hardware where you can post your progress and maybe we can extend the library for several similar USB HID hardwares in the future. I was planning to add a new document with a couple of clarifications on how to calculate CRC in a better way, and how to discover available commands using brute force, but I haven't had the time yet; I'll try to do it this weekend.