免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6329 | 回复: 3
打印 上一主题 下一主题

用shc加密shell或其他脚本解析程序. [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-01-19 11:33 |只看该作者 |倒序浏览
随便google或百度一下shc, 都可以找到很多下载的.
安装很简单, 可能有些系统要改一下makefile, 当然需要gcc或cc.
编译的结果就是根据里面的C程序产生一个shc的可执行程序. 随便拷到系统目录可以用了.

shc的原理是先将你的程序转化成标准C程序, 在一些老Unix上可能要改点东西.
我在Solaris, HP-unix下用发现有不同, 可能有些系统头文件中没有PTRACE_ATTACH只要把shc翻译后的C程序里面的这个变量替换为0就行, 然后在手动gcc yourscrip就行.

shc会对脚本进行分析产生对应sh命令的一些宏地址, 然后再将C程序编译成二进制的可执行文件. 相信很少人能看懂哪些宏代表什么, 个人认为是比较使用的, 当然原程序要保存好.

不过每次改sh程序后必须执行shc来产生执行文件.

下面是我的一个例子.


  1. [zec#/usr1/zec/bin]: cat a.sh
  2. #!/bin/ksh

  3. echo mypid is $$
  4. echo today is $(date)
  5. awk 'BEGIN{print "hello world"}'
  6. [zec#/usr1/zec/bin]: sh a.sh
  7. mypid is 22316
  8. today is Thu, Jan 19, 2006 11:10:12 AM
  9. hello world
  10. [zec#/usr1/zec/bin]: shc -r -f a.sh
  11. ####这个时侯shc程序会翻译成a.sh.x.c并通过gcc/cc编译成一个a.sh.x.exe
  12. ####如果你的gcc/cc编译这个.x.c文件出错, 一般都是ptrace(PTRACE_ATTACH, pid, 0, 0);
  13. ####这个函数的问题, 把PTRACE_ATTACH替换为0, 有些系统可能参数不够要增加一两个0在后面. ptrace(PTRACE_ATTACH, pid, 0, 0, 0, 0);
  14. ####然后在手动gcc a.sh.x.c就okay拉.
  15. [zec#/usr1/zec/bin]: cat a.sh.x.c
  16. #if 0
  17.         shc Version 3.8, Generic Script Compiler
  18.         Copyright (c) 1994-2004 Francisco Rosales <frosal@fi.upm.es>

  19.         shc -r -f a.sh
  20. #endif

  21. static  char data [] =
  22. #define      msg2_z     19
  23. #define      msg2       ((&data[3]))
  24.         "\022\224\115\321\302\126\016\131\044\366\370\166\032\000\074\245"
  25.         "\272\074\254\146\111\107\116\360\254\035"
  26. #define      chk2_z     19
  27. #define      chk2       ((&data[30]))
  28.         "\172\251\262\030\340\356\056\211\175\251\350\135\154\104\267\131"
  29.         "\244\352\327\226\001\075\157"
  30. #define      tst1_z     22
  31. #define      tst1       ((&data[51]))
  32.         "\106\053\237\301\117\002\102\011\161\027\035\275\005\243\137\262"
  33.         "\214\226\373\214\056\322\026\157\256\052"
  34. #define      rlax_z     1
  35. #define      rlax       ((&data[75]))
  36.         "\353"
  37. #define      pswd_z     256
  38. #define      pswd       ((&data[80]))
  39.         "\164\020\143\353\054\376\061\236\304\260\250\223\171\305\137\040"
  40.         "\127\003\177\252\343\374\262\024\174\213\111\043\135\062\254\173"
  41.         "\004\354\254\031\144\042\350\164\257\302\271\346\025\276\341\272"
  42.         "\141\044\160\201\236\047\040\347\325\014\230\303\015\217\312\246"
  43.         "\217\234\370\202\217\344\176\310\121\103\363\220\365\153\263\050"
  44.         "\317\314\205\234\257\071\121\211\331\074\235\360\035\212\314\062"
  45.         "\335\241\327\011\106\364\176\217\312\257\227\052\345\177\203\225"
  46.         "\123\123\204\242\333\242\331\022\223\020\146\032\010\020\355\345"
  47.         "\013\116\135\265\200\175\206\377\016\170\177\003\017\041\027\133"
  48.         "\174\011\316\361\102\105\137\321\346\170\232\164\245\211\173\267"
  49.         "\025\362\067\074\204\043\114\174\357\007\034\045\371\367\113\024"
  50.         "\254\072\131\235\134\206\251\107\213\131\165\336\036\321\341\334"
  51.         "\313\316\251\020\370\002\240\253\015\302\172\116\166\024\023\220"
  52.         "\037\042\267\165\371\071\220\063\024\222\316\364\375\072\236\276"
  53.         "\324\040\215\136\326\257\147\142\356\071\367\273\017\175\324\354"
  54.         "\377\015\074\261\006\213\357\372\021\011\031\230\114\000\256\036"
  55.         "\126\005\160\062\027\110\132\073\023\065\337\347\024\100\076\334"
  56.         "\024\175\351\146\156\204\010\036\103\263\125\116\026\305\144\203"
  57.         "\345\106\336\204\231\341\370\141\103\335\147\334"
  58. #define      text_z     85
  59. #define      text       ((&data[396]))
  60.         "\320\257\311\257\354\040\054\221\135\302\144\131\266\336\255\207"
  61.         "\264\032\105\126\377\305\055\346\377\106\026\173\360\303\362\023"
  62.         "\247\127\017\322\371\266\044\114\240\357\012\147\200\306\045\151"
  63.         "\177\277\017\163\351\072\215\145\253\323\151\017\061\064\262\371"
  64.         "\230\075\160\045\322\053\044\265\100\271\245\256\124\335\352\225"
  65.         "\010\312\222\364\075\162\146\046\354\272\230\202\317\344\063\143"
  66.         "\310\057\355\110\047\351\102\063\156\071\100\026\154\025\157\124"
  67.         "\264"
  68. #define      tst2_z     19
  69. #define      tst2       ((&data[492]))
  70.         "\011\276\306\311\166\013\114\152\236\045\325\204\027\073\142\150"
  71.         "\264\326\076\305\333\335"
  72. #define      opts_z     1
  73. #define      opts       ((&data[511]))
  74.         "\173"
  75. #define      msg1_z     42
  76. #define      msg1       ((&data[519]))
  77.         "\145\357\240\071\350\302\117\050\302\364\151\223\330\317\227\013"
  78.         "\236\046\324\344\313\014\242\220\070\167\111\373\121\043\106\150"
  79.         "\227\260\307\173\102\174\146\200\101\120\124\120\341\316\352\155"
  80.         "\313\370"
  81. #define      date_z     8
  82. #define      date       ((double*)(&data[568]))
  83.         "\377\307\035\122\152\177\041\123\046\211\230\370\201\253"
  84. #define      chk1_z     22
  85. #define      chk1       ((&data[577]))
  86.         "\345\344\060\210\022\034\313\256\166\302\327\254\335\201\222\327"
  87.         "\372\153\354\127\373\061\157"
  88. #define      shll_z     9
  89. #define      shll       ((&data[600]))
  90.         "\344\111\003\267\211\255\311\112\344\011\032\211"
  91. #define      inlo_z     3
  92. #define      inlo       ((&data[611]))
  93.         "\256\137\345"
  94. #define      xecc_z     15
  95. #define      xecc       ((&data[615]))
  96.         "\164\150\003\041\116\313\136\322\031\060\373\003\365\175\061\302"
  97.         "\073\225\356"
  98. #define      lsto_z     1
  99. #define      lsto       ((&data[633]))
  100.         "\332"/* End of data[] */;
  101. #define      hide_z     4096
  102. #define DEBUGEXEC       0       /* Define as 1 to debug execvp calls */
  103. #define TRACEABLE       0       /* Define as 1 to enable ptrace the executable */

  104. /* rtc.c */

  105. /* 这里是程序的主要函数和处理, 这里放不下,  看下一回复 */
  106. [zec#/usr1/zec/bin]: file a.sh.exe
  107. a.sh.exe:       PA-RISC1.1 shared executable dynamically linked -not stripped
  108. [zec#/usr1/zec/bin]: a.sh.exe
  109. mypid is 22399
  110. today is Thu, Jan 19, 2006 11:13:35 AM
  111. hello world
  112. [zec#/usr1/zec/bin]:
复制代码


相信大家一看就大概知道他的原理, 不管什么sh或perl或expect, 第一行必须是:
类似#!/bin/sh这样的说明, 如#!/bin/perl或#!/usr/local/bin/expect
不同的程序编译后的.c文件只有在前面哪个宏定义不同. 相信很少人能看到这些宏对应的sh命令地址吧.

论坛徽章:
0
2 [报告]
发表于 2006-01-19 11:35 |只看该作者
接上面/* rtc.c */后面.
  1. #include <sys/stat.h>
  2. #include <sys/types.h>

  3. #include <errno.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <time.h>
  8. #include <unistd.h>

  9. /**
  10. * 'Alleged RC4' Source Code picked up from the news.
  11. * From: allen@gateway.grumman.com (John L. Allen)
  12. * Newsgroups: comp.lang.c
  13. * Subject: Shrink this C code for fame and fun
  14. * Date: 21 May 1996 10:49:37 -0400
  15. */

  16. static unsigned char stte[256], indx, jndx, kndx;

  17. /*
  18. * Reset arc4 stte.
  19. */
  20. void stte_0(void)
  21. {
  22.         indx = jndx = kndx = 0;
  23.         do {
  24.                 stte[indx] = indx;
  25.         } while (++indx);
  26. }

  27. /*
  28. * Set key. Can be used more than once.
  29. */
  30. void key(void * str, int len)
  31. {
  32.         unsigned char tmp, * ptr = (unsigned char *)str;
  33.         while (len > 0) {
  34.                 do {
  35.                         tmp = stte[indx];
  36.                         kndx += tmp;
  37.                         kndx += ptr[(int)indx % len];
  38.                         stte[indx] = stte[kndx];
  39.                         stte[kndx] = tmp;
  40.                 } while (++indx);
  41.                 ptr += 256;
  42.                 len -= 256;
  43.         }
  44. }

  45. /*
  46. * Crypt data.
  47. */
  48. void arc4(void * str, int len)
  49. {
  50.         unsigned char tmp, * ptr = (unsigned char *)str;
  51.         while (len > 0) {
  52.                 indx++;
  53.                 tmp = stte[indx];
  54.                 jndx += tmp;
  55.                 stte[indx] = stte[jndx];
  56.                 stte[jndx] = tmp;
  57.                 tmp += stte[indx];
  58.                 *ptr ^= stte[tmp];
  59.                 ptr++;
  60.                 len--;
  61.         }
  62. }

  63. /*
  64. * Key with file invariants.
  65. */
  66. int key_with_file(char * file)
  67. {
  68.         struct stat statf[1];
  69.         struct stat control[1];

  70.         if (stat(file, statf) < 0)
  71.                 return -1;

  72.         /* Turn on stable fields */
  73.         memset(control, 0, sizeof(control));
  74.         control->st_ino = statf->st_ino;
  75.         control->st_dev = statf->st_dev;
  76.         control->st_rdev = statf->st_rdev;
  77.         control->st_uid = statf->st_uid;
  78.         control->st_gid = statf->st_gid;
  79.         control->st_size = statf->st_size;
  80.         control->st_mtime = statf->st_mtime;
  81.         control->st_ctime = statf->st_ctime;
  82.         key((char *)control, sizeof(control));
  83.         return 0;
  84. }

  85. #if DEBUGEXEC
  86. void debugexec(char * shll, int argc, char ** argv)
  87. {
  88.         int i;
  89. fprintf(stderr, "shll=%s\n", shll ? shll : "<null>");
  90.         fprintf(stderr, "argc=%d\n", argc);
  91.         if (!argv) {
  92.                 fprintf(stderr, "argv=<null>\n");
  93.         } else {
  94.                 for (i = 0; i <= argc ; i++)
  95.                         fprintf(stderr, "argv[%d]=%.60s\n", i, argv[i] ? argv[i] : "<null>");
  96.         }
  97. }
  98. #endif /* DEBUGEXEC */

  99. void rmarg(char ** argv, char * arg)
  100. {
  101.         for (; argv && *argv && *argv != arg; argv++);
  102.         for (; argv && *argv; argv++)
  103.                 *argv = argv[1];
  104. }

  105. int chkenv(int argc)
  106. {
  107.         char buff[512];
  108.         unsigned mask, m;
  109.         int l, a, c;
  110.         char * string;
  111.         extern char ** environ;

  112.         mask  = (unsigned)chkenv;
  113.         mask ^= (unsigned)getpid() * ~mask;
  114.         sprintf(buff, "x%x", mask);
  115.         string = getenv(buff);
  116. #if DEBUGEXEC
  117.         fprintf(stderr, "getenv(%s)=%s\n", buff, string ? string : "<null>");
  118. #endif
  119.         l = strlen(buff);
  120.         if (!string) {
  121.                 /* 1st */
  122.                 sprintf(&buff[l], "=%u %d", mask, argc);
  123.                 putenv(strdup(buff));
  124.                 return 0;
  125.         }
  126.         c = sscanf(string, "%u %d%c", &m, &a, buff);
  127.         if (c == 2 && m == mask) {
  128.                 /* 3rd */
  129.                 rmarg(environ, &string[-l - 1]);
  130.                 return 1 + (argc - a);
  131.         }
  132.         return -1;
  133. }

  134. #if !TRACEABLE

  135. #define _LINUX_SOURCE_COMPAT
  136. #include <sys/ptrace.h>
  137. #include <sys/types.h>
  138. #include <sys/wait.h>
  139. #include <fcntl.h>
  140. #include <signal.h>
  141. #include <stdio.h>
  142. #include <unistd.h>

  143. void untraceable(char * argv0)
  144. {
  145.         char proc[80];
  146.         int pid, mine;

  147.         switch(pid = vfork()) {
  148.         case  0:
  149.                 pid = getppid();
  150.                 /* For problematic SunOS ptrace */
  151.                 sprintf(proc, "/proc/%d/as", (int)pid);
  152.                 close(0);
  153.                 mine = !open(proc, O_RDWR|O_EXCL);
  154.                 if (!mine && errno != EBUSY)
  155.                         mine = !ptrace(0, pid, 0, 0, 0);
  156.                 if (mine) {
  157.                         kill(pid, SIGCONT);
  158.                 } else {
  159.                         perror(argv0);
  160.                         kill(pid, SIGKILL);
  161.                 }
  162.                 _exit(mine);
  163.         case -1:
  164.                 break;
  165.         default:
  166.                 if (pid == waitpid(pid, 0, 0))
  167.                         return;
  168.         }
  169.         perror(argv0);
  170.         _exit(1);
  171. }
  172. #endif /* !TRACEABLE */

  173. char * xsh(int argc, char ** argv)
  174. {
  175.         char * scrpt;
  176.         int ret, i, j;
  177.         char ** varg;

  178.         stte_0();
  179.          key(pswd, pswd_z);
  180.         arc4(msg1, msg1_z);
  181.         arc4(date, date_z);
  182.         if (date[0] && date[0]<time(NULL))
  183.                 return msg1;
  184.         arc4(shll, shll_z);
  185.         arc4(inlo, inlo_z);
  186.         arc4(xecc, xecc_z);
  187.         arc4(lsto, lsto_z);
  188.         arc4(tst1, tst1_z);
  189.          key(tst1, tst1_z);
  190.         arc4(chk1, chk1_z);
  191.         if ((chk1_z != tst1_z) || memcmp(tst1, chk1, tst1_z))
  192.                 return tst1;
  193.         ret = chkenv(argc);
  194.         arc4(msg2, msg2_z);
  195.         if (ret < 0)
  196.                 return msg2;
  197.         varg = (char **)calloc(argc + 10, sizeof(char *));
  198.         if (!varg)
  199.                 return 0;
  200.         if (ret) {
  201.                 arc4(rlax, rlax_z);
  202.                 if (!rlax[0] && key_with_file(shll))
  203.                         return shll;
  204.                 arc4(opts, opts_z);
  205.                 arc4(text, text_z);
  206.                 arc4(tst2, tst2_z);
  207.                  key(tst2, tst2_z);
  208.                 arc4(chk2, chk2_z);
  209.                 if ((chk2_z != tst2_z) || memcmp(tst2, chk2, tst2_z))
  210.                         return tst2;
  211.                 if (text_z < hide_z) {
  212.                         /* Prepend spaces til a hide_z script size. */
  213.                         scrpt = malloc(hide_z);
  214.                         if (!scrpt)
  215.                                 return 0;
  216.                         memset(scrpt, (int) ' ', hide_z);
  217.                         memcpy(&scrpt[hide_z - text_z], text, text_z);
  218.                 } else {
  219.                         scrpt = text;   /* Script text */
  220.                 }
  221.         } else {                        /* Reexecute */
  222.                 if (*xecc) {
  223.                         scrpt = malloc(512);
  224.                         if (!scrpt)
  225.                                 return 0;
  226.                         sprintf(scrpt, xecc, argv[0]);
  227.                 } else {
  228.                         scrpt = argv[0];
  229.                 }
  230.         }
  231.         j = 0;
  232.         varg[j++] = argv[0];            /* My own name at execution */
  233.         if (ret && *opts)
  234.                 varg[j++] = opts;       /* Options on 1st line of code */
  235.         if (*inlo)
  236.                 varg[j++] = inlo;       /* Option introducing inline code */
  237.         varg[j++] = scrpt;              /* The script itself */
  238.         if (*lsto)
  239.                 varg[j++] = lsto;       /* Option meaning last option */
  240.         i = (ret > 1) ? ret : 0;        /* Args numbering correction */
  241.         while (i < argc)
  242.                 varg[j++] = argv[i++];  /* Main run-time arguments */
  243.         varg[j] = 0;                    /* NULL terminated array */
  244. #if DEBUGEXEC
  245.         debugexec(shll, j, varg);
  246. #endif
  247.         execvp(shll, varg);
  248.         return shll;
  249. }

  250. int main(int argc, char ** argv)
  251. {
  252. #if DEBUGEXEC
  253.         debugexec("main", argc, argv);
  254. #endif
  255. #if !TRACEABLE
  256.         untraceable(argv[0]);
  257. #endif
  258.         argv[1] = xsh(argc, argv);
  259.         fprintf(stderr, "%s%s%s: %s\n", argv[0],
  260.                 errno ? ": " : "",
  261.                 errno ? strerror(errno) : "",
  262.                 argv[1] ? argv[1] : "<null>"
  263.         );
  264.         return 1;
  265. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2006-01-24 21:53 |只看该作者
加密后系统经常出现死进程
test     32639  0.0  0.0     0    0 ?        Z    19:45   0:00 [kill.sh.x <defunct>]
test     32637  0.0  0.0  1372  260 ?        T    19:45   0:00 /home/test/kill.sh.x
test       330  0.0  0.0     0    0 ?        Z    19:50   0:00 [kill.sh.x <defunct>]
test       329  0.0  0.0  1372  260 ?        T    19:50   0:00 /home/test/kill.sh.x
test      2211  0.0  0.0     0    0 ?        Z    20:40   0:00 [kill.sh.x <defunct>]
test      2208  0.0  0.0  1380  260 ?        T    20:40   0:00 /home/test/kill.sh.x
test      4311  0.0  0.0     0    0 ?        Z    21:35   0:00 [kill.sh.x <defunct>]
test      4310  0.0  0.0  1364  260 ?        T    21:35   0:00 /home/test/kill.sh.x
test      4861  0.0  0.0     0    0 ?        Z    21:50   0:00 [kill.sh.x <defunct>]
test      4858  0.0  0.0  1372  260 ?        T    21:50   0:00 /home/test/kill.sh.x

不知楼主碰到过没有

论坛徽章:
0
4 [报告]
发表于 2010-05-27 11:44 |只看该作者
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP