get-phishing-domains.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import { getGorhillPublicSuffixPromise } from './get-gorhill-publicsuffix';
  2. import { processDomainLists } from './parse-filter';
  3. import { getSubdomain, getPublicSuffix } from 'tldts-experimental';
  4. import type { Span } from '../trace';
  5. import { appendArrayInPlaceCurried } from './append-array-in-place';
  6. import { PHISHING_DOMAIN_LISTS } from './reject-data-source';
  7. const BLACK_TLD = new Set([
  8. 'accountant',
  9. 'autos',
  10. 'bar',
  11. 'bid',
  12. 'biz',
  13. 'bond',
  14. 'business',
  15. 'buzz',
  16. 'cc',
  17. 'cf',
  18. 'cfd',
  19. 'click',
  20. 'cloud',
  21. 'club',
  22. 'cn',
  23. 'codes',
  24. 'co.uk',
  25. 'co.in',
  26. 'com.br',
  27. 'com.cn',
  28. 'com.pl',
  29. 'com.vn',
  30. 'cool',
  31. 'cricket',
  32. 'cyou',
  33. 'date',
  34. 'download',
  35. 'faith',
  36. 'fit',
  37. 'fun',
  38. 'ga',
  39. 'gd',
  40. 'gq',
  41. 'group',
  42. 'host',
  43. 'icu',
  44. 'id',
  45. 'info',
  46. 'ink',
  47. 'life',
  48. 'live',
  49. 'link',
  50. 'loan',
  51. 'ltd',
  52. 'men',
  53. 'ml',
  54. 'mobi',
  55. 'net.pl',
  56. 'one',
  57. 'online',
  58. 'party',
  59. 'pro',
  60. 'pl',
  61. 'pw',
  62. 'racing',
  63. 'rest',
  64. 'review',
  65. 'rf.gd',
  66. 'sa.com',
  67. 'sbs',
  68. 'science',
  69. 'shop',
  70. 'site',
  71. 'space',
  72. 'store',
  73. 'stream',
  74. 'tech',
  75. 'tk',
  76. 'tokyo',
  77. 'top',
  78. 'trade',
  79. 'vip',
  80. 'vn',
  81. 'webcam',
  82. 'website',
  83. 'win',
  84. 'xyz',
  85. 'za.com',
  86. 'lat',
  87. 'design'
  88. ]);
  89. export const WHITELIST_MAIN_DOMAINS = new Set([
  90. 'w3s.link', // ipfs gateway
  91. 'dweb.link', // ipfs gateway
  92. 'nftstorage.link', // ipfs gateway
  93. 'fleek.cool', // ipfs gateway
  94. 'business.site', // Drag'n'Drop site building platform
  95. 'page.link', // Firebase URL Shortener
  96. 'notion.site'
  97. ]);
  98. const tldtsOpt: Parameters<typeof getSubdomain>[1] = {
  99. allowPrivateDomains: false,
  100. extractHostname: false,
  101. validateHostname: false,
  102. detectIp: false,
  103. mixedInputs: false
  104. };
  105. export const getPhishingDomains = (parentSpan: Span) => parentSpan.traceChild('get phishing domains').traceAsyncFn(async (span) => {
  106. const gorhill = await getGorhillPublicSuffixPromise();
  107. const domainArr = await span.traceChildAsync('download/parse/merge phishing domains', async (curSpan) => {
  108. const domainSet: string[] = [];
  109. (await Promise.all(PHISHING_DOMAIN_LISTS.map(entry => processDomainLists(curSpan, ...entry))))
  110. .forEach(appendArrayInPlaceCurried(domainSet));
  111. return domainSet;
  112. });
  113. const domainCountMap: Record<string, number> = {};
  114. span.traceChildSync('process phishing domain set', () => {
  115. for (let i = 0, len = domainArr.length; i < len; i++) {
  116. const line = domainArr[i];
  117. const safeGorhillLine = line[0] === '.' ? line.slice(1) : line;
  118. const apexDomain = gorhill.getDomain(safeGorhillLine);
  119. if (!apexDomain) {
  120. continue;
  121. }
  122. const tld = getPublicSuffix(safeGorhillLine, tldtsOpt);
  123. if (!tld || !BLACK_TLD.has(tld)) continue;
  124. domainCountMap[apexDomain] ||= 0;
  125. domainCountMap[apexDomain] += calcDomainAbuseScore(line);
  126. }
  127. });
  128. for (const domain in domainCountMap) {
  129. if (domainCountMap[domain] >= 8 && !WHITELIST_MAIN_DOMAINS.has(domain)) {
  130. domainArr.push(`.${domain}`);
  131. }
  132. }
  133. return domainArr;
  134. });
  135. export function calcDomainAbuseScore(line: string) {
  136. let weight = 1;
  137. const isPhishingDomainMockingCoJp = line.includes('-co-jp');
  138. if (isPhishingDomainMockingCoJp) {
  139. weight += 0.5;
  140. }
  141. if (line.startsWith('.amaz')) {
  142. weight += 0.5;
  143. if (line.startsWith('.amazon-')) {
  144. weight += 4.5;
  145. }
  146. if (isPhishingDomainMockingCoJp) {
  147. weight += 4;
  148. }
  149. } else if (line.includes('.customer')) {
  150. weight += 0.25;
  151. }
  152. const lineLen = line.length;
  153. if (lineLen > 19) {
  154. // Add more weight if the domain is long enough
  155. if (lineLen > 44) {
  156. weight += 3.5;
  157. } else if (lineLen > 34) {
  158. weight += 2.5;
  159. } else if (lineLen > 29) {
  160. weight += 1.5;
  161. } else if (lineLen > 24) {
  162. weight += 0.75;
  163. } else {
  164. weight += 0.25;
  165. }
  166. }
  167. const subdomain = getSubdomain(line, tldtsOpt);
  168. if (subdomain) {
  169. if (subdomain.slice(1).includes('.')) {
  170. weight += 1;
  171. }
  172. if (subdomain.length > 40) {
  173. weight += 3;
  174. } else if (subdomain.length > 30) {
  175. weight += 1.5;
  176. } else if (subdomain.length > 20) {
  177. weight += 1;
  178. } else if (subdomain.length > 10) {
  179. weight += 0.1;
  180. }
  181. }
  182. return weight;
  183. }