Home > other >  The SYN packet, don't get a reply
The SYN packet, don't get a reply

Time:10-18

 # include & lt; Stdio. H> 

# include & lt; Stdlib. H>

# include & lt; String. H>

# include & lt; Unistd. H>

# include & lt; Errno. H>

# include & lt; Sys/socket. H>

# include & lt; Sys/ioctl. H>

# include & lt; Sys/types. H>

# include & lt; Netinet/in. H>

# include & lt; Netinet/IP. H>

# include & lt; Netinet/if_ether. H>

# include & lt; Net/if_arp. H>

# include & lt; Netpacket/packet. H>

# include & lt; Net/if. H>

# include & lt; Net/Ethernet. H>

# include & lt; Linux/TCP. H>

# define BUFLEN 54

//CRC checksum calculation

Unsigned short check_sum (unsigned short * addr, int len) {

The register int nleft=len;

The register int sum=0;

The register short * w=addr.

Short answer=0;



While (nleft> 1)

{

The sum +=* w++;

Nleft -=2;

}

If (nleft==1)

{

* (unsigned char *) (& amp; Answer)=* (unsigned char *) w;

The sum +=answer;

}



Sum=(sum> 16) + (sum& 0 XFFFF);

The sum +=(sum> 16);

Answer=~ sum;

Return (answer);

}

Int main (int arg c, char * * argv)
{

Int SKFD, n;

Char buf [BUFLEN]={0};

Struct ether_header * eth;

Struct sockaddr_ll toaddr;

Struct in_addr targetIP srcIP;

Struct IP * IP;

Struct TCPHDR * TCP;

Struct ifreq ifr.



Unsigned char src_mac [ETH_ALEN]={0};

Unsigned char dst_mac [ETH_ALEN]={0};

Unsigned char src_ip [15]={0};

Unsigned char dst_ip [15]={0};

Unsigned srcport dstport;

MAC: printf (" source ");
The scanf (" % % % % x: x: x: x: x: % % x ",
& Src_mac [0], & amp; Src_mac [1], & amp; Src_mac [2],
& Src_mac [3], & amp; Src_mac [4], & amp; Src_mac [5]);

MAC: printf (" target ");
The scanf (" % % % % x: x: x: x: x: % % x ",
& Dst_mac [0], & amp; Dst_mac [1], & amp; Dst_mac [2],
& Dst_mac [3], & amp; Dst_mac [4], & amp; Dst_mac [5]);

Printf (" source IP: ");
The scanf (" % s ", & amp; Src_ip);

Printf (" target IP: ");
The scanf (" % s ", & amp; Dst_ip);

Printf (" target port: ");
The scanf (" % d ", & amp; Dstport);

Printf (" source port: ");
The scanf (" % d ", & amp; Srcport);


If ((SKFD=socket (PF_PACKET SOCK_RAW, htons (ETH_P_ALL))) & lt; 0)
{

Perror (" Create Error ");

exit(1);

}



Bzero (& amp; Toaddr, sizeof (toaddr));

Bzero (& amp; Ifr, sizeof (ifr));

Strcpy (ifr) ifr_name, argv [1]).




//access interface index

If (1==ioctl (SKFD SIOCGIFINDEX, & amp; Ifr)) {

Perror (" get dev index error: ");

exit(1);

}

Toaddr. Sll_ifindex=ifr. Ifr_ifindex;

Printf (" interface Index: % d \ n ", ifr, ifr_ifindex);



//fill, construct the etheric head

Buf eth=(struct ether_header *);

Memcpy (eth - & gt; Ether_dhost dst_mac, ETH_ALEN);

Memcpy (eth - & gt; Ether_shost src_mac, ETH_ALEN);

The eth - & gt; Ether_type=htons (ETHERTYPE_IP);


//fill the IP header

IP=(struct IP *) (buf + sizeof (struct ether_header));



IP - & gt; Ip_v=IPVERSION;

IP - & gt; Ip_hl=sizeof (struct IP) & gt;> 2;

IP - & gt; Ip_tos=0;

IP - & gt; Ip_len=htons (sizeof (struct IP) + sizeof (struct TCPHDR));

IP - & gt; Ip_id=0;

IP - & gt; Ip_off=0;

IP - & gt; Ip_ttl=MAXTTL;

IP - & gt; Ip_p=IPPROTO_TCP;

//IP - & gt; Ip_sum=check_sum ((unsigned short *) IP, sizeof (struct IP));

IP - & gt; Ip_sum=0;

Inet_pton (AF_INET, dst_ip, & amp; TargetIP);

IP - & gt; Ip_dst=targetIP;

Inet_pton (AF_INET, src_ip, & amp; SrcIP);

IP - & gt; Ip_src=https://bbs.csdn.net/topics/srcIP;

//start filling the TCP header

TCP=(struct TCPHDR *) (buf + sizeof (struct IP) + sizeof (struct ether_header));

TCP - & gt; Source=htons (srcport);

TCP - & gt; Dest=htons (dstport);

TCP - & gt; Seq=0;

TCP - & gt; Ack=0;

TCP - & gt; The window=htons (8192);

TCP - & gt; Doff=5;

TCP - & gt; The syn=1;

TCP - & gt; Check=0;

Toaddr. Sll_family=PF_PACKET;

TCP - & gt; Check=check_sum ((unsigned short *) TCP, sizeof (struct TCPHDR));
IP - & gt; Ip_sum=check_sum ((unsigned short *) IP, sizeof (struct IP));



N=sendto (SKFD, buf BUFLEN, 0, (struct sockaddr *) & amp; Toaddr, sizeof (toaddr));


Close (SKFD);

return 0;

}


Above for the C program source code



Above is two Windows machine under.net application connects caught the results



Above is under Linux in C programs to the Windows of the SYN packet result (.net applications to monitor), don't get reply

This is how to return a responsibility? The great spirit to solvenullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related