1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/*
* InspIRCd -- Internet Relay Chat Daemon
*
* Copyright (C) 2023-2025 Sadie Powell <sadie@sadiepowell.dev>
* Copyright (C) 2017 Adam <Adam@anope.org>
*
* This file is part of InspIRCd. InspIRCd is free software: you can
* redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/// $CompilerFlags: require_library("libpsl") find_compiler_flags("libpsl") -DHAS_LIBPSL
/// $LinkerFlags: require_library("libpsl") find_linker_flags("libpsl")
/// $PackageInfo: require_system("alpine") libpsl-dev pkgconf
/// $PackageInfo: require_system("arch") libpsl pkgconf
/// $PackageInfo: require_system("darwin") libpsl pkg-config
/// $PackageInfo: require_system("debian~") libpsl-dev pkg-config
/// $PackageInfo: require_system("rhel~") libpsl-devel pkg-config
#ifdef _WIN32
# if __has_include(<libpsl.h>)
# define HAS_LIBPSL
# endif
#endif
#ifdef HAS_LIBPSL
# include <libpsl.h>
#else
typedef void psl_ctx_t;
#endif
#include "inspircd.h"
#include "modules/cloak.h"
#include "modules/hash.h"
#include "utility/string.h"
class SHA256Method final
: public Cloak::Method
{
private:
// The base32 table used for upper-case cloaks.
static constexpr unsigned char base32upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
// The base32 table used for lower-case cloaks.
static constexpr unsigned char base32lower[] = "abcdefghijklmnopqrstuvwxyz234567";
// The number of bytes of the hash to use when downsampling.
static constexpr size_t segmentlen = 8;
// Whether to cloak the hostname if available.
const bool cloakhost;
// The number of parts of the hostname shown.
const unsigned long hostparts;
// The secret used for generating cloaks.
const std::string key;
// The number of parts of the UNIX socket path shown.
const unsigned long pathparts;
// The prefix for cloaks (e.g. MyNet).
const std::string prefix;
#ifdef HAS_LIBPSL
// Whether to enforce the use of the Public Suffix List in link data.
bool enforcepsl;
// Handle to the Public Suffix List library.
psl_ctx_t* psl;
#endif
// Dynamic reference to the sha256 implementation.
dynamic_reference_nocheck<HashProvider> sha256;
// The base32 table used when encoding.
const unsigned char* table;
// The suffix for IP cloaks (e.g. IP).
const std::string suffix;
std::string CloakAddress(const irc::sockets::sockaddrs& sa)
{
switch (sa.family())
{
case AF_INET:
return CloakIPv4(sa.in4.sin_addr.s_addr);
case AF_INET6:
return CloakIPv6(sa.in6.sin6_addr.s6_addr);
case AF_UNIX:
return CloakHost(sa.un.sun_path, '/', pathparts);
}
// Should never be reached.
return {};
}
std::string CloakIPv4(unsigned long address)
{
// IPv4 addresses are cloaked in the form ALPHA.BETA.GAMMA
//
// ALPHA is unique for a.b.c.d
// BETA is unique for a.b.c
// GAMMA is unique for a.b
unsigned int a = (unsigned int)(address) & 0xFF;
unsigned int b = (unsigned int)(address >> 8) & 0xFF;
unsigned int c = (unsigned int)(address >> 16) & 0xFF;
unsigned int d = (unsigned int)(address >> 24) & 0xFF;
const std::string alpha = Hash(INSP_FORMAT("{}.{}.{}.{}", a, b, c, d));
const std::string beta = Hash(INSP_FORMAT("{}.{}.{}", a, b, c));
const std::string gamma = Hash(INSP_FORMAT("{}.{}", a, b));
return Wrap(INSP_FORMAT("{}.{}.{}", alpha, beta, gamma), suffix, '.');
}
std::string CloakIPv6(const unsigned char* address)
{
// IPv6 addresses are cloaked in the form ALPHA.BETA.GAMMA.IP
//
// ALPHA is unique for a:b:c:d:e:f:g:h
// BETA is unique for a:b:c:d:e:f:g
// GAMMA is unique for a:b:c:d
const uint16_t* address16 = reinterpret_cast<const uint16_t*>(address);
unsigned int a = ntohs(address16[0]);
unsigned int b = ntohs(address16[1]);
unsigned int c = ntohs(address16[2]);
unsigned int d = ntohs(address16[3]);
unsigned int e = ntohs(address16[4]);
unsigned int f = ntohs(address16[5]);
unsigned int g = ntohs(address16[6]);
unsigned int h = ntohs(address16[7]);
const std::string alpha = Hash(INSP_FORMAT("{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}", a, b, c, d, e, f, g, h));
const std::string beta = Hash(INSP_FORMAT("{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}", a, b, c, d, e, f, g));
const std::string gamma = Hash(INSP_FORMAT("{:x}:{:x}:{:x}:{:x}", a, b, c, d));
return Wrap(INSP_FORMAT("{}:{}:{}", alpha, beta, gamma), suffix, ':');
}
std::string CloakHost(const std::string& host, char separator, unsigned long parts)
{
// Attempt to divine the public part of the hostname.
std::string visiblepart;
#ifdef HAS_LIBPSL
if (psl && separator == '.')
{
// Attempt to look up the suffix with libpsl.
const char* publicsuffix = psl_registrable_domain(psl, host.c_str());
if (!publicsuffix || publicsuffix == host)
publicsuffix = psl_unregistrable_domain(psl, host.c_str());
if (publicsuffix && publicsuffix != host)
visiblepart = publicsuffix;
}
#endif
// If libpsl failed to find a suffix or wasn't available fall back.
if (visiblepart.empty() && parts)
visiblepart = Cloak::VisiblePart(host, parts, separator);
// Convert the host to lowercase to avoid ban evasion.
std::string lowerhost(host.length(), '\0');
std::transform(host.begin(), host.end(), lowerhost.begin(), ::tolower);
return Wrap(Hash(lowerhost), visiblepart, separator);
}
std::string Hash(const std::string& str)
{
std::string out;
for (const auto chr : sha256->hmac(key, str).substr(0, segmentlen))
out.push_back(table[chr & 0x1F]);
return out;
}
std::string Wrap(const std::string& cloak, const std::string& cloaksuffix, char separator)
{
std::string fullcloak;
if (!prefix.empty())
fullcloak.append(prefix).append(1, separator);
fullcloak.append(cloak);
if (!cloaksuffix.empty())
fullcloak.append(1, separator).append(cloaksuffix);
return fullcloak;
}
public:
SHA256Method(const Cloak::Engine* engine, const std::shared_ptr<ConfigTag>& tag, const std::string& k, psl_ctx_t* p, bool ch) ATTR_NOT_NULL(2)
: Cloak::Method(engine, tag)
, cloakhost(ch)
, hostparts(cloakhost ? tag->getNum<unsigned long>("hostparts", 3, 0, ServerInstance->Config->Limits.MaxHost / 2) : 0)
, key(k)
, pathparts(tag->getNum<unsigned long>("pathparts", 1, 0, ServerInstance->Config->Limits.MaxHost / 2))
, prefix(tag->getString("prefix"))
#ifdef HAS_LIBPSL
, enforcepsl(p ? tag->getBool("enforcepsl") : false)
, psl(p)
#endif
, sha256(engine->creator, "hash/sha256")
, suffix(tag->getString("suffix", "ip"))
{
table = tag->getEnum("case", base32lower, {
{ "upper", base32upper },
{ "lower", base32lower }
});
}
#ifdef HAS_LIBPSL
~SHA256Method() override
{
if (psl)
psl_free(psl);
}
#endif
std::string Generate(LocalUser* user) override ATTR_NOT_NULL(2)
{
if (!sha256 || !MatchesUser(user))
return {};
irc::sockets::sockaddrs sa(false);
if (!cloakhost || (sa.from(user->GetRealHost()) && sa.addr() == user->client_sa.addr()))
return CloakAddress(user->client_sa);
return CloakHost(user->GetRealHost(), '.', hostparts);
}
std::string Generate(const std::string& hostip) override
{
if (!sha256)
return {};
irc::sockets::sockaddrs sa(false);
if (sa.from(hostip))
return CloakAddress(sa);
if (cloakhost)
return CloakHost(hostip, '.', hostparts);
return {}; // Only reachable on hmac-sha256-ip.
}
void GetLinkData(Module::LinkData& data, std::string& compatdata) override
{
// The value we use for cloaks when the sha2 module is missing.
const std::string broken = "missing-sha2-module";
// IMPORTANT: link data is sent over unauthenticated server links so we
// can't directly send the key here. Instead we use dummy cloaks that
// allow verification of or less the same thing.
data["cloak-v4"] = sha256 ? Generate("123.123.123.123") : broken;
data["cloak-v6"] = sha256 ? Generate("dead:beef:cafe::") : broken;
data["cloak-unix"] = sha256 ? Generate("/extremely/long/inspircd/cloak.example") : broken;
data["path-parts"] = ConvToStr(pathparts);
data["prefix"] = prefix;
data["suffix"] = suffix;
if (!cloakhost)
return;
data["cloak-host"] = sha256 ? Generate("extremely.long.inspircd.cloak.example") : broken;
data["host-parts"] = ConvToStr(hostparts);
#ifdef HAS_LIBPSL
data["using-psl"] = enforcepsl ? "yes" : "no";
#else
data["using-psl"] = "no";
#endif
}
bool IsLinkSensitive() const override
{
// This method always wants to be at the front.
return true;
}
};
class SHA256Engine final
: public Cloak::Engine
{
private:
// The minimum length of a cloak key.
static constexpr size_t minkeylen = 30;
// Whether to cloak the hostname if available.
const bool cloakhost;
// Dynamic reference to the sha256 implementation.
dynamic_reference_nocheck<HashProvider> sha256;
public:
SHA256Engine(Module* Creator, const std::string& Name, bool ch)
: Cloak::Engine(Creator, Name)
, cloakhost(ch)
, sha256(Creator, "hash/sha256")
{
}
Cloak::MethodPtr Create(const std::shared_ptr<ConfigTag>& tag, bool primary) override
{
if (!sha256)
throw ModuleException(creator, "Unable to create a " + name.substr(6) + " cloak without the sha2 module, at" + tag->source.str());
// Ensure that we have the <cloak:key> parameter.
const std::string key = tag->getString("key");
if (key.length() < minkeylen)
throw ModuleException(creator, "Your cloak key should be at least {} characters long, at {}", minkeylen, tag->source.str());
psl_ctx_t* psl = nullptr;
std::string psldb = tag->getString("psl");
if (cloakhost && !psldb.empty())
{
#ifdef HAS_LIBPSL
if (insp::equalsci(psldb, "system"))
{
psldb = psl_dist_filename();
if (psldb.empty())
throw ModuleException(creator, "You specified \"system\" in <cloak:psl> but libpsl was built without a system copy, at " + tag->source.str());
}
psl = psl_load_file(psldb.c_str());
if (!psl)
throw ModuleException(creator, "The database specified in <cloak:psl> (" + psldb + ") does not exist, at " + tag->source.str());
#else
throw ModuleException(creator, "You specified <cloak:psl> but InspIRCd was built without libpsl, at " + tag->source.str());
#endif
}
return std::make_shared<SHA256Method>(this, tag, key, psl, cloakhost);
}
};
class ModuleCloakSHA256 final
: public Module
{
private:
SHA256Engine addrcloak;
SHA256Engine hostcloak;
public:
ModuleCloakSHA256()
: Module(VF_VENDOR, "Adds the hmac-sha256 and hmac-sha256-addr cloaking methods for use with the cloak module.")
, addrcloak(this, "hmac-sha256-addr", false)
, hostcloak(this, "hmac-sha256", true)
{
}
#ifdef HAS_LIBPSL
void init() override
{
ServerInstance->Logs.Normal(MODNAME, "Module was compiled against libpsl version {} and is running against version {}",
PSL_VERSION, psl_get_version());
}
#endif
};
MODULE_INIT(ModuleCloakSHA256)
|