get-phishing-domains.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import { getGorhillPublicSuffixPromise } from './get-gorhill-publicsuffix';
  2. import { processDomainLists } from './parse-filter';
  3. import { getSubdomain } from 'tldts';
  4. import { TTL } from './cache-filesystem';
  5. import { add as SetAdd } from 'mnemonist/set';
  6. import type { Span } from '../trace';
  7. import { appendArrayInPlace } from './append-array-in-place';
  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. export const getPhishingDomains = (parentSpan: Span) => parentSpan.traceChild('get phishing domains').traceAsyncFn(async (span) => {
  91. const gorhill = await getGorhillPublicSuffixPromise();
  92. const domainArr = await span.traceChildAsync('download/parse/merge phishing domains', async (curSpan) => {
  93. const [domainSet, domainSet2] = await Promise.all([
  94. processDomainLists(curSpan, 'https://curbengh.github.io/phishing-filter/phishing-filter-domains.txt', true, TTL.THREE_HOURS()),
  95. processDomainLists(curSpan, 'https://phishing.army/download/phishing_army_blocklist.txt', true, TTL.THREE_HOURS())
  96. ]);
  97. appendArrayInPlace(domainSet, domainSet2);
  98. return domainSet;
  99. });
  100. const domainCountMap: Record<string, number> = {};
  101. span.traceChildSync('process phishing domain set', () => {
  102. for (let i = 0, len = domainArr.length; i < len; i++) {
  103. const line = domainArr[i];
  104. const safeGorhillLine = line[0] === '.' ? line.slice(1) : line;
  105. const apexDomain = gorhill.getDomain(safeGorhillLine);
  106. if (!apexDomain) {
  107. continue;
  108. }
  109. const tld = gorhill.getPublicSuffix(safeGorhillLine);
  110. if (!tld || !BLACK_TLD.has(tld)) continue;
  111. domainCountMap[apexDomain] ||= 0;
  112. domainCountMap[apexDomain] += calcDomainAbuseScore(line);
  113. }
  114. });
  115. span.traceChildSync('get final phishing results', () => {
  116. for (const domain in domainCountMap) {
  117. if (domainCountMap[domain] >= 8) {
  118. domainArr.push(`.${domain}`);
  119. }
  120. }
  121. });
  122. return domainArr;
  123. });
  124. export function calcDomainAbuseScore(line: string) {
  125. let weight = 1;
  126. const isPhishingDomainMockingCoJp = line.includes('-co-jp');
  127. if (isPhishingDomainMockingCoJp) {
  128. weight += 0.5;
  129. }
  130. if (line.startsWith('.amaz')) {
  131. weight += 0.5;
  132. if (line.startsWith('.amazon-')) {
  133. weight += 4.5;
  134. }
  135. if (isPhishingDomainMockingCoJp) {
  136. weight += 4;
  137. }
  138. } else if (line.includes('.customer')) {
  139. weight += 0.25;
  140. }
  141. const lineLen = line.length;
  142. if (lineLen > 19) {
  143. // Add more weight if the domain is long enough
  144. if (lineLen > 44) {
  145. weight += 3.5;
  146. } else if (lineLen > 34) {
  147. weight += 2.5;
  148. } else if (lineLen > 29) {
  149. weight += 1.5;
  150. } else if (lineLen > 24) {
  151. weight += 0.75;
  152. } else {
  153. weight += 0.25;
  154. }
  155. }
  156. const subdomain = getSubdomain(line, { detectIp: false });
  157. if (subdomain) {
  158. if (subdomain.slice(1).includes('.')) {
  159. weight += 1;
  160. }
  161. if (subdomain.length > 40) {
  162. weight += 3;
  163. } else if (subdomain.length > 30) {
  164. weight += 1.5;
  165. } else if (subdomain.length > 20) {
  166. weight += 1;
  167. } else if (subdomain.length > 10) {
  168. weight += 0.1;
  169. }
  170. }
  171. return weight;
  172. }