get-phishing-domains.ts 4.6 KB

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