get-phishing-domains.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. 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 getPhishingDomains = (parentSpan: Span) => parentSpan.traceChild('get phishing domains').traceAsyncFn(async (span) => {
  90. const gorhill = await getGorhillPublicSuffixPromise();
  91. const domainSet = await span.traceChildAsync('download/parse/merge phishing domains', async (curSpan) => {
  92. const [domainSet, domainSet2] = await Promise.all([
  93. processDomainLists(curSpan, 'https://curbengh.github.io/phishing-filter/phishing-filter-domains.txt', true, TTL.THREE_HOURS()),
  94. processDomainLists(curSpan, 'https://phishing.army/download/phishing_army_blocklist.txt', true, TTL.THREE_HOURS())
  95. ]);
  96. SetAdd(domainSet, domainSet2);
  97. return domainSet;
  98. });
  99. const domainCountMap: Record<string, number> = {};
  100. span.traceChildSync('process phishing domain set', () => {
  101. const domainArr = Array.from(domainSet);
  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. const results = span.traceChildSync('get final phishing results', () => {
  116. const res: string[] = [];
  117. for (const domain in domainCountMap) {
  118. if (domainCountMap[domain] >= 8) {
  119. res.push(`.${domain}`);
  120. }
  121. }
  122. return res;
  123. });
  124. return [results, domainSet] as const;
  125. });
  126. export function calcDomainAbuseScore(line: string) {
  127. let weight = 1;
  128. const isPhishingDomainMockingCoJp = line.includes('-co-jp');
  129. if (isPhishingDomainMockingCoJp) {
  130. weight += 0.5;
  131. }
  132. if (line.startsWith('.amaz')) {
  133. weight += 0.5;
  134. if (line.startsWith('.amazon-')) {
  135. weight += 4.5;
  136. }
  137. if (isPhishingDomainMockingCoJp) {
  138. weight += 4;
  139. }
  140. } else if (line.includes('.customer')) {
  141. weight += 0.25;
  142. }
  143. const lineLen = line.length;
  144. if (lineLen > 19) {
  145. // Add more weight if the domain is long enough
  146. if (lineLen > 44) {
  147. weight += 3.5;
  148. } else if (lineLen > 34) {
  149. weight += 2.5;
  150. } else if (lineLen > 29) {
  151. weight += 1.5;
  152. } else if (lineLen > 24) {
  153. weight += 0.75;
  154. } else {
  155. weight += 0.25;
  156. }
  157. }
  158. const subdomain = getSubdomain(line, { detectIp: false });
  159. if (subdomain) {
  160. if (subdomain.slice(1).includes('.')) {
  161. weight += 1;
  162. }
  163. if (subdomain.length > 40) {
  164. weight += 3;
  165. } else if (subdomain.length > 30) {
  166. weight += 1.5;
  167. } else if (subdomain.length > 20) {
  168. weight += 1;
  169. } else if (subdomain.length > 10) {
  170. weight += 0.1;
  171. }
  172. }
  173. return weight;
  174. }