免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 11259 | 回复: 19

用tcpdump分析协议后用C语言自己编写一个BBS发贴机器人 [复制链接]

论坛徽章:
0
发表于 2006-07-24 21:47 |显示全部楼层
大家可能经常上各种论坛发贴,但每次都要打开论坛、登录、输入贴子主题、贴子内容,然后点提交才可以完成。
如果你发贴比较少,可能不算什么,但如果你经常发,在很多网站发贴,可不容易哦,发贴可能花你好多时间。
鉴于这种苦恼,我自己想了一个简单方法,那就是分析一下网络协议,找到每次发贴内容变化的部分,再用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语言源代码如下:

  1. /* ****** 用tcpdump分析协议后用C语言自己编写一个BBS发贴机器人 bbsrobot.c 开始 *********** */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <errno.h>
  8. #include <unistd.h>
  9. #include <netinet/in.h>
  10. #include <limits.h>
  11. #include <netdb.h>
  12. #include <arpa/inet.h>
  13. #include <ctype.h>

  14. /* some global variable */
  15. #ifdefine OS_LINUX
  16.   #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"
  17. #else
  18.   #define USER_AGENT "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
  19. #endif

  20. #define SERVER_HOST "www.myweb.com"
  21. #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"
  22. #define CLIENT_LANGUAGE "zh-cn,zh;q=0.5"
  23. #define CLIENT_CHARSET "gb2312,utf-8;q=0.7,*;q=0.7"
  24. #define BORDID 52

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

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

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

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

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

  59. void SendRequest(void) { /* send my http-request to web server */
  60.   dsend = 0;totalsend = 0;
  61.   nbytes=strlen(request);
  62.   while(totalsend < nbytes) {
  63.     dsend = write(sockfd, request + totalsend, nbytes - totalsend);
  64.     if(dsend==-1)  {fprintf(stderr, "send error!%s\n", strerror(errno));exit(0);}
  65.     totalsend+=dsend;
  66.     printf("\nRequest.%d %d bytes send OK!\n", reqn - 1, totalsend);
  67.   }
  68. }
复制代码

[ 本帖最后由 zhoulifa 于 2006-10-19 08:42 编辑 ]

论坛徽章:
0
发表于 2006-07-24 21:51 |显示全部楼层

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

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

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

  17.   if(FD_ISSET(sockfd, &writefds))    {
  18.     memset(buffer, 0, 1024);
  19.     memset(httpheader, 0, 1024);

  20.     /* receive data from web server */
  21.     while((nbytes=read(sockfd,buffer,1))==1)
  22.     {
  23.       if(i < 4)  {
  24.         if(buffer[0] == '\r' || buffer[0] == '\n')  i++;
  25.         else i = 0;
  26.         memcpy(httpheader + j, buffer, 1); j++;
  27.       }
  28.       else  {
  29. //        fprintf(stdout, "%c", buffer[1]);/* print content on the screen */
  30.         i++;
  31.       }
  32.     }
  33.   }
  34. }

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

  37.   /* send a request */
  38.   SendRequest();

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

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

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

  45.   DoOnce();

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

  48. int main(int argc, char ** argv)
  49. {
  50.   char *tp1, *tp2, *tp3;
  51.   char ltime[16]="1152109555207", rtime[16]="0", cnzz_eid[16]="20369173-";
  52.   char bblastvisit[16] = "", bblastactivity[16] = "";
  53.   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";
  54.   char PHPSESSID[1024] = "";
  55.   char bblasturl[1024] = "";
  56.   char bbsessionhash[1024] = "";
  57.   char bbforum_view[1024] = "";
  58.   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="; /* 在这里可以写发贴主题和发贴内容 */

  59.     portnumber=80;

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

  66.   /* my first request */
  67.   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);

  68.   DoneWithList();

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

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

  76.   /* my second request */
  77.   memset(request, 0, 1024);
  78.   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: [url]http://%s/bbs/[/url]\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);

  79.   DoneWithList();

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

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

  87.   tp1 = strstr(httpheader, "bblasturl=");
  88.   tp3 = strstr(tp1 + strlen("bblasturl="), "bblasturl=");
  89.   if(tp3) {
  90.     tp2 = strchr(tp3, ';');
  91.     memcpy(bblasturl, tp3 + strlen("bblasturl="), strlen(tp3) - strlen(tp2) - strlen("bblasturl="));
  92.   }
  93.   else {
  94.     tp2 = strchr(tp1, ';');
  95.     memcpy(bblasturl, tp1 + strlen("bblasturl="), strlen(tp1) - strlen(tp2) - strlen("bblasturl="));
  96.   }

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

  101.   /* my third request */
  102.   memset(request, 0, 1024);
  103.   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: [url]http://%s/bbs/[/url]\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);

  104.   DoneWithList();

  105.   /* get some cookie */
  106.   tp1 = strstr(httpheader, "bbforum_view=");
  107.   if(tp1) {
  108.     tp2 = strchr(tp1, ';');
  109.     memcpy(bbforum_view, tp1 + strlen("bbforum_view="), strlen(tp1) - strlen(tp2) - strlen("bbforum_view="));
  110.   }
  111. //  printf("\n%s\n", bbforum_view);

  112.   /* my fourth request */
  113.   memset(request, 0, 1024);
  114.   if(strlen(bbforum_view) > 0)
  115.     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: [url]http://%s/bbs/f%d.html[/url]\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);
  116.   else
  117.     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: [url]http://%s/bbs/f%d.html[/url]\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);

  118.   DoneWithList();

  119.   /* my fifth request */
  120.   memset(request, 0, 1024);
  121.   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: [url]http://%s/bbs/newthread.php?do=newthread&f=%d[/url]\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);

  122.   DoneWithList();

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

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

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

[ 本帖最后由 mq110 于 2006-7-25 06:53 编辑 ]

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2006-07-24 23:02 |显示全部楼层
看了这个帖子后,才知道自己学会了 Perl 是多么的幸福啊。

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
发表于 2006-07-25 06:59 |显示全部楼层
这种封包 不具有通用性.

相信 每个表单里的 <input>中的text的name都不是完全相同的,而且有些<input>控件还可能有hidden等字段.不具有通用性.

如果真的非常想用C来写的话,推荐看一些浏览器的源代码,是要分析html语言的.


用perl这样的脚本语言实现是比较好的. 不过鼓励原创.加为精华,让大家参考一下封http协议的包的方式.

论坛徽章:
0
发表于 2006-07-25 07:13 |显示全部楼层
原帖由 mq110 于 2006-7-25 06:59 发表
这种封包 不具有通用性.

相信 每个表单里的 <input>中的text的name都不是完全相同的,而且有些<input>控件还可能有hidden等字段.不具有通用性.

如果真的非常想用C来写的话,推荐看一些浏览器的源 ...

是不是说的这个啊

  1. 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=";
复制代码

我怎么觉得这个 posthash 来的不简单啊

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
发表于 2006-07-25 07:24 |显示全部楼层
原帖由 platinum 于 2006-7-25 07:13 发表

是不是说的这个啊
[code]
char postmsg[1024] = "subject=贴子主题&WYSIWYG_HTML=贴子内容在这里&iconid=0&s=&do= postthread&posthash=b77747456dec01432fe5252c1ad9aa41&post ...


除了posthash 还有很多这样的东西..

比如说username 在某些表单里 可能叫User也可能叫userName.

我感觉直观的方式,可以直接打开个浏览器看html代码.

论坛徽章:
0
发表于 2006-07-25 10:57 |显示全部楼层
原帖由 mq110 于 2006-7-25 07:24 发表


除了posthash 还有很多这样的东西..

比如说username 在某些表单里 可能叫User也可能叫userName.

我感觉直观的方式,可以直接打开个浏览器看html代码.

如果这样的话,不同的 ID 应该有不同的特征串,这个程序的确不具备通用性,不过设计者的初衷不错

论坛徽章:
0
发表于 2006-07-25 12:46 |显示全部楼层

回复 4楼 mq110 的帖子

>>
>>
>>expect更为方便一些,可以自动和服务器做交互,模拟真实用户行为
>>
>>

论坛徽章:
0
发表于 2006-07-25 16:57 |显示全部楼层
.... 这样的cc 我也设计过的 不具备通用性

论坛徽章:
0
发表于 2006-07-25 16:57 |显示全部楼层
对方加个图形验证码  就没办法了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP