get-phishing-domains.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import { processDomainLists, processHosts } 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, PHISHING_HOSTS_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. (await Promise.all(PHISHING_HOSTS_EXTRA.map(entry => processHosts(curSpan, ...entry, cacheKey))))
  158. .forEach(appendArrayInPlaceCurried(domainArr));
  159. return domainArr;
  160. });
  161. return span.traceChildAsync(
  162. 'process phishing domain set',
  163. () => processPhihsingDomains(domainArr)
  164. );
  165. });
  166. async function processPhihsingDomains(domainArr: string[]) {
  167. const hash = await sha256(fastStringArrayJoin(domainArr, '|'));
  168. return fsFetchCache.apply(
  169. cacheKey('processPhihsingDomains|' + hash),
  170. () => {
  171. const domainCountMap: Record<string, number> = {};
  172. const domainScoreMap: Record<string, number> = {};
  173. for (let i = 0, len = domainArr.length; i < len; i++) {
  174. const line = domainArr[i];
  175. const {
  176. publicSuffix: tld,
  177. domain: apexDomain,
  178. subdomain,
  179. isPrivate
  180. } = tldts.parse(line, loosTldOptWithPrivateDomains);
  181. if (isPrivate) {
  182. continue;
  183. }
  184. if (!tld) {
  185. console.log(picocolors.yellow('[phishing domains] E0001'), 'missing tld', { line, tld });
  186. continue;
  187. }
  188. if (!apexDomain) {
  189. console.log(picocolors.yellow('[phishing domains] E0002'), 'missing domain', { line, apexDomain });
  190. continue;
  191. }
  192. domainCountMap[apexDomain] ||= 0;
  193. domainCountMap[apexDomain] += 1;
  194. if (!(apexDomain in domainScoreMap)) {
  195. domainScoreMap[apexDomain] = 0;
  196. if (BLACK_TLD.has(tld)) {
  197. domainScoreMap[apexDomain] += 4;
  198. } else if (tld.length > 6) {
  199. domainScoreMap[apexDomain] += 2;
  200. }
  201. }
  202. if (
  203. subdomain
  204. && !WHITELIST_MAIN_DOMAINS.has(apexDomain)
  205. ) {
  206. domainScoreMap[apexDomain] += calcDomainAbuseScore(subdomain);
  207. }
  208. }
  209. for (const apexDomain in domainCountMap) {
  210. if (
  211. // !WHITELIST_MAIN_DOMAINS.has(apexDomain)
  212. domainScoreMap[apexDomain] >= 12
  213. || (domainScoreMap[apexDomain] >= 5 && domainCountMap[apexDomain] >= 4)
  214. ) {
  215. domainArr.push('.' + apexDomain);
  216. }
  217. }
  218. return Promise.resolve(domainArr);
  219. },
  220. {
  221. ttl: 2 * 86400 * 1000,
  222. serializer: serializeArray,
  223. deserializer: deserializeArray,
  224. incrementTtlWhenHit: true
  225. }
  226. );
  227. }
  228. export function calcDomainAbuseScore(subdomain: string) {
  229. let weight = 0;
  230. const hitLowKeywords = lowKeywords(subdomain);
  231. const sensitiveKeywordsHit = sensitiveKeywords(subdomain);
  232. if (sensitiveKeywordsHit) {
  233. weight += 8;
  234. if (hitLowKeywords) {
  235. weight += 4;
  236. }
  237. } else if (hitLowKeywords) {
  238. weight += 1;
  239. }
  240. const subdomainLength = subdomain.length;
  241. if (subdomainLength > 4) {
  242. weight += 0.5;
  243. if (subdomainLength > 10) {
  244. weight += 0.5;
  245. if (subdomainLength > 20) {
  246. weight += 1;
  247. if (subdomainLength > 30) {
  248. weight += 2;
  249. if (subdomainLength > 40) {
  250. weight += 4;
  251. }
  252. }
  253. }
  254. }
  255. if (subdomain.startsWith('www.')) {
  256. weight += 4;
  257. } else if (subdomain.slice(1).includes('.')) {
  258. weight += 1;
  259. if (subdomain.includes('www.')) {
  260. weight += 4;
  261. }
  262. }
  263. }
  264. return weight;
  265. }
  266. if (require.main === module) {
  267. getPhishingDomains(dummySpan)
  268. .catch(console.error);
  269. }