找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 3085|回复: 2

【原创】自动生成系统关键api的hash函数

[复制链接]

307

主题

228

回帖

7345

积分

用户组: 真·技术宅

UID
2
精华
76
威望
291 点
宅币
5595 个
贡献
253 次
宅之契约
0 份
在线时间
949 小时
注册时间
2014-1-25
发表于 2014-7-24 13:58:53 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
    《0day安全 软件漏洞分析技术》第二版一书中,有一节动态定位API地址的shellcode,一年前看到该篇文章时,第一次发现hash函数的妙用,不过该文章的缺憾是需要对用到的api,自己分析出不产生碰撞的hash函数,如果api数比较多,用文中使用的hash函数就可能碰撞,因此有必要设计出一种自动适应算法自动生成散列函数
    文中是这么说的:shellcode最重要放到缓冲区,为了让shellcode更加通用能被大多数缓冲区容纳,总希望shellcode尽可能短。因此在函数名导出表中搜索函数名时,一般不会用"MessageBoxA"这么长的字符串直接比较。通常情况下我们会对所需的api函数进行hash运算,在搜索导出表是对当前遇到的函数进行同样的hash,这样只要比对hash所得的摘要就能判定是不是我们所需的api了,虽然这种搜索算法需要引入额外的hash算法但是可以节省出存储函数名字符串的代码。
    现在考虑,如果想构造出hash表达式可以对任意api都生成不碰撞摘要,应该怎么做呢?基于算法复杂度和计算时间的考虑,应该采用多元一次表达式,设函数名字符串数组为name[64],则hash应为k0*name[0]+k1*name[1]+k2*name[2]+....+k63*name[63];现在就是选择一种算法计算出k0~k63。巧妇难为无米之炊,所以我们先要生成api列表,如何获取这个列表呢?列表中应该有哪些api呢?我们仅考虑系统关键函数kernel32.dll user32.dll gdi32.dll ntdll.dll中的导出函数(如需其它函数,做法类似)。参照我之前的一篇文章:http://www.0xaa55.com/thread-312-1-1.html

1.首先保证dumpbin.exe在搜索路径中
2.cd c:\windows\system32
3.(for %i in (dir /s /b kernel32.dll gdi32.dll user32.dll ntdll.dll) do dumpbin -exports %i) >out.txt
4.findstr /X ".*[0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F].*[a-zA-Z_]$" out.txt > out1.txt
5.findstr /V "@  $ \. : characteristics" out1.txt > out2.txt
6.notepad++正则表达式  将out2.txt用^.*[0-9A-F]{8} ([a-zA-Z_0-9]+)$替换为($1)
7.用perl脚本排序,结果发现out2.txt共4700+个win7 api函数

  1. open RESULT,'out1.txt';
  2. my $hash=();
  3. foreach $key (<RESULT>)
  4. {
  5.         chomp $key;
  6.         $hash{$key}=length $key;
  7. }
  8. close RESULT;

  9. open RESULT,'>out5.txt';
  10. select RESULT;

  11. #for $i (sort keys %hash)按字母排序
  12. for $i (sort sort_by_len keys %hash)
  13. {
  14.         print $i,"\n";
  15. }
  16. close RESULT;

  17. sub sort_by_len
  18. {
  19.         if(length($a)<length($b)){-1}
  20.         elsif(length($a)>length($b)){1}
  21.         else{0}
  22. }
复制代码

现仅列出前20个:
A_SHAFinal
A_SHAInit
A_SHAUpdate
AbortDoc
AbortPath
ActivateActCtx
ActivateKeyboardLayout
AddAtomA
AddAtomW
AddClipboardFormatListener
AddConsoleAliasA
AddConsoleAliasW
AddFontMemResourceEx
AddFontResourceA
AddFontResourceExA
AddFontResourceExW
AddFontResourceTracking
AddFontResourceW
AddIntegrityLabelToBoundaryDescriptor
AddLocalAlternateComputerNameA
AddLocalAlternateComputerNameW
AddRefActCtx
AddSIDToBoundaryDescriptor
AddSecureMemoryCacheCallback
AdjustCalendarDate
AdjustWindowRect
AdjustWindowRectEx
AlignRects
AllocConsole
AllocateUserPhysicalPages
AllocateUserPhysicalPagesNuma
AllowForegroundActivation
AllowSetForegroundWindow
AlpcAdjustCompletionListConcurrencyCount
AlpcFreeCompletionListMessage
AlpcGetCompletionListLastMessageInformation
AlpcGetCompletionListMessageAttributes
AlpcGetHeaderSize
AlpcGetMessageAttribute
AlpcGetMessageFromCompletionList

统计出来的api最大长度为65,最小为3,win7有不到5000个API

下面开始编程:
    由于hash要消耗很大内存空间所以先将编译器栈区大小设置为0x400000以上的,我们会生成0x100000元素的大数组,利用率=5000/0x100000=4.7‰。
算稀疏矩阵了,碰撞率相对较低(PS:很多hash算法都是0x100000000的,自然碰撞率更低,我曾尝试过0xFFFF的数组,利用率=5000/65536=76.3‰,但是计算太慢了,不知道会不会有解)

设coef为hash系数矩阵,apiname为函数名,hash=apiname[0]*coef[0]+apiname[1]*coef[1]+....

我在解决这个问题时遇到的问题有:
1.如何设计算法在短时间内(<1min)计算出系数矩阵,且生成的hash摘要尽可能短
2.遇到2个api名碰撞以后,如何调整系数矩阵coef使这2个api不碰撞

下面的代码解决了2个问题:


  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. using namespace std;
  8. typedef unsigned char ubyte;
  9. typedef unsigned short uword;

  10. int main(int argc, char* argv[])
  11. {
  12.         string filename;//存储api字符串的txt
  13.         vector<string> apinamearray;//存储所有api字符串
  14.         bool find=false;//是否找到正确hash函数
  15.         int maxsize=0;//获取最长api长度
  16.         bool flag[0x100000];//hash表
  17.         int index[0x100000];//用于存储已经占用hash表的api在apinamearray的索引
  18.         //0x100000意味着生成的hash值在0~0x100000,占用20位,比"AA"的长度还小,很节省空间

  19.         //下面2各变量用于定位当前搜索进程
  20.         char curchar='A';
  21.         int curindx=0;

  22.         cout<<"input filename"<<endl;
  23.         cin>>filename;
  24.         ifstream file(filename.c_str());
  25.         string temp;
  26.         while(getline(file,temp))
  27.         {
  28.                 if(temp.size() > maxsize)
  29.                         maxsize=temp.size();//寻找最长api以设置hash表达式系数数组长度
  30.                 apinamearray.push_back(temp);//添加所有api
  31.         }

  32.         time_t begin=time(NULL);

  33.         //hash=apiname[0]*coef[0]+apiname[1]*coef[1]+....
  34.         ubyte* coef=new ubyte[maxsize];//使coef元素不会超过65535
  35.         for(int i=0;i<maxsize;i++)
  36.         {
  37.                 coef[i]=0;//初始化系数
  38.         }

  39.         while(!find)
  40.         {
  41.                 memset(flag,0,sizeof(flag));
  42.                 bool duplicate=false;
  43.                 vector<string>::const_iterator itor=apinamearray.begin();
  44.                 int size=0;
  45.                 int indx=0;
  46.                 while(itor != apinamearray.end())
  47.                 {
  48.                         temp=*itor;
  49.                         int sum=1;

  50.                         //核心算法
  51.                         for(int i=0;i<temp.size();i++)
  52.                         {
  53.                                 sum += temp.at(i)*coef[i];
  54.                                 sum &= 0xFFFFF;
  55.                         }


  56.                         if(flag[sum])//如果已存在,则调整hash表达式coef系数
  57.                         {
  58.                                 string other=apinamearray.at(index[sum]);
  59. //                                printf("%s duplicate with %s\n",temp.c_str(),other.c_str());//输出碰撞函数,便于调整算法
  60.                                 int i;

  61. /*                                {//显示当前系数矩阵
  62.                                         for(i=0;i<maxsize;i++)
  63.                                         {
  64.                                                 if(i%16 == 0)
  65.                                                         printf("\n");
  66.                                                 printf("%d ",coef[i]);
  67.                                         }
  68.                                 }*/
  69.                                
  70.                                 //遇到碰撞调整系数
  71.                                 int size1=temp.size(),size2=other.size();

  72.                                 //核心算法:对这2个字符串选最后一位不同字符进行修改,这样就可以使下次计算出的结果不同
  73.                                 //PS:我先前的方法是吧所有不同位存储起来,随机取一位进行设置,不过效果没有这个好
  74.                                 if(size1<size2)
  75.                                 {
  76.                                         coef[size2-1]++;
  77.                                 }
  78.                                 else if(size1>size2)
  79.                                 {
  80.                                         coef[size1-1]++;
  81.                                 }
  82.                                 else
  83.                                 {
  84.                                         for(i=size1-1;i>=0;i--)
  85.                                         {
  86.                                                 if(temp.at(i) != other.at(i))
  87.                                                 {
  88.                                                         coef[i]++;
  89.                                                         break;
  90.                                                 }
  91.                                         }
  92.                                 }

  93.                                 duplicate=true;

  94.                                 if(indx>curindx)
  95.                                 {//显示进度
  96.                                         curindx=indx;
  97.                                         printf("%d:%d\n",indx,apinamearray.size());
  98.                                 }

  99.                                 if(temp.at(0) > curchar)
  100.                                 {//显示进度
  101.                                         curchar=temp.at(0);
  102.                                         printf("%c %d:%d\n",curchar,indx,apinamearray.size());
  103.                                 }

  104.                                 break;//已经碰撞了,所以修正系数后进行下次检测
  105.                         }
  106.                         else
  107.                         {
  108.                                 flag[sum]=true;
  109.                                 index[sum]=indx;
  110.                         }

  111.                         itor++;
  112.                         indx++;
  113.                 }

  114.                 if(duplicate)
  115.                 {
  116.                 }
  117.                 else
  118.                 {
  119.                         printf("最佳hash系数:");
  120.                         for(int i=0;i<maxsize;i++)
  121.                         {
  122.                                 if(i%30 == 0)
  123.                                         printf("\n");
  124.                                 printf("%d ",coef[i]);
  125.                         }
  126.                         printf("\n");
  127.                         find=true;//找到hash表达式解
  128.                 }
  129.         }

  130.         printf("\ntime used:%d\n",time(NULL)-begin);

  131.         delete []coef;
  132.        
  133.         return 0;
  134. }

复制代码



input filename
out.txt
1:4718
3:4718
7:4718
11:4718
15:4718
17:4718
20:4718
26:4718
30:4718
32:4718
48:4718
54:4718
58:4718
65:4718
B 65:4718
82:4718
97:4718
98:4718
110:4718
112:4718
117:4718
125:4718
C 125:4718
131:4718
141:4718
218:4718
219:4718
226:4718
234:4718
249:4718
282:4718
288:4718
298:4718
299:4718
306:4718
314:4718
320:4718
322:4718
324:4718
347:4718
381:4718
387:4718
388:4718
449:4718
D 449:4718
459:4718
483:4718
484:4718
486:4718
489:4718
515:4718
525:4718
584:4718
598:4718
618:4718
629:4718
697:4718
E 697:4718
727:4718
736:4718
824:4718
829:4718
857:4718
F 857:4718
880:4718
913:4718
944:4718
G 944:4718
967:4718
995:4718
1086:4718
1089:4718
1209:4718
1216:4718
1274:4718
1286:4718
1299:4718
1339:4718
1479:4718
1501:4718
1647:4718
1730:4718
I 1730:4718
1779:4718
1866:4718
L 1866:4718
1899:4718
1938:4718
2079:4718
N 2079:4718
2096:4718
2187:4718
2251:4718
2321:4718
2412:4718
2468:4718
O 2468:4718
2529:4718
P 2529:4718
2676:4718
R 2676:4718
2699:4718
2711:4718
2907:4718
2940:4718
2982:4718
3007:4718
3036:4718
3055:4718
3083:4718
3128:4718
3140:4718
3157:4718
3242:4718
3398:4718
3443:4718
3447:4718
3498:4718
S 3498:4718
3593:4718
3684:4718
3750:4718
3813:4718
4249:4718
Z 4249:4718
4330:4718
4390:4718
4511:4718
_ 4511:4718
4525:4718
4532:4718
4545:4718
4588:4718
4607:4718
i 4607:4718
4627:4718
l 4627:4718
4658:4718
s 4658:4718
4667:4718
4673:4718
4695:4718
w 4695:4718
4703:4718
4710:4718
最佳hash系数:
1 1 1 1 2 30 94 117 223 364 466 567 884 751 1087 1244 883 1210 1084 1245 1355 1937 2059 1924 2089 2381 2596 1498 1949 1516
1207 937 724 593 497 271 539 245 241 230 82 148 177 64 83 0 22 0 0 49

time used:22
Press any key to continue

确定了hash表达式系数以后,用hash表达式计算出需要的api的hash值存于shellcode中即可,可以发现这样做很节省空间
回复

使用道具 举报

307

主题

228

回帖

7345

积分

用户组: 真·技术宅

UID
2
精华
76
威望
291 点
宅币
5595 个
贡献
253 次
宅之契约
0 份
在线时间
949 小时
注册时间
2014-1-25
 楼主| 发表于 2014-7-24 16:45:47 | 显示全部楼层
系数矩阵coef取8位存储空间而摘要flag取20位时得到:

最佳hash系数:byte hash[50]=
1 1 1 1 2 30 94 117 223 144 78 181 229 235 183 179 112 129 210 209 233 153 137 112 125 149 196 197 187 252
115 40 168 252 58 189 253 201 228 250 192 202 164 101 137 0 44 0 0 119
time used:1065

对任意api生成的摘要为0~0x100000的不重复数
如果你有n个api,则shellcode占用空间为:20n+400 位
而直接存储api字符串,即使假设平均长度10字节(如果统计一下,会发现平均为18),也需要80n 位
当然节省了空间是用时间换取的,计算hash会稍微损耗一点时间,不过影响不大。


系数矩阵coef取16位存储空间而摘要flag取18位时得到:
最佳hash系数:
1 1 1 1 2 30 94 117 223 364 466 567 1150 1486 3358 7907 11009 28953 45305 30959
9274 9249 37291 47423 61016 51947 25556 31961 24339 13642
45878 13531 33854 5953 60503 37374 30621 7421 43669 11549 21584 3134 36705 19501
24594 0 12359 0 0 41187

time used:6324

对任意api生成的摘要为0~0x40000的不重复数
如果你有n个api,则shellcode占用空间为:18n+800 位
而直接存储api字符串,假设平均长度10字节,同样需要80n 位,如果n较大,这种方式更合适


下面是txt文件:
out.txt (94.59 KB, 下载次数: 2)
回复 赞! 靠!

使用道具 举报

307

主题

228

回帖

7345

积分

用户组: 真·技术宅

UID
2
精华
76
威望
291 点
宅币
5595 个
贡献
253 次
宅之契约
0 份
在线时间
949 小时
注册时间
2014-1-25
 楼主| 发表于 2014-7-26 01:47:59 | 显示全部楼层
下面是这个问题的回溯解法,这种方式适合文本按长度排列的情况:

  1. // hashcalc.cpp : Defines the entry point for the console application.
  2. //


  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6. #include <vector>
  7. #include <stdlib.h>
  8. #include <time.h>
  9. using namespace std;
  10. typedef unsigned char ubyte;
  11. vector<string> apinamearray;//存储所有api字符串
  12. bool flag[0x10000];//hash表
  13. int index[0x10000];//用于存储已经占用hash表的api在apinamearray的索引

  14. bool valid(ubyte* coef,int curdepth)
  15. {
  16.         memset(flag,0,sizeof(flag));
  17.         vector<string>::const_iterator itor=apinamearray.begin();
  18.         int indx=0;
  19.         while(itor != apinamearray.end() && curdepth+1 >= (*itor).size())
  20.         {
  21.                 int sum=0;
  22.                 //核心算法
  23.                 int size=(*itor).size();
  24.                 for(int i=0;i<size;i++)
  25.                 {
  26.                         sum += (*itor).at(i)*coef[i];
  27.                         sum &= 0xFFFF;
  28.                 }       
  29.                 if(flag[sum])
  30.                 {
  31.                         string other=apinamearray.at(index[sum]);
  32. //                        printf("%s duplicate with %s\n",(*itor).c_str(),other.c_str());
  33.                         return false;
  34.                 }
  35.                 else
  36.                 {
  37.                         flag[sum]=true;
  38.                         index[sum]=indx;
  39.                 }
  40.                 itor++;
  41.                 indx++;
  42.         }
  43.         return true;
  44. }

  45. void showhash(ubyte* coef,int maxsize)
  46. {
  47.         vector<string>::const_iterator itor=apinamearray.begin();
  48.         while(itor != apinamearray.end())
  49.         {
  50.                 int sum=0;
  51.                 //核心算法
  52.                 int size=(*itor).size();
  53.                 for(int i=0;i<size;i++)
  54.                 {
  55.                         sum += (*itor).at(i)*coef[i];
  56.                         sum &= 0xFFFF;
  57.                 }

  58.                 printf("%s->%d ",(*itor).c_str(),sum);
  59.                 itor++;
  60.         }
  61. }

  62. int main(int argc, char* argv[])
  63. {
  64.         int maxcoef=64;//参数界限
  65.         string filename;//存储api字符串的txt

  66.         int minsize=INT_MAX;//获取最短api长度
  67.         int maxsize=0;//获取最长api长度

  68.         //下面变量用于定位当前搜索进程
  69.         int curmaxdepth=0;//搜索到的最大深度
  70.         time_t begin=time(NULL);

  71.         cout<<"input filename"<<endl;
  72.         cin>>filename;
  73.         ifstream file(filename.c_str());
  74.         string temp;
  75.         while(getline(file,temp))
  76.         {
  77.                 if(temp.size() > maxsize)
  78.                         maxsize=temp.size();//寻找最长api以设置hash表达式系数数组长度
  79.                 if(temp.size() < minsize)
  80.                         minsize=temp.size();//寻找最短api长度
  81.                 apinamearray.push_back(temp);
  82.         }

  83.         ubyte* coef=new ubyte[maxsize+1];//使coef元素不会超过65535

  84.         for(int i=0;i<maxsize;i++)
  85.         {
  86.                 coef[i]=0;//初始化系数
  87.         }

  88.         int layer=0;
  89.         while(layer >= 0)
  90.         {
  91.                 if(curmaxdepth<layer)
  92.                 {
  93.                         curmaxdepth=layer;
  94.                         printf("max:%d:%d\n",layer,maxsize);
  95.                 }
  96.                 else if(layer < 2)
  97.                 {
  98.                         printf("%d:%d/%d\n",layer,coef[layer],maxcoef);
  99.                 }

  100.                 while(coef[layer]<maxcoef && !valid(coef,layer))
  101.                 {
  102.                         coef[layer]++;
  103.                 }
  104.                 if(coef[layer]<maxcoef)
  105.                 {
  106.                         if(layer == maxsize)
  107.                         {
  108.                                 //显示当前系数矩阵
  109.                                 for(i=0;i<maxsize;i++)
  110.                                 {
  111.                                         if(i%16 == 0)
  112.                                                 printf("\n");
  113.                                         printf("%d ",coef[i]);
  114.                                         showhash(coef,maxsize);
  115.                                 }
  116.                                 printf("\n");
  117.                                 layer--;
  118.                                 coef[layer]++;
  119.                         }
  120.                         else
  121.                         {
  122.                                 layer++;
  123.                                 coef[layer]=0;
  124.                         }
  125.                 }
  126.                 else
  127.                 {
  128.                         layer--;
  129.                         if(layer>=0)
  130.                                 coef[layer]++;
  131.                 }
  132.         }


  133.         printf("\ntime used:%d\n",time(NULL)-begin);
  134.        

  135.         delete []coef;
  136.        
  137.         return 0;
  138. }

复制代码
回复 赞! 靠!

使用道具 举报

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-4-29 15:08 , Processed in 0.072111 second(s), 34 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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