get-phishing-domains.ts 4.8 KB

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