|
Приложение 4. Программа сканирования TCP портов.
/*
* resolve.c
* by Uriel Maimon (lifesux@cox.org)
*/
#include
#include
#include
#include
#include
int resolve( const char *name, struct sockaddr_in *addr, int port )
{
struct hostent *host;
/* clear everything in case I forget something */
bzero(addr,sizeof(struct sockaddr_in));
if (( host = gethostbyname(name) ) == NULL ) {
#ifndef RESOLVE_QUIET
fprintf(stderr,"unable to resolve host \"%s\" -- ",name);
perror("");
#endif
return -1;
}
addr->sin_family = host->h_addrtype;
memcpy((caddr_t)&addr->sin_addr,host->h_addr,host->h_length);
addr->sin_port = htons(port);
return 0;
}
int resolve_rns( char *name , unsigned long addr )
{
struct hostent *host;
unsigned long address;
address = addr;
host = gethostbyaddr((char *)&address,4,AF_INET);
if (!host) {
#ifndef RESOLVE_QUIET
fprintf(stderr,"unable to resolve host \"%s\" -- ",inet_ntoa(addr));
perror("");
#endif
return -1;
}
strcpy(name,host->h_name);
return 0;
}
unsigned long addr_to_ulong(struct sockaddr_in *addr)
{
return addr->sin_addr.s_addr;
}
/*
* EOF
*//*
* tcp_pkt.c
* by Uriel Maimon (lifesux@cox.org)
*/
/* remove inlines for smaller size but lower speed */
#include
#include
#include
#include
#include
#define IPHDRSIZE sizeof(struct iphdr)
#define TCPHDRSIZE sizeof(struct tcphdr)
#define PSEUDOHDRSIZE sizeof(struct pseudohdr)
/*
* in_cksum --
* Checksum routine for Internet Protocol family headers (C Version)
*/
unsigned short in_cksum(addr, len)
u_short *addr;
int len;
{
register int nleft = len;
register u_short *w = addr;
register int sum = 0;
u_short answer = 0;
/*
* Our algorithm is simple, using a 32 bit accumulator (sum), we add
* sequential 16 bit words to it, and at the end, fold back all the
* carry bits from the top 16 bits into the lower 16 bits.
*/
while (nleft > 1) {
sum += *w++;
nleft -= 2;
}
/* mop up an odd byte, if necessary */
if (nleft == 1) {
*(u_char *)(&answer) = *(u_char *)w ;
sum += answer;
}
/* add back carry outs from top 16 bits to low 16 bits */
sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
sum += (sum >> 16); /* add carry */
answer = ~sum; /* truncate to 16 bits */
return(answer);
}
/*
* HEXDUMP()
*
* not too much to explain
*/
inline void HEXDUMP(unsigned len, unsigned char *data)
{
unsigned i;
for (i=0;isaddr = s_addr;
pseudo->daddr = t_addr;
pseudo->protocol = IPPROTO_TCP;
pseudo->tcplength = htons(TCPHDRSIZE+datasize);
/* The TCP pseudo-header was created. */
tcp->th_sport = htons(s_port);
tcp->th_dport = htons(t_port);
tcp->th_off = 5; /* 20 bytes, (no options) */
tcp->th_flags = tcpflags;
tcp->th_seq = htonl(seq);
tcp->th_ack = htonl(ack);
tcp->th_win = htons(win); /* we don't need any bigger, I guess. */
/* The necessary TCP header fields are set. */
tcp->th_sum = in_cksum(pseudo,PSEUDOHDRSIZE+TCPHDRSIZE+datasize);
memset(packet,0,IPHDRSIZE);
/* The pseudo-header is wiped to clear the IP header fields */
ip->saddr = s_addr;
ip->daddr = t_addr;
ip->version = 4;
ip->ihl = 5;
ip->ttl = 255;
ip->id = random()%1996;
ip->protocol = IPPROTO_TCP; /* should be 6 */
ip->tot_len = htons(IPHDRSIZE + TCPHDRSIZE + datasize);
ip->check = in_cksum((char *)packet,IPHDRSIZE);
/* The IP header is intact. The packet is ready. */
#ifdef TCP_PKT_DEBUG
printf("Packet ready. Dump: \n");
#ifdef TCP_PKT_DEBUG_DATA
HEXDUMP(IPHDRSIZE+TCPHDRSIZE+datasize,packet);
#else
HEXDUMP(IPHDRSIZE+TCPHDRSIZE,packet);
#endif
printf("\n");
#endif
return sendto(socket, packet, IPHDRSIZE+TCPHDRSIZE+datasize, 0, (struct sockaddr *)address, sizeof(struct sockaddr));
}
/*
* EOF
*/
/*
* Port Scanning without the SYN flag / Uriel Maimon
* (lifesux@cox.org)
*/
#define RESOLVE_QUIET
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "resolve.c"
#include "tcp_pkt.c"
#define STCP_VERSION "1.32"
#define STCP_PORT 1234 /* Our local port. */
#define STCP_SENDS 3
#define STCP_THRESHOLD 8
#define STCP_SLOWFACTOR 10
void banner(void)
{
printf("\nscantcp\n");
printf("version %s\n",STCP_VERSION);
}
void usage(const char *progname)
{
printf("\nusage: \n");
printf("%s |