get-phishing-domains.ts 3.9 KB

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