24,854
社区成员
发帖
与我相关
我的任务
分享
void SessionTree::GetHTML1(char * path)
{
pHttpFile p = httpfile;
int pathlen = strlen(path);
char * title = (char *)malloc(pathlen + 1);
memcpy(title,path,pathlen);
memcpy(title + pathlen,"\\",1);
memcpy(title + pathlen + 1,"\0",1);
string report = title;
report +="report2.txt";
ofstream out;
out.open(report.c_str(),ios::out|ios::trunc|ios::binary);
if (!out)
{
cout<<"open error";
}
while(p != NULL)
{
FILE * file;
pcap_file_header file_header;
pcap_pkthdr pcap_header;
ip_header ih;
tcp_header th;
vector<char *> vec;
FILE * temptxtfile = NULL;
fopen_s(&file,p->filename,"rb");
if (file == NULL)
{
exit(0);
}
//计算整个文件的字节数
fseek(file,0,SEEK_END);
u_long Lenght = ftell(file);
fseek(file,0,SEEK_SET);
//读取文件头
fread_s((void *)&file_header,sizeof(pcap_file_header),sizeof(pcap_file_header),1,file);
u_long Index = 24;
while(Index < Lenght)
{
//读取包头
fread_s((void *)&pcap_header,sizeof(pcap_header),sizeof(pcap_header),1,file);
fseek(file,14 ,SEEK_CUR);
fread_s((void *)&ih,sizeof(ip_header),sizeof(ip_header),1,file);
if (((ih.ver_ihl & 0x0f) * 4-20) >0)
{
fseek(file,((ih.ver_ihl & 0x0f) * 4-20),SEEK_CUR);
}
fread_s((void *)&th,sizeof(tcp_header),sizeof(tcp_header),1,file);
int DataLength = ntohs(ih.tlen)- (ih.ver_ihl & 0x0f) * 4 -(th.dataoffset>>4)*4;
//////////////////////////////////////////////////////////////////////////
//获取标题,subfile信息,
//问题出现了一对会话之间没有换行符。
//////////////////////////////////////////////////////////////////////////
if (DataLength > 0)
{
fseek(file,(th.dataoffset>>4)*4 - 20,SEEK_CUR);
//读取数据
char * data = (char *)malloc(DataLength);
memset(data,0,DataLength);
fread_s(data,DataLength,DataLength,1,file);
data[DataLength] = '\0';
fseek(file,-((th.dataoffset>>4)*4 - 20 + DataLength),SEEK_CUR);
//只有是GET请求或者HTTP/1.1 200 OK才进入
if (temptxtfile == NULL && (strncmp("GET ",data,strlen("GET ")) == 0 ||
strncmp("HTTP/1.1 200 OK",data,strlen("HTTP/1.1 200 OK")) == 0 || strncmp("POST /",data,strlen("POST /")) == 0))
{
char seps[] = "\r\n";
char *next_token = NULL;
char * txt = (char *)malloc(strlen(data)+1);
memcpy(txt,data,strlen(data) + 1);
//strncpy_s(txt,strlen(txt),data,strlen(data));
char * token = strtok_s(txt,seps,&next_token);
while (token != NULL)
{
if (strncmp("GET / HTTP/1.1",token,strlen("GET / HTTP/1.1")) == 0)
{
pushData(data);
break;
}
else if (strncmp("GET /",token,strlen("GET /")) == 0)//|| strncmp("POST /",data,strlen("POST /")) == 0 排除POST请求,后面好做一些
{
if (temptxtfile == NULL)
{
char * filename = (char *)malloc(strlen(token) + pathlen + 1);
memcpy(filename,title,pathlen + 1);
char *filename1 = StrReplace(token);
memcpy(filename + pathlen + 1,"\0",1);
strcat_s(filename,strlen(token) + pathlen + 2,filename1);
//创建文件存储会话数据
p->txtfilename = filename;
fopen_s(&temptxtfile,filename,"wb+");
}
fwrite(data,DataLength,1,temptxtfile);
break;
}
else if (strncmp("Content-Location: http://",token,strlen("Content-Location: http://")) == 0)
{
int i = strlen(token);
int j = strlen("Content-Location: http://");
char * filename = (char *)malloc(i - j+ pathlen + 10);
memcpy(filename,title,pathlen + 1);
char *filename1 = StrReplace(token + j);
memcpy(filename + pathlen + 1,"\0",1);
strcat_s(filename,i - j + pathlen + 10,filename1);
//创建文件存储会话数据
if (temptxtfile == NULL)
{
p->txtfilename = filename;
fopen_s(&temptxtfile,filename,"wb");
}
//写入存储的数据
char * str = popData();
fwrite(str,strlen(str),1,temptxtfile);
fwrite(data,DataLength,1,temptxtfile);
break;
}
token = strtok_s(NULL,seps,&next_token);
}
}
else
{
if (temptxtfile == NULL)
{
pushData(data);
//vec.push_back(data);
}
else
{
if (strncmp("GET ",data,strlen("GET ")) == 0)
{
fwrite("\r\n\r\n\r\n\r\n\r\n",strlen("\r\n\r\n\r\n\r\n\r\n"),1,temptxtfile);
}
fwrite(data,DataLength,1,temptxtfile);
}
}
}
fseek(file,pcap_header.caplen -14 - 20 - 20,SEEK_CUR);
Index += pcap_header.caplen + 16;
}
if (temptxtfile != NULL)
{
out<<p->filename<<"--> +"<<p->txtfilename<<"\r\n";
}
if (temptxtfile != NULL)
{
fclose(temptxtfile);
}
//记录对应的对应的关系
p = p->next;
}
out.close();
}