天下网吧 >> 网吧天地 >> 网吧技术 >> 网吧系统 >> 正文

用tcpdump分析协议后用C语言自己编写一个BBS发贴机器人

2008-4-8chinaunix佚名

  大家可能经常上各种论坛发贴,但每次都要打开论坛、登录、输入贴子主题、贴子内容,然后点提交才可以完成。
  如果你发贴比较少,可能不算什么,但如果你经常发,在很多网站发贴,可不容易哦,发贴可能花你好多时间。
  鉴于这种苦恼,我自己想了一个简单方法,那就是分析一下网络协议,找到每次发贴内容变化的部分,再用C语言来写一个小程序,以后每次只要写好主题和贴子内容,运行那个小程序就可以完成发贴了。
  当然,可以把程序的配置文件做齐全一点,就可以实现一次输入同时向N个论坛发贴了。呵,肯定有人想到了把这个程序设成定时运行的,那么就可以每隔一定时间往某个版发一次了。各位版主可要想好防止策略啊,我不能保证大家都正常使用我的代码。

1、tcpdump
  首先,如果要实现机器自动发贴,你得人工分析一下要发贴的论坛,这里得用一个工具tcpdump。
  tcpdump是个tcp流分析工具,通常我用来调试网络故障,因为从这上面比较容易看到网络通讯过程,可以大致分析到通讯两端谁出了问题。
  基本用法是:
  tcpdump -X -s0 host x.y.z.c port p
  这里x.y.z.c是你要跟踪的主机的IP地址,p是端口号。
  一个网站的IP地址比较容易获得,一种方法是通过DNS查询,另一种简单方法就是你打开浏览器,只打开那一个网站,同时用netstat -anp|grep ESTABLISHED命令就可以看到对方的IP了,通常端口是80的就是了。

  但上面这种命令格式并不很方便,比如我对www.163.com跟踪结果如下:
引用:
tcpdump -X -s0 host 220.181.28.42
Password:
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
21:13:14.585191 IP 172.16.100.218.37028 > 220.181.28.42.www: S 823660970:823660970(0) win 5840 <mss 1460,sackOK,timestamp 11131654 0,nop,wscale 2>
        0x0000:  4500 003c caac 4000 4006 6645 ac10 64da  E..<..@.@.fE..d.
        0x0010:  dcb5 1c2a 90a4 0050 3118 11aa 0000 0000  ...*...P1.......
        0x0020:  a002 16d0 7804 0000 0204 05b4 0402 080a  ....x...........
        0x0030:  00a9 db06 0000 0000 0103 0302            ............
21:13:15.042840 IP 220.181.28.42.www > 172.16.100.218.37028: S 2097205944:2097205944(0) ack 823660971 win 5792 <mss 1460,sackOK,timestamp 2176039398 11131654,nop,wscale 7>
        0x0000:  4500 003c 0000 4000 3406 3cf2 dcb5 1c2a  E..<..@.4.<....*
        0x0010:  ac10 64da 0050 90a4 7d00 d2b8 3118 11ab  ..d..P..}...1...
        0x0020:  a012 16a0 ecca 0000 0204 05b4 0402 080a  ................
        0x0030:  81b3 b9e6 00a9 db06 0103 0307            ............
21:13:15.042887 IP 172.16.100.218.37028 > 220.181.28.42.www: . ack 1 win 1460 <nop,nop,timestamp 11131769 2176039398>
        0x0000:  4500 0034 caad 4000 4006 664c ac10 64da  E..4..@.@.fL..d.
        0x0010:  dcb5 1c2a 90a4 0050 3118 11ab 7d00 d2b9  ...*...P1...}...
        0x0020:  8010 05b4 2c10 0000 0101 080a 00a9 db79  ....,..........y
        0x0030:  81b3 b9e6                                ....
21:13:15.043037 IP 172.16.100.218.37028 > 220.181.28.42.www: P 1:700(699) ack 1 win 1460 <nop,nop,timestamp 11131769 2176039398>
        0x0000:  4500 02ef caae 4000 4006 6390 ac10 64da  E.....@.@.c...d.
        0x0010:  dcb5 1c2a 90a4 0050 3118 11ab 7d00 d2b9  ...*...P1...}...
        0x0020:  8018 05b4 e97c 0000 0101 080a 00a9 db79  .....|.........y
        0x0030:  81b3 b9e6 4745 5420 2f20 4854 5450 2f31  ....GET./.HTTP/1
        0x0040:  2e31 0d0a 486f 7374 3a20 7777 772e 3136  .1..Host:.www.16
        0x0050:  332e 636f 6d0d 0a55 7365 722d 4167 656e  3.com..User-Agen
        0x0060:  743a 204d 6f7a 696c 6c61 2f35 2e30 2028  t:.Mozilla/5.0.(
        0x0070:  5831 313b 2055 3b20 4c69 6e75 7820 6936  X11;.U;.Linux.i6
        0x0080:  3836 3b20 7a68 2d43 4e3b 2072 763a 312e  86;.zh-CN;.rv:1.
        0x0090:  382e 302e 3429 2047 6563 6b6f 2f32 3030  8.0.4).Gecko/200
        0x00a0:  3630 3631 3120 5562 756e 7475 2f75 6e73  60611.Ubuntu/uns
        0x00b0:  7461 626c 6520 4669 7265 666f 782f 312e  table.Firefox/1.
        0x00c0:  352e 302e 340d 0a41 6363 6570 743a 2074  5.0.4..Accept:.t
        0x00d0:  6578 742f 786d 6c2c 6170 706c 6963 6174  ext/xml,applicat
        0x00e0:  696f 6e2f 786d 6c2c 6170 706c 6963 6174  ion/xml,applicat
        0x00f0:  696f 6e2f 7868 746d 6c2b 786d 6c2c 7465  ion/xhtml+xml,te
        0x0100:  7874 2f68 746d 6c3b 713d 302e 392c 7465  xt/html;q=0.9,te
        0x0110:  7874 2f70 6c61 696e 3b71 3d30 2e38 2c69  xt/plain;q=0.8,i
        0x0120:  6d61 6765 2f70 6e67 2c2a 2f2a 3b71 3d30  mage/png,*/*;q=0
        0x0130:  2e35 0d0a 4163 6365 7074 2d4c 616e 6775  .5..Accept-Langu
        0x0140:  6167 653a 207a 682d 636e 2c7a 683b 713d  age:.zh-cn,zh;q=
        0x0150:  302e 350d 0a41 6363 6570 742d 456e 636f  0.5..Accept-Enco
        0x0160:  6469 6e67 3a20 677a 6970 2c64 6566 6c61  ding:.gzip,defla
        0x0170:  7465 0d0a 4163 6365 7074 2d43 6861 7273  te..Accept-Chars
        0x0180:  6574 3a20 6762 3233 3132 2c75 7466 2d38  et:.gb2312,utf-8
        0x0190:  3b71 3d30 2e37 2c2a 3b71 3d30 2e37 0d0a  ;q=0.7,*;q=0.7..
        0x01a0:  4b65 6570 2d41 6c69 7665 3a20 3330 300d  Keep-Alive:.300.
        0x01b0:  0a43 6f6e 6e65 6374 696f 6e3a 206b 6565  .Connection:.kee
        0x01c0:  702d 616c 6976 650d 0a43 6f6f 6b69 653a  p-alive..Cookie:
        0x01d0:  2050 726f 7669 6e63 653d 3032 303b 204d  .Province=020;.M
        0x01e0:  4149 4c31 3633 5f53 534e 3d67 646c 6374  AIL163_SSN=gdlct
        0x01f0:  6573 743b 205f 6e74 6573 5f6e 6e73 6974  est;._ntes_nnsit
        0x0200:  3d7c 7777 777c 6375 6c74 7572 657c 6865  =|www|culture|he
        0x0210:  616c 7468 7c73 746f 636b 7c75 7273 7c74  alth|stock|urs|t
        0x0220:  7261 7665 6c7c 3b20 5f6e 7465 735f 6e76  ravel|;._ntes_nv
        0x0230:  746d 3d31 323b 204e 4554 4541 5345 5f53  tm=12;.NETEASE_S
        0x0240:  534e 3d67 646c 6366 6f72 6d65 3b20 7468  SN=gdlcforme;.th
        0x0250:  6561 6464 723d b9e3 b6ab 3b20 7171 6d61  eaddr=....;.qqma
        0x0260:  696c 3d67 646c 6366 6f72 6d65 4031 3633  il=gdlcforme@163
        0x0270:  2e63 6f6d 3b20 4e45 5445 4153 455f 4144  .com;.NETEASE_AD
        0x0280:  563d 3131 2632 353b 2043 6974 793d 3032  V=11&25;.City=02
        0x0290:  300d 0a49 662d 4d6f 6469 6669 6564 2d53  0..If-Modified-S
        0x02a0:  696e 6365 3a20 4d6f 6e2c 2032 3420 4a75  ince:.Mon,.24.Ju
        0x02b0:  6c20 3230 3036 2031 333a 3132 3a30 3020  l.2006.13:12:00.
        0x02c0:  474d 540d 0a49 662d 4e6f 6e65 2d4d 6174  GMT..If-None-Mat
        0x02d0:  6368 3a20 2231 6162 3265 312d 3133 6264  ch:."1ab2e1-13bd
        0x02e0:  382d 3937 3638 3038 3030 220d 0a0d 0a    8-97680800"....
21:13:15.556849 IP 220.181.28.42.www > 172.16.100.218.37028: . ack 700 win 57 <nop,nop,timestamp 2176039950 11131769>
        0x0000:  4500 0034 6eb9 4000 3406 ce40 dcb5 1c2a  E..4n.@.4..@...*
        0x0010:  ac10 64da 0050 90a4 7d00 d2b9 3118 1466  ..d..P..}...1..f
        0x0020:  8010 0039 2ca8 0000 0101 080a 81b3 bc0e  ...9,...........
        0x0030:  00a9 db79                                ...y
21:13:15.558577 IP 220.181.28.42.www > 172.16.100.218.37028: P 1:184(183) ack 700 win 57 <nop,nop,timestamp 2176039951 11131769>
        0x0000:  4500 00eb 6ebb 4000 3406 cd87 dcb5 1c2a  E...n.@.4......*
        0x0010:  ac10 64da 0050 90a4 7d00 d2b9 3118 1466  ..d..P..}...1..f
        0x0020:  8018 0039 38af 0000 0101 080a 81b3 bc0f  ...98...........
        0x0030:  00a9 db79 4854 5450 2f31 2e30 2033 3034  ...yHTTP/1.0.304
        0x0040:  204e 6f74 204d 6f64 6966 6965 640d 0a44  .Not.Modified..D
        0x0050:  6174 653a 204d 6f6e 2c20 3234 204a 756c  ate:.Mon,.24.Jul
        0x0060:  2032 3030 3620 3133 3a31 333a 3133 2047  .2006.13:13:13.G
        0x0070:  4d54 0d0a 5365 7276 6572 3a20 4170 6163  MT..Server:.Apac
        0x0080:  6865 2f32 2e30 2e35 3220 2855 6e69 7829  he/2.0.52.(Unix)
        0x0090:  0d0a 4554 6167 3a20 2231 6162 3265 312d  ..ETag:."1ab2e1-
        0x00a0:  3133 6264 382d 3937 3638 3038 3030 220d  13bd8-97680800".
        0x00b0:  0a58 2d43 6163 6865 3a20 4d49 5353 2066  .X-Cache:.MISS.f
        0x00c0:  726f 6d20 7777 772e 3136 332e 636f 6d0d  rom.www.163.com.
        0x00d0:  0a43 6f6e 6e65 6374 696f 6e3a 206b 6565  .Connection:.kee
        0x00e0:  702d 616c 6976 650d 0a0d 0a              p-alive....
21:13:15.558601 IP 172.16.100.218.37028 > 220.181.28.42.www: . ack 184 win 1728 <nop,nop,timestamp 11131898 2176039951>
        0x0000:  4500 0034 caaf 4000 4006 664a ac10 64da  E..4..@.@.fJ..d.
        0x0010:  dcb5 1c2a 90a4 0050 3118 1466 7d00 d370  ...*...P1..f}..p
        0x0020:  8010 06c0 24e8 0000 0101 080a 00a9 dbfa  ....$...........
        0x0030:  81b3 bc0f                                ....


2、tcpdump专门针对http协议的分析
tcpdump有个专门用来分析http协议的参数A,使用起来就是这样的:
引用:
tcpdump -A -s0 host 220.181.28.42
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
21:16:25.130387 IP 172.16.100.218.48508 > 220.181.28.42.www: S 1030084898:1030084898(0) win 5840 <mss 1460,sackOK,timestamp 11179287 0,nop,wscale 2>
E..<L5@.@.....d....*.|.P=e.".........T.........
............
21:16:25.705703 IP 220.181.28.42.www > 172.16.100.218.48508: S 915774756:915774756(0) ack 1030084899 win 5792 <mss 1460,sackOK,timestamp 2151451523 11179287,nop,wscale 7>
E..<..@.4.<....*..d..P.|6..$=e.#...............
.<..........
21:16:25.705759 IP 172.16.100.218.48508 > 220.181.28.42.www: . ack 1 win 1460 <nop,nop,timestamp 11179431 2151451523>
E..4L6@.@.....d....*.|.P=e.#6..%...........
.....<..
21:16:25.705895 IP 172.16.100.218.48508 > 220.181.28.42.www: P 1:700(699) ack 1 win 1460 <nop,nop,timestamp 11179431 2151451523>
E...L7@.@.....d....*.|.P=e.#6..%...........
.....<..GET / HTTP/1.1
Host: www.163.com
User-Agent: Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.8.0.4) Gecko/20060611 Ubuntu/unstable Firefox/1.5.0.4
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: Province=020; MAIL163_SSN=gdlctest; _ntes_nnsit=|www|culture|health|stock|urs|travel|; _ntes_nvtm=12; NETEASE_SSN=gdlcforme; theaddr=....; qqmail=gdlcforme@163.com; NETEASE_ADV=11&25; City=020
If-Modified-Since: Mon, 24 Jul 2006 13:12:00 GMT
If-None-Match: "1ab2e1-13bd8-97680800"


21:16:26.288904 IP 220.181.28.42.www > 172.16.100.218.48508: . ack 700 win 57 <nop,nop,timestamp 2151452108 11179431>
E..4..@.4.,_...*..d..P.|6..%=e.....9.......
.<......
21:16:26.299749 IP 220.181.28.42.www > 172.16.100.218.48508: P 1:1448(1447) ack 700 win 57 <nop,nop,timestamp 2151452115 11179431>
E.....@.4.&....*..d..P.|6..%=e.....93;.....
.<......HTTP/1.0 200 OK
Date: Mon, 24 Jul 2006 13:16:24 GMT
Server: Apache/2.0.52 (Unix)
Last-Modified: Mon, 24 Jul 2006 13:15:01 GMT
ETag: "1ab2e6-13bd8-a231df40"
Accept-Ranges: bytes
Vary: Accept-Encoding,User-Agent
Content-Encoding: gzip
Content-Length: 22079
Content-Type: text/html; charset=GB2312
X-Cache: MISS from www.163.com
Connection: keep-alive

.............w...0.9.+.q..bK3......CB....p_..X.."........7.....$&.&o.ny.7y....B .@.H.!...g.../....q............._~..D...}....3..-f......*..0....m..~..1.......p8Pb0466...z...p.......2..8,..w.]:G.....^./T......\m\.e\...
%V3..P.^A...D\.....].+q...`).e..n_....#LYZ.8...........:...p..g.Y.t.7.K.H./...u.....:..[D.}.H.......F.
.....r...G.."..:.....]......~.c.nOS....mT.../b...b.?.b..M...8....y6.v;..O...ryX..E.Hq..(A..+`..N...,y.$.J       ...........<6.q.X.(S..G..
u$......q.1D......J..
.WqO..LH....>.W........?.W........}..pCQ........_.........._.q.<.T..D.>+Y..2..%.EJ.H.E).J.
.g.l....U....|3F...{....Ao=@..N...v........ ..S..htp`H...5.. Pi..J(Hd".+..[..b..[.B...X..3..... 3#.&\...eq$..*P.=.,u.../."...b.u
w..)....S..
...zi..z.0.o.......~....l....".<.f<E....<.*.i74..#yp....a..Z...|..w1Oxy5.CZ......*.#. 3.b$......8...P.....4..^,.m@\.......xn2qs...9. ..+a$.
21:16:26.299775 IP 172.16.100.218.48508 > 220.181.28.42.www: . ack 1448 win 2184 <nop,nop,timestamp 11179580 2151452115>
E..4L8@.@.....d....*.|.P=e..6..............
...<.<..


这样就很清楚地看到你的主机和对方通信的过程如下:
首先,你的主机和对方TCP三次握手建立连接,
然后,你的主机向对方发了一个http请求:
引用:
GET / HTTP/1.1
Host: www.163.com
User-Agent: Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.8.0.4) Gecko/20060611 Ubuntu/unstable Firefox/1.5.0.4
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: Province=020; MAIL163_SSN=gdlctest; _ntes_nnsit=|www|culture|health|stock|urs|travel|; _ntes_nvtm=12; NETEASE_SSN=gdlcforme; theaddr=....; qqmail=gdlcforme@163.com; NETEASE_ADV=11&25; City=020
If-Modified-Since: Mon, 24 Jul 2006 13:12:00 GMT
If-None-Match: "1ab2e1-13bd8-97680800"


最后,对方回复了一个http请求:
引用:
HTTP/1.0 200 OK
Date: Mon, 24 Jul 2006 13:16:24 GMT
Server: Apache/2.0.52 (Unix)
Last-Modified: Mon, 24 Jul 2006 13:15:01 GMT
ETag: "1ab2e6-13bd8-a231df40"
Accept-Ranges: bytes
Vary: Accept-Encoding,User-Agent
Content-Encoding: gzip
Content-Length: 22079
Content-Type: text/html; charset=GB2312
X-Cache: MISS from www.163.com
Connection: keep-alive


后面就是22079字节的网页内容,这里省去没有显示啦。

3、自己用C语言来写一个这样的程序吧
现在假设我要去www.myweb.com/bbs这个论坛去发贴,发贴步骤是这样的:
Step.1、打开www.myweb.com/bbs论坛首页
Step.2、输入用户名和密码登录
Step.3、点击某个版
Step.4、点击发表新贴
Step.5、输入自己的内容,点击提交
C语言源代码如下:

/* ****** 用tcpdump分析协议后用C语言自己编写一个BBS发贴机器人 bbsrobot.c 开始 *********** */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/in.h>
#include <limits.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <ctype.h>

/* some global variable */
#ifdefine OS_LINUX
  #define USER_AGENT "Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.8.0.4) Gecko/20060611 Ubuntu/unstable Firefox/1.5.0.4"
#else
  #define USER_AGENT "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
#endif

#define SERVER_HOST "www.myweb.com"
#define CLIENT_ACCEPT "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
#define CLIENT_LANGUAGE "zh-cn,zh;q=0.5"
#define CLIENT_CHARSET "gb2312,utf-8;q=0.7,*;q=0.7"
#define BORDID 52

struct sockaddr_in server_addr;
int portnumber = 0, sockfd = 0, dsend = 0, totalsend = 0, nbytes = 0, reqn = 0, i = 0, j = 0, ret = 0;
struct hostent *host;
char request[1024] = "", buffer[1024] = "", httpheader[1024] = "";

/* some functions */
void ConnectWeb(void) { /* 连接网站 */
  /* create a socket descriptor */
  if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
  {
    fprintf(stderr,"Socket Error:%s\a\n",strerror(errno));
    exit(1);
  }

  /* bind address */
  bzero(&server_addr,sizeof(server_addr));
  server_addr.sin_family=AF_INET;
  server_addr.sin_port=htons(portnumber);
  server_addr.sin_addr=*((struct in_addr *)host->h_addr);

  /* connect to the server */
  if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
  {
    fprintf(stderr,"Connect Error:%s\a\n",strerror(errno));
    exit(1);
  }
}

/************关于本文档********************************************
*filename:用tcpdump分析协议后用C语言自己编写一个BBS发贴机器人
*purpose:自己编写的一个http协议网络客户端程序,可以用来定时往网站发贴
*wrote by: zhoulifa(zhoulifa@163.com) 周立发(http://zhoulifa.bokee.com)
Linux爱好者 Linux知识传播者 SOHO族 开发者 最擅长C语言编程
*date time:2006-07-24 21:00:00
*Note: 任何人可以任意复制代码并运用这些文档,当然包括你的商业用途
* 但请遵循GPL。
*Hope:希望越来越多的人贡献自己的力量,为科学技术发展出力
*********************************************************************/

void SendRequest(void) { /* send my http-request to web server */
  dsend = 0;totalsend = 0;
  nbytes=strlen(request);
  while(totalsend < nbytes) {
    dsend = write(sockfd, request + totalsend, nbytes - totalsend);
    if(dsend==-1)  {fprintf(stderr, "send error!%s\n", strerror(errno));exit(0);}
    totalsend+=dsend;
    printf("\nRequest.%d %d bytes send OK!\n", reqn - 1, totalsend);
  }
}

 

 

--------------------------------------------------------------------------------


void ReceiveResponse(void) { /* get response from web server */
  fd_set writefds;
  struct timeval tival;
  int retry = 0;

  i=0; j = 0;
__ReCeive:
  FD_ZERO(&writefds);
  tival.tv_sec = 10;
  tival.tv_usec = 0;
  if(sockfd > 0) FD_SET(sockfd, &writefds);
  else {fprintf(stderr, "\nError, socket is negative!\n"); exit(0);}

  ret = select(sockfd + 1, &writefds, NULL, NULL, &tival);
  if(ret ==0 ) {
    if(retry++ < 10) goto __ReCeive;
  }
  if(ret <= 0) {fprintf(stderr, "\nError while receiving!\n"); exit(0);}

  if(FD_ISSET(sockfd, &writefds))    {
    memset(buffer, 0, 1024);
    memset(httpheader, 0, 1024);

    /* receive data from web server */
    while((nbytes=read(sockfd,buffer,1))==1)
    {
      if(i < 4)  {
        if(buffer[0] == '\r' || buffer[0] == '\n')  i++;
        else i = 0;
        memcpy(httpheader + j, buffer, 1); j++;
      }
      else  {
//        fprintf(stdout, "%c", buffer[1]);/* print content on the screen */
        i++;
      }
    }
  }
}

void DoOnce() { /* send and receive */
  ConnectWeb(); /* connect to the web server */

  /* send a request */
  SendRequest();

  /* receive a response message from web server */
  ReceiveResponse();

  close(sockfd); /* because HTTP protocol do something one connection, so I can close it after receiving */
}

void DoneWithList() {
  printf("Request.%d is:\n%s", ++reqn, request);

  DoOnce();

  printf("\nThe following is the response header:\n%s\n", httpheader);
}

int main(int argc, char ** argv)
{
  char *tp1, *tp2, *tp3;
  char ltime[16]="1152109555207", rtime[16]="0", cnzz_eid[16]="20369173-";
  char bblastvisit[16] = "", bblastactivity[16] = "";
  char loginpost[256] ="vb_login_username=myusername&vb_login_password=mypassword&s=&do=login&forceredirect=1&vb_login_md5password=99b291003954c7637a30bc6c799785f0&vb_login_md5password_utf=99b291003954c7637a30bc6c799785f0";
  char PHPSESSID[1024] = "";
  char bblasturl[1024] = "";
  char bbsessionhash[1024] = "";
  char bbforum_view[1024] = "";
  char postmsg[1024] = "subject=贴子主题&WYSIWYG_HTML=贴子内容在这里&iconid=0&s=&do=postthread&posthash=b77747456dec01432fe5252c1ad9aa41&poststarttime=1153704013&parseurl=1&emailupdate=1&polloptions=4&sbutton=%CC%E1%BD%BB%D0%C2%D6%F7%CC%E2&f="; /* 在这里可以写发贴主题和发贴内容 */

    portnumber=80;

//  if((host=gethostbyname(host_addr))==NULL)/* get ip address by domain */
  if((host=gethostbyname(SERVER_HOST))==NULL)/* get ip address by domain */
  {
    fprintf(stderr,"Gethostname error, %s\n", strerror(errno));
    exit(1);
  }

  /* my first request */
  sprintf(request, "GET /bbs/ HTTP/1.1\r\nHost: %s\r\nUser-Agent: %s\r\nAccept: %s\r\nAccept-Language: %s\r\nAccept-Encoding: gzip,deflate\r\nAccept-Charset: %s\r\nKeep-Alive: 300\r\nConnection: keep-alive\r\nCookie: rtime=%s; cnzz_eid=%s; ltime=%s\r\n\r\n", SERVER_HOST, USER_AGENT, CLIENT_ACCEPT, CLIENT_LANGUAGE, CLIENT_CHARSET, rtime, cnzz_eid, ltime);

  DoneWithList();

  /* get some cookie */
  tp1 = strstr(httpheader, "bblastvisit=");
  tp2 = strchr(tp1, ';');
  memcpy(bblastvisit, tp1 + strlen("bblastvisit="), strlen(tp1) - strlen(tp2) - strlen("bblastvisit="));

  tp1 = strstr(httpheader, "bblastactivity=");
  tp2 = strchr(tp1, ';');
  memcpy(bblastactivity, tp1 + strlen("bblastactivity="), strlen(tp1) - strlen(tp2) - strlen("bblastactivity="));

  /* my second request */
  memset(request, 0, 1024);
  sprintf(request, "POST /bbs/login.php HTTP/1.1\r\nHost: %s\r\nUser-Agent: %s\r\nAccept: %s\r\nAccept-Language: %s\r\nAccept-Encoding: gzip,deflate\r\nAccept-Charset: %s\r\nKeep-Alive: 300\r\nConnection: keep-alive\r\nReferer: http://%s/bbs/\r\nCookie: ltime=%s; rtime=%s; cnzz_eid=%s; bblastvisit=%s; bblastactivity=%s\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\n%s", SERVER_HOST, USER_AGENT, CLIENT_ACCEPT, CLIENT_LANGUAGE, CLIENT_CHARSET, SERVER_HOST, ltime, rtime, cnzz_eid, bblastvisit, bblastactivity, strlen(loginpost), loginpost);

  DoneWithList();

  /* get some cookie */
  tp1 = strstr(httpheader, "bblastactivity=");
  tp2 = strchr(tp1, ';');
  memcpy(bblastactivity, tp1 + strlen("bblastactivity="), strlen(tp1) - strlen(tp2) - strlen("bblastactivity="));

  tp1 = strstr(httpheader, "PHPSESSID=");
  tp2 = strchr(tp1, ';');
  memcpy(PHPSESSID, tp1 + strlen("PHPSESSID="), strlen(tp1) - strlen(tp2) - strlen("PHPSESSID="));

  tp1 = strstr(httpheader, "bblasturl=");
  tp3 = strstr(tp1 + strlen("bblasturl="), "bblasturl=");
  if(tp3) {
    tp2 = strchr(tp3, ';');
    memcpy(bblasturl, tp3 + strlen("bblasturl="), strlen(tp3) - strlen(tp2) - strlen("bblasturl="));
  }
  else {
    tp2 = strchr(tp1, ';');
    memcpy(bblasturl, tp1 + strlen("bblasturl="), strlen(tp1) - strlen(tp2) - strlen("bblasturl="));
  }

  tp1 = strstr(httpheader, "bbsessionhash=");
  tp2 = strchr(tp1, ';');
  memcpy(bbsessionhash, tp1 + strlen("bbsessionhash="), strlen(tp1) - strlen(tp2) - strlen("bbsessionhash="));
//  printf("\n%s\t%s\t%\t%s\n", bblastactivity, PHPSESSID, bblasturl, bbsessionhash);

  /* my third request */
  memset(request, 0, 1024);
  sprintf(request, "GET /bbs/f%d.html HTTP/1.1\r\nHost: %s\r\nUser-Agent: %s\r\nAccept: %s\r\nAccept-Language: %s\r\nAccept-Encoding: gzip,deflate\r\nAccept-Charset: %s\r\nKeep-Alive: 300\r\nConnection: keep-alive\r\nReferer: http://%s/bbs/\r\nCookie: bblasturl=%s; ltime=%s; rtime=%s; cnzz_eid=%s; bblastvisit=%s; bblastactivity=%s; PHPSESSID=%s; bbsessionhash=%s\r\n\r\n", BORDID, SERVER_HOST, USER_AGENT, CLIENT_ACCEPT, CLIENT_LANGUAGE, CLIENT_CHARSET, SERVER_HOST, bblasturl, ltime, rtime, cnzz_eid, bblastvisit, bblastactivity, PHPSESSID, bbsessionhash);

  DoneWithList();

  /* get some cookie */
  tp1 = strstr(httpheader, "bbforum_view=");
  if(tp1) {
    tp2 = strchr(tp1, ';');
    memcpy(bbforum_view, tp1 + strlen("bbforum_view="), strlen(tp1) - strlen(tp2) - strlen("bbforum_view="));
  }
//  printf("\n%s\n", bbforum_view);

  /* my fourth request */
  memset(request, 0, 1024);
  if(strlen(bbforum_view) > 0)
    sprintf(request, "GET /bbs/newthread.php?do=newthread&f=%d HTTP/1.1\r\nHost: %s\r\nUser-Agent: %s\r\nAccept: %s\r\nAccept-Language: %s\r\nAccept-Encoding: gzip,deflate\r\nAccept-Charset: %s\r\nKeep-Alive: 300\r\nConnection: keep-alive\r\nReferer: http://%s/bbs/f%d.html\r\nCookie: bblasturl=%s; ltime=%s; rtime=%s; cnzz_eid=%s; bblastvisit=%s; bblastactivity=%s; PHPSESSID=%s; bbsessionhash=%s; bbforum_view=%s\r\n\r\n", BORDID, SERVER_HOST, USER_AGENT, CLIENT_ACCEPT, CLIENT_LANGUAGE, CLIENT_CHARSET, SERVER_HOST, BORDID, bblasturl, ltime, rtime, cnzz_eid, bblastvisit, bblastactivity, PHPSESSID, bbsessionhash, bbforum_view);
  else
    sprintf(request, "GET /bbs/newthread.php?do=newthread&f=%d HTTP/1.1\r\nHost: %s\r\nUser-Agent: %s\r\nAccept: %s\r\nAccept-Language: %s\r\nAccept-Encoding: gzip,deflate\r\nAccept-Charset: %s\r\nKeep-Alive: 300\r\nConnection: keep-alive\r\nReferer: http://%s/bbs/f%d.html\r\nCookie: bblasturl=%s; ltime=%s; rtime=%s; cnzz_eid=%s; bblastvisit=%s; bblastactivity=%s; PHPSESSID=%s; bbsessionhash=%s\r\n\r\n", BORDID, SERVER_HOST, USER_AGENT, CLIENT_ACCEPT, CLIENT_LANGUAGE, CLIENT_CHARSET, SERVER_HOST, BORDID, bblasturl, ltime, rtime, cnzz_eid, bblastvisit, bblastactivity, PHPSESSID, bbsessionhash);

  DoneWithList();

  /* my fifth request */
  memset(request, 0, 1024);
  sprintf(request, "POST /bbs/newthread.php HTTP/1.1\r\nHost: %s\r\nUser-Agent: %s\r\nAccept: %s\r\nAccept-Language: %s\r\nAccept-Encoding: gzip,deflate\r\nAccept-Charset: %s\r\nKeep-Alive: 300\r\nConnection: keep-alive\r\nReferer: http://%s/bbs/newthread.php?do=newthread&f=%d\r\nCookie: bblasturl=%s; ltime=%s; rtime=%s; cnzz_eid=%s; bblastvisit=%s; bblastactivity=%s; PHPSESSID=%s; bbsessionhash=%s; bbforum_view=%s\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 232\r\n\r\n%s%d", SERVER_HOST, USER_AGENT, CLIENT_ACCEPT, CLIENT_LANGUAGE, CLIENT_CHARSET, SERVER_HOST, BORDID, bblasturl, ltime, rtime, cnzz_eid, bblastvisit, bblastactivity, PHPSESSID, bbsessionhash, bbforum_view, postmsg, BORDID);

  DoneWithList();

  exit(0);
}
/* ****** 用C语言自己编写一个BBS发贴机器人 bbsrobot.c 结束 *********** */


编译程序命令如下:
gcc bbsrobot.c -o bbsrobot
运行效果就不贴出来的,可能涉及某些网站的安全吧

至于http协议的几个标记
GET/POST、Host、User-Agent、Accept、Accept-Language、Accept-Encoding、Accept-Charset……
大家google一下大把说明,这里就不罗嗦了

欢迎访问最专业的网吧论坛,无盘论坛,网吧经营,网咖管理,网吧专业论坛https://bbs.txwb.com

关注天下网吧微信,了解网吧网咖经营管理,安装维护:


本文来源:chinaunix 作者:佚名

声明
本文来源地址:0
声明:本站所发表的文章、评论及图片仅代表作者本人观点,与本站立场无关。若文章侵犯了您的相关权益,请及时与我们联系,我们会及时处理,感谢您对本站的支持!联系Email:support@txwb.com.,本站所有有注明来源为天下网吧或天下网吧论坛的原创作品,各位转载时请注明来源链接!
天下网吧·网吧天下
  • 本周热门
  • 本月热门
  • 阅读排行