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