get-phishing-domains.ts 4.0 KB

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