|
|
|
|
Strumenti |
14-09-2017, 12:52 | #1 |
Senior Member
Iscritto dal: Apr 2001
Città: Milano
Messaggi: 3594
|
c++ scanner di rete
oggi nmi hanno consegnato un nas del quale nn conosceno l'indirizzo IP. Dopo qualche tentativo anche attraverso un qualche ping di tipo broadcast, ho desistito ed ho usato il software di scansione messo a disposizione dal nas il quale ha identificato quasi subito l'indirizzo IP assegnato che era di classe B.
Da qui l'idea di scrivermi uno scanner per la mia lan, allo scopo di scoprire se esistono dispositivi settati con sottoreti diverse: da dove si parte? Ho letto un pò per quanto concerne il traffico di tipo broadcast ed in effetti mie era venuta l'idea che fosse sufficiente mettere un pacchetto broadcast in rete ed attendere che qualcuno risponda ma non è così semplice. grazie |
14-09-2017, 16:48 | #2 |
Senior Member
Iscritto dal: Apr 2001
Città: Milano
Messaggi: 3594
|
certo grazie 1000, ma volevo capire cosa c'era sotto. Ho già implementato tempo fa uno scanner per la mia lan, ma si ferma agli host della mia sottorete. Avevo iniziato a studiare il traffico di tipo broadcast, poi ho pensato ai server DHCP i quali restano in ascolto delle richieste dei client e forse si deve lavorare intorno a questo tipo di implementazioni. L'host si presenta a il server e attraverso richieste da parte dell'host, il server conosce l'IP che sino a quel momento gli è stato assegnato di default forse dall'SO stesso. Ho provato a capire anche attraverso uno sniffer in C molto semplice che avevo implementato ma non si vede nulla. Rimane aperta la strada del server dhcp.
|
15-09-2017, 11:38 | #3 |
Senior Member
Iscritto dal: Apr 2001
Città: Milano
Messaggi: 3594
|
credo di aver capito che si deve lavorare sul livello 2 iso/osi e cioè col protocollo arp
|
15-09-2017, 15:13 | #4 | |
Senior Member
Iscritto dal: May 2001
Messaggi: 12580
|
Quote:
Se hai già familiarità con la programmazione di rete (in C) non dovrebbe esserti difficile: http://man7.org/linux/man-pages/man7/packet.7.html Per quanto riguarda altri linguaggi non so se i linguaggi di più alto livello del C supportano questa tipologia di socket. |
|
15-09-2017, 15:50 | #5 | |
Senior Member
Iscritto dal: Apr 2001
Città: Milano
Messaggi: 3594
|
Quote:
si, qualcosa l'ho già sviluppato in ambiente windows. Grazie |
|
19-09-2017, 06:30 | #6 |
Senior Member
Iscritto dal: Apr 2001
Città: Milano
Messaggi: 3594
|
sbaglio o le raw socket in windows non sono implementate al 1oo% nel senso che si hanno delle limitazioni?
Vorrei evitare di perdere tempo dietro ad un SO sempre più blindato |
20-09-2017, 06:33 | #7 |
Senior Member
Iscritto dal: Apr 2001
Città: Milano
Messaggi: 3594
|
nel caso vi servisse a scopo studio
Codice:
#pragma comment(lib, "ws2_32.lib") #include <winsock2.h> #include <stdio.h> #include <string.h> #include <windows.h> #include <ws2tcpip.h> #include <time.h> /* packet header structures */ #pragma pack(push, 1) struct iphdr { unsigned char ihl:4; unsigned char ver:4; unsigned char tos; unsigned short totlen; unsigned short id; unsigned short frag_and_flags; unsigned char ttl; unsigned char proto; unsigned short checksum; unsigned int src; unsigned int dst; }; struct tcphdr { unsigned short sport; unsigned short dport; unsigned int seq; unsigned int acknum; unsigned char unused:4; unsigned char tcphl:4; unsigned char Flags; unsigned short Windows; unsigned short cksum; unsigned short UrgPointer; }; #pragma pack(pop) #define HOSTNAME_LEN 1024 #define SIO_RCVALL _WSAIOW(IOC_VENDOR,1) #define PAKSIZE 65536 void init_opt(int, char **); void init_net(void); void die(char *); void process_pak(char *, int); void bind_to_interface(int); void WriteData( const char* ); /* G L O B A L S */ SOCKET s0k; short promiscuous=1; int minSize = 30; unsigned char NavyFieldHDR [48] = {4, 3, 2, 1}; // Pin C o d e versturen // 04 03 02 01 01 61 01 80 00 00 00 00 00 00 00 00 // 04 00 00 00 XX XX XX XX CC OO DD EE // File.cpp //#include "header.h" #include <iostream> using namespace std; /* open raw socket, set promiscuous mode */ void init_net() { WSADATA w; SOCKADDR_IN sa; DWORD bytes; char hostname[HOSTNAME_LEN]; struct hostent *h; unsigned int opt = 1; if (WSAStartup(MAKEWORD(2,2), &w) != 0) die("WSAStartup failed\n"); if ((s0k = socket(AF_INET, SOCK_RAW, IPPROTO_IP)) == INVALID_SOCKET) die("unable to open raw socket\n"); // use default interface if ((gethostname(hostname, HOSTNAME_LEN)) == SOCKET_ERROR) die("unable to gethostname\n"); if ((h = gethostbyname(hostname)) == NULL) die("unable to gethostbyname\n"); sa.sin_family = AF_INET; sa.sin_port = htons(6000); memcpy(&sa.sin_addr.S_un.S_addr, h->h_addr_list[0], h->h_length); if ((bind(s0k, (SOCKADDR *)&sa, sizeof(sa))) == SOCKET_ERROR) die("unable to bind() socket\n"); /* -d on the command line to disable promiscuous mode */ if ((WSAIoctl(s0k, SIO_RCVALL, &opt, sizeof(opt), NULL, 0, &bytes, NULL, NULL)) == SOCKET_ERROR) die("failed to set promiscuous mode\n"); } int main() { char pak[PAKSIZE]; DWORD bytes; init_net(); WriteData( "Program has started: " ); WriteData( "\r\n\r\n" ); while(1) { memset(pak, 0, sizeof(pak)); if ((bytes = recv(s0k, pak, sizeof(pak), 0)) == SOCKET_ERROR) { die("socket error on recv\n"); }else{ process_pak(pak, bytes); } } } void WriteData( const char* buffer ) { FILE * pFile; pFile = fopen( "./File1.txt", "a" ); printf( buffer ); fprintf( pFile, buffer ); fclose( pFile ); } /* parse pak, print out requested fields */ void process_pak(char *pak, int len) { struct iphdr *ip; struct tcphdr *tcp; char *data; unsigned char proto; /* to avoid repeated dereferencing */ int i, j, k, end, datasize; ip = (struct iphdr *) pak; proto = ip->proto; printf("Prototype: %i\n", (unsigned char)proto); if(proto == IPPROTO_TCP) { tcp = (struct tcphdr *) (pak + (ip->ihl * 4)); data = pak + (ip->ihl * 4) + (tcp->tcphl * 4); datasize = ntohs(ip->totlen) - (ip->ihl*4) - (tcp->tcphl*4); /* ==================================== */ /* START CONTROLE FUNCTIE */ /* ==================================== */ i = 0; // If the packet size is smaller than the required size, just trow it away! // if( datasize < minSize ) // { // return; // } char temp[50]; sprintf(temp, "New Package: %x %x %x %x", data[0], data[1], data[2], data[3]); WriteData( temp ); WriteData("\r\n"); printf("DataSize: %i\r\n", datasize); // Print all data to the file. for(j=0; j<datasize; j++) { char temp [10]; sprintf(temp, "%4i", data[j]); WriteData( temp ); i = (i+1); if(i==4){ WriteData(" "); } if(i==8) { WriteData(" "); } if(i==12){ WriteData(" "); } if(i==16) { WriteData(" -> "); i=0; for(k=(j-15); k<(j+1); k++) { char temp [10]; sprintf(temp, "%1c", data[k]); if(data[k] < 32) { sprintf(temp, "%1c", '.'); } WriteData( temp ); i = (i+1); if(i==8) { WriteData(" "); } } WriteData("\r\n"); i=0; } } if(i > 0) WriteData("\r\n"); WriteData("\r\n"); }else{ // No TCP protocol: return and don't waist any time! return; } } void bind_to_interface(int choice) { SOCKET sd; sd = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0); if (sd == SOCKET_ERROR) printf("error on WSASocket\n"); INTERFACE_INFO InterfaceList[20]; unsigned long nBytesReturned; if (WSAIoctl(sd, SIO_GET_INTERFACE_LIST, 0, 0, &InterfaceList, sizeof(InterfaceList), &nBytesReturned, 0, 0) == SOCKET_ERROR) { printf("error fetching interface list\n"); } int nNumInterfaces = nBytesReturned / sizeof(INTERFACE_INFO); if (choice > nNumInterfaces) { die("invalid interface selection\n"); } if (choice) { // bind to the specified interface and return SOCKADDR_IN *pAddress; pAddress = (SOCKADDR_IN *) & (InterfaceList[choice-1].iiAddress); printf("using interface: %s\n", inet_ntoa(pAddress->sin_addr)); if ((bind(s0k, (SOCKADDR *)&(InterfaceList[choice-1].iiAddress), sizeof(SOCKADDR_IN))) == SOCKET_ERROR) die("unable to bind() socket\n"); return; } } void die(char *s) { WSACleanup(); WriteData( "%s" ); exit(-1); } Ultima modifica di misterx : 20-09-2017 alle 06:36. |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 18:22.