' Service banner grabber via TCP — connect and read initial response
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i  AS LONG
    LOCAL numTargets  AS LONG
    LOCAL targetIP(10) AS STRING
    LOCAL targetPort(10) AS LONG
    LOCAL targetName(10) AS STRING
    LOCAL bannerResult(10) AS STRING
    LOCAL connected  AS LONG
    LOCAL bannerLen  AS LONG
    LOCAL detectedService  AS LONG
    LOCAL serviceVersion  AS LONG
    LOCAL s  AS STRING
    LOCAL b  AS STRING
    PRINT "=== Service Banner Grabber ==="
    PRINT

    numTargets = 5
    targetIP(0) = "192.168.1.1    "
    targetPort(0) = 22
    targetName(0) = "SSH                     "

    targetIP(1) = "192.168.1.1    "
    targetPort(1) = 80
    targetName(1) = "HTTP                    "

    targetIP(2) = "192.168.1.10   "
    targetPort(2) = 21
    targetName(2) = "FTP                     "

    targetIP(3) = "192.168.1.20   "
    targetPort(3) = 443
    targetName(3) = "HTTPS                   "

    targetIP(4) = "8.8.8.8        "
    targetPort(4) = 53
    targetName(4) = "DNS                     "

    bannerResult(0) = "SSH-2.0-OpenSSH_8.9p1 Ubuntu-3               "
    bannerResult(1) = "HTTP/1.1 200 OK\r\nServer: Apache/2.4.41   "
    bannerResult(2) = "220 ProFTPD 1.3.5 Server Ready              "
    bannerResult(3) = "TLSv1.3 handshake initiated...              "
    bannerResult(4) = "DNS response received (53 bytes)            "

    PRINT "--- Banner Grab Results ---"
    PRINT

    FOR i = 0 TO numTargets - 1
        s = TRIM$(targetName(i))
        b = TRIM$(bannerResult(i))
        PRINT "  Target: " & TRIM$(targetIP(i)) & ":" & FORMAT$(targetPort(i)) & " (" & s & ")"

        PRINT "  Connecting..."
        connected = 1
        IF connected = 1 THEN
            PRINT "  Connected!"
            PRINT "  Reading banner..."
            PRINT "  Banner (" & FORMAT$(LEN(b)) & " bytes):"
            PRINT "    " & b

            IF INSTR(1, b, "SSH") > 0 THEN
                detectedService = 1
                PRINT "  Detected: SSH server"
            ELSE
                IF INSTR(1, b, "HTTP") > 0 THEN
                    detectedService = 2
                    PRINT "  Detected: HTTP/Web server"
                ELSE
                    IF INSTR(1, b, "FTP") > 0 OR INSTR(1, b, "220") > 0 THEN
                        detectedService = 3
                        PRINT "  Detected: FTP server"
                    ELSE
                        IF INSTR(1, b, "TLS") > 0 THEN
                            detectedService = 4
                            PRINT "  Detected: HTTPS/TLS"
                        ELSE
                            detectedService = 0
                            PRINT "  Detected: Unknown service"
                        END IF
                    END IF
                END IF
            END IF
        ELSE
            PRINT "  Connection failed or timed out"
        END IF
        PRINT "  Closing connection."
        PRINT
    NEXT i

    PRINT "--- Banner Grab Techniques ---"
    PRINT "  HTTP: Send GET / HTTP/1.0\r\n\r\n"
    PRINT "  SMTP: Wait for 220 greeting"
    PRINT "  FTP: Wait for 220 greeting"
    PRINT "  SSH: Read SSH-2.0- banner"
    PRINT "  POP3: Wait for +OK greeting"
    PRINT "  IMAP: Wait for * OK greeting"
    PRINT

    PRINT "--- Security Notes ---"
    PRINT "  Banner grabbing can reveal service versions"
    PRINT "  Many services allow banner suppression"
    PRINT "  Change Server: / Banner: headers"
    PRINT "  Use SSHd Config: DebianBanner no"

    PRINT
    PRINT "Banner grabber simulation complete."

    FUNCTION = 0
END FUNCTION
