get-phishing-domains.ts 6.5 KB

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