评论

python3-企业微信-会话内容存档-对接Linux_C_SDK-libWeWorkFinanceSdk_C.so

python3接入企业微信会话内容存档 libWeWorkFinanceSdk_C.so

import base64
import ctypes
import json

import Crypto
from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA


class WxWork:
    CORP_ID = ''
    PRI_KEY = ''
    CHAT_SECRET = ''

    @classmethod
    def sync_msg(cls):
        dll = ctypes.cdll.LoadLibrary(os.getcwd() + "/libWeWorkFinanceSdk_C.so")  # 真实libWeWorkFinanceSdk_C位置
        new_sdk = dll.NewSdk()
        result = dll.Init(new_sdk, cls.CORP_ID.encode(), cls.CHAT_SECRET.encode())
        if result != 0:
            return
        private_key = RSA.import_key(cls.PRI_KEY)
        cipher = Crypto.Cipher.PKCS1_v1_5.new(private_key)
        seq = 0
        while True:
            s = dll.NewSlice()
            dll.GetChatData(new_sdk, seq, 1000, '', '', 5, ctypes.c_long(s))
            data = dll.GetContentFromSlice(s)
            data = ctypes.string_at(data, -1).decode("utf-8")
            dll.FreeSlice(s)
            data = json.loads(data).get('chatdata')
            if not data:
                break
            seq = data[-1].get('seq')
            for msg in data:
                encrypt_key = cipher.decrypt(base64.b64decode(msg.get('encrypt_random_key')), "ERROR")
                ss = dll.NewSlice()
                dll.DecryptData(encrypt_key, msg.get('encrypt_chat_msg').encode(), ctypes.c_long(ss))
                result = dll.GetContentFromSlice(ss)
                result = ctypes.string_at(result, -1).decode("utf-8")
                result = json.loads(result)
                dll.FreeSlice(ss)
                print(result)
        dll.DestroySdk(new_sdk)


if __name__ == '__main__':
    WxWork.sync_msg()

最后一次编辑于  09-03  
点赞 1
收藏
评论

1 个评论

  • 周六
    周六
    09-04

    求助:有没有遇到服务器ctypes.cdll.LoadLibrary so文件崩溃的?

    *** Error in `venv3.6/bin/python': free(): invalid pointer: 0x00007fcdbb73b600 ***

    ======= Backtrace: =========

    /lib/x86_64-linux-gnu/libc.so.6(+0x70bfb)[0x7fcdd5812bfb]

    /lib/x86_64-linux-gnu/libc.so.6(+0x76fc6)[0x7fcdd5818fc6]

    /lib/x86_64-linux-gnu/libc.so.6(+0x7780e)[0x7fcdd581980e]

    /utils/libWeWorkFinanceSdk_C.so(_ZNSt6locale5_Impl16_M_install_facetEPKNS_2idEPKNS_5facetE+0x142)[0x7fcdbb3896a2]

    /utils/libWeWorkFinanceSdk_C.so(_ZNSt6locale5_ImplC2Em+0x1e3)[0x7fcdbb389b03]

    /utils/libWeWorkFinanceSdk_C.so(_ZNSt6locale18_S_initialize_onceEv+0x15)[0x7fcdbb38aa75]

    /lib/x86_64-linux-gnu/libpthread.so.0(+0xe759)[0x7fcdd625a759]

    /utils/libWeWorkFinanceSdk_C.so(_ZNSt6locale13_S_initializeEv+0x21)[0x7fcdbb38aac1]

    /utils/libWeWorkFinanceSdk_C.so(_ZNSt6localeC2Ev+0x13)[0x7fcdbb38ab03]

    /utils/libWeWorkFinanceSdk_C.so(_ZNSt8ios_base4InitC1Ev+0xb4)[0x7fcdbb387e54]

    /utils/libWeWorkFinanceSdk_C.so(+0x3f6814)[0x7fcdbb37a814]

    /utils/libWeWorkFinanceSdk_C.so(+0x45da16)[0x7fcdbb3e1a16]

    ======= Memory map: ========

    55bb6505d000-55bb65303000 r-xp 00000000 fe:01 1057215                    /usr/local/bin/python3.6

    55bb65502000-55bb65504000 r--p 002a5000 fe:01 1057215                    /usr/local/bin/python3.6

    55bb65504000-55bb65568000 rw-p 002a7000 fe:01 1057215                    /usr/local/bin/python3.6

    55bb65568000-55bb65598000 rw-p 00000000 00:00 0 

    55bb655cf000-55bb65fb1000 rw-p 00000000 00:00 0                          [heap]

    7fcdb4000000-7fcdb4021000 rw-p 00000000 00:00 0 

    7fcdb4021000-7fcdb8000000 ---p 00000000 00:00 0 

    09-04
    赞同
    回复
登录 后发表内容