Skip to content

Instantly share code, notes, and snippets.

@R3DHULK
Created February 4, 2023 16:38
Show Gist options
  • Save R3DHULK/361305024c0f7317fdc0b9dcea80bd0e to your computer and use it in GitHub Desktop.
Save R3DHULK/361305024c0f7317fdc0b9dcea80bd0e to your computer and use it in GitHub Desktop.
Check If There Is Any Banner Available Or Not In CPP
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <time.h>
// github : https://github.com/R3DHULK/
#define PORT 80 // the port number to connect to
#define BUFSIZE 1024
int main(int argc, char *argv[])
{
int sockfd;
struct sockaddr_in target_addr;
struct hostent *target;
char buf[BUFSIZE];
int numbytes;
if (argc != 2) {
fprintf(stderr, "Usage: %s <hostname>\n", argv[0]);
return 1;
}
if ((target = gethostbyname(argv[1])) == NULL) {
perror("gethostbyname");
return 1;
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
return 1;
}
target_addr.sin_family = AF_INET;
target_addr.sin_port = htons(PORT);
target_addr.sin_addr = *((struct in_addr *)target->h_addr);
memset(&(target_addr.sin_zero), '\0', 8);
if (connect(sockfd, (struct sockaddr *)&target_addr, sizeof(struct sockaddr)) == -1) {
perror("connect");
return 1;
}
// send a simple HTTP request to retrieve the banner
sprintf(buf, "HEAD / HTTP/1.0\r\n\r\n");
send(sockfd, buf, strlen(buf), 0);
if ((numbytes = recv(sockfd, buf, BUFSIZE - 1, 0)) == -1) {
perror("recv");
return 1;
}
buf[numbytes] = '\0';
printf("Banner:\n%s\n", buf);
close(sockfd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment