get-phishing-domains.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import { processDomainLists } from './parse-filter';
  2. import * as tldts from 'tldts-experimental';
  3. import type { Span } from '../trace';
  4. import { appendArrayInPlaceCurried } from './append-array-in-place';
  5. import { PHISHING_DOMAIN_LISTS_EXTRA } from '../constants/reject-data-source';
  6. import { loosTldOptWithPrivateDomains } from '../constants/loose-tldts-opt';
  7. import picocolors from 'picocolors';
  8. import createKeywordFilter from './aho-corasick';
  9. import { createCacheKey } from './cache-filesystem';
  10. const BLACK_TLD = new Set([
  11. 'accountant',
  12. 'autos',
  13. 'bar',
  14. 'bid',
  15. 'biz',
  16. 'bond',
  17. 'business',
  18. 'buzz',
  19. 'cc',
  20. 'cf',
  21. 'cfd',
  22. 'click',
  23. 'cloud',
  24. 'club',
  25. 'cn',
  26. 'codes',
  27. 'co.uk',
  28. 'co.in',
  29. 'com.br',
  30. 'com.cn',
  31. 'com.pl',
  32. 'com.vn',
  33. 'cool',
  34. 'cricket',
  35. 'cyou',
  36. 'date',
  37. 'digital',
  38. 'download',
  39. 'faith',
  40. 'fit',
  41. 'fun',
  42. 'ga',
  43. 'gd',
  44. 'gives',
  45. 'gq',
  46. 'group',
  47. 'host',
  48. 'icu',
  49. 'id',
  50. 'info',
  51. 'ink',
  52. 'life',
  53. 'live',
  54. 'link',
  55. 'loan',
  56. 'ltd',
  57. 'men',
  58. 'ml',
  59. 'mobi',
  60. 'net.pl',
  61. 'one',
  62. 'online',
  63. 'party',
  64. 'pro',
  65. 'pl',
  66. 'pw',
  67. 'racing',
  68. 'rest',
  69. 'review',
  70. 'rf.gd',
  71. 'sa.com',
  72. 'sbs',
  73. 'science',
  74. 'shop',
  75. 'site',
  76. 'space',
  77. 'store',
  78. 'stream',
  79. 'surf',
  80. 'tech',
  81. 'tk',
  82. 'tokyo',
  83. 'top',
  84. 'trade',
  85. 'vip',
  86. 'vn',
  87. 'webcam',
  88. 'website',
  89. 'win',
  90. 'xyz',
  91. 'za.com',
  92. 'lat',
  93. 'design'
  94. ]);
  95. const WHITELIST_MAIN_DOMAINS = new Set([
  96. 'w3s.link', // ipfs gateway
  97. // 'dweb.link', // ipfs gateway
  98. // 'nftstorage.link', // ipfs gateway
  99. 'fleek.cool', // ipfs gateway
  100. 'business.site', // Drag'n'Drop site building platform
  101. 'page.link', // Firebase URL Shortener
  102. // 'notion.site',
  103. // 'vercel.app',
  104. 'gitbook.io'
  105. ]);
  106. const sensitiveKeywords = createKeywordFilter([
  107. '-roblox',
  108. '.amazon-',
  109. '-amazon',
  110. 'fb-com',
  111. 'facebook.',
  112. 'facebook-',
  113. '.facebook',
  114. '-facebook',
  115. 'coinbase',
  116. 'metamask-',
  117. '-metamask',
  118. 'virus-',
  119. 'icloud-',
  120. 'apple-',
  121. 'www.apple.',
  122. '-coinbase',
  123. 'coinbase-',
  124. 'lcloud.',
  125. 'lcloud-'
  126. ]);
  127. const lowKeywords = createKeywordFilter([
  128. '-co-jp',
  129. 'customer.',
  130. 'customer-',
  131. '.www-',
  132. 'instagram'
  133. ]);
  134. const cacheKey = createCacheKey(__filename);
  135. export const getPhishingDomains = (parentSpan: Span) => parentSpan.traceChild('get phishing domains').traceAsyncFn(async (span) => {
  136. const domainArr = await span.traceChildAsync('download/parse/merge phishing domains', async (curSpan) => {
  137. const domainArr: string[] = [];
  138. (await Promise.all(PHISHING_DOMAIN_LISTS_EXTRA.map(entry => processDomainLists(curSpan, ...entry, cacheKey))))
  139. .forEach(appendArrayInPlaceCurried(domainArr));
  140. return domainArr;
  141. });
  142. const domainCountMap: Record<string, number> = {};
  143. const domainScoreMap: Record<string, number> = {};
  144. span.traceChildSync('process phishing domain set', () => {
  145. for (let i = 0, len = domainArr.length; i < len; i++) {
  146. const line = domainArr[i];
  147. const {
  148. publicSuffix: tld,
  149. domain: apexDomain,
  150. subdomain,
  151. isPrivate
  152. } = tldts.parse(line, loosTldOptWithPrivateDomains);
  153. if (isPrivate) {
  154. continue;
  155. }
  156. if (!tld) {
  157. console.log(picocolors.yellow('[phishing domains] E0001'), 'missing tld', { line, tld });
  158. continue;
  159. }
  160. if (!apexDomain) {
  161. console.log(picocolors.yellow('[phishing domains] E0002'), 'missing domain', { line, apexDomain });
  162. continue;
  163. }
  164. domainCountMap[apexDomain] ||= 0;
  165. domainCountMap[apexDomain] += 1;
  166. if (!(apexDomain in domainScoreMap)) {
  167. domainScoreMap[apexDomain] = 0;
  168. if (BLACK_TLD.has(tld)) {
  169. domainScoreMap[apexDomain] += 4;
  170. } else if (tld.length > 6) {
  171. domainScoreMap[apexDomain] += 2;
  172. }
  173. }
  174. domainScoreMap[apexDomain] += calcDomainAbuseScore(subdomain);
  175. }
  176. });
  177. for (const domain in domainCountMap) {
  178. if (
  179. !WHITELIST_MAIN_DOMAINS.has(domain)
  180. && (
  181. domainScoreMap[domain] >= 12
  182. || (domainScoreMap[domain] >= 5 && domainCountMap[domain] >= 4)
  183. )
  184. ) {
  185. console.log({ domain });
  186. domainArr.push(`.${domain}`);
  187. }
  188. }
  189. return domainArr;
  190. });
  191. export function calcDomainAbuseScore(subdomain: string | null) {
  192. let weight = 0;
  193. if (subdomain) {
  194. const hitLowKeywords = lowKeywords(subdomain);
  195. const sensitiveKeywordsHit = sensitiveKeywords(subdomain);
  196. if (sensitiveKeywordsHit) {
  197. weight += 8;
  198. if (hitLowKeywords) {
  199. weight += 4;
  200. }
  201. } else if (hitLowKeywords) {
  202. weight += 1;
  203. }
  204. const subdomainLength = subdomain.length;
  205. if (subdomainLength > 4) {
  206. weight += 0.5;
  207. if (subdomainLength > 10) {
  208. weight += 0.5;
  209. if (subdomainLength > 20) {
  210. weight += 1;
  211. if (subdomainLength > 30) {
  212. weight += 2;
  213. if (subdomainLength > 40) {
  214. weight += 4;
  215. }
  216. }
  217. }
  218. }
  219. if (subdomain.startsWith('www.')) {
  220. weight += 4;
  221. } else if (subdomain.slice(1).includes('.')) {
  222. weight += 1;
  223. if (subdomain.includes('www.')) {
  224. weight += 4;
  225. }
  226. }
  227. }
  228. }
  229. return weight;
  230. }