get-phishing-domains.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import { processDomainLists } from './parse-filter';
  2. import * as tldts from 'tldts-experimental';
  3. import { dummySpan } from '../trace';
  4. import type { Span } from '../trace';
  5. import { appendArrayInPlaceCurried } from './append-array-in-place';
  6. import { PHISHING_DOMAIN_LISTS_EXTRA } from '../constants/reject-data-source';
  7. import { loosTldOptWithPrivateDomains } from '../constants/loose-tldts-opt';
  8. import picocolors from 'picocolors';
  9. import createKeywordFilter from './aho-corasick';
  10. import { createCacheKey, deserializeArray, fsFetchCache, serializeArray } from './cache-filesystem';
  11. import { fastStringArrayJoin } from './misc';
  12. import { sha256 } from 'hash-wasm';
  13. const BLACK_TLD = new Set([
  14. 'accountant',
  15. 'autos',
  16. 'bar',
  17. 'beauty',
  18. 'bid',
  19. 'biz',
  20. 'bond',
  21. 'business',
  22. 'buzz',
  23. 'cc',
  24. 'cf',
  25. 'cfd',
  26. 'click',
  27. 'cloud',
  28. 'club',
  29. 'cn',
  30. 'codes',
  31. 'co.uk',
  32. 'co.in',
  33. 'com.br',
  34. 'com.cn',
  35. 'com.pl',
  36. 'com.vn',
  37. 'cool',
  38. 'cricket',
  39. 'cyou',
  40. 'date',
  41. 'digital',
  42. 'download',
  43. 'faith',
  44. 'fit',
  45. 'fun',
  46. 'ga',
  47. 'gd',
  48. 'gives',
  49. 'gq',
  50. 'group',
  51. 'host',
  52. 'icu',
  53. 'id',
  54. 'info',
  55. 'ink',
  56. 'life',
  57. 'live',
  58. 'link',
  59. 'loan',
  60. 'lol',
  61. 'ltd',
  62. 'me',
  63. 'men',
  64. 'ml',
  65. 'mobi',
  66. 'mom',
  67. 'net.pl',
  68. 'one',
  69. 'online',
  70. 'party',
  71. 'pro',
  72. 'pl',
  73. 'pw',
  74. 'racing',
  75. 'rest',
  76. 'review',
  77. 'rf.gd',
  78. 'sa.com',
  79. 'sbs',
  80. 'science',
  81. 'shop',
  82. 'site',
  83. 'space',
  84. 'store',
  85. 'stream',
  86. 'surf',
  87. 'tech',
  88. 'tk',
  89. 'tokyo',
  90. 'top',
  91. 'trade',
  92. 'vip',
  93. 'vn',
  94. 'webcam',
  95. 'website',
  96. 'win',
  97. 'xyz',
  98. 'za.com',
  99. 'lat',
  100. 'design'
  101. ]);
  102. const WHITELIST_MAIN_DOMAINS = new Set([
  103. // 'w3s.link', // ipfs gateway
  104. // 'dweb.link', // ipfs gateway
  105. // 'nftstorage.link', // ipfs gateway
  106. 'fleek.cool', // ipfs gateway
  107. 'business.site', // Drag'n'Drop site building platform
  108. 'page.link', // Firebase URL Shortener
  109. // 'notion.site',
  110. // 'vercel.app',
  111. 'gitbook.io'
  112. ]);
  113. const sensitiveKeywords = createKeywordFilter([
  114. '-roblox',
  115. '.amazon-',
  116. '-amazon',
  117. 'fb-com',
  118. 'facebook.',
  119. 'facebook-',
  120. 'facebook-com',
  121. '.facebook',
  122. '-facebook',
  123. 'coinbase',
  124. 'metamask-',
  125. '-metamask',
  126. 'virus-',
  127. 'icloud-',
  128. 'apple-',
  129. 'www.apple',
  130. '-coinbase',
  131. 'coinbase-',
  132. 'lcloud.',
  133. 'lcloud-',
  134. 'booking-com',
  135. 'booking.com-',
  136. 'booking-eu',
  137. 'vinted-cz',
  138. 'inpost-pl',
  139. 'login.microsoft',
  140. 'login-microsoft'
  141. ]);
  142. const lowKeywords = createKeywordFilter([
  143. '-co-jp',
  144. 'customer.',
  145. 'customer-',
  146. '.www-',
  147. 'instagram',
  148. 'microsoft',
  149. 'passwordreset'
  150. ]);
  151. const cacheKey = createCacheKey(__filename);
  152. export const getPhishingDomains = (parentSpan: Span) => parentSpan.traceChild('get phishing domains').traceAsyncFn(async (span) => {
  153. const domainArr = await span.traceChildAsync('download/parse/merge phishing domains', async (curSpan) => {
  154. const domainArr: string[] = [];
  155. (await Promise.all(PHISHING_DOMAIN_LISTS_EXTRA.map(entry => processDomainLists(curSpan, ...entry, cacheKey))))
  156. .forEach(appendArrayInPlaceCurried(domainArr));
  157. return domainArr;
  158. });
  159. return span.traceChildAsync(
  160. 'process phishing domain set',
  161. () => processPhihsingDomains(domainArr)
  162. );
  163. });
  164. async function processPhihsingDomains(domainArr: string[]) {
  165. const hash = await sha256(fastStringArrayJoin(domainArr, '|'));
  166. return fsFetchCache.apply(
  167. cacheKey('processPhihsingDomains|' + hash),
  168. () => {
  169. const domainCountMap: Record<string, number> = {};
  170. const domainScoreMap: Record<string, number> = {};
  171. for (let i = 0, len = domainArr.length; i < len; i++) {
  172. const line = domainArr[i];
  173. const {
  174. publicSuffix: tld,
  175. domain: apexDomain,
  176. subdomain,
  177. isPrivate
  178. } = tldts.parse(line, loosTldOptWithPrivateDomains);
  179. if (isPrivate) {
  180. continue;
  181. }
  182. if (!tld) {
  183. console.log(picocolors.yellow('[phishing domains] E0001'), 'missing tld', { line, tld });
  184. continue;
  185. }
  186. if (!apexDomain) {
  187. console.log(picocolors.yellow('[phishing domains] E0002'), 'missing domain', { line, apexDomain });
  188. continue;
  189. }
  190. domainCountMap[apexDomain] ||= 0;
  191. domainCountMap[apexDomain] += 1;
  192. if (!(apexDomain in domainScoreMap)) {
  193. domainScoreMap[apexDomain] = 0;
  194. if (BLACK_TLD.has(tld)) {
  195. domainScoreMap[apexDomain] += 4;
  196. } else if (tld.length > 6) {
  197. domainScoreMap[apexDomain] += 2;
  198. }
  199. }
  200. if (
  201. subdomain
  202. && !WHITELIST_MAIN_DOMAINS.has(apexDomain)
  203. ) {
  204. domainScoreMap[apexDomain] += calcDomainAbuseScore(subdomain);
  205. }
  206. }
  207. for (const apexDomain in domainCountMap) {
  208. if (
  209. // !WHITELIST_MAIN_DOMAINS.has(apexDomain)
  210. domainScoreMap[apexDomain] >= 12
  211. || (domainScoreMap[apexDomain] >= 5 && domainCountMap[apexDomain] >= 4)
  212. ) {
  213. domainArr.push('.' + apexDomain);
  214. }
  215. }
  216. return Promise.resolve(domainArr);
  217. },
  218. {
  219. ttl: 2 * 86400 * 1000,
  220. serializer: serializeArray,
  221. deserializer: deserializeArray,
  222. incrementTtlWhenHit: true
  223. }
  224. );
  225. }
  226. export function calcDomainAbuseScore(subdomain: string) {
  227. let weight = 0;
  228. const hitLowKeywords = lowKeywords(subdomain);
  229. const sensitiveKeywordsHit = sensitiveKeywords(subdomain);
  230. if (sensitiveKeywordsHit) {
  231. weight += 8;
  232. if (hitLowKeywords) {
  233. weight += 4;
  234. }
  235. } else if (hitLowKeywords) {
  236. weight += 1;
  237. }
  238. const subdomainLength = subdomain.length;
  239. if (subdomainLength > 4) {
  240. weight += 0.5;
  241. if (subdomainLength > 10) {
  242. weight += 0.5;
  243. if (subdomainLength > 20) {
  244. weight += 1;
  245. if (subdomainLength > 30) {
  246. weight += 2;
  247. if (subdomainLength > 40) {
  248. weight += 4;
  249. }
  250. }
  251. }
  252. }
  253. if (subdomain.startsWith('www.')) {
  254. weight += 4;
  255. } else if (subdomain.slice(1).includes('.')) {
  256. weight += 1;
  257. if (subdomain.includes('www.')) {
  258. weight += 4;
  259. }
  260. }
  261. }
  262. return weight;
  263. }
  264. if (require.main === module) {
  265. getPhishingDomains(dummySpan)
  266. .catch(console.error);
  267. }