get-phishing-domains.ts 4.7 KB

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