[程序源码] C51反篡改源码下载(菜农独创双向CRC初值碰撞反篡改)

[复制链接]
 楼主| hotpower 发表于 2023-10-26 13:22 | 显示全部楼层 |阅读模式
点击下载工程源码:https://bbs.21ic.com/icview-3335982-1-1.html
  1. #include <stdio.h>
  2. #include <REG52.H>
  3. #ifdef MONITOR51
  4. char code reserve [3] _at_ 0x23;
  5. #endif

  6. typedef unsigned char uint8_t;
  7. typedef unsigned int uint16_t;
  8. typedef unsigned long uint32_t;



  9. code uint32_t CRC32Array[8] = {//菜农HotPower@163.com
  10. //全局数组,在Flash里
  11.         0x55555555,//头数据,为注册机提供搜索位置,改写即篡改非零
  12.         0xAAAAAAAA,//头数据,为注册机提供搜索位置,改写即篡改非零
  13.         0x22345678,//头数据,为注册机提供搜索位置,改写即篡改非零
  14.         0x20231025,//多项式可以是随机数,例如今天是2023.10.25
  15.         0,//Flash基地址
  16.         0x0FD7,//HEX长度
  17.         0xF99741E0,//CRC32初值1,正向验证
  18.         0x0C38B4F3 //CRC32初值2,反向验证
  19. };
  20. uint32_t CRC32Val(uint32_t input, uint32_t poly, uint32_t init, uint32_t xorout)
  21. {//菜农HotPower@163.com
  22.         uint32_t ch;
  23.         int i;
  24.         input ^= init;
  25.         if ((poly & 1) > 0)
  26.         {
  27.                 for (i = 0; i < 32; i++)
  28.                 {
  29.                         ch = input & 0x80000000;
  30.                         input <<= 1;
  31.                         if (ch > 0)
  32.                         {
  33.                                 input ^= poly;
  34.                         }
  35.                 }
  36.         }
  37.         else
  38.         {
  39.                 for (i = 0; i < 32; i++)
  40.                 {
  41.                         ch = input & 1;
  42.                         input >>= 1;
  43.                         if (ch > 0)
  44.                         {
  45.                                 input ^= poly | 0x80000000;
  46.                         }
  47.                 }
  48.         }
  49.         if ((poly & 0x80000001) == 0)
  50.         {
  51.                 input ^= poly ^ 0x80000001;
  52.         }
  53.         input ^= xorout;
  54.         return input;
  55. }

  56. uint32_t CRC32Valx(uint32_t output, uint32_t poly, uint32_t init, uint32_t xorout)
  57. {//菜农HotPower@163.com
  58.         int i;
  59.         output ^= xorout;
  60.         if ((poly & 0x80000001) == 0)
  61.         {
  62.                 output ^= poly ^ 0x80000001;
  63.         }
  64.         if ((poly & 1) > 0)
  65.         {
  66.                 for (i = 0; i < 32; i++)
  67.                 {
  68.                         if ((output & 1) > 0)
  69.                         {
  70.                                 output ^= poly;
  71.                                 output >>= 1;
  72.                                 output |= 0x80000000;
  73.                         }
  74.                         else
  75.                         {
  76.                                 output >>= 1;
  77.                         }
  78.                 }
  79.         }
  80.         else
  81.         {
  82.                 for (i = 0; i < 32; i++)
  83.                 {
  84.                         if ((output & 0x80000000) > 0)
  85.                         {
  86.                                 output ^= poly;
  87.                                 output <<= 1;
  88.                                 output |= 1;
  89.                         }
  90.                         else
  91.                         {
  92.                                 output <<= 1;
  93.                         }
  94.                 }
  95.         }
  96.         output ^= init;
  97.         return output;
  98. }

  99. void SetCRC32Init(uint32_t* init1, uint32_t* init2)
  100. {//菜农HotPower@163.com
  101.         uint16_t len = (CRC32Array[5] - ((int)(&CRC32Array[6]) & 3)) / 4;
  102.         uint32_t CRC32Init;
  103.         uint32_t crc32;
  104.         uint32_t crcpoly = CRC32Array[3];
  105.         int pos1 = (int)(&CRC32Array[6] - (uint16_t)CRC32Array[4]) / 4;
  106.         int pos2 = (int)(&CRC32Array[7] - (uint16_t)CRC32Array[4]) / 4;
  107.         int i;
  108.         unsigned long volatile code *Flash = (unsigned long volatile code *)((int)(&CRC32Array[6]) & 3);
  109.         CRC32Init = 0;
  110.         for(i = len - 1;i >= 0;i--)
  111.         {
  112.                 crc32 = Flash[(uint16_t)CRC32Array[4] + i];
  113.                 if(!(i == pos1 || i == pos2))
  114.                 {
  115.                         CRC32Init = CRC32Valx(CRC32Init, crcpoly, crc32, 0);
  116.                 }
  117.                 else
  118.                 {
  119.                         *init1 = CRC32Init;
  120.                 }
  121.         }
  122.         *init1 = CRC32Init;
  123.         CRC32Init = 0;
  124.         for(i = 0;i < len;i++)
  125.         {
  126.                 crc32 = Flash[(uint16_t)CRC32Array[4] + i];
  127.                 if(!(i == pos2))
  128.                 {
  129.                         CRC32Init = CRC32Valx(CRC32Init, crcpoly, crc32, 0);
  130.                 }
  131.                 else
  132.                 {
  133.                         *init2 = CRC32Init;
  134.                 }
  135.         }
  136.         *init2 = CRC32Init;        
  137. }

  138. uint32_t GetCRC32Init12()
  139. {//菜农HotPower@163.com
  140.         uint16_t len = (CRC32Array[5] - ((int)(&CRC32Array[6]) & 3)) / 4;
  141.         uint32_t CRC32Init;
  142.         uint32_t crc32;
  143.         uint32_t crcpoly = CRC32Array[3];
  144.         int pos1 = (int)(&CRC32Array[6] - (uint16_t)CRC32Array[4]) / 4;
  145.         int pos2 = (int)(&CRC32Array[7] - (uint16_t)CRC32Array[4]) / 4;
  146.         int i;
  147.         unsigned long volatile code *Flash = (unsigned long volatile code *)((int)(&CRC32Array[6]) & 3);
  148.         CRC32Init = CRC32Array[6];
  149.         for(i = 0;i < len;i++)
  150.         {
  151.                 crc32 = Flash[(uint16_t)CRC32Array[4] + i];
  152.                 if(!(i == pos1 || i == pos2))
  153.                 {
  154.                         CRC32Init = CRC32Val(crc32, crcpoly, CRC32Init, 0);
  155.                 }
  156.         }
  157.         CRC32Init ^= CRC32Array[7];
  158.         for(i = len - 1;i >= 0;i--)
  159.         {
  160.                 crc32 = Flash[(uint16_t)CRC32Array[4] + i];
  161.                 if(!(i == pos2))
  162.                 {
  163.                         CRC32Init = CRC32Val(crc32, crcpoly, CRC32Init, 0);
  164.                 }
  165.         }
  166.         return CRC32Init;
  167. }

  168. uint32_t GetCRC32Init1()
  169. {//菜农HotPower@163.com
  170.         uint16_t len = (CRC32Array[5] - ((int)(&CRC32Array[6]) & 3)) / 4;
  171.         uint32_t CRC32Init;
  172.         uint32_t crc32;
  173.         uint32_t crcpoly = CRC32Array[3];
  174.         int pos1 = (int)(&CRC32Array[6] - (uint16_t)CRC32Array[4]) / 4;
  175.         int pos2 = (int)(&CRC32Array[7] - (uint16_t)CRC32Array[4]) / 4;
  176.         int i;
  177.         unsigned long volatile code *Flash = (unsigned long volatile code *)((int)(&CRC32Array[6]) & 3);
  178.         CRC32Init = CRC32Array[6];
  179.         for(i = 0;i < len;i++)
  180.         {
  181.                 crc32 = Flash[(uint16_t)CRC32Array[4] + i];
  182.                 if(!(i == pos1 || i == pos2))
  183.                 {
  184.                         CRC32Init = CRC32Val(crc32, crcpoly, CRC32Init, 0);
  185.                 }
  186.         }
  187.         return CRC32Init;
  188. }

  189. uint32_t GetCRC32Init2()
  190. {//菜农HotPower@163.com
  191.         uint16_t len = (CRC32Array[5] - ((int)(&CRC32Array[6]) & 3)) / 4;
  192.         uint32_t CRC32Init;
  193.         uint32_t crc32;
  194.         uint32_t crcpoly = CRC32Array[3];
  195.         int pos2 = (int)(&CRC32Array[7] - (uint16_t)CRC32Array[4]) / 4;
  196.         int i;
  197.         unsigned long volatile code *Flash = (unsigned long volatile code *)((int)(&CRC32Array[6]) & 3);
  198.         CRC32Init = CRC32Array[7];
  199.         for(i = len - 1;i >= 0;i--)
  200.         {
  201.                 crc32 = Flash[(uint16_t)CRC32Array[4] + i];
  202.                 if(!(i == pos2))
  203.                 {
  204.                         CRC32Init = CRC32Val(crc32, crcpoly, CRC32Init, 0);
  205.                 }
  206.         }
  207.         return CRC32Init;
  208. }

  209. void main(void)
  210. {//菜农HotPower@163.com
  211.         uint32_t init1 = 0;//填入CRC32Array[6]的CRC32初值1
  212.         uint32_t init2;//填入CRC32Array[7]的CRC32初值2
  213.         volatile uint32_t init12;
  214. #ifndef MONITOR51
  215.     SCON  = 0x50;
  216.     TMOD |= 0x20;
  217.     TH1   = 221;
  218.     TR1   = 1;
  219.     TI    = 1;
  220. #endif
  221.         SetCRC32Init(&init1, &init2);//结果填入CRC32初值1(第1次),CRC32初值2(第2次),需要编译运行两次!!!
  222.         init12 = GetCRC32Init12();//被篡改非零!!!可以篡改CRC32Array[0],CRC32Array[1],CRC32Array[2]
  223.         init1 = GetCRC32Init1();//被篡改非零!!!可以篡改CRC32Array[0],CRC32Array[1],CRC32Array[2]
  224.         init2 = GetCRC32Init2();//被篡改非零!!!可以篡改CRC32Array[0],CRC32Array[1],CRC32Array[2]
  225.         printf("init12 = 0x%08LX\n", init12);
  226.         printf("init1 = 0x%08LX\n", init1);
  227.         printf("init2 = 0x%08LX\n", init2);
  228.   while (1) {
  229.     printf ("Hello World\n");   /* Print "Hello World" */
  230.   }
  231.         
  232. }
































zwsam 发表于 2023-12-14 09:36 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:[url=http://www.21ic.com/tools/HotWC3_V1.23.html]

1460

主题

21619

帖子

508

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