Verify the "IP address or domain name" obtained from the outside to avoid command injection

[question:]

Because the parameters of the IP address or domain name are obtained through the configuration file, and when the client connection broker is abnormal, it is necessary to Ping the IP through the system() system call and record the results in the log;

However, if the command executed by the system() function is abnormally modified, it may cause system exceptions. Therefore, in order to solve the problem of malicious modification when external data is executed by system (), that is, to avoid command injection, it is necessary to verify the parameters obtained from the outside.

[solution method:]

The parameters of IP address or domain name are obtained through the configuration file, and then verified: 1) if it is an IP address, INET is used_ aton()   Check; 2) If it is a domain name, you need to resolve the IP address from the domain name, and then verify the IP address;

int   inet_pton(int af,const char*src,void*dst);   // This function is often used to convert decimal string ip into network byte order.

1. Convert the ip address into binary value for network transmission
int inet_aton(const char *cp, struct in_addr *inp);

inet_aton() converts the network host address IP (e.g. 192.168.1.10) into binary value and stores it in struct in_ In the addr structure, that is, the second parameter * inp, the function returns a non-0, indicating that the cp host is valid, and a return of 0 indicates that the host address is invalid( After this conversion, it cannot be used for network transmission. You need to call htons or htonl function to convert host byte order to network byte order)

in_addr_t inet_addr(const char *cp);

inet_ The addr function converts the network host address (e.g. 192.168.1.10) to the network byte order binary value. If the parameter char *cp is invalid, the function returns - 1(INADDR_NONE), this function also returns when the processing address is 255.255.255.255 - 1255.255.255.255 is a valid address, but inet_addr cannot process;


2. Convert the binary value transmitted by the network into dot decimal ip address

char *inet_ntoa(struct in_addr in);

inet_ The ntoa function converts the network byte sorted address to the standard ASCII dot separated address. The function returns a pointer to the dot separated string address (such as 192.168.1.10). The space of the string is statically allocated, which means that when the function is called the second time, the last call will be overwritten (overwritten), So if you need to save the string, copy it out and manage it yourself!
 

#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>

int errnoStr;
 
int main(int argc, char **argv)
{
    if (argc != 2) {
        printf("Use example: %s www.baidu.com\n", *argv);
        return -1;
    }
 
    char *name = argv[1];
    struct hostent *hptr = gethostbyname(name);
    if (hptr == NULL) {
        printf("gethostbyname error, host: %s: %s\n", name, hstrerror(errnoStr));
        return -1;
    }
    // The canonical name of the output host
    printf("\tofficial: %s\n", hptr->h_name);
 
    // Alias of the output host
    char **pptr;
    char str[INET_ADDRSTRLEN] = {0};
    for (pptr=hptr->h_aliases; *pptr!=NULL; pptr++) {
        printf("\ttalias: %s\n", *pptr);
    }
	char buffer[31]={"192.168.1.1"};
	struct in_addr *ipData = NULL;
	if (inet_aton(buffer, ipData) == 0) {
		printf("inet_aton failed\n");
	} else {
		printf("inet_aton success\n");
	}

	// int inet_aton(const char *cp, struct in_addr *inp);
    // Output ip address
    switch (hptr->h_addrtype) {
        case AF_INET:
            pptr = hptr->h_addr_list;
            for (; *pptr != NULL; pptr++) {
                printf("\taddress: %s\n",
				       inet_ntop(hptr->h_addrtype, hptr->h_addr, str, sizeof(str)));
					   
				/* IP Address translation to */
				if (inet_aton(hptr->h_addr, ipData) == 0) {
					printf("inet_aton failed\n");
				} else {
					printf("inet_aton success\n");
				}
            }
            break;
        default:
            printf("unknown address type\n");
            break;
    }
    return 0;
}

/*
[zll@zll 5_linux_net]$ ./gethostbyname localhost
	official: localhost
	talias: localhost.localdomain
	talias: localhost4
	talias: localhost4.localdomain4
	talias: localhost.localdomain
	talias: localhost6
	talias: localhost6.localdomain6
inet_ntop success
	address: 127.0.0.1
inet_ntop failed
	address: 127.0.0.1
inet_ntop failed



*/

https://blog.csdn.net/daaikuaichuan/article/details/83061117 - hao

https://blog.csdn.net/daiyudong2020/article/details/51946080
Detailed explanation of gethostbyname() function

Tags: IoT

Posted on Sat, 04 Sep 2021 18:59:19 -0400 by IndianaRogers