我做了一个小程序,向串口发送数据,如何才能判断我要发送数据的串口有没有被拦截,主要是不想让别人看见我发的数据,前问如何判断电脑上是否开了串口拦截软件?

解决方案 »

  1.   

    下面是 hook comm的 例子/*
    first call: "\\.\COM1 CreateFileW"
    then  call: "\\.\COM1 CreateFileA"
    */ 
    HANDLE WINAPI myCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, 
     LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
     DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
    {// unhook first          
    g_HookApi.UnhookOneApi(&myapi_info[CREATEFILEA]);
    HANDLE handle=CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
         dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
    // hook again
    g_HookApi.HookOneApi(&myapi_info[CREATEFILEA]);
    //
    if(handle != INVALID_HANDLE_VALUE)
    {// handle
    if(g_CommFile != handle)// not comes
    {// because first call: "\\.\COM1 CreateFileW"
    char *CommPort=0;
    CommPort=strrchr(lpFileName,'\\');
    if(CommPort) CommPort++;
    else  CommPort=(char*)lpFileName;
    if(strnicmp(CommPort,"COM",3)==0)
    {//save handle and port name
    g_CommFile=handle;
    strcpy(g_CommStruc.CommName,CommPort);
    #ifdef _DEBUG
    SendData2Dialog(WM_CFA_MSG,(BYTE*)(g_CommStruc.CommName),strlen(g_CommStruc.CommName)+1);
    #endif
    }
    }
    }
    return handle;
    }
    //
    HANDLE WINAPI myCreateFileW(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
    LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
    DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
    {// unhook first
    g_HookApi.UnhookOneApi(&myapi_info[CREATEFILEW]);
    HANDLE handle=CreateFileW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
    dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
    // hook again
    g_HookApi.HookOneApi(&myapi_info[CREATEFILEW]);
    //
    if(handle != INVALID_HANDLE_VALUE)
    {
    if(g_CommFile != handle)
    {
    char fname[MAX_PATH]={0};//'\\.\COM1'
    WideCharToMultiByte(CP_ACP, 0, lpFileName, -1, fname, MAX_PATH/2 ,NULL,NULL); 

    char *CommPort=0;
    CommPort=strrchr(fname,'\\');
    if(CommPort) CommPort++;
    else  CommPort=fname;
    if(strnicmp(CommPort,"COM",3)==0)
    {//save handle and port name
    g_CommFile = handle;
    strcpy(g_CommStruc.CommName,CommPort);
    #ifdef _DEBUG
    SendData2Dialog(WM_CFW_MSG,(BYTE*)(g_CommStruc.CommName),strlen(g_CommStruc.CommName)+1);
    #endif
    }
    }
    }
    return handle;
    }
      

  2.   

    https://download.csdn.net/download/schlafenhamster/5202391本程序 窥视 串口的 活动。主要 hook 了: {"KERNEL32.DLL", "CreateFileA","myCreateFileA",(FARPROC)myCreateFileA}, {"KERNEL32.DLL", "CreateFileW","myCreateFileW",(FARPROC)myCreateFileW}, {"KERNEL32.DLL", "GetCommState","myGetCommState",(FARPROC)myGetCommState}, {"KERNEL32.DLL", "ReadFile", "myReadFile", (FARPROC)myReadFile}, {"KERNEL32.DLL", "ReadFileEx", "myReadFileEx", (FARPROC)myReadFileEx}, {"KERNEL32.DLL", "WriteFile", "myWriteFile", (FARPROC)myWriteFile}, {"KERNEL32.DLL", "WriteFileEx","myWriteFileEx",(FARPROC)myWriteFileEx}, {"KERNEL32.DLL", "CloseHandle","myCloseHandle",(FARPROC)myCloseHandle},