get-phishing-domains.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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', '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. '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. ]);
  73. const lowKeywords = createKeywordFilter([
  74. 'transactions-',
  75. 'payment-',
  76. '-transactions',
  77. '-payment',
  78. '-faceb', // facebook fake
  79. '.faceb', // facebook fake
  80. 'facebook',
  81. 'virus-',
  82. 'icloud-',
  83. 'apple-',
  84. '-roblox',
  85. '-co-jp',
  86. 'customer.',
  87. 'customer-',
  88. '.www-',
  89. '.www.',
  90. '.www2',
  91. 'instagram',
  92. 'microsoft',
  93. 'passwordreset',
  94. '.google-',
  95. 'recover'
  96. ]);
  97. const cacheKey = createCacheKey(__filename);
  98. export const getPhishingDomains = (parentSpan: Span) => parentSpan.traceChild('get phishing domains').traceAsyncFn(async (span) => {
  99. const domainArr = await span.traceChildAsync('download/parse/merge phishing domains', async (curSpan) => {
  100. const domainArr: string[] = [];
  101. (await Promise.all(PHISHING_DOMAIN_LISTS_EXTRA.map(entry => processDomainLists(curSpan, ...entry, cacheKey))))
  102. .forEach(appendArrayInPlaceCurried(domainArr));
  103. (await Promise.all(PHISHING_HOSTS_EXTRA.map(entry => processHosts(curSpan, ...entry, cacheKey))))
  104. .forEach(appendArrayInPlaceCurried(domainArr));
  105. return domainArr;
  106. });
  107. const cacheHash = span.traceChildSync('get hash', () => stringHash(fastStringArrayJoin(domainArr, '|')));
  108. return span.traceChildAsync(
  109. 'process phishing domain set',
  110. () => processPhihsingDomains(domainArr, cacheHash)
  111. );
  112. });
  113. async function processPhihsingDomains(domainArr: string[], cacheHash = '') {
  114. return fsFetchCache.apply(
  115. cacheKey('processPhihsingDomains|' + cacheHash),
  116. () => {
  117. const domainCountMap: Record<string, number> = {};
  118. const domainScoreMap: Record<string, number> = {};
  119. for (let i = 0, len = domainArr.length; i < len; i++) {
  120. const line = domainArr[i];
  121. const {
  122. publicSuffix: tld,
  123. domain: apexDomain,
  124. subdomain,
  125. isPrivate
  126. } = tldts.parse(line, loosTldOptWithPrivateDomains);
  127. if (isPrivate) {
  128. continue;
  129. }
  130. if (!tld) {
  131. console.log(picocolors.yellow('[phishing domains] E0001'), 'missing tld', { line, tld });
  132. continue;
  133. }
  134. if (!apexDomain) {
  135. console.log(picocolors.yellow('[phishing domains] E0002'), 'missing domain', { line, apexDomain });
  136. continue;
  137. }
  138. domainCountMap[apexDomain] ||= 0;
  139. domainCountMap[apexDomain] += 1;
  140. if (!(apexDomain in domainScoreMap)) {
  141. domainScoreMap[apexDomain] = 0;
  142. if (BLACK_TLD.has(tld)) {
  143. domainScoreMap[apexDomain] += 4;
  144. } else if (tld.length > 6) {
  145. domainScoreMap[apexDomain] += 2;
  146. }
  147. if (apexDomain.length >= 18) {
  148. domainScoreMap[apexDomain] += 0.5;
  149. }
  150. }
  151. if (
  152. subdomain
  153. && !WHITELIST_MAIN_DOMAINS.has(apexDomain)
  154. ) {
  155. domainScoreMap[apexDomain] += calcDomainAbuseScore(subdomain, line);
  156. }
  157. }
  158. for (const apexDomain in domainCountMap) {
  159. if (
  160. // !WHITELIST_MAIN_DOMAINS.has(apexDomain)
  161. domainScoreMap[apexDomain] >= 16
  162. || (domainScoreMap[apexDomain] >= 13 && domainCountMap[apexDomain] >= 7)
  163. || (domainScoreMap[apexDomain] >= 5 && domainCountMap[apexDomain] >= 10)
  164. ) {
  165. domainArr.push('.' + apexDomain);
  166. }
  167. }
  168. // console.log({
  169. // count: domainCountMap['google.com'],
  170. // score: domainScoreMap['google.com']
  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. }