Advertisement
Guest User

nudgeval.c tweaks sysctl(2) values related to TCP/IP stack

a guest
Apr 20th, 2018
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. /*
  2. * - nudgeval (nudge sysctl values related to network stack)
  3. * nudges sysctl(2) values using sysctlbyname(3)
  4. * specifically those controlling the network stack, _TCP/IP/ICMP/IGMP/UDP/...
  5. * this works wonders for nmap -O and other fingerprint detection evasion.
  6. *
  7. * -n@mod.net
  8. *
  9. * TODO: port to Linux sysctl interface with new naming convention, net.ipv4/ipv6/etc...
  10. * Under the /proc/sys filesystem directory.
  11. * TODO: Some of these sysctls are using values outside the normal ranges, tune that, and add more.
  12. *
  13. * Notes: before running this, nmap -O on high sierra, returns 'Aggressive OS guesses' that include OSX
  14. * after running this, it has no idea what OS I am running (whether nmap -O localhost or from remote host)
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <stdarg.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22.  
  23. #include <sys/time.h>
  24. #include <sys/types.h>
  25. #include <sys/socket.h>
  26. #include <sys/sysctl.h>
  27.  
  28. #include <netinet/in.h>
  29. #include <arpa/inet.h>
  30. #include <netdb.h>
  31. #include <err.h>
  32.  
  33. #define RANDOMIZE (-1)
  34.  
  35. #define NET_INET "net.inet."
  36. #define _IP_PORTRANGE NET_INET "ip.portrange"
  37. #define _IP NET_INET "ip."
  38. #define _ICMP NET_INET "icmp."
  39. #define _TCP NET_INET "tcp."
  40. #define _IGMP NET_INET "igmp."
  41. #define _UDP NET_INET "udp."
  42. #define _IPSEC NET_INET "ipsec."
  43. #define _RAW NET_INET "raw."
  44. #define _MPTCP NET_INET "mptcp."
  45.  
  46. #define _USE_SYSLOG 0
  47.  
  48. #if _USE_SYSLOG == 1
  49. #define syslog syslog
  50. #else
  51. #define syslog warnx
  52. #endif
  53.  
  54. struct sysctls {
  55. char *name;
  56. int def;
  57. int low;
  58. int hi;
  59. int mlsecs;
  60. struct timeval last;
  61. };
  62.  
  63. struct sysctls ctl[] = {
  64. { _IP "forwarding", 0, 0, 0, 0, { 0, 0 } },
  65. { _IP "sourceroute", 0, 0, 0, 0, { 0, 0 } },
  66. { _IP "redirect", 0, 0, 0, 0, { 0, 0 } },
  67. { _IP "gifttl", 0, 0, 0, 0, { 0, 0 } },
  68. { _IP "accept_sourceroute", 0, 0, 0, 0, { 0, 0 } },
  69. { _IP "ttl", 64, 30, 64, 100000, { 0, 0 } },
  70. { _IP "rtexpire", 10, 5, 25, 100000, { 0, 0 } },
  71. { _IP "rtminexpire", 10, 1, 16, 40000, { 0, 0 } },
  72. { _IP "rtmaxcache", 128, 100, 200, 10000000, { 0, 0 } },
  73. { _IP "log_restricted", 1, 1, 1, 0, { 0, 0 } },
  74. { _UDP "log_in_vain", 4, 4, 4, 0, { 0, 0 } },
  75. { _UDP "blackhole", 1, 1, 1, 0, { 0, 0 } },
  76. { _MPTCP "enable", 0, 1, 0, 10000, { 0, 0 } },
  77. { _TCP "log_in_vain", 4, 4, 4, 0, { 0, 0 } },
  78. { _TCP "rfc1644", 0, 1, 0, 200000, { 0, 0 } },
  79. { _TCP "delayed_ack", 3, 0, 4, 100000, { 0, 0 } },
  80. { _TCP "blackhole", 2, 1, 2, 50000, { 0, 0 } },
  81. { _ICMP "icmplim", 5, 0, 50, 25000, { 0, 0 } },
  82. { _MPTCP "log_restricted", 1, 1, 1, 0, { 0, 0 } },
  83. { _TCP "cc_debug", 1, 1, 1, 0, { 0, 0 } },
  84. { _TCP "newreno_sockets", 0, 0, 16, 30000, { 0, 0 } },
  85. { _TCP "use_newreno", 1, 0, 1, 40000, { 0, 0 } },
  86. { _TCP "cubic_sockets", 0, 10, 0, 20000, { 0, 0 } },
  87. { _TCP "background_sockets", 1, 20, 0, 120000, { 0, 0 } },
  88. { _TCP "cubic_tcp_friendliness", 0, 0, 1, 100000, { 0, 0 } },
  89. { _TCP "cubic_fast_convergence", 0, 0, 1, 10000, { 0, 0 } },
  90. { _TCP "slowstart_flightsize", 15, 5, 50, 10000, { 0, 0 } },
  91. { _TCP "icmp_may_rst", 0, 0, 1, 12000, { 0, 0 } },
  92. { _TCP "fastopen", 3, 0, 3, 48000, { 0, 0 } },
  93. { _TCP "fastopen_backlog", 12, 3, 16, 240000, { 0, 0 } },
  94. { _TCP "sack", 1, 0, 1, 10000, { 0, 0 } },
  95. { _TCP "rfc3390", 1, 0, 1, 10000, { 0, 0 } },
  96. { _TCP "rfc3645", 1, 1, 1, 0, { 0, 0 } },
  97. { _TCP "rfc3645_lim2", 1, 0, 3, 20000, { 0, 0 } },
  98. { _TCP "drop_synfin", 1, 0, 1, 24000, { 0, 0 } },
  99. { _TCP "enable_tlp", 1, 0, 1, 50000, { 0, 0 } },
  100. { _TCP "ack_prioritize", 1, 0, 1, 40000, { 0, 0 } },
  101. { _TCP "rfc3390", 1, 0, 1, 205000, { 0, 0 } },
  102. { _TCP "tcp_lq_overflow", 1, 0, 1, 200000, { 0, 0 } },
  103. { NULL, 0, 0, 0, 0, { 0, 0 } },
  104. };
  105.  
  106.  
  107. int do_sysctl();
  108. int nudgesys();
  109.  
  110. /*
  111. * do a sysctlbyname(2) and return success or failure.
  112. */
  113. int
  114. do_sysctl(char *name, void *newp, size_t size)
  115. {
  116. int err, oval;
  117. size_t olen = 0;
  118. char buf[256];
  119.  
  120.  
  121. /* get size of buffer we will need */
  122. err = sysctlbyname(name, &oval, &olen, newp, size);
  123. // sprintf(buf, "sysctl -w %s=%d", name, *(int *)newp);
  124. // system(buf);
  125.  
  126. if (err < 0) {
  127. perror("sysctlbyname");
  128. return -1;
  129. }
  130.  
  131. return 0;
  132. }
  133.  
  134. /*
  135. * nudgesys
  136. * takes care of nudging a sysctl value if the time is right, and updating the last timeval.
  137. */
  138. int
  139. nudgesys(struct sysctls *sc)
  140. {
  141. size_t size = sizeof(int);
  142. int i, val, do_nudge = 0;
  143. struct timeval tv, tvdiff;
  144.  
  145. gettimeofday(&tv, NULL);
  146. if (sc->last.tv_sec == 0 && sc->last.tv_usec == 0) {
  147. /*
  148. * Never been updated since first started.
  149. */
  150. memcpy(&sc->last, &tv, sizeof(struct timeval));
  151. do_nudge++;
  152. }
  153.  
  154. if (tv.tv_sec > sc->last.tv_sec) /* seconds make it automtically furhter/larger in time. */
  155. {
  156. memcpy(&sc->last, &tv, sizeof(struct timeval));
  157. do_nudge++;
  158. }
  159.  
  160. if (tv.tv_sec == sc->last.tv_sec) { /* Our television sets are getting hacked. */
  161. if (tv.tv_sec > sc->last.tv_sec)
  162. fprintf(stderr, "Unless 'usec' is higher than 'usec' (sec == sec).\n");
  163. if (tv.tv_usec > sc->last.tv_usec)
  164. do_nudge++;
  165. }
  166. else
  167. if (tv.tv_usec > sc->last.tv_usec && tv.tv_sec == sc->last.tv_sec)
  168. fprintf(stderr, "Unless 'sec' is higher than 'usec'\n");
  169.  
  170. /*
  171. * Analysis portion.
  172. */
  173. if (do_nudge) {
  174. if (sc->mlsecs == 0 || (sc->hi == 0 && sc->low == 0))
  175. return 0;
  176.  
  177. val = (rand() % (sc->hi - sc->low)) + sc->low;
  178. size = sizeof(val);
  179. if (do_sysctl(sc->name, &val, size) < 0) {
  180. syslog("nudgeval[%d]: nudgesys(): do_sysctl(%s, %d, %zu) < 0", getpid(),
  181. sc->name, val, size);
  182. syslog("nudgeval[%d]: nudgesys(): sc->def = %d, sc->hi = %d, sc->low = %d,"
  183. " sc->mlsecs = %u, sc->last.tv_sec = %u, sc->last.tv_usec = %u",
  184. getpid(), sc->def, sc->hi, sc->low, sc->mlsecs, sc->last.tv_sec,
  185. sc->last.tv_usec);
  186. return -1;
  187. }
  188.  
  189. /*
  190. * Succeeded in nudging the sysctl value.
  191. */
  192. return 0;
  193. }
  194.  
  195. return -2; /* Not changed. */
  196. }
  197.  
  198. int
  199. main(int argc, char **argv)
  200. {
  201. struct sysctls *sc;
  202. int i;
  203. time_t t;
  204.  
  205. time(&t);
  206. srand(t);
  207.  
  208. for (;;) {
  209. for (i = 0; ctl[i].name != NULL; i++) {
  210. sc = &ctl[i];
  211. nudgesys(sc);
  212. usleep(sc->mlsecs);
  213. }
  214. }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement