Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5760668
  • 博文数量: 675
  • 博客积分: 20301
  • 博客等级: 上将
  • 技术积分: 7671
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-31 16:15
文章分类

全部博文(675)

文章存档

2012年(1)

2011年(20)

2010年(14)

2009年(63)

2008年(118)

2007年(141)

2006年(318)

分类: C/C++

2006-11-07 21:52:05

/* ****** 用tcpdump分析协议后用C语言自己编写一个BBS发贴机器人 bbsrobot.c 开始 *********** */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

/* 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 ""
#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) 周立发()
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);
}
阅读(6442) | 评论(12) | 转发(0) |
给主人留下些什么吧!~~

清风9992009-12-08 21:55:24

如何用 ,zhanghhwgh@sina.com

清风9992009-12-08 21:55:24

如何用 ,zhanghhwgh@sina.com

chinaunix网友2009-11-11 13:16:04

http://s.click.taobao.com/a/qXMWqaY1wJw=-10742190

chinaunix网友2009-11-11 13:16:04

http://s.click.taobao.com/a/qXMWqaY1wJw=-10742190