免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7143 | 回复: 6
打印 上一主题 下一主题

Linux raw socket Permission denied [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-05 16:59 |只看该作者 |倒序浏览
大家好。

我再练习raw socket编程,
写了个小程序。但是在运行的时候(sendto)出现“Permission denied”


我已经加了setuid(getuid()); 怎么会没有权限呢???

哪位帮我改改啊???

谢谢谢谢:::::::


新手写的代码,大家见谅!
代码:

#define DST 10.117.4.11
#define P 80
struct ipheader {
unsigned char ip_hl:4, ip_v:4; /* this means that each member is 4 bits */
unsigned char ip_tos;
unsigned short int ip_len;
unsigned short int ip_id;
unsigned short int ip_off;
unsigned char ip_ttl;
unsigned char ip_p;
unsigned short int ip_sum;
unsigned int ip_src;
unsigned int ip_dst;
}; /* total ip header length: 20 bytes (=160 bits) */


struct icmpheader {
unsigned char icmp_type;
unsigned char icmp_code;
unsigned short int icmp_cksum;
/* The following data structures are ICMP type specific */
unsigned short int icmp_id;
unsigned short int icmp_seq;
}; /* total icmp header length: 8 bytes (=64 bits) */

struct tcpheader {
unsigned short int th_sport;
unsigned short int th_dport;
unsigned int th_seq;
unsigned int th_ack;
unsigned char th_x2:4, th_off:4;
unsigned char th_flags;
unsigned short int th_win;
unsigned short int th_sum;
unsigned short int th_urp;
}; /* total tcp header length: 20 bytes (=160 bits) */


unsigned short          /* this function generates header checksums */
csum (unsigned short *buf, int nwords)
{
  unsigned long sum;
  for (sum = 0; nwords > 0; nwords--)
    sum += *buf++;
  sum = (sum >> 16) + (sum & 0xffff);
  sum += (sum >> 16);
  return ~sum;
}

int main(void)

{

        int sockfd=socket(PF_INET,SOCK_RAW,IPPROTO_TCP);
        printf("socket created succefully and  fd is %d\n",sockfd);
        char buff[4096];
        struct ipheader *iph;
        struct tcpheader *tcph;
        iph=(struct ipheader *) buff;
        tcph=(struct tcpheader *)(buff+sizeof(struct ipheader));
        struct sockaddr_in sin;
        sin.sin_family=AF_INET;
        sin.sin_port=htons(P);
        sin.sin_addr.s_addr=inet_addr("DST");
        memset(buff,0,4096);
        setuid(getuid());
        printf("my uid is %d\n",getuid());
        /*start to make packet*/
        iph->ip_hl = 5;
        iph->ip_v = 4;
        iph->ip_tos = 0;
        iph->ip_len = sizeof (struct ipheader) + sizeof (struct tcpheader);    /                                                                              * no payload */
        iph->ip_id = htonl (54321);     /* the value doesn't matter here */
        iph->ip_off = 0;
        iph->ip_ttl = 255;
        iph->ip_p = 6;
        iph->ip_sum = 0;                /* set it to 0 before computing the actu                                                                              al checksum later */
        iph->ip_src = inet_addr ("1.2.3.4");/* SYN's can be blindly spoofed */
        iph->ip_dst = sin.sin_addr.s_addr;
        tcph->th_sport = htons (1234);  /* arbitrary port */
        tcph->th_dport = htons (P);
        tcph->th_seq = random ();/* in a SYN packet, the sequence is a random */
        tcph->th_ack = 0;/* number, and the ack sequence is 0 in the 1st packet                                                                               */
        tcph->th_x2 = 0;
        tcph->th_off = 0;               /* first and only tcp segment */
        tcph->th_flags = 0x02;  /* initial connection request */
        tcph->th_win = htonl (65535);   /* maximum allowed window size */
        tcph->th_sum = 0;/* if you set a checksum to zero, your kernel's IP stac                                                                              k
                      should fill in the correct checksum during transmission */
        tcph->th_urp = 0;

        iph->ip_sum = csum ((unsigned short *) buff, iph->ip_len >> 1);







/*t the kernel knows the header is included in the data, and doesn't
   insert its own header into the packet before our data */


{                               /* lets do it the ugly way.. */
    int one = 1;
    const int *val = &one;
    if (setsockopt (sockfd, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0)
      printf ("Warning: Cannot set HDRINCL!\n");
  }





while (1)
    {
      if (sendto (sockfd,               /* our socket */
                  buff, /* the buffer containing headers and data */
                  iph->ip_len,  /* total length of our datagram */
                  0,            /* routing flags, normally always 0 */
                  (struct sockaddr *) &sin,     /* socket addr, just like in */
                  sizeof (sin)) < 0)            /* a normal send() */
        {
        perror("error:");
        }
      else
        printf ("date send");
    }



        return 0;
}

论坛徽章:
0
2 [报告]
发表于 2010-01-05 17:19 |只看该作者
root

论坛徽章:
0
3 [报告]
发表于 2010-01-05 17:35 |只看该作者
1024以下的端口不是随便什么人都能开的

论坛徽章:
0
4 [报告]
发表于 2010-01-05 17:51 |只看该作者
原帖由 churchmice 于 2010-1-5 17:35 发表
1024以下的端口不是随便什么人都能开的



多谢两位的回复。我本身就是以root,编译,执行的。

所以我才奇怪权限问题

论坛徽章:
0
5 [报告]
发表于 2010-01-05 17:55 |只看该作者
sin.sin_addr.s_addr=inet_addr("DST")
这句错了,把引号去了

论坛徽章:
0
6 [报告]
发表于 2010-01-05 17:58 |只看该作者

回复 #5 @sky 的帖子

应该是在宏定义的时候就写引号的吧

论坛徽章:
0
7 [报告]
发表于 2010-01-05 19:01 |只看该作者
果然如2位所说。感谢!!

我以后都在宏定义的时候加引号吧
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP