Ircd_cloaking.c

C Detected boxlabs 19 Views Size: 9.30 KB Posted on: Sep 15, 25 @ 8:44 PM
  1. 1/*
  2. 2 * IRC - Internet Relay Chat, ircd/ircd_cloaking.c
  3. 3 * Copyright (C) 1999 Thomas Helvey
  4. 4 *
  5. 5 * This program is free software; you can redistribute it and/or modify
  6. 6 * it under the terms of the GNU General Public License as published by
  7. 7 * the Free Software Foundation; either version 1, or (at your option)
  8. 8 * any later version.
  9. 9 *
  10. 10 * This program is distributed in the hope that it will be useful,
  11. 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. 13 * GNU General Public License for more details.
  14. 14 *
  15. 15 * You should have received a copy of the GNU General Public License
  16. 16 * along with this program; if not, write to the Free Software
  17. 17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. 18 */
  19. 19/** @file
  20. 20 * @brief Implementation of IP and host cloaking functions..
  21. 21 * @version $Id$
  22. 22 */
  23. 23#include "config.h"
  24. 24
  25. 25#include "ircd_chattr.h"
  26. 26#include "ircd_cloaking.h"
  27. 27#include "ircd_defs.h"
  28. 28#include "ircd_features.h"
  29. 29#include "ircd_md5.h"
  30. 30#include "ircd_snprintf.h"
  31. 31#include "res.h"
  32. 32
  33. 33#include <netinet/in.h>
  34. 34#include <string.h>
  35. 35
  36. 36#undef KEY1
  37. 37#undef KEY2
  38. 38#undef KEY3
  39. 39#undef PREFIX
  40. 40#define KEY1 feature_str(FEAT_HOST_HIDING_KEY1)
  41. 41#define KEY2 feature_str(FEAT_HOST_HIDING_KEY2)
  42. 42#define KEY3 feature_str(FEAT_HOST_HIDING_KEY3)
  43. 43#define PREFIX feature_str(FEAT_HOST_HIDING_PREFIX)
  44. 44
  45. 45/** Downsamples a 128bit result to 32bits (md5 -> unsigned int) */
  46. 46static inline unsigned int downsample(unsigned char *i)
  47. 47{
  48. 48unsigned char r[4];
  49. 49
  50. 50 r[0] = i[0] ^ i[1] ^ i[2] ^ i[3];
  51. 51 r[1] = i[4] ^ i[5] ^ i[6] ^ i[7];
  52. 52 r[2] = i[8] ^ i[9] ^ i[10] ^ i[11];
  53. 53 r[3] = i[12] ^ i[13] ^ i[14] ^ i[15];
  54. 54
  55. 55 return ( ((unsigned int)r[0] << 24) +
  56. 56 ((unsigned int)r[1] << 16) +
  57. 57 ((unsigned int)r[2] << 8) +
  58. 58 (unsigned int)r[3]);
  59. 59}
  60. 60
  61. 61/** Downsamples a 128bit result to 24bits (md5 > unsigned int) */
  62. 62static inline unsigned int downsample24(unsigned char *i)
  63. 63{
  64. 64unsigned char r[4];
  65. 65
  66. 66 r[0] = i[0] ^ i[1] ^ i[2] ^ i[3] ^ i[4];
  67. 67 r[1] = i[5] ^ i[6] ^ i[7] ^ i[8] ^ i[9] ^ i[10];
  68. 68 r[2] = i[11] ^ i[12] ^ i[13] ^ i[14] ^ i[15];
  69. 69
  70. 70 return ( ((unsigned int)r[0] << 16) +
  71. 71 ((unsigned int)r[1] << 8) +
  72. 72 (unsigned int)r[2]);
  73. 73}
  74. 74
  75. 75
  76. 76char *hidehost_ipv4(struct irc_in_addr *ip)
  77. 77{
  78. 78unsigned int a, b, c, d;
  79. 79static char buf[512], res[512], res2[512], result[128];
  80. 80unsigned long n;
  81. 81unsigned int alpha, beta, gamma, delta;
  82. 82unsigned char *pch;
  83. 83
  84. 84 /*
  85. 85 * Output: ALPHA.BETA.GAMMA.DELTA.IP
  86. 86 * ALPHA is unique for a.b.c.d
  87. 87 * BETA is unique for a.b.c.*
  88. 88 * GAMMA is unique for a.b.*
  89. 89 * We cloak like this:
  90. 90 * ALPHA = downsample24(md5(md5("KEY2:A.B.C.D:KEY3")+"KEY1"));
  91. 91 * BETA = downsample24(md5(md5("KEY3:A.B.C:KEY1")+"KEY2"));
  92. 92 * GAMMA = downsample24(md5(md5("KEY1:A.B:KEY2")+"KEY3"));
  93. 93 * DELTA = downsample24(md5(md5("KEY2:A:KEY1:KEY3")+"KEY1"));
  94. 94 */
  95. 95 if (!irc_in_addr_is_ipv4(ip))
  96. 96 return hidehost_ipv6(ip);
  97. 97
  98. 98 pch = (unsigned char*)&ip->in6_16[6];
  99. 99 a = *pch++;
  100. 100 b = *pch;
  101. 101 pch = (unsigned char*)&ip->in6_16[7];
  102. 102 c = *pch++;
  103. 103 d = *pch;
  104. 104
  105. 105 /* ALPHA... */
  106. 106 ircd_snprintf(0, buf, 512, "%s:%03d.%03d.%03d.%03d:%s", KEY2, a, b, c, d, KEY3);
  107. 107 DoMD5((unsigned char *)&res, (unsigned char *)&buf, strlen(buf));
  108. 108 strcpy(res+16, KEY1); /* first 16 bytes are filled, append our key.. */
  109. 109 n = strlen(res+16) + 16;
  110. 110 DoMD5((unsigned char *)&res2, (unsigned char *)&res, n);
  111. 111 alpha = downsample24((unsigned char *)&res2);
  112. 112
  113. 113 /* BETA... */
  114. 114 ircd_snprintf(0, buf, 512, "%s:%03d.%03d.%03d:%s", KEY3, a, b, c, KEY1);
  115. 115 DoMD5((unsigned char *)&res, (unsigned char *)&buf, strlen(buf));
  116. 116 strcpy(res+16, KEY2); /* first 16 bytes are filled, append our key.. */
  117. 117 n = strlen(res+16) + 16;
  118. 118 DoMD5((unsigned char *)&res2, (unsigned char *)&res, n);
  119. 119 beta = downsample24((unsigned char *)&res2);
  120. 120
  121. 121 /* GAMMA... */
  122. 122 ircd_snprintf(0, buf, 512, "%s:%03d.%03d:%s", KEY1, a, b, KEY2);
  123. 123 DoMD5((unsigned char *)&res, (unsigned char *)&buf, strlen(buf));
  124. 124 strcpy(res+16, KEY3); /* first 16 bytes are filled, append our key.. */
  125. 125 n = strlen(res+16) + 16;
  126. 126 DoMD5((unsigned char *)&res2, (unsigned char *)&res, n);
  127. 127 gamma = downsample24((unsigned char *)&res2);
  128. 128
  129. 129 /* DELTA... */
  130. 130 ircd_snprintf(0, buf, 512, "%s:%03d:%s:%s", KEY2, a, KEY1, KEY3);
  131. 131 DoMD5((unsigned char *)&res, (unsigned char *)&buf, strlen(buf));
  132. 132 strcpy(res+16, KEY1); /* first 16 bytes are filled, append our key.. */
  133. 133 n = strlen(res+16) + 16;
  134. 134 DoMD5((unsigned char *)&res2, (unsigned char *)&res, n);
  135. 135 delta = downsample24((unsigned char *)&res2);
  136. 136
  137. 137 ircd_snprintf(0, result, HOSTLEN, "%X.%X.%X.%X.IP", alpha, beta, gamma, delta);
  138. 138 return result;
  139. 139}
  140. 140
  141. 141char *hidehost_ipv6(struct irc_in_addr *ip)
  142. 142{
  143. 143unsigned int a, b, c, d, e, f, g, h;
  144. 144static char buf[512], res[512], res2[512], result[128];
  145. 145unsigned long n;
  146. 146unsigned int alpha, beta, gamma, delta;
  147. 147
  148. 148 /*
  149. 149 * Output: ALPHA:BETA:GAMMA:IP
  150. 150 * ALPHA is unique for a:b:c:d:e:f:g:h
  151. 151 * BETA is unique for a:b:c:d:e:f:g
  152. 152 * GAMMA is unique for a:b:c:d
  153. 153 * We cloak like this:
  154. 154 * ALPHA = downsample24(md5(md5("KEY2:a:b:c:d:e:f:g:h:KEY3")+"KEY1"));
  155. 155 * BETA = downsample24(md5(md5("KEY3:a:b:c:d:e:f:g:KEY1")+"KEY2"));
  156. 156 * GAMMA = downsample24(md5(md5("KEY1:a:b:c:d:KEY2")+"KEY3"));
  157. 157 * DELTA = downsample24(md5(md5("KEY2:a:b:KEY1:KEY3")+"KEY1"));
  158. 158 */
  159. 159
  160. 160 if (irc_in_addr_is_ipv4(ip))
  161. 161 return hidehost_ipv4(ip);
  162. 162
  163. 163 a = ntohs(ip->in6_16[0]);
  164. 164 b = ntohs(ip->in6_16[1]);
  165. 165 c = ntohs(ip->in6_16[2]);
  166. 166 d = ntohs(ip->in6_16[3]);
  167. 167 e = ntohs(ip->in6_16[4]);
  168. 168 f = ntohs(ip->in6_16[5]);
  169. 169 g = ntohs(ip->in6_16[6]);
  170. 170 h = ntohs(ip->in6_16[7]);
  171. 171
  172. 172 /* ALPHA... */
  173. 173 ircd_snprintf(0, buf, 512, "%s:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%s", KEY2, a, b, c, d, e, f, g, h, KEY3);
  174. 174 DoMD5((unsigned char *)&res, (unsigned char *)&buf, strlen(buf));
  175. 175 strcpy(res+16, KEY1); /* first 16 bytes are filled, append our key */
  176. 176 n = strlen(res+16) + 16;
  177. 177 DoMD5((unsigned char *)&res2, (unsigned char *)&res, n);
  178. 178 alpha = downsample24((unsigned char *)&res2);
  179. 179
  180. 180 /* BETA... */
  181. 181 ircd_snprintf(0, buf, 512, "%s:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%s", KEY3, a, b, c, d, e, f, g, KEY1);
  182. 182 DoMD5((unsigned char *)&res, (unsigned char *)&buf, strlen(buf));
  183. 183 strcpy(res+16, KEY2); /* first 16 bytes are filled, append our key.. */
  184. 184 n = strlen(res+16) + 16;
  185. 185 DoMD5((unsigned char *)&res2, (unsigned char *)&res, n);
  186. 186 beta = downsample24((unsigned char *)&res2);
  187. 187
  188. 188 /* GAMMA... */
  189. 189 ircd_snprintf(0, buf, 512, "%s:%04x:%04x:%04x:%04x:%s", KEY1, a, b, c, d, KEY2);
  190. 190 DoMD5((unsigned char *)&res, (unsigned char *)&buf, strlen(buf));
  191. 191 strcpy(res+16, KEY3); /* first 16 bytes are filled, append our key.. */
  192. 192 n = strlen(res+16) + 16;
  193. 193 DoMD5((unsigned char *)&res2, (unsigned char *)&res, n);
  194. 194 gamma = downsample24((unsigned char *)&res2);
  195. 195
  196. 196 /* DELTA... */
  197. 197 ircd_snprintf(0, buf, 512, "%s:%04x:%04x:%s:%s", KEY2, a, b, KEY1, KEY3);
  198. 198 DoMD5((unsigned char *)&res, (unsigned char *)&buf, strlen(buf));
  199. 199 strcpy(res+16, KEY1); /* first 16 bytes are filled, append our key.. */
  200. 200 n = strlen(res+16) + 16;
  201. 201 DoMD5((unsigned char *)&res2, (unsigned char *)&res, n);
  202. 202 delta = downsample24((unsigned char *)&res2);
  203. 203
  204. 204 ircd_snprintf(0, result, HOSTLEN, "%X:%X:%X:%X:IP", alpha, beta, gamma, delta);
  205. 205 return result;
  206. 206}
  207. 207
  208. 208char *hidehost_normalhost(char *host, int components)
  209. 209{
  210. 210char *p, *c;
  211. 211static char buf[512], res[512], res2[512], result[HOSTLEN+1];
  212. 212unsigned int alpha, n;
  213. 213int comps = 0;
  214. 214
  215. 215 ircd_snprintf(0, buf, 512, "%s:%s:%s", KEY1, host, KEY2);
  216. 216 DoMD5((unsigned char *)&res, (unsigned char *)&buf, strlen(buf));
  217. 217 strcpy(res+16, KEY3); /* first 16 bytes are filled, append our key */
  218. 218 n = strlen(res+16) + 16;
  219. 219 DoMD5((unsigned char *)&res2, (unsigned char *)&res, n);
  220. 220 alpha = downsample((unsigned char *)&res2);
  221. 221
  222. 222 for (p = host; *p; p++) {
  223. 223 if (*p == '.') {
  224. 224 comps++;
  225. 225 if ((comps >= components) && IsHostChar(*(p + 1)))
  226. 226 break;
  227. 227 }
  228. 228 }
  229. 229
  230. 230 if (*p)
  231. 231 {
  232. 232 unsigned int len;
  233. 233 p++;
  234. 234
  235. 235 ircd_snprintf(0, result, HOSTLEN, "%s-%X.", PREFIX, alpha);
  236. 236 len = strlen(result) + strlen(p);
  237. 237 if (len <= HOSTLEN)
  238. 238 strcat(result, p);
  239. 239 else
  240. 240 {
  241. 241 c = p + (len - HOSTLEN);
  242. 242 if ((*c == '.') && *(c+1))
  243. 243 c++;
  244. 244 strcat(result, c);
  245. 245 }
  246. 246 } else
  247. 247 ircd_snprintf(0, result, HOSTLEN, "%s-%X", PREFIX, alpha);
  248. 248
  249. 249 return result;
  250. 250}

Raw Paste

Comments 0
Login to post a comment.
  • No comments yet. Be the first.
Login to post a comment. Login or Register
We use cookies. To comply with GDPR in the EU and the UK we have to show you these.

We use cookies and similar technologies to keep this website functional (including spam protection via Google reCAPTCHA), and — with your consent — to measure usage and show ads. See Privacy.