?? dps.c
字號:
libnet_addr2name4( spoofed_src_ip, LIBNET_DONT_RESOLVE ),
dst_port, src_port );
}
/* Compile the filter */
if( pcap_compile( pcap_cfg.p, &pcap_cfg.f_program,
pcap_cfg.f_code, 1, pcap_cfg.netmask ) == -1 )
{
printf("Cannot compile the filter code: %s\n", pcap_geterr( pcap_cfg.p ) );
pcap_close( pcap_cfg.p );
exit( EXIT_FAILURE );
}
free( pcap_cfg.f_code );
/* Set the filter program on the interface */
dps_set_filter( pcap_cfg.f_program );
/* Write the scan packet */
dps_write_packet();
/* Listen for response */
received = 0;
start_time = time( NULL );
while( start_time + cfg.timeout > time( NULL ) && !received )
{
rcv_packet = ( u_int8_t * ) pcap_next( pcap_cfg.p, &header );
if( rcv_packet == NULL || rcv_packet == 0 )
continue;
ip = ( struct libnet_ipv4_hdr * ) ( rcv_packet + LIBNET_ETH_H );
switch( ip->ip_p )
{
case IPPROTO_TCP:
tcp = ( struct libnet_tcp_hdr * )
( rcv_packet + LIBNET_ETH_H + ( ip->ip_hl << 2 ) );
current_port->recv_control = tcp->th_flags;
if( tcp->th_flags == ( TH_RST | TH_ACK ) )
{
current_port->status = PORT_CLOSED;
result.closed++;
if( cfg.verbosity == 2 )
printf("STATUS [closed]\n");
if( current_port->sent_control == TH_SYN )
current_port->status_win = PORT_CLOSED;
else
current_port->status_win = PORT_OPEN | PORT_CLOSED;
}
else if( tcp->th_flags == ( TH_SYN | TH_ACK ) )
{
current_port->status = PORT_OPEN;
current_port->status_win = PORT_OPEN;
result.open++;
if( cfg.verbosity == 2 )
printf("STATUS [open]\n");
if( cfg.verbosity == 1 )
printf(" Port %d [OPEN]\n", current_port->port );
}
else if( tcp->th_flags == ( TH_RST ) )
{
current_port->status = PORT_UNFILTERED;
current_port->status_win = PORT_UNFILTERED;
result.unfiltered++;
if( cfg.verbosity == 2 )
printf("STATUS [unfiltered]\n");
else if( cfg.verbosity == 1 )
printf(" Port %d [UNFILTERED]\n", current_port->port );
}
received = 1;
break;
case IPPROTO_UDP:
udp = ( struct libnet_udp_hdr * )
( rcv_packet + LIBNET_ETH_H + ( ip->ip_hl << 2 ) );
current_port->status = PORT_OPEN;
current_port->status_win = PORT_OPEN;
result.open++;
if( cfg.verbosity == 2 )
printf("STATUS [open]\n");
else if( cfg.verbosity == 1 )
printf(" Port %d [OPEN]\n", current_port->port );
received = 1;
break;
case IPPROTO_ICMP:
icmp = ( struct libnet_icmp_hdr * )
( rcv_packet + LIBNET_ETH_H + ( ip->ip_hl << 2 ) );
if( icmp->icmp_type != ICMP_UNREACH ||
icmp->icmp_code != ICMP_UNREACH_PORT )
continue;
udp = ( struct libnet_udp_hdr * )
( rcv_packet + LIBNET_ETH_H + ( ip->ip_hl << 2 )
+ LIBNET_ICMPV4_UNREACH_H );
current_port->status = PORT_CLOSED;
current_port->status_win = PORT_CLOSED;
result.closed++;
if( cfg.verbosity == 2 )
printf("STATUS [closed]\n");
received = 1;
break;
}
}
if( !received )
{
if( strcmp( cfg.scan_type, "UDP" ) != 0 )
{
/* TCP Scan */
if( current_port->sent_control == SCAN_ACK ||
current_port->sent_control == SCAN_SYN )
{
current_port->status = PORT_FILTERED;
current_port->status_win = PORT_FILTERED;
result.filtered++;
if( cfg.verbosity == 2 )
printf("STATUS [filtered]\n");
}
else
{
current_port->status = PORT_FILTERED | PORT_OPEN;
current_port->status_win = PORT_FILTERED;
result.open_filtered++;
if( cfg.verbosity == 2 )
printf("STATUS [open|filtered]\n");
else if( cfg.verbosity == 1 )
printf(" Port %d [OPEN|FILTERED]\n", current_port->port );
}
}
else
{
/* UDP Scan */
current_port->status = PORT_FILTERED | PORT_OPEN;
current_port->status_win = PORT_FILTERED | PORT_OPEN;
result.open_filtered++;
if( cfg.verbosity == 2 )
printf("STATUS [open|filtered]\n");
}
}
current_port->next = ( struct port_data * ) malloc( sizeof( struct port_data ) );
current_port = current_port->next;
current_port->next = NULL;
usleep( 50000 );
}
}
current_port = NULL;
scan_end = time( NULL );
scan_time = scan_end - scan_begin;
if( cfg.verbosity )
printf("Ending Scanning...\n");
}
void dps_build_arp( int arp_op, u_int32_t src_ip, u_int32_t dst_ip,
u_int8_t *src_eth, u_int8_t *dst_eth )
{
libnet_cfg.arp = libnet_build_arp(
ARPHRD_ETHER, /* hardware type */
ETHERTYPE_IP, /* protocol type */
HRD_ADDR_LENGTH, /* hardware address length */
PRO_ADDR_LENGTH, /* protocol address length */
arp_op, /* ARP packet operation */
src_eth, /* Ethernet source address */
(u_char *)&src_ip, /* IP destination address */
dst_eth, /* Ethernet destination address */
(u_char *)&dst_ip, /* IP destination address */
NULL, /* optional payload */
0, /* payload size */
libnet_cfg.l, /* libnet handle */
0 /* libnet protocol tag */
);
if( libnet_cfg.arp == -1 )
{
printf("Cannot build ARP header\n");
exit( EXIT_FAILURE );
}
libnet_cfg.eth = libnet_build_ethernet(
dst_eth, /* Ethernet destinatin address */
src_eth, /* Ethernet source address */
ETHERTYPE_ARP, /* protocol type */
NULL, /* optional payload */
0, /* payload size */
libnet_cfg.l, /* libnet handle */
0 /* libnet protocol tag */
);
if( libnet_cfg.eth == -1 )
{
printf("Cannot build Ethernet header\n");
exit( EXIT_FAILURE );
}
}
void dps_build_tcp( u_int8_t control, u_int16_t src_port, u_int16_t dst_port,
u_int32_t src_ip, u_int32_t dst_ip, u_int8_t *src_eth,
u_int8_t *dst_eth )
{
libnet_cfg.tcp = libnet_build_tcp(
src_port, /* source port */
dst_port, /* destination port */
SEQ, /* sequence number */
ACK, /* ack number */
control, /* control flags */
WIN, /* TCP windows size */
0, /* header checksum */
0, /* urgent pointer */
LIBNET_TCP_H, /* TCP packet size */
NULL, /* optional payload */
0, /* payload size */
libnet_cfg.l, /* libnet_handle */
0 /* libnet protocol tag */
);
if( libnet_cfg.tcp == -1 )
{
printf("Cannot build TCP header\n");
exit( EXIT_FAILURE );
}
libnet_cfg.ip = libnet_build_ipv4(
TCPIP_LEN, /* total packet size */
TOS, /* Type of Service */
ID, /* Identification */
0, /* fragmentation */
TTL, /* Time to Live */
IPPROTO_TCP, /* protocol */
0, /* checksum */
src_ip, /* IP source address */
dst_ip, /* IP destination address */
NULL, /* optional payload */
0, /* payload size */
libnet_cfg.l, /* libnet handle */
0 /* libnet protocol tag */
);
if( libnet_cfg.ip == -1 )
{
printf("Cannot build IP header\n");
exit( EXIT_FAILURE );
}
libnet_cfg.eth = libnet_build_ethernet(
dst_eth, /* Ethernet destinatin address */
src_eth, /* Ethernet source address */
ETHERTYPE_IP, /* protocol type */
NULL, /* optional payload */
0, /* payload size */
libnet_cfg.l, /* libnet handle */
0 /* libnet protocol tag */
);
if( libnet_cfg.eth == -1 )
{
printf("Cannot build Ethernet header\n");
exit( EXIT_FAILURE );
}
}
void dps_build_udp( u_int16_t src_port, u_int16_t dst_port, u_int32_t src_ip,
u_int32_t dst_ip, u_int8_t *src_eth, u_int8_t *dst_eth )
{
libnet_cfg.udp = libnet_build_udp(
src_port, /* source port */
dst_port, /* destination port */
LIBNET_UDP_H, /* packet size */
0, /* checksum */
NULL, /* optional payload */
0, /* payload size */
libnet_cfg.l, /* libnet handle */
0 /* libnet protocol tag */
);
if( libnet_cfg.udp == -1 )
{
printf("Cannot build UDP header\n");
exit( EXIT_FAILURE );
}
libnet_cfg.ip = libnet_build_ipv4(
UDPIP_LEN, /* total packet size */
TOS, /* Type of Service */
ID, /* Identification */
0, /* fragmentation */
TTL, /* Time to Live */
IPPROTO_UDP, /* protocol */
0, /* checksum */
src_ip, /* IP source address */
dst_ip, /* IP destination address */
NULL, /* optional payload */
0, /* payload size */
libnet_cfg.l, /* libnet handle */
0 /* libnet protocol tag */
);
if( libnet_cfg.ip == -1 )
{
printf("Cannot build IP header\n");
exit( EXIT_FAILURE );
}
libnet_cfg.eth = libnet_build_ethernet(
dst_eth, /* Ethernet destinatin address */
src_eth, /* Ethernet source address */
ETHERTYPE_IP, /* protocol type */
NULL, /* optional payload */
0, /* payload size */
libnet_cfg.l, /* libnet handle */
0 /* libnet protocol tag */
);
if( libnet_cfg.eth == -1 )
{
printf("Cannot build Ethernet header\n");
exit( EXIT_FAILURE );
}
}
int dps_ping()
{
time_t start_time;
u_int8_t *rcv_packet;
u_int8_t ip_hl;
struct pcap_pkthdr header;
struct libnet_icmpv4_hdr *icmp;
/*
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -