DEV Community

Mohammad Waseem
Mohammad Waseem

Posted on

Leveraging TypeScript and Open Source Tools to Detect Phishing Patterns Effectively

Detecting Phishing Patterns with TypeScript and Open Source Tools

In today’s cybersecurity landscape, phishing remains a persistent threat, leveraging social engineering to deceive users and compromise sensitive information. As a Lead QA Engineer, developing an automated, reliable approach to detect phishing patterns is crucial for safeguarding users and systems.

This guide demonstrates how to leverage TypeScript along with open source libraries to identify and analyze potential phishing URLs or email patterns with precision. We'll explore a modular approach, integrating pattern recognition, similarity scoring, and real-time alerts.

Setting Up the Environment

First, ensure you have Node.js and npm installed. Then, initialize your project:

mkdir phishing-detection && cd phishing-detection
npm init -y
npm install typescript @types/node axios string-similarity
npx tsc --init
Enter fullscreen mode Exit fullscreen mode

The key libraries used are:

  • axios — For fetching email content and URL data.
  • string-similarity — For comparing URLs and detecting common phishing mimicry.

Core Logic: Pattern Detection

The core of this detection system involves identifying suspicious URL patterns or email content signatures.

Example: URL Similarity Check

import * as stringSimilarity from 'string-similarity';

const knownPhishingDomains = [
  'secure-login.com',
  'accounts-google.com',
  'bank-secure.net'
];

function isSuspiciousURL(url: string): boolean {
  const { hostname } = new URL(url);
  const similarityScores = knownPhishingDomains.map(domain => {
    return stringSimilarity.compareTwoStrings(hostname, domain);
  });
  const maxScore = Math.max(...similarityScores);
  // Threshold of 0.7 for suspicious match
  return maxScore > 0.7;
}

// Usage example
console.log(isSuspiciousURL('https://secure-login.com/account')); // true
Enter fullscreen mode Exit fullscreen mode

This function compares incoming URLs to a whitelist of known malicious domains, flagging high-similarity matches.

Email Content Pattern Checks

Email content often contains triggers such as urgent language or unexpected links.

function containsPhishingSignals(emailBody: string): boolean {
  const suspiciousKeywords = ['urgent', 'verify now', 'click here', 'update your account'];
  return suspiciousKeywords.some(keyword => emailBody.toLowerCase().includes(keyword));
}

// Integrate as part of your email processing pipeline
Enter fullscreen mode Exit fullscreen mode

Enhancing Detection with Open Source Tools

Beyond string similarity and keyword detection, you can incorporate tools like AYIMA’s Phish-Scanner or PhishTank API for real-time URL verification.

For URL validation with PhishTank:

import axios from 'axios';

async function isUrlFraudulent(url: string): Promise<boolean> {
  const apiUrl = `https://checkurl.phishtank.com/checkurl/`;
  const response = await axios.post(apiUrl, { url });
  // Pseudocode: check response data for URL verdict
  return response.data.is_phished;
}
Enter fullscreen mode Exit fullscreen mode

Note: Always check the latest API documentation for proper integration.

Putting It All Together

Combine URL similarity checks, email content analysis, and third-party API integration into a unified detection pipeline. Here's a simplified flow:

async function detectPhishing(emailContent: string, emailUrl: string): Promise<boolean> {
  const urlSuspicious = isSuspiciousURL(emailUrl);
  const contentSuspicious = containsPhishingSignals(emailContent);
  const urlFake = await isUrlFraudulent(emailUrl);
  return urlSuspicious || contentSuspicious || urlFake;
}
Enter fullscreen mode Exit fullscreen mode

Final Remarks

By integrating TypeScript, established open source libraries, and external validation services, QA teams can build resilient phishing detection systems. The modular nature of this approach allows easy updates, such as expanding the list of malicious keywords or updating URL similarity thresholds, ensuring continuous improvement.

Proactive detection reduces the potential of successful phishing attacks, enhancing overall security posture. Regularly review and update your detection logic, and leverage community sources and open source repositories for emerging threats.


🛠️ QA Tip

To test this safely without using real user data, I use TempoMail USA.

Top comments (0)