get-phishing-domains.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 { stringHash } from './string-hash';
  13. const BLACK_TLD = new Set([
  14. 'accountant', 'art', 'autos',
  15. 'bar', 'beauty', 'bid', 'bio', '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. 'focebaak',
  56. '.facebook.',
  57. 'metamask-',
  58. '-metamask',
  59. 'www.apple',
  60. '-coinbase',
  61. 'coinbase-',
  62. 'booking-com',
  63. 'booking.com-',
  64. 'booking-eu',
  65. 'vinted-cz',
  66. 'inpost-pl',
  67. 'login.microsoft',
  68. 'login-microsoft',
  69. 'microsoftonline',
  70. 'google.com-',
  71. 'minecraft',
  72. 'staemco'
  73. ]);
  74. const lowKeywords = createKeywordFilter([
  75. 'transactions-',
  76. 'payment-',
  77. '-transactions',
  78. '-payment',
  79. '-faceb', // facebook fake
  80. '.faceb', // facebook fake
  81. 'facebook',
  82. 'virus-',
  83. 'icloud-',
  84. 'apple-',
  85. '-roblox',
  86. '-co-jp',
  87. 'customer.',
  88. 'customer-',
  89. '.www-',
  90. '.www.',
  91. '.www2',
  92. 'instagram',
  93. 'microsof',
  94. 'passwordreset',
  95. '.google-',
  96. 'recover'
  97. ]);
  98. const cacheKey = createCacheKey(__filename);
  99. export function getPhishingDomains(parentSpan: Span) {
  100. return parentSpan.traceChild('get phishing domains').traceAsyncFn(async (span) => {
  101. const domainArr = await span.traceChildAsync('download/parse/merge phishing domains', async (curSpan) => {
  102. const domainArr: string[] = [];
  103. (await Promise.all(PHISHING_DOMAIN_LISTS_EXTRA.map(entry => processDomainLists(curSpan, ...entry, cacheKey))))
  104. .forEach(appendArrayInPlaceCurried(domainArr));
  105. (await Promise.all(PHISHING_HOSTS_EXTRA.map(entry => processHosts(curSpan, ...entry, cacheKey))))
  106. .forEach(appendArrayInPlaceCurried(domainArr));
  107. return domainArr;
  108. });
  109. const cacheHash = span.traceChildSync('get hash', () => stringHash(fastStringArrayJoin(domainArr, '|')));
  110. return span.traceChildAsync(
  111. 'process phishing domain set',
  112. () => processPhihsingDomains(domainArr, cacheHash)
  113. );
  114. });
  115. }
  116. async function processPhihsingDomains(domainArr: string[], cacheHash = '') {
  117. return fsFetchCache.apply(
  118. cacheKey('processPhihsingDomains|' + cacheHash),
  119. () => {
  120. const domainCountMap: Record<string, number> = {};
  121. const domainScoreMap: Record<string, number> = {};
  122. for (let i = 0, len = domainArr.length; i < len; i++) {
  123. const line = domainArr[i];
  124. const {
  125. publicSuffix: tld,
  126. domain: apexDomain,
  127. subdomain,
  128. isPrivate
  129. } = tldts.parse(line, loosTldOptWithPrivateDomains);
  130. if (isPrivate) {
  131. continue;
  132. }
  133. if (!tld) {
  134. console.log(picocolors.yellow('[phishing domains] E0001'), 'missing tld', { line, tld });
  135. continue;
  136. }
  137. if (!apexDomain) {
  138. console.log(picocolors.yellow('[phishing domains] E0002'), 'missing domain', { line, apexDomain });
  139. continue;
  140. }
  141. domainCountMap[apexDomain] ||= 0;
  142. domainCountMap[apexDomain] += 1;
  143. if (!(apexDomain in domainScoreMap)) {
  144. domainScoreMap[apexDomain] = 0;
  145. if (BLACK_TLD.has(tld)) {
  146. domainScoreMap[apexDomain] += 4;
  147. } else if (tld.length > 6) {
  148. domainScoreMap[apexDomain] += 2;
  149. }
  150. if (apexDomain.length >= 18) {
  151. domainScoreMap[apexDomain] += 0.5;
  152. }
  153. }
  154. if (
  155. subdomain
  156. && !WHITELIST_MAIN_DOMAINS.has(apexDomain)
  157. ) {
  158. domainScoreMap[apexDomain] += calcDomainAbuseScore(subdomain, line);
  159. }
  160. }
  161. for (const apexDomain in domainCountMap) {
  162. if (
  163. // !WHITELIST_MAIN_DOMAINS.has(apexDomain)
  164. domainScoreMap[apexDomain] >= 16
  165. || (domainScoreMap[apexDomain] >= 13 && domainCountMap[apexDomain] >= 7)
  166. || (domainScoreMap[apexDomain] >= 5 && domainCountMap[apexDomain] >= 10)
  167. || (domainScoreMap[apexDomain] >= 3 && domainCountMap[apexDomain] >= 16)
  168. ) {
  169. domainArr.push('.' + apexDomain);
  170. }
  171. }
  172. return Promise.resolve(domainArr);
  173. },
  174. {
  175. ttl: 2 * 86400 * 1000,
  176. serializer: serializeArray,
  177. deserializer: deserializeArray,
  178. incrementTtlWhenHit: true
  179. }
  180. );
  181. }
  182. export function calcDomainAbuseScore(subdomain: string, fullDomain: string = subdomain) {
  183. let weight = 0;
  184. const hitLowKeywords = lowKeywords(fullDomain);
  185. const sensitiveKeywordsHit = sensitiveKeywords(fullDomain);
  186. if (sensitiveKeywordsHit) {
  187. weight += 9;
  188. if (hitLowKeywords) {
  189. weight += 5;
  190. }
  191. } else if (hitLowKeywords) {
  192. weight += 1.5;
  193. }
  194. const subdomainLength = subdomain.length;
  195. if (subdomainLength > 4) {
  196. weight += 0.5;
  197. if (subdomainLength > 10) {
  198. weight += 0.6;
  199. if (subdomainLength > 20) {
  200. weight += 1;
  201. if (subdomainLength > 30) {
  202. weight += 2;
  203. if (subdomainLength > 40) {
  204. weight += 4;
  205. }
  206. }
  207. }
  208. }
  209. if (subdomain.startsWith('www.')) {
  210. weight += 1;
  211. } else if (subdomain.slice(1).includes('.')) {
  212. weight += 1;
  213. if (subdomain.includes('www.')) {
  214. weight += 1;
  215. }
  216. }
  217. }
  218. return weight;
  219. }
  220. if (require.main === module) {
  221. getPhishingDomains(dummySpan)
  222. .catch(console.error);
  223. }