吵吵   2011-03-03  阅读:4,648

最近要忙着考研罗,把代码就先贴出来吧,这个是linux下802.1x的源代码,原先可能没怎么做注释,就看各位看客的眼光了:使用libpcap和多线程编写的。这是主程序
smustar.c


#include
#include
#include
#include #include
#include
#include
#include
#include "md5.h"
#include "config.h"
#include unsigned char start_Packet[64]={0x01, 0x80, 0xc2, 0x00, 0x00, 0x03,
0x20, 0x6a, 0x8a, 0x08, 0x50, 0x5e,
0x88, 0x8e, 0x01, 0x01, 0x00, 0x00,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5,0xa5};
unsigned char user_Packet[64] = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x03,
0x20, 0x6a, 0x8a, 0x08, 0x50, 0x5e,
0x88, 0x8e, 0x01, 0x00, 0x00, 0xff,
0x02, 0x01, 0x00, 0xff, 0x01, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5,0xa5};
unsigned char pass_Packet[64] = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x03,
0x20, 0x6a, 0x8a, 0x08, 0x50, 0x5e,
0x88, 0x8e, 0x01, 0x00, 0x00, 0xff,
0x02, 0x02, 0x00, 0xff, 0x04, 0x10,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5,0xa5};
unsigned char logoff_Packet[64]= {0x01, 0x80, 0xc2, 0x00, 0x00, 0x03,
0x20, 0x6a, 0x8a, 0x08, 0x50, 0x5e,
0x88, 0x8e, 0x01, 0x02, 0x00, 0x00,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5,0xa5};

char username[32];
char userpass[32];
char *dev, errbuf[PCAP_ERRBUF_SIZE];
struct pcap_pkthdr *header;
const u_char *pkt_data;
pcap_t *pcap_handle;
unsigned char mac_Address[6];
int bLoop=0;
void initial_conf()
{
int i;

user_Packet[17]=strlen(username)+5;
user_Packet[21]=strlen(username)+5;
for(i=23;i {
user_Packet[i]=username[i-23];
}
pass_Packet[17]=22+strlen(username);
pass_Packet[21]=22+strlen(username);
for(i=0;i {
pass_Packet[i+40]=username[i];

}
for(i=0;i {
start_Packet[i+6]=mac_Address[i];
user_Packet[i+6]=mac_Address[i];
pass_Packet[i+6]=mac_Address[i];
logoff_Packet[i+6]=mac_Address[i];
}

}

void recvThread(void)
{
printf(“发送开始验证包\n”);
pcap_sendpacket(pcap_handle,start_Packet,sizeof(start_Packet));
while (bLoop==0)
{

while (pcap_next_ex(pcap_handle,&header,&pkt_data)>0)
{
if ((pkt_data[12]==0x88)&&(pkt_data[13]==0x8e))
{
switch(pkt_data[18])
{
case 0x01:
if ( pkt_data[22]==0x01)
{
time_t timep;
time(&timep);
struct tm *p;
p=localtime(&timep);
printf(“%d:%d:%d发送帐号包或者保持包,%s,–www.chaochaoblog.com\n命令提示:输入数字 1 ,将会退出帐号和本程序(关闭本窗口前请务必退出帐号,否则进程无法结束,帐号依然在线)\n”,p->tm_hour,p->tm_min,p->tm_sec,username);
user_Packet[19]=pkt_data[19];
pcap_sendpacket(pcap_handle,user_Packet,sizeof(user_Packet));
}
if ( pkt_data[22]==0x04)
{
u_char md5Source[16];
u_char md5Data[256];
u_char *md5Dig;
int md5Len = 1;
int n;
for(n=0;n {
md5Source[n]=pkt_data[n+24];
}
md5Data[0]=pkt_data[19];
memcpy(md5Data+1,userpass,strlen(userpass));
md5Len+=strlen(userpass);
memcpy(md5Data+md5Len,md5Source,16);
md5Len+=16;
md5Dig=ComputeHash(md5Data, md5Len);
for(n=0;n {
pass_Packet[n+24]=md5Dig[n];
}
pass_Packet[17]=22+strlen(username);
pass_Packet[21]=22+strlen(username);
for(n=0;n {
pass_Packet[n+40]=username[n];
}
printf(“发送密码包\n”);
pass_Packet[19]=pkt_data[19];
pcap_sendpacket(pcap_handle,pass_Packet,sizeof(pass_Packet));
}
break;

case 0x03:

printf(“%s登录成功\n为了保持在线,请勿关闭本窗口\n命令提示:输入数字 1 ,将会退出帐号和本程序(关闭本窗口前请务必退出帐号,否则进程无法结束)\n”,username);

break;
case 0x04:

printf(“%s登录失败,可能是帐号或者密码错误\n”,username);
break;
}

}
}
}
}
int main(int argc,char *argv[])
{
printf(“吵吵802.1x认证客户端-港湾-for linux v1.0.0.1\n”);
//sleep(1);
printf(“使用多线程和libpcap编写\n”);
//sleep(1);
printf(“www.chaochaoblog.com\n”);
//sleep(1);
printf(“QQ:419388920\n”);
//sleep(1);
printf(“初始化开始…..\n”);
//usleep(500000);
printf(“读取ini文件中用户名和密码\n”);
char buf[1024] = { 0 };
char newname[1024];
int n;
n = readlink(“/proc/self/exe” , buf , sizeof(buf));
if( n > 0 && n < sizeof(buf))
{
printf(“读取程序路径成功\n”);
char * pathof;
pathof=strrchr(buf,’/’);

memcpy(newname,buf,strlen(buf)-strlen(pathof));
newname[strlen(buf)-strlen(pathof)]=’\0′;
strcat(newname,”/user.ini”);

}
else
{
printf(“读取程序路径失败\n”);
return -1;
}
if(getconfigstr(“user”,”name”,username,sizeof(username),newname)==0)
{
printf(“读取ini文件中用户名成功\n”);
}
else
{
printf(“读取ini文件中用户名失败\n请输入用户名:\n”);
while(1)
{

scanf(“%s”,username);
if( strlen(username)>1 ) break;
printf(“input error\n”);
}
}
//usleep(500000);
//获取ini文件中保存的密码
if(getconfigstr(“user”,”pass”,userpass,sizeof(userpass),newname)==0)
{
printf(“读取ini文件中密码成功\n”);
}
else
{
printf(“读取ini文件中密码失败\n请输入密码:\n”);
while(1)
{

scanf(“%s”,userpass);
if(strlen(userpass)>1) break;
printf(“input error\n”);

}
}

dev=pcap_lookupdev(errbuf);
if (dev==NULL){
printf(“查找网卡失败,请确认网卡驱动安装好了或者以root权限运行本程序\n”);
return 0;
}
else
{
printf(“找到网卡%s\n”,dev);
}
printf(“开始获取网卡mac地址\n”);

int sock;
//struct sockaddr_in sin;
struct ifreq ifr;

sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1)
{
printf(“socket 出错”);
return -1;
}

strncpy(ifr.ifr_name, dev, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ – 1] = 0;

if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0)
{
printf(“获取网卡mac地址失败”);
return -1;
}
int i;
for(i=0;i {
mac_Address[i]=ifr.ifr_hwaddr.sa_data[i];
}
fprintf(stdout, “网卡mac地址为: %02x:%02x:%02x:%02x:%02x:%02x\n”, (unsigned char)ifr.ifr_hwaddr.sa_data[0], (unsigned char)ifr.ifr_hwaddr.sa_data[1], (unsigned char)ifr.ifr_hwaddr.sa_data[2], (unsigned char)ifr.ifr_hwaddr.sa_data[3], (unsigned char)ifr.ifr_hwaddr.sa_data[4], (unsigned char)ifr.ifr_hwaddr.sa_data[5]);
initial_conf();

pcap_handle = pcap_open_live(dev,65536,1, 500, errbuf);
if (pcap_handle==NULL){
printf(“打开网卡失败,请确认网卡驱动安装好了或者以root权限运行本程序\n”);
return 1;
}
else{
printf(“打开网卡成功\n”);
}

pthread_t id;
int ret;
printf(“创建循环捕包线程\n”);
ret=pthread_create(&id,NULL,(void *)recvThread,NULL);
if(ret!=0){
printf (“创建线程失败\n”);
return 0;
}

int input_cmd;
while (bLoop==0)
{
scanf(“%d”,&input_cmd);
if((input_cmd0))
{
switch(input_cmd)
{
case 1:
bLoop=1;
pcap_sendpacket(pcap_handle,logoff_Packet,sizeof(logoff_Packet));
printf(“帐号已退出\n程序已退出\n”);
pcap_close(pcap_handle);
sleep(1);
break;

}
}
}
return 0;
}

吵吵微信朋友圈,请付款实名加入:

吵吵 吵吵

9条回应:“smustar即802.1x(港湾客户端)linux源代码”

  1. SMU说道:

    V5

  2. 三十那年说道:

    请大大给做成linux软件包 我可以直接用了谢谢

  3. 三十那年说道:

    谢谢给做成软件包 给我发到邮箱
    sry-2004@163.com
    谢谢

    • 吵吵说道:

      我博客本来就有编译好了的,只是没有gui版的,但是可以用。你搜索一下smustar

  4. 三十那年说道:

    那个smustar 我下载了,双击解压后在user。init里输入 用户名字和密码,然后运行smustar 不行阿或许我装错了?前辈给个包直接安装吧 ,我有点笨,给个傻瓜包也行 ,麻烦你了。下面是报错
    root@localhost smu]# chmod 777 user.ini
    [root@localhost smu]# ll
    总计 28
    -rwxr-xr-x 1 1000 1000 21857 06-09 22:14 smustar
    -rwxrwxrwx 1 1000 1000 33 07-29 07:49 user.ini
    [root@localhost smu]# ./smustar
    ./smustar: error while loading shared libraries: libpcap.so.0.8: cannot open shared object file: No such file or directory

    • 吵吵说道:

      你需要确认你的linux系统已经装好了libpcap,可以到网上下载。

  5. 吵吵说道:

    主要是我也没有安装包的,没有打包。

  6. guge007说道:

    博主:编译了您网页的源码,有很多问题,比如有些地方缺了,能否给发个能用的?谢谢!cmruan@sohu.com

  7. guge0007说道:

    终于编译了自己的港湾登陆器,谢谢anyway!

发表评论

电子邮件地址不会被公开。 必填项已用*标注