c语言端口扫描_c端口扫描代码

hacker|
223

100分求linux下C语言端口扫描代码

linux tcp udp 端口扫描源程序

#include sys/socket.h

#include netinet/in.h

#include arpa/inet.h

#include unistd.h

#include errno.h

#include netdb.h

#include stdio.h

#include string.h

#include netinet/ip_icmp.h

#include stdlib.h

#include signal.h

#include libxml/parser.h

#include libxml/tree.h

#define TRUE 1

#define FALSE 0

#define UDP "UDP"

#define TCP "TCP"

#define tcp "tcp"

#define udp "udp"

typedef struct _GsSockStru{

int fd;

int len;

struct sockaddr_in addr;

}GsSockStru;

static int tcptest( char ip[32], char port[20]);

static int udptest( char ip[32], char port[20]);

void sig_alrm( int signo );

static GsSockStru test_sock;

int

main( int argc, char** argv)

{

char string[64];

char port[20];

char pro[20];

char ip[32];

int res;

int i = 0;

int k = 0;

if( argc2 || argc2 )

{

printf("鍙傛暟涓嶆纭?-1\n");

return ( -1 );

}

strcpy( string, argv[1]);

while( *string )

{

if( string[i] == ':' )

break;

pro[k] = string[i];

k++;

i++;

}

pro[k] = '\0';

i++;

k = 0;

while( *string )

{

if( string[i] == ':')

break;

ip[k] = string[i];

k++;

i++;

}

ip[k] = '\0';

i++;

k=0;

while( *string )

{

if( string[i] == '\0')

break;

port[k] = string[i];

k++;

i++;

}

port[k] = '\0';

i++;

memset( test_sock, 0, sizeof( test_sock ) );

if ( ( strcmp( TCP, pro) != 0 ) ( strcmp( UDP, pro) != 0 ) ( strcmp( tcp, pro) != 0 ) ( strcmp( udp, pro) != 0 ))

{

printf ( "鍙傛暟涓嶆纭?锛?\n" );

return (-1);

}

if ( strcmp( TCP, pro) == 0 || strcmp( tcp, pro) == 0 )

res = tcptest( ip, port );

if ( strcmp( UDP, pro) == 0 || strcmp( udp, pro) == 0 )

res = udptest( ip, port );printf("%d\n",res);

return ( res );

}

int

tcptest( char ip[32], char port[20])

{

int res;

struct timeval tv;

test_sock.fd = socket( AF_INET, SOCK_STREAM, 0 );

if ( test_sock.fd 0 )

{

printf( "create socket failed -3 \n" );

return ( -3 );

}

memset( ( test_sock.addr ), 0, sizeof( test_sock.addr ) );

test_sock.addr.sin_family = AF_INET;

test_sock.addr.sin_port = htons( atoi( port ) );

inet_pton( AF_INET, ip, test_sock.addr.sin_addr );

test_sock.len = sizeof( struct sockaddr );

tv.tv_sec = 10;

tv.tv_usec = 0;

setsockopt( test_sock.fd, SOL_SOCKET, SO_RCVTIMEO,

(const char *)tv, sizeof( tv ) );

res = connect( test_sock.fd,

( struct sockaddr * )( ( test_sock.addr ) ),

test_sock.len );

if ( res 0 )

{

fprintf( stderr, "connect failed 0\n" );

close( test_sock.fd );

return FALSE;

}

close( test_sock.fd );

return TRUE;

}

int udptest( char ip[32], char port[20])

{

struct icmphdr *icmp_header;

struct sockaddr_in target_info;

int target_info_len;

fd_set read_fd;

int scan_port;

char recvbuf[5000];

struct sockaddr_in target_addr;

int icmp_socket;

int udp_socket;

struct timeval tv;

icmp_header = (struct icmphdr *)(recvbuf+sizeof(struct iphdr));

scan_port = atoi( port );

target_addr.sin_family = AF_INET;

inet_pton( AF_INET, ip, target_addr.sin_addr );

target_addr.sin_port = htons(scan_port);

if ((udp_socket=socket(AF_INET,SOCK_DGRAM,0))==-1)

{

printf("create socket failed -3\n");

return -3;

}

if ((icmp_socket=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP))==-1)

{

printf("Create raw socket failed -3\n");

return -3;

}

sendto(udp_socket,NULL,0,0,(void *)target_addr,sizeof(target_addr));

FD_ZERO(read_fd);

FD_SET(icmp_socket,read_fd);

tv.tv_sec = 1;

tv.tv_usec = 0;

select(FD_SETSIZE,read_fd,NULL,NULL,tv);

for (;;){

if (FD_ISSET(icmp_socket,read_fd))

{

target_info_len = sizeof(target_info);

recvfrom(icmp_socket,recvbuf,5000,0,

(struct sockaddr *)target_info,target_info_len);

if (target_info.sin_addr.s_addr == target_addr.sin_addr.s_addr

icmp_header-type == 3 icmp_header-code=12)

{

printf("Port %d : Close\n",scan_port);

return (0);

}

}

return (1) ;

}

}

批处理扫描IP段 指定端口 给个代码谢谢 需要导出结果的

@echo off

setlocal

cls

color e

title SYN(正在扫描,请稍候.......)

for /f "eol= tokens=1,2 delims= " %%i in (ip.txt) do s syn %%i %%j 端口号 /save

for /f "eol=- tokens=1 delims= " %%i in (result.txt) do echo %%is1.txt

for /f "eol=P tokens=1 delims= " %%i in (s1.txt) do echo %%is2.txt

for /f "eol=S tokens=1 delims= " %%i in (s2.txt) do echo %%iips.txt

del s1.txt

del s2.txt

exit

前提: 这是一段批处理 在批处理同一目录下得有个s扫描器

然后机器得破解了tcpip的并发数补丁

另外翻盘好不容易找到了这段批处理~~~~

8*8双色点阵怎么用,C语言代码怎么写?

1、8*8双色点阵实际上是两个8*8点阵,必须增加一个端口来控制是使用哪个颜色的8*8点阵;或者连接成一个8*16点阵来控制。C语言代码和控制8*8点阵类似,增加扫描端口就可以了。

2、例程:

#include reg51.h 

unsigned char code tab[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; 

unsigned char code digittab[10][8]={

                                    {0x00,0x00,0x3e,0x41,0x41,0x41,0x3e,0x00}, //0 

                                    {0x00,0x00,0x00,0x00,0x21,0x7f,0x01,0x00}, //1 

                                    {0x00,0x00,0x27,0x45,0x45,0x45,0x39,0x00}, //2 

                                    {0x00,0x00,0x22,0x49,0x49,0x49,0x36,0x00}, //3 

                                    {0x00,0x00,0x0c,0x14,0x24,0x7f,0x04,0x00}, //4 

                                    {0x00,0x00,0x72,0x51,0x51,0x51,0x4e,0x00}, //5 

                                    {0x00,0x00,0x3e,0x49,0x49,0x49,0x26,0x00}, //6 

                                    {0x00,0x00,0x40,0x40,0x40,0x4f,0x70,0x00}, //7 

                                    {0x00,0x00,0x36,0x49,0x49,0x49,0x36,0x00}, //8 

                                    {0x00,0x00,0x32,0x49,0x49,0x49,0x3e,0x00} //9

                                    }; 

  

unsigned int timecount; 

unsigned char cnta; 

unsigned char cntb; 

  

void main(void) 

  TMOD=0x01; 

  TH0=(65536-1000)/256; 

  TL0=(65536-1000)%256; 

  TR0=1; 

  ET0=1; 

  EA=1; 

  while(1) 

    {; 

    } 

  

void t0(void) interrupt 1 using 0 

  TH0=(65536-1000)/256; 

  TL0=(65536-1000)%256; 

  P3=tab[cnta]; 

  P1=digittab[cntb][cnta]; 

  cnta++; 

  if(cnta==8) 

    { 

      cnta=0; 

    } 

  timecount++; 

  if(timecount==333) 

    { 

      timecount=0; 

      cntb++; 

      if(cntb==10) 

        { 

          cntb=0; 

        } 

    } 

}

arduino怎么实现不断扫描端口

arduino程序运行时,就是不断循环,就是程序中的

void loop(){

// 在这里加入你的loop代码,它会不断重复运行:

}

只要在loop里面加入读取端口状态的代码,就能实现扫描端口。

但是要考虑完成每个loop循环的时间,看是否能及时发现端口状态的改变。

而如果需要即时对端口状态作出反应,应该使用中断技术而不是扫描。

C语言与 *** 问题 高手进

ping命令是dos命令可以创建一个cmd进程,然后建立两个管道用来得到结果 实现.

下面是一个 *** 编程的实例, Syn扫描器代码,可以作为参考.

基本可以看为是 ping命令的实现

//getallIP.cpp

#include winsock2.h

#include stdio.h

#pragma comment(lib,"ws2_32.lib")

int main()

{

////////////////

// 初始化 Windows sockets API.

//

WORD wVersionRequested = MAKEWORD(2, 2);

WSADATA wsaData;

if (WSAStartup(wVersionRequested, wsaData)) {

printf("WSAStartup failed %s\n", WSAGetLastError());

return 0;

}

//////////////////

// 获得主机名.

//

char hostname[256];

int res = gethostname(hostname, sizeof(hostname));

if (res != 0) {

printf("Error: %u\n", WSAGetLastError());

return 0;

}

printf("hostname=%s\n", hostname);

////////////////

// 根据主机名获取主机信息.

//

hostent* pHostent = gethostbyname(hostname);

if (pHostent==NULL) {

printf("Error: %u\n", WSAGetLastError());

return 0;

}

//////////////////

// 解析返回的hostent信息.

//

hostent he = *pHostent;

printf("name=%s\naliases=%s\naddrtype=%d\nlength=%d\n",

he.h_name, he.h_aliases, he.h_addrtype, he.h_length);

sockaddr_in sa;

//根据 he.h_addr_list[nAdapter]是否为空来获取所有IP地址

for (int nAdapter=0; he.h_addr_list[nAdapter]; nAdapter++) {

memcpy ( sa.sin_addr.s_addr, he.h_addr_list[nAdapter],he.h_length);

// 输出机器的IP地址.

printf("Address [%d%]: %s\n",nAdapter, inet_ntoa(sa.sin_addr)); // 显示地址串

}

//////////////////

// 终止 Windows sockets API

//

WSACleanup();

return 0;

}

//mstcpip.h

// Copyright (C) Microsoft Corporation, 1996-1999

#if _MSC_VER 1000

#pragma once

#endif

/* Argument structure for SIO_KEEPALIVE_VALS */

struct tcp_keepalive {

u_long onoff;

u_long keepalivetime;

u_long keepaliveinterval;

};

// New WSAIoctl Options

#define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)

#define SIO_RCVALL_MCAST _WSAIOW(IOC_VENDOR,2)

#define SIO_RCVALL_IGMPMCAST _WSAIOW(IOC_VENDOR,3)

#define SIO_KEEPALIVE_VALS _WSAIOW(IOC_VENDOR,4)

#define SIO_ABSORB_RTRALERT _WSAIOW(IOC_VENDOR,5)

#define SIO_UCAST_IF _WSAIOW(IOC_VENDOR,6)

#define SIO_LIMIT_BROADCASTS _WSAIOW(IOC_VENDOR,7)

#define SIO_INDEX_BIND _WSAIOW(IOC_VENDOR,8)

#define SIO_INDEX_MCASTIF _WSAIOW(IOC_VENDOR,9)

#define SIO_INDEX_ADD_MCAST _WSAIOW(IOC_VENDOR,10)

#define SIO_INDEX_DEL_MCAST _WSAIOW(IOC_VENDOR,11)

//synscan.cpp

#include winsock2.h

#include ws2tcpip.h

#include stdio.h

#include time.h

#include "mstcpip.h"

#pragma comment(lib,"ws2_32.lib")

#define SEQ 0x28376839

SOCKET sockRaw = INVALID_SOCKET,

sockListen = INVALID_SOCKET;

struct sockaddr_in dest;

BOOL ScanOK=FALSE;

char *DEST_HOST;

int DEST_PORT;

int DEST_PORTEND;

int play=0;

clock_t start,end;//程序运行的起始和结束时间

float costtime;//程序耗时

typedef struct _iphdr

{

unsigned char h_lenver; //4位首部长度+4位IP版本号

unsigned char tos; //8位服务类型TOS

unsigned short total_len; //16位总长度(字节)

unsigned short ident; //16位标识

unsigned short frag_and_flags; //3位标志位

unsigned char ttl; //8位生存时间 TTL

unsigned char proto; //8位协议 (TCP, UDP 或其他)

unsigned short checksum; //16位IP首部校验和

unsigned int sourceIP; //32位源IP地址

unsigned int destIP; //32位目的IP地址

}IP_HEADER;

typedef struct _tcphdr //定义TCP首部

{

USHORT th_sport; //16位源端口

USHORT th_dport; //16位目的端口

unsigned int th_seq; //32位序列号

unsigned int th_ack; //32位确认号

unsigned char th_lenres; //4位首部长度/6位保留字

unsigned char th_flag; //6位标志位

USHORT th_win; //16位窗口大小

USHORT th_sum; //16位校验和

USHORT th_urp; //16位紧急数据偏移量

}TCP_HEADER;

struct //定义TCP伪首部

{

unsigned long saddr; //源地址

unsigned long daddr; //目的地址

char mbz;

char ptcl; //协议类型

unsigned short tcpl; //TCP长度

}psd_header;

//SOCK错误处理程序

void CheckSockError(int iErrorCode, char *pErrorMsg)

{

if(iErrorCode==SOCKET_ERROR)

{

printf("%s Error:%d\n", pErrorMsg, GetLastError());

closesocket(sockRaw);

ExitProcess(-1);

}

}

//计算检验和

USHORT checksum(USHORT *buffer, int size)

{

unsigned long cksum=0;

while (size 1)

{

cksum += *buffer++;

size -= sizeof(USHORT);

}

if (size)

{

cksum += *(UCHAR*)buffer;

}

cksum = (cksum 16) + (cksum 0xffff);

cksum += (cksum 16);

return (USHORT)(~cksum);

}

//IP解包程序

bool DecodeIPHeader(char *buf, int bytes)

{

IP_HEADER *iphdr;

TCP_HEADER *tcphdr;

unsigned short iphdrlen;

iphdr = (IP_HEADER *)buf;

iphdrlen = sizeof(unsigned long) * (iphdr-h_lenver 0xf);

tcphdr = (TCP_HEADER*)(buf + iphdrlen);

//是否来自目标IP

if(iphdr-sourceIP != dest.sin_addr.s_addr) return false;

//序列号是否正确

if((ntohl(tcphdr-th_ack) != (SEQ+1)) (ntohl(tcphdr-th_ack) != SEQ)) return false;

//if(tcphdr-th_flag == 20)return true;

//SYN/ACK - 扫描到一个端口

if(tcphdr -th_flag == 18)

{

printf("\t%d\t open \n",ntohs(tcphdr-th_sport));

return true;

}

return true;

}

void usage(void)

{

printf("\t===================SYN portscaner======================\n");

printf("\t============gxisone@hotmail.com 2004/7/6===========\n");

printf("\tusage: synscan DomainName[IP] StartPort-EndPort\n");

printf("\tExample: synscan 1-139\n");

printf("\tExample: synscan 192.168.1.1 8000-9000\n");

}

DWORD WINAPI RecvThread(LPVOID para)//接收数据线程函数

{

int iErrorCode;

struct hostent *hp;

char RecvBuf[65535]={0};

sockListen = socket(AF_INET , SOCK_RAW , IPPROTO_IP);

CheckSockError(sockListen, "socket");

//设置IP头操作选项

BOOL bOpt = true;

iErrorCode = setsockopt(sockRaw,IPPROTO_IP,IP_HDRINCL,(char *)bOpt,sizeof(bOpt));

CheckSockError(iErrorCode, "setsockopt()");

//获得本地IP

SOCKADDR_IN sa;

unsigned char LocalName[256];

iErrorCode = gethostname((char*)LocalName,sizeof(LocalName)-1);

CheckSockError(iErrorCode, "gethostname()");

if((hp = gethostbyname((char*)LocalName)) == NULL)

{

CheckSockError(SOCKET_ERROR, "gethostbyname()");

}

memcpy(sa.sin_addr.S_un.S_addr,hp-h_addr_list[1],hp-h_length);

sa.sin_family = AF_INET;

sa.sin_port = htons(7000);

iErrorCode = bind(sockListen, (PSOCKADDR)sa, sizeof(sa));

CheckSockError(iErrorCode, "bind");

//设置SOCK_RAW为SIO_RCVALL,以便接收所有的IP包

DWORD dwBufferLen[10] ;

DWORD dwBufferInLen = 1 ;

DWORD dwBytesReturned = 0 ;

iErrorCode=WSAIoctl(sockListen, SIO_RCVALL,dwBufferInLen, sizeof

(dwBufferInLen),dwBufferLen, sizeof(dwBufferLen),dwBytesReturned , NULL , NULL );

CheckSockError(iErrorCode, "Ioctl");

memset(RecvBuf, 0, sizeof(RecvBuf));

//接收数据

for(;;)

{

iErrorCode = recv(sockListen, RecvBuf, sizeof(RecvBuf), 0);

//CheckSockError(iErrorCode, "recv");

DecodeIPHeader(RecvBuf,iErrorCode) ;

}

if(ScanOK)

{

closesocket(sockListen);

return 0;

}

}

void playx(void) // 定义状态提示函数

{

// 进度条

char *plays[12]=

{

" | ",

" / ",

" - ",

" \\ ",

" | ",

" / ",

" - ",

" \\ ",

" | ",

" / ",

" - ",

" \\ ",

};

printf(" =%s=\r", plays[play]);

play=(play==11)?0:play+1;

Sleep(2);

}

//主函数

int main(int argc,char **argv)

{

char *p;

if(argc!=3)

{

usage();

return 0;

}

p=argv[2];//处理端口参数

if(strstr(argv[2],"-"))

{ DEST_PORT=atoi(argv[2]);

for(;*p;)

if(*(p++)=='-')break;

DEST_PORTEND=atoi(p);

if(DEST_PORT1 || DEST_PORTEND65535)

{ printf("Port Error!\n");

return 0;

}

}

DEST_HOST=argv[1];

usage();

int iErrorCode;

int datasize;

struct hostent *hp;

IP_HEADER ip_header;

TCP_HEADER tcp_header;

char SendBuf[128]={0};

//初始化SOCKET

WSADATA wsaData;

iErrorCode = WSAStartup(MAKEWORD(2,2),wsaData);

CheckSockError(iErrorCode, "WSAStartup()");

sockRaw = socket(AF_INET , SOCK_RAW , IPPROTO_IP);

CheckSockError(sockRaw, "socket()");

sockListen = socket(AF_INET , SOCK_RAW , IPPROTO_IP);

CheckSockError(sockListen, "socket");

//设置IP头操作选项

BOOL bOpt = true;

iErrorCode = setsockopt(sockRaw,IPPROTO_IP,IP_HDRINCL,(char *)bOpt,sizeof(bOpt));

CheckSockError(iErrorCode, "setsockopt()");

//获得本地IP

SOCKADDR_IN sa;

unsigned char LocalName[256];

iErrorCode = gethostname((char*)LocalName,sizeof(LocalName)-1);

CheckSockError(iErrorCode, "gethostname()");

if((hp = gethostbyname((char*)LocalName)) == NULL)

{

CheckSockError(SOCKET_ERROR, "gethostbyname()");

}

memcpy(sa.sin_addr.S_un.S_addr,hp-h_addr_list[1],hp-h_length);

sa.sin_family = AF_INET;

sa.sin_port = htons(7000);

iErrorCode = bind(sockListen, (PSOCKADDR)sa, sizeof(sa));

CheckSockError(iErrorCode, "bind");

//获得目标主机IP

memset(dest,0,sizeof(dest));

dest.sin_family = AF_INET;

dest.sin_port = htons(DEST_PORT);

if((dest.sin_addr.s_addr = inet_addr(DEST_HOST)) == INADDR_NONE)

{

if((hp = gethostbyname(DEST_HOST)) != NULL)

{

memcpy((dest.sin_addr),hp-h_addr_list[1],hp-h_length);

dest.sin_family = hp-h_addrtype;

printf("dest.sin_addr = %s\n",inet_ntoa(dest.sin_addr));

}

else

{

CheckSockError(SOCKET_ERROR, "gethostbyname()");

}

}

//开启监听线程

HANDLE Thread=CreateThread(NULL,0,RecvThread,0,0,0);

//填充IP首部

ip_header.h_lenver=(44 | sizeof(ip_header)/sizeof(unsigned long));

//高四位IP版本号,低四位首部长度

ip_header.total_len=htons(sizeof(IP_HEADER)+sizeof(TCP_HEADER)); //16位总长度(字节)

ip_header.ident=1; //16位标识

ip_header.frag_and_flags=0; //3位标志位

ip_header.ttl=128; //8位生存时间TTL

ip_header.proto=IPPROTO_TCP; //8位协议(TCP,UDP…)

ip_header.checksum=0; //16位IP首部校验和

ip_header.sourceIP=sa.sin_addr.s_addr; //32位源IP地址

ip_header.destIP=dest.sin_addr.s_addr; //32位目的IP地址

//填充TCP首部

tcp_header.th_sport=htons(7000); //源端口号

tcp_header.th_lenres=(sizeof(TCP_HEADER)/44|0); //TCP长度和保留位

tcp_header.th_win=htons(16384);

//填充TCP伪首部(用于计算校验和,并不真正发送)

psd_header.saddr=ip_header.sourceIP;

psd_header.daddr=ip_header.destIP;

psd_header.mbz=0;

psd_header.ptcl=IPPROTO_TCP;

psd_header.tcpl=htons(sizeof(tcp_header));

Sleep(500);

printf("\n");

printf("Scaning %s\n",DEST_HOST);

start=clock();//开始计时

for(;DEST_PORTDEST_PORTEND;DEST_PORT++)

{

playx();

tcp_header.th_dport=htons(DEST_PORT); //目的端口号

tcp_header.th_ack=0; //ACK序列号置为0

tcp_header.th_flag=2; //SYN 标志

tcp_header.th_seq=htonl(SEQ); //SYN序列号

tcp_header.th_urp=0; //偏移

tcp_header.th_sum=0; //校验和

//计算TCP校验和,计算校验和时需要包括TCP pseudo header

memcpy(SendBuf,psd_header,sizeof(psd_header));

memcpy(SendBuf+sizeof(psd_header),tcp_header,sizeof(tcp_header));

tcp_header.th_sum=checksum((USHORT *)SendBuf,sizeof(psd_header)+sizeof(tcp_header));

//计算IP校验和

memcpy(SendBuf,ip_header,sizeof(ip_header));

memcpy(SendBuf+sizeof(ip_header),tcp_header,sizeof(tcp_header));

memset(SendBuf+sizeof(ip_header)+sizeof(tcp_header),0,4);

datasize=sizeof(ip_header)+sizeof(tcp_header);

ip_header.checksum=checksum((USHORT *)SendBuf,datasize);

//填充发送缓冲区

memcpy(SendBuf,ip_header,sizeof(ip_header));

//发送TCP报文

iErrorCode=sendto(sockRaw,SendBuf,datasize,0,(struct sockaddr*) dest,

sizeof(dest));

CheckSockError(iErrorCode, "sendto()");

}

end=clock();//计时结束

ScanOK=TRUE;

printf("Closeing Thread.....\n");

WaitForSingleObject(Thread,5000);

CloseHandle(Thread);

costtime= (float)(end - start) / CLOCKS_PER_SEC; //转换时间格式

printf("Cost time:%f Sec",costtime);//显示耗时

//退出前清理

if(sockRaw != INVALID_SOCKET) closesocket(sockRaw);

WSACleanup();

return 0;

}

linux下用C写的一个端口扫描器,想得到扫描主机的操作系统类型

nmap 命令行

zenmap 图形化界面

一般能扫描出主机的操作系统版本

0条大神的评论

发表评论