get-phishing-domains.ts 6.4 KB

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