郁金香灬老师 游戏安全  驱动 逆向调试 C/C++编程  脚本 UE4/UE5

找回密码
立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
发新帖
课程大纲和价格
官方联系方式2024在线课大纲QQ咨询

34万

积分

123

好友

132

主题
发表于 2024-2-21 22:30:01 | 查看: 345| 回复: 0
  1. // pch.cpp: 与预编译标头对应的源文件

  2. #include "pch.h"


  3. #include <windows.h>
  4. #include <wincrypt.h>
  5. #include <wintrust.h>
  6. #include <stdio.h>
  7. #include <tchar.h>
  8. #pragma comment(lib, "crypt32.lib")
  9. #define ENCODING (X509_ASN_ENCODING | PKCS_7_ASN_ENCODING)
  10. typedef struct {
  11.         LPWSTR lpszProgramName;//程序名
  12.         LPWSTR lpszPublisherLink;//发布者链接
  13.         LPWSTR lpszMoreInfoLink;//更多信息链接
  14. } SPROG_PUBLISHERINFO, * PSPROG_PUBLISHERINFO;


  15. BOOL GetProgAndPublisherInfo(PCMSG_SIGNER_INFO pSignerInfo,
  16.         PSPROG_PUBLISHERINFO Info);
  17. //获取时间戳日期
  18. BOOL GetDateOfTimeStamp(PCMSG_SIGNER_INFO pSignerInfo, SYSTEMTIME* st);
  19. //打印证书信息
  20. BOOL PrintCertificateInfo(PCCERT_CONTEXT pCertContext);
  21. //获取签名信息的时间戳
  22. BOOL GetTimeStampSignerInfo(PCMSG_SIGNER_INFO pSignerInfo,
  23.         PCMSG_SIGNER_INFO* pCounterSignerInfo);


  24. int _tmain(int argc, TCHAR* argv[])
  25. {
  26.         WCHAR szFileName[MAX_PATH];
  27.         HCERTSTORE hStore = NULL;
  28.         HCRYPTMSG hMsg = NULL;
  29.         PCCERT_CONTEXT pCertContext = NULL;
  30.         BOOL fResult;
  31.         DWORD dwEncoding, dwContentType, dwFormatType;
  32.         PCMSG_SIGNER_INFO pSignerInfo = NULL;
  33.         PCMSG_SIGNER_INFO pCounterSignerInfo = NULL;
  34.         DWORD dwSignerInfo;
  35.         CERT_INFO CertInfo;
  36.         SPROG_PUBLISHERINFO ProgPubInfo;
  37.         SYSTEMTIME st;
  38.         ZeroMemory(&ProgPubInfo, sizeof(ProgPubInfo));
  39.         __try
  40.         {
  41.                 if (argc != 2)
  42.                 {
  43.                         _tprintf(_T("Usage: SignedFileInfo <filename>\n"));
  44.                         return 0;
  45.                 }
  46. #ifdef UNICODE
  47.                 lstrcpynW(szFileName, argv[1], MAX_PATH);
  48. #else
  49.                 if (mbstowcs(szFileName, argv[1], MAX_PATH) == -1)
  50.                 {
  51.                         printf("Unable to convert to unicode.\n");
  52.                         __leave;
  53.                 }
  54. #endif
  55.                 // Get message handle and store handle from the signed file.
  56.                 //查询文件签名信息
  57.                 fResult = CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  58.                         szFileName,
  59.                         CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED,
  60.                         CERT_QUERY_FORMAT_FLAG_BINARY,
  61.                         0,
  62.                         OUT  &dwEncoding,
  63.                         OUT  &dwContentType,
  64.                         OUT  &dwFormatType,
  65.                         OUT  &hStore,
  66.                         OUT  &hMsg,
  67.                         NULL);
  68.                 if (!fResult)
  69.                 {
  70.                         _tprintf(_T("CryptQueryObject failed with %x\n"), GetLastError());
  71.                         __leave;
  72.                 }
  73.                 // Get signer information size.
  74.                 fResult = CryptMsgGetParam(hMsg,
  75.                         CMSG_SIGNER_INFO_PARAM,
  76.                         0,
  77.                         NULL,
  78.                         &dwSignerInfo);
  79.                 if (!fResult)
  80.                 {
  81.                         _tprintf(_T("CryptMsgGetParam failed with %x\n"), GetLastError());
  82.                         __leave;
  83.                 }
  84.                 // Allocate memory for signer information.
  85.                 pSignerInfo = (PCMSG_SIGNER_INFO)LocalAlloc(LPTR, dwSignerInfo);
  86.                 if (!pSignerInfo)
  87.                 {
  88.                         _tprintf(_T("Unable to allocate memory for Signer Info.\n"));
  89.                         __leave;
  90.                 }
  91.                 // Get Signer Information.
  92.                 fResult = CryptMsgGetParam(hMsg,
  93.                         CMSG_SIGNER_INFO_PARAM,
  94.                         0,
  95.                         (PVOID)pSignerInfo,
  96.                         &dwSignerInfo);
  97.                 if (!fResult)
  98.                 {
  99.                         _tprintf(_T("CryptMsgGetParam failed with %x\n"), GetLastError());
  100.                         __leave;
  101.                 }
  102.                 // Get program name and publisher information from
  103.                 // signer info structure.
  104.                 //获取程序名和发布者信息
  105.                 if (GetProgAndPublisherInfo(pSignerInfo, &ProgPubInfo))
  106.                 {
  107.                         if (ProgPubInfo.lpszProgramName != NULL)
  108.                         {
  109.                                 wprintf(L"Program Name : %s\n",
  110.                                         ProgPubInfo.lpszProgramName);
  111.                         }
  112.                         if (ProgPubInfo.lpszPublisherLink != NULL)
  113.                         {
  114.                                 wprintf(L"Publisher Link : %s\n",
  115.                                         ProgPubInfo.lpszPublisherLink);
  116.                         }
  117.                         if (ProgPubInfo.lpszMoreInfoLink != NULL)
  118.                         {
  119.                                 wprintf(L"MoreInfo Link : %s\n",
  120.                                         ProgPubInfo.lpszMoreInfoLink);
  121.                         }
  122.                 }
  123.                 _tprintf(_T("\n"));
  124.                 // Search for the signer certificate in the temporary
  125.                 // certificate store.
  126.                 CertInfo.Issuer = pSignerInfo->Issuer;
  127.                 CertInfo.SerialNumber = pSignerInfo->SerialNumber;
  128.                 pCertContext = CertFindCertificateInStore(hStore,
  129.                         ENCODING,
  130.                         0,
  131.                         CERT_FIND_SUBJECT_CERT,
  132.                         (PVOID)&CertInfo,
  133.                         NULL);
  134.                 if (!pCertContext)
  135.                 {
  136.                         _tprintf(_T("CertFindCertificateInStore failed with %x\n"),
  137.                                 GetLastError());
  138.                         __leave;
  139.                 }
  140.                 // Print Signer certificate information.
  141.                 _tprintf(_T("Signer Certificate:\n\n"));
  142.                 PrintCertificateInfo(pCertContext);
  143.                 _tprintf(_T("\n"));
  144.                 // Get the timestamp certificate signerinfo structure.
  145.                 if (GetTimeStampSignerInfo(pSignerInfo, &pCounterSignerInfo))
  146.                 {
  147.                         // Search for Timestamp certificate in the temporary
  148.                         // certificate store.
  149.                         CertInfo.Issuer = pCounterSignerInfo->Issuer;
  150.                         CertInfo.SerialNumber = pCounterSignerInfo->SerialNumber;
  151.                         pCertContext = CertFindCertificateInStore(hStore,
  152.                                 ENCODING,
  153.                                 0,
  154.                                 CERT_FIND_SUBJECT_CERT,
  155.                                 (PVOID)&CertInfo,
  156.                                 NULL);
  157.                         if (!pCertContext)
  158.                         {
  159.                                 _tprintf(_T("CertFindCertificateInStore failed with %x\n"),
  160.                                         GetLastError());
  161.                                 __leave;
  162.                         }
  163.                         // Print timestamp certificate information.
  164.                         _tprintf(_T("TimeStamp Certificate:\n\n"));
  165.                         PrintCertificateInfo(pCertContext);
  166.                         _tprintf(_T("\n"));
  167.                         // Find Date of timestamp.
  168.                         if (GetDateOfTimeStamp(pCounterSignerInfo, &st))
  169.                         {
  170.                                 _tprintf(_T("Date of TimeStamp : %02d/%02d/%04d %02d:%02d\n"),
  171.                                         st.wMonth,
  172.                                         st.wDay,
  173.                                         st.wYear,
  174.                                         st.wHour,
  175.                                         st.wMinute);
  176.                         }
  177.                         _tprintf(_T("\n"));
  178.                 }
  179.         }
  180.         __finally
  181.         {
  182.                 // Clean up.
  183.                 if (ProgPubInfo.lpszProgramName != NULL)
  184.                         LocalFree(ProgPubInfo.lpszProgramName);
  185.                 if (ProgPubInfo.lpszPublisherLink != NULL)
  186.                         LocalFree(ProgPubInfo.lpszPublisherLink);
  187.                 if (ProgPubInfo.lpszMoreInfoLink != NULL)
  188.                         LocalFree(ProgPubInfo.lpszMoreInfoLink);
  189.                 if (pSignerInfo != NULL) LocalFree(pSignerInfo);
  190.                 if (pCounterSignerInfo != NULL) LocalFree(pCounterSignerInfo);
  191.                 if (pCertContext != NULL) CertFreeCertificateContext(pCertContext);
  192.                 if (hStore != NULL) CertCloseStore(hStore, 0);
  193.                 if (hMsg != NULL) CryptMsgClose(hMsg);
  194.         }
  195.         return 0;
  196. }

  197. // 获取颁布者名字
  198. BOOL PrintCertificateInfo(PCCERT_CONTEXT pCertContext)
  199. {
  200.         BOOL fReturn = FALSE;
  201.         LPTSTR szName = NULL;
  202.         DWORD dwData;
  203.         __try
  204.         {
  205.                 // Print Serial Number.
  206.                 _tprintf(_T("Serial Number: "));
  207.                 dwData = pCertContext->pCertInfo->SerialNumber.cbData;
  208.                 for (DWORD n = 0; n < dwData; n++)
  209.                 {
  210.                         _tprintf(_T("%02x "),
  211.                                 pCertContext->pCertInfo->SerialNumber.pbData[dwData - (n + 1)]);
  212.                 }

  213.                 _tprintf(_T("\n"));
  214.                 // Get Issuer name size.
  215.                 if (!(dwData = CertGetNameString(pCertContext,
  216.                         CERT_NAME_SIMPLE_DISPLAY_TYPE,
  217.                         CERT_NAME_ISSUER_FLAG,
  218.                         NULL,
  219.                         NULL,
  220.                         0)))
  221.                 {
  222.                         _tprintf(_T("CertGetNameString failed.\n"));
  223.                         __leave;
  224.                 }
  225.                 // Allocate memory for Issuer name.
  226.                 szName = (LPTSTR)LocalAlloc(LPTR, dwData * sizeof(TCHAR));
  227.                 if (!szName)
  228.                 {
  229.                         _tprintf(_T("Unable to allocate memory for issuer name.\n"));
  230.                         __leave;
  231.                 }
  232.                 // Get Issuer name.
  233.                 if (!(CertGetNameString(pCertContext,
  234.                         CERT_NAME_SIMPLE_DISPLAY_TYPE,
  235.                         CERT_NAME_ISSUER_FLAG,
  236.                         NULL,
  237.                         szName,
  238.                         dwData //名字大小
  239.                 )))
  240.                 {
  241.                         _tprintf(_T("CertGetNameString failed.\n"));
  242.                         __leave;
  243.                 }
  244.                 // print Issuer name.
  245.                 _tprintf(_T("Issuer Name: %s\n"), szName);
  246.                 LocalFree(szName);
  247.                 szName = NULL;
  248.                 // Get Subject name size. 获取颁发者名字大小
  249.                 if (!(dwData = CertGetNameString(pCertContext,
  250.                         CERT_NAME_SIMPLE_DISPLAY_TYPE,
  251.                         0,
  252.                         NULL,
  253.                         NULL,
  254.                         0)))
  255.                 {
  256.                         _tprintf(_T("CertGetNameString failed.\n"));
  257.                         __leave;
  258.                 }
  259.                 // Allocate memory for subject name. 获取颁发者名字
  260.                 szName = (LPTSTR)LocalAlloc(LPTR, dwData * sizeof(TCHAR));
  261.                 if (!szName)
  262.                 {
  263.                         _tprintf(_T("Unable to allocate memory for subject name.\n"));
  264.                         __leave;
  265.                 }
  266.                 // Get subject name. 获取颁发者名字
  267.                 if (!(CertGetNameString(pCertContext,
  268.                         CERT_NAME_SIMPLE_DISPLAY_TYPE,
  269.                         0,
  270.                         NULL,
  271.                         szName,
  272.                         dwData //名字大小
  273.                 )))
  274.                 {
  275.                         _tprintf(_T("CertGetNameString failed.\n"));
  276.                         __leave;
  277.                 }
  278.                 // Print Subject Name.
  279.                 _tprintf(_T("Subject Name: %s\n"), szName);
  280.                 fReturn = TRUE;
  281.         }
  282.         __finally
  283.         {
  284.                 if (szName != NULL) LocalFree(szName);
  285.         }
  286.         return fReturn;
  287. }

  288. LPWSTR AllocateAndCopyWideString(LPCWSTR inputString)
  289. {
  290.         LPWSTR outputString = NULL;
  291.         outputString = (LPWSTR)LocalAlloc(LPTR,
  292.                 (wcslen(inputString) + 1) * sizeof(WCHAR));
  293.         if (outputString != NULL)
  294.         {
  295.                 lstrcpyW(outputString, inputString);
  296.         }
  297.         return outputString;
  298. }
  299. BOOL GetProgAndPublisherInfo(PCMSG_SIGNER_INFO pSignerInfo,
  300.         PSPROG_PUBLISHERINFO Info)
  301. {
  302.         BOOL fReturn = FALSE;
  303.         PSPC_SP_OPUS_INFO OpusInfo = NULL;
  304.         DWORD dwData;
  305.         BOOL fResult;
  306.         __try
  307.         {
  308.                 // Loop through authenticated attributes and find
  309.                 // SPC_SP_OPUS_INFO_OBJID OID.
  310.                 for (DWORD n = 0; n < pSignerInfo->AuthAttrs.cAttr; n++)
  311.                 {
  312.                         if (lstrcmpA(SPC_SP_OPUS_INFO_OBJID,
  313.                                 pSignerInfo->AuthAttrs.rgAttr[n].pszObjId) == 0)
  314.                         {
  315.                                 // Get Size of SPC_SP_OPUS_INFO structure.
  316.                                 fResult = CryptDecodeObject(ENCODING,
  317.                                         SPC_SP_OPUS_INFO_OBJID,
  318.                                         pSignerInfo->AuthAttrs.rgAttr[n].rgValue[0].pbData,
  319.                                         pSignerInfo->AuthAttrs.rgAttr[n].rgValue[0].cbData,
  320.                                         0,
  321.                                         NULL,
  322.                                         &dwData);
  323.                                 if (!fResult)
  324.                                 {
  325.                                         _tprintf(_T("CryptDecodeObject failed with %x\n"),
  326.                                                 GetLastError());
  327.                                         __leave;
  328.                                 }
  329.                                 // Allocate memory for SPC_SP_OPUS_INFO structure.
  330.                                 OpusInfo = (PSPC_SP_OPUS_INFO)LocalAlloc(LPTR, dwData);
  331.                                 if (!OpusInfo)
  332.                                 {
  333.                                         _tprintf(_T("Unable to allocate memory for Publisher Info.\n"));
  334.                                         __leave;
  335.                                 }
  336.                                 // Decode and get SPC_SP_OPUS_INFO structure.
  337.                                 fResult = CryptDecodeObject(ENCODING,
  338.                                         SPC_SP_OPUS_INFO_OBJID,
  339.                                         pSignerInfo->AuthAttrs.rgAttr[n].rgValue[0].pbData,
  340.                                         pSignerInfo->AuthAttrs.rgAttr[n].rgValue[0].cbData,
  341.                                         0,
  342.                                         OpusInfo,
  343.                                         &dwData);
  344.                                 if (!fResult)
  345.                                 {
  346.                                         _tprintf(_T("CryptDecodeObject failed with %x\n"),
  347.                                                 GetLastError());
  348.                                         __leave;
  349.                                 }
  350.                                 // Fill in Program Name if present.
  351.                                 if (OpusInfo->pwszProgramName)
  352.                                 {
  353.                                         Info->lpszProgramName =
  354.                                                 AllocateAndCopyWideString(OpusInfo->pwszProgramName);
  355.                                 }
  356.                                 else
  357.                                         Info->lpszProgramName = NULL;
  358.                                 // Fill in Publisher Information if present.
  359.                                 if (OpusInfo->pPublisherInfo)
  360.                                 {
  361.                                         switch (OpusInfo->pPublisherInfo->dwLinkChoice)
  362.                                         {
  363.                                         case SPC_URL_LINK_CHOICE:
  364.                                                 Info->lpszPublisherLink =
  365.                                                         AllocateAndCopyWideString(OpusInfo->pPublisherInfo->pwszUrl);
  366.                                                 break;
  367.                                         case SPC_FILE_LINK_CHOICE:
  368.                                                 Info->lpszPublisherLink =
  369.                                                         AllocateAndCopyWideString(OpusInfo->pPublisherInfo->pwszFile);
  370.                                                 break;
  371.                                         default:
  372.                                                 Info->lpszPublisherLink = NULL;
  373.                                                 break;
  374.                                         }
  375.                                 }
  376.                                 else
  377.                                 {
  378.                                         Info->lpszPublisherLink = NULL;
  379.                                 }
  380.                                 // Fill in More Info if present.
  381.                                 if (OpusInfo->pMoreInfo)
  382.                                 {
  383.                                         switch (OpusInfo->pMoreInfo->dwLinkChoice)
  384.                                         {
  385.                                         case SPC_URL_LINK_CHOICE:
  386.                                                 Info->lpszMoreInfoLink =
  387.                                                         AllocateAndCopyWideString(OpusInfo->pMoreInfo->pwszUrl);
  388.                                                 break;
  389.                                         case SPC_FILE_LINK_CHOICE:
  390.                                                 Info->lpszMoreInfoLink =
  391.                                                         AllocateAndCopyWideString(OpusInfo->pMoreInfo->pwszFile);
  392.                                                 break;
  393.                                         default:
  394.                                                 Info->lpszMoreInfoLink = NULL;
  395.                                                 break;
  396.                                         }
  397.                                 }
  398.                                 else
  399.                                 {
  400.                                         Info->lpszMoreInfoLink = NULL;
  401.                                 }
  402.                                 fReturn = TRUE;
  403.                                 break; // Break from for loop.
  404.                         } // lstrcmp SPC_SP_OPUS_INFO_OBJID
  405.                 } // for
  406.         }
  407.         __finally
  408.         {
  409.                 if (OpusInfo != NULL) LocalFree(OpusInfo);
  410.         }
  411.         return fReturn;
  412. }
  413. BOOL GetDateOfTimeStamp(PCMSG_SIGNER_INFO pSignerInfo, SYSTEMTIME* st)
  414. {
  415.         BOOL fResult;
  416.         FILETIME lft, ft;
  417.         DWORD dwData;
  418.         BOOL fReturn = FALSE;
  419.         // Loop through authenticated attributes and find
  420.         // szOID_RSA_signingTime OID.
  421.         for (DWORD n = 0; n < pSignerInfo->AuthAttrs.cAttr; n++)
  422.         {
  423.                 if (lstrcmpA(szOID_RSA_signingTime,
  424.                         pSignerInfo->AuthAttrs.rgAttr[n].pszObjId) == 0)
  425.                 {
  426.                         // Decode and get FILETIME structure.
  427.                         dwData = sizeof(ft);
  428.                         fResult = CryptDecodeObject(ENCODING,
  429.                                 szOID_RSA_signingTime,
  430.                                 pSignerInfo->AuthAttrs.rgAttr[n].rgValue[0].pbData,
  431.                                 pSignerInfo->AuthAttrs.rgAttr[n].rgValue[0].cbData,
  432.                                 0,
  433.                                 (PVOID)&ft,
  434.                                 &dwData);
  435.                         if (!fResult)
  436.                         {
  437.                                 _tprintf(_T("CryptDecodeObject failed with %x\n"),
  438.                                         GetLastError());
  439.                                 break;
  440.                         }
  441.                         // Convert to local time.
  442.                         FileTimeToLocalFileTime(&ft, &lft);
  443.                         FileTimeToSystemTime(&lft, st);
  444.                         fReturn = TRUE;
  445.                         break; // Break from for loop.
  446.                 } //lstrcmp szOID_RSA_signingTime
  447.         } // for
  448.         return fReturn;
  449. }
  450. BOOL GetTimeStampSignerInfo(PCMSG_SIGNER_INFO pSignerInfo, PCMSG_SIGNER_INFO* pCounterSignerInfo)
  451. {
  452.         PCCERT_CONTEXT pCertContext = NULL;
  453.         BOOL fReturn = FALSE;
  454.         BOOL fResult;
  455.         DWORD dwSize;
  456.         __try
  457.         {
  458.                 *pCounterSignerInfo = NULL;
  459.                 // Loop through unathenticated attributes for
  460.                 // szOID_RSA_counterSign OID.
  461.                 for (DWORD n = 0; n < pSignerInfo->UnauthAttrs.cAttr; n++)
  462.                 {
  463.                         if (lstrcmpA(pSignerInfo->UnauthAttrs.rgAttr[n].pszObjId,
  464.                                 szOID_RSA_counterSign) == 0)
  465.                         {
  466.                                 // Get size of CMSG_SIGNER_INFO structure.
  467.                                 fResult = CryptDecodeObject(ENCODING,
  468.                                         PKCS7_SIGNER_INFO,
  469.                                         pSignerInfo->UnauthAttrs.rgAttr[n].rgValue[0].pbData,
  470.                                         pSignerInfo->UnauthAttrs.rgAttr[n].rgValue[0].cbData,
  471.                                         0,
  472.                                         NULL,
  473.                                         &dwSize);
  474.                                 if (!fResult)
  475.                                 {
  476.                                         _tprintf(_T("CryptDecodeObject failed with %x\n"),
  477.                                                 GetLastError());
  478.                                         __leave;
  479.                                 }
  480.                                 // Allocate memory for CMSG_SIGNER_INFO.
  481.                                 *pCounterSignerInfo = (PCMSG_SIGNER_INFO)LocalAlloc(LPTR, dwSize);
  482.                                 if (!*pCounterSignerInfo)
  483.                                 {
  484.                                         _tprintf(_T("Unable to allocate memory for timestamp info.\n"));
  485.                                         __leave;
  486.                                 }
  487.                                 // Decode and get CMSG_SIGNER_INFO structure
  488.                                 // for timestamp certificate.
  489.                                 fResult = CryptDecodeObject(ENCODING,
  490.                                         PKCS7_SIGNER_INFO,
  491.                                         pSignerInfo->UnauthAttrs.rgAttr[n].rgValue[0].pbData,
  492.                                         pSignerInfo->UnauthAttrs.rgAttr[n].rgValue[0].cbData,
  493.                                         0,
  494.                                         (PVOID)*pCounterSignerInfo,
  495.                                         &dwSize);
  496.                                 if (!fResult)
  497.                                 {
  498.                                         _tprintf(_T("CryptDecodeObject failed with %x\n"),
  499.                                                 GetLastError());
  500.                                         __leave;
  501.                                 }
  502.                                 fReturn = TRUE;
  503.                                 break; // Break from for loop.
  504.                         }
  505.                 }
  506.         }
  507.         __finally
  508.         {
  509.                 // Clean up.
  510.                 if (pCertContext != NULL) CertFreeCertificateContext(pCertContext);
  511.         }
  512.         return fReturn;
  513. }
复制代码


游戏安全课程 学员办理咨询联系QQ150330575 手机 139 9636 2600  免费课程 在 www.bilibili.com 搜 郁金香灬老师
您需要登录后才可以回帖 登录 | 立即注册

QQ咨询

QQ|Archiver|手机版|小黑屋|郁金香游戏技术

GMT+8, 2024-4-27 18:04 , Processed in 0.089579 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表