Port scannen?



  • Weißde keiner Rat?

    anonymous2k



  • schreib doch erstmal wie du es geschafft hast. Evtl. kann dir dann jemand weiterhelfen.



  • OK,
    ich habe winsock2.h includiert.

    Dann mit diesem Code

    int startWinsock(void)
    {
      WSADATA wsa;
      return WSAStartup(MAKEWORD(2,0),&wsa);
    }
    
    int ScanPort(String IP, int Port)
    {
      long rc;
      SOCKET s;
    
      SOCKADDR_IN addr;
      rc=startWinsock();
      if(rc!=0)
      {
        return 1;
      }
    
    s=socket(AF_INET,SOCK_STREAM,0);
    if(s==INVALID_SOCKET)
    {
      return 1;
    }
    
      memset(&addr,0,sizeof(SOCKADDR_IN));
    addr.sin_family=AF_INET;
    addr.sin_port=htons(Port); // Port der üergeben wurde
    addr.sin_addr.s_addr=inet_addr(IP.c_str()); //Das zu scannende IP
    
    rc=connect(s,(SOCKADDR*)&addr,sizeof(SOCKADDR));
    
    if(rc==SOCKET_ERROR)
    {
      return 1;
    }
    else
    {
      closesocket(Port);
      return 0;
    }
    }
    

    Der Code funzt auch, aber leider dauert das Timeout zulange. (3 Sek ungefähr)

    Mit einem Registrywert konnte ich das Wiederholen bei Timeout schonmal ausstellen, aber es müsste ja eigendlich eine Möglichkeit geben Timeout auch auf 200ms zu setzen.

    Jemand Ideen?

    anonymous2k



  • Dieser Thread wurde von Moderator/in Jansen aus dem Forum Borland C++ Builder (VCL/CLX) in das Forum WinAPI verschoben.

    Im Zweifelsfall bitte auch folgende Hinweise beachten:
    C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?

    Dieses Posting wurde automatisch erzeugt.





  • Also, ich habe jetzt mal das setsockopt Beispiel kopiert und auf den Timeoutcode angepasst (nur den KEEPALIVE teil mit Timeout gewechselt)

    #include <stdio.h>
    #include "winsock2.h"
    
    void main() {
    
      //---------------------------------------
      // Declare variables
      WSADATA wsaData;
      SOCKET ListenSocket;
      sockaddr_in service;
    
      //---------------------------------------
      // Initialize Winsock
      int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
      if( iResult != NO_ERROR )
        printf("Error at WSAStartup\n");
    
      //---------------------------------------
      // Create a listening socket
      ListenSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
      if (ListenSocket == INVALID_SOCKET) {
        printf("Error at socket()\n");
        WSACleanup();
        return;
      }
    
      //---------------------------------------
      // Bind the socket to the local IP address
      // and port 27015
      hostent* thisHost;
      char* ip;
      u_short port;
      port = 80;
      thisHost = gethostbyname("");
      ip = inet_ntoa (*(struct in_addr *)*thisHost->h_addr_list);
      service.sin_family = AF_INET;
      service.sin_addr.s_addr = inet_addr(ip);
      service.sin_port = htons(port);
    
      if ( bind( ListenSocket,(SOCKADDR*) &service, sizeof(service) )  == SOCKET_ERROR ) {
        printf("bind failed\n");
        closesocket(ListenSocket);
        return;
      }
    
      //---------------------------------------
      // Initialize variables and call setsockopt. 
      // The SO_KEEPALIVE parameter is a socket option 
      // that makes the socket send keepalive messages
      // on the session. The SO_KEEPALIVE socket option
      // requires a boolean value to be passed to the
      // setsockopt function. If TRUE, the socket is
      // configured to send keepalive messages, if FALSE
      // the socket configured to NOT send keepalive messages.
      // This section of code tests the setsockopt function
      // by checking the status of SO_KEEPALIVE on the socket
      // using the getsockopt function.
      int bOptVal = 100;
      int bOptLen = sizeof(BOOL);
      int iOptVal;
      int iOptLen = sizeof(int);
    
      if (getsockopt(ListenSocket, SOL_SOCKET, SO_RCVTIMEO, (char*)&iOptVal, &iOptLen) != SOCKET_ERROR) {
        printf("SO_RCVTIMEO Value: %ld\n", iOptVal);
      }
    
      if (setsockopt(ListenSocket, SOL_SOCKET, SO_RCVTIMEO, (char*)&bOptVal,//hier bOptLen) != SOCKET_ERROR) { 
        printf("Set v: 100\n");
      }
    
      if (getsockopt(ListenSocket, SOL_SOCKET, SO_RCVTIMEO, (char*)&iOptVal, &iOptLen) != SOCKET_ERROR) {
        printf("SO_RCVTIMEO Value: %ld\n", iOptVal);
      }
      connect(ListenSocket,(SOCKADDR*) &service,sizeof(service));
      WSACleanup();
      return;
    
    }
    

    Jedoch funktioniert das nicht - warum?
    Es kommt kein Fehler, aber Timout wieder ert nach einigen Sekunden.

    anonymous2k



  • Ich weiß, Doppel+Nervpost,
    aber weiß noch jemand Vorschläge?



  • *auf ein letztes mal hoffend*



  • Hi...

    Also leider kann ich dir mit dem Timeoutproblem net helfen aber wenn du nur beabsichtigst deinen portscanner schneller zu machen dann lass ihn doch einfach paar Threads erstellen.

    while(startport <= endport+1)
    {
     Sleep(5);
     CreateThread(0, 0, &threadFkt, &startport, 0, 0);
     startport++;
    }
    

    und deine Funktion

    int ScanPort(String IP, int Port)
    

    baust du einfach zum Thread um.
    Ich weiss net ob du das beachsichtigs aber vlt. hilft dir das hier was, so würds jedenfalls ich machen, wenn ich in schneller machen wollte.
    Wenn du ne Lösung für das Timeoutproblem gefunden hast poste mal bitte würde ich auch interessieren *g*.



  • OK, stimmt, das ist auch eine Lösung!

    Jo danke, mach ich mal 😉

    anonymous2k


Anmelden zum Antworten