I made a nice interactive viewer for UDP Packets that calculates the checksum, but is…
- User
I made a nice interactive viewer for UDP Packets that calculates the checksum, but is there some bug inside? This problem tests integer overflow + buffer overflow. Note the prevalent use of u16 for fields. Look for a place where a (useful) integer overflow would occur. Hint: If you managed to get the overflow correct but are struggling to get shell after jumping to win, jump to the (address of win) + 5 instead.
Server code: #include <stdio.h> #include <stdint.h> #include <stdbool.h>
typedef unsigned short u16; typedef unsigned int u32;
#define HEXDUMP_COLS 16
typedef struct { u16 src_port; u16 dst_port; u16 len; u16 checksum; } UDPHeader;
typedef struct { UDPHeader header; char data[0x1000]; // put a cap on amount of data } UDPPacket;
void setup(); u16 checksum(UDPPacket packet); void read_packet(UDPPacket packet, u16 data_len); void hexdump(void *mem, unsigned int len);
// cough this is just here for uh.. fun :) void win() { system("/bin/sh"); }
int main() { setup();
UDPPacket packet; packet.header.checksum = 0; puts("######### CS2105 UDP Packet Viewer #########"); printf("Source Port > "); scanf("%hu", &packet.header.src_port); printf("Destination Port > "); scanf("%hu", &packet.header.dst_port); u16 data_len; printf("Data Length > "); scanf("%hu", &data_len); getchar(); //ignore this line // len is length in bytes of UDP Header and UDP data packet.header.len = data_len + sizeof(UDPHeader); if (packet.header.len > sizeof(UDPPacket)) { puts("Too much data!"); return 1; } printf("Data > "); read_packet(&packet, data_len); // Calculate checksum packet.header.checksum = checksum(&packet); puts("\nPacket bytes: \n"); // View packet bytes hexdump(&packet, packet.header.len);}
// IGNORE void setup() { setbuf(stdin, NULL); setbuf(stdout, NULL); setbuf(stderr, NULL); }
u16 checksum(UDPPacket *packet) { u16 *bytes = (u16 *)packet; u32 sum = 0; for (int i = 0; i < packet->header.len; i += 2) { sum += bytes[i]; u16 carry = (sum >> 16) & 1; sum = (sum & 0xffff) + carry; } return ~sum; }
void read_packet(UDPPacket* packet, u16 data_len) { fgets(packet->data, data_len, stdin); }
void hexdump(void *mem, unsigned int len) { unsigned int i, j;
for (i = 0; i < len + ((len % HEXDUMP_COLS) ? (HEXDUMP_COLS - len % HEXDUMP_COLS) : 0); i++) { /* print offset */ if (i % HEXDUMP_COLS == 0) { printf("0x%06x: ", i); } /* print hex data */ if (i < len) { printf("%02x ", 0xFF & ((char *)mem)[i]); } else /* end of block, just aligning for ASCII dump */ { printf(" "); } /* print ASCII dump */ if (i % HEXDUMP_COLS == (HEXDUMP_COLS - 1)) { for (j = i - (HEXDUMP_COLS - 1); j <= i; j++) { if (j >= len) /* end of block, not really printing */ { putchar(' '); } else if (isprint(((char *)mem)[j])) /* printable char */ { putchar(0xFF & ((char *)mem)[j]); } else /* other char */ { putchar('.'); } } putchar('\n'); } } - Assistant
It seems that there is a possible buffer overflow vulnerability in the program. The data length input by the user is not checked properly before reading data from the user.
In the following code,
data_lenis received from the user input, and the program calculates the packet length by adding thesizeof(UDPHeader)todata_len. Ifdata_lenis very large, it may cause an integer overflow, and the value ofpacket.header.lenbecomes smaller than expected.packet.header.len = data_len + sizeof(UDPHeader);No proper check is made to ensure that is within the bounds of the buffer size (0x1000). As a result, when the function reads data from the user, it may overflow the buffer in the structure.
- User
what combination of inputs can I use in order to gain access to the shell?
- Assistant
To gain access to the shell, we need to exploit the buffer overflow vulnerability present in the UDP Packet Viewer program described in the previous response.
Here is a step-by-step guide on how to do this:
-
Calculate the length of
UDPPacket.header(which issizeof(UDPHeader)) by examining the source code. In this case, it is 8 bytes. -
Find the address of the function. To do this, you can disassemble the program using a tool like or run it with a debugger like and inspect the function address.
-
- User
can you give an example input in the following format requested by the program to achieve this: Source Port > Destination Port > Data Length > Data >
- Assistant
In the given example, let's assume the following values:
-
Address of
win(): 0x080485e6 (You have to determine the actual address in your compiled binary using a tool likegdb,objdump, or any other disassembler. This example address might not match your binary.) -
Distance between buffer and return address on the stack (offset): 128 (This varies depending on the compiler and system. You can also find the actual buffer to return address offset for your compiled binary using a tool like
gdboredb. This example offset might not match your binary.)
We will craft the payload based on the calculated values:
Payload:
- Padding: 'A' x 128
- Address of
win() + 5: 0x080485eb (since the hint suggests jumping to )
-