1

我想自动化一些需要安全解锁的 WDBI 服务。我有一个可以从 CANoe 调用的 dll,但我不想使用独木舟硬件,我也不知道 dll 中的函数调用。有什么方法可以从 python 程序调用 dll 来执行会话解锁?

4

1 回答 1

2

有了手头的 DLL,您可以使用DependencyWalker之类的工具来查看 DLL 的导出符号。但是,如果您已经知道您的 DLL 在 CANoe 中工作,它将遵循Vector Informatik指定的 API,在 CANoe的安全访问 DLL 中实现:GenerateKeyEx 和 GenerateKeyExOpt。

生成密钥:

int VKeyGenResult ExGenerateKeyEx (
  const unsigned char* ipSeedArray,
  unsigned int iSeedArraySize,
  const unsigned int iSecurityLevel,
  const char* ipVariant,
  unsigned char* iopKeyArray,
  unsigned int iMaxKeyArraySize,
  unsigned int& oActualKeyArraySize );

GenerateKeyExOpt:

VKeyGenResult ExOptGenerateKeyExOpt (
  const unsigned char* ipSeedArray,
  unsigned int iSeedArraySize,
  const unsigned int iSecurityLevel,
  const char* ipVariant,
  const char* ipOptions,
  unsigned char* iopKeyArray,
  unsigned int iMaxKeyArraySize,
  unsigned int& oActualKeyArraySize );

然后只需从 python 调用这个 Dll,即使用ctypes

import ctypes

mylib = ctypes.WinDLL("./GenerateKeyExImpl.dll")
seed = (ctypes.c_byte * 4)(0xff, 0xfe, 0xfd, 0xfc) # these bytes you should get from the ECU i.e.
# Tx 27 01
# Rx 67 01 ff fe fd fc
key = (ctypes.c_byte * 4)() # this will contain the secret key after Dll call
keylength = ctypes.c_int(4) # this will contain the secret key length after Dll call
mylib.ExGenerateKeyEx(
     ctypes.pointer(seed), # Seed from the ECU
     ctypes.c_int(4), # Example: Seed length = 4 bytes
     ctypes.c_int(1), # Example: Security Level 1
     POINTER(c_int)(), # Example: NULL = No variant string
     ctypes.pointer(key), # Key to send back to the ECU
     ctypes.c_int(4), # Example: Key Max length = 4 bytes
     ctypes.pointer(keylength), # Example: Seed length = 4 bytes
)
# TODO: Send "key" back to the ECU i.e.
# Tx 27 02 XX XX XX XX
# Rx 67 02   
于 2021-06-29T13:09:07.813 回答