get-phishing-domains.ts 6.3 KB

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