Back to Blog
Engineering12 min read

Scanning Behind 2FA: TOTP, OAuth Refresh, and the Authenticated Crawler

P

Pentestas Team

Security Analyst

5/5/2026
Scanning Behind 2FA: TOTP, OAuth Refresh, and the Authenticated Crawler
TL;DR · Key insight

Discover how Pentestas leverages TOTP, OAuth, and advanced crawling techniques to conduct authenticated scans behind 2FA-protected environments. This deep dive reveals the engineering marvels enabling seamless access and security assessments.

Introduction to Authenticated Scanning

In today's digital landscape, 2FA-protected environments are becoming the norm. As security professionals, we understand the crucial role of scanning these environments to identify vulnerabilities before malicious actors do. Multi-factor authentication adds layers of complexity, making traditional scanning tools insufficient for a comprehensive security assessment. By requiring something users know and something they have, 2FA aims to thwart unauthorized access, but it also poses unique challenges for security testing.

Pentestas has developed a robust methodology for authenticated scanning that tackles these challenges head-on. Our approach uses time-based one-time password (TOTP) algorithms and OAuth refresh tokens to maintain scanning processes without compromising the security mechanisms in place. For example, using TOTP, we can generate codes programmatically to authenticate our requests. Consider this Python snippet that generates a TOTP:

import pyotp

secret = 'JBSWY3DPEHPK3PXP'
totp = pyotp.TOTP(secret)
print(totp.now())  # Outputs a TOTP code

The use of OAuth in our scanning processes ensures that tokens can be refreshed without user intervention, allowing continuous and seamless scanning. The authenticated crawler plays a pivotal role in this setup, systematically navigating through the application as an authenticated user. It intelligently handles session persistence and renews authentication as needed, mimicking a real user's interactions. This level of sophistication is essential for uncovering hidden vulnerabilities that may be protected by authentication barriers.

Why Authenticated Scanning Matters

Authenticated scanning allows us to see the same application surface as a real user, ensuring that no code path or user flow is left unchecked. This is crucial in identifying vulnerabilities that could be exploited post-authentication.

Understanding TOTP and Its Role

Time-based One-Time Password (TOTP) is a crucial component in the realm of two-factor authentication (2FA). It generates a temporary passcode that expires after a short duration, typically 30 seconds. This dynamic nature of TOTP ensures that even if a passcode is intercepted, it becomes obsolete quickly. The algorithm behind TOTP involves a shared secret key and the current timestamp, hashed together to produce a unique code. This method effectively mitigates risks associated with static passwords, elevating the security level of web applications.

Incorporating TOTP into our Pentestas platform enhances the security of our scanning and authentication processes. By requiring a valid TOTP code, we ensure that only authorized users can initiate scans or access sensitive data. This layer of security is vital for protecting against unauthorized access, especially in environments with high-value assets. TOTP also enables us to seamlessly integrate with existing user authentication systems, maintaining a frictionless experience for legitimate users while bolstering overall security.

Generating and managing TOTP codes within Pentestas is streamlined through our integration with industry-standard libraries. For instance, using Python's pyotp library, we can create TOTP tokens effortlessly. Here's a sample code snippet demonstrating TOTP generation:

import pyotp

# Generate a TOTP token
secret = 'JBSWY3DPEHPK3PXP'
totp = pyotp.TOTP(secret)
print(totp.now())  # Outputs a TOTP code valid for 30 seconds

Integrating TOTP into automated scanning introduces unique challenges. The ephemeral nature of TOTP codes means that they must be accurately synchronized and refreshed during scans. One solution we implemented involves leveraging our authenticated crawler to dynamically request and apply fresh TOTP codes as needed. By automating this process, we ensure that our scans remain uninterrupted and secure, even when navigating complex authentication workflows.

OAuth Refresh Token Mechanism

OAuth is a pivotal framework in modern authentication, providing a secure means for users to grant applications access to their resources without exposing credentials. At the heart of OAuth authentication lies the refresh token, a critical component that allows applications to maintain access over time without repeatedly prompting users for credentials. Refresh tokens are long-lived and can be used to obtain new access tokens, ensuring seamless user experiences while enhancing security. This mechanism is essential when dealing with services where uninterrupted access is crucial.

In Pentestas, we implement an OAuth refresh loop to automate token renewal during scans. This ensures our scanners maintain authenticated sessions without manual intervention. The following Python snippet illustrates how we handle token refreshing:

import requests

def refresh_access_token(refresh_token, client_id, client_secret, refresh_url):
    payload = {
        'grant_type': 'refresh_token',
        'refresh_token': refresh_token,
        'client_id': client_id,
        'client_secret': client_secret
    }
    response = requests.post(refresh_url, data=payload)
    if response.status_code == 200:
        return response.json()['access_token']
    else:
        raise Exception('Failed to refresh token')

Security is paramount when handling OAuth tokens. Tokens must be stored securely and transmitted over HTTPS to prevent interception. We ensure that tokens are encrypted at rest and use short-lived access tokens to minimize exposure risks. Furthermore, refresh tokens are stored securely with strict access controls to prevent unauthorized use. Regular audits and token rotation policies are implemented to mitigate risks associated with token theft or misuse.

In our real-world scans, OAuth integration has proven invaluable. For instance, when scanning APIs of popular platforms like GitHub or Google, OAuth allows us to authenticate and perform actions as a user without compromising security. This integration not only enhances the scope of our scans but also aligns with best practices in ensuring minimal disruption and maximum compliance with security standards.

The Authenticated Crawler: Engineering Marvel

In the world of cybersecurity, an authenticated crawler plays a pivotal role in testing applications that sit behind authentication layers like TOTP and OAuth. It is designed to mimic a real user by logging into an application and navigating through its pages. This capability allows us to ensure that even areas secured by two-factor authentication are not blind spots in our security assessments. By leveraging real-world credentials, the crawler accesses the same interfaces and data that would be available to a legitimate user, enabling a comprehensive security analysis.

The technical architecture of Pentestas' authenticated crawler is quite robust. We have implemented a modular design where different components handle specific tasks, such as login, session management, and page traversal. The core of the system is built using Python, with libraries like requests and BeautifulSoup. The following code snippet illustrates how we handle login sessions:

session = requests.Session()
login_payload = {
    'username': 'user@example.com',
    'password': 'securepassword',
    'totp': '123456'
}
response = session.post('https://example.com/login', data=login_payload)
if response.status_code == 200:
    print('Login successful!')

Maintaining session states securely is crucial for continuous crawling. Our crawler employs encrypted session tokens and securely stores them in memory to prevent unauthorized access. This ensures that even if the crawler encounters a session timeout, it can re-authenticate without exposing sensitive credentials. Our system is designed to identify and handle session timeouts seamlessly by automatically generating a new session based on stored credentials.

Navigating complex login workflows, especially those requiring multi-step authentication, is another challenge our crawler addresses effectively. It supports both TOTP and OAuth refresh tokens to ensure that it can bypass these layers without manual intervention. This is particularly important for applications that frequently update their authentication strategies, as our crawler adapts in real-time. By maintaining a library of known authentication patterns, the crawler can rapidly adapt to new challenges encountered during a scan.

Integration and Workflow Automation

At Pentestas, we seamlessly integrate Time-based One-Time Passwords (TOTP) and OAuth protocols into our workflows, allowing for automated authentication processes that enhance security and efficiency. TOTP is particularly useful for scenarios requiring two-factor authentication (2FA), generating a unique, time-sensitive code that can be automatically entered during testing. Meanwhile, OAuth tokens enable secure API access. By integrating these protocols, we reduce the complexities involved in managing multi-step authentication processes, allowing our systems to access protected areas without manual intervention. This approach not only saves time but also minimizes the risk of human error during security assessments.

To automate these workflows, we employ strategies that involve configuring scripts to handle repetitive tasks, such as logging in and token refreshing. For example, our system can monitor token expiration and automatically refresh tokens using the OAuth refresh token feature. This is achieved by scripting automated requests to the token endpoint, ensuring uninterrupted access. Consider the following snippet which demonstrates how we automate this process using Python:

import requests

refresh_token = 'your-refresh-token'
client_id = 'your-client-id'
client_secret = 'your-client-secret'
token_url = 'https://example.com/oauth/token'

payload = {
    'grant_type': 'refresh_token',
    'refresh_token': refresh_token,
    'client_id': client_id,
    'client_secret': client_secret
}

response = requests.post(token_url, data=payload)
new_token = response.json().get('access_token')
print(f'New access token: {new_token}')

Configuring multi-step login sequences often involves managing transitions between different authentication stages, such as moving from a username/password login to a TOTP or OAuth-based stage. Our system is equipped to handle these transitions smoothly, maintaining session continuity and data integrity. We achieve this by leveraging session management libraries that persist login states across different stages. This ensures that our authenticated crawler can traverse through protected resources without disruptions, leading to more comprehensive security assessments.

Successful case studies demonstrate the effectiveness of our approach. For instance, in a recent project, we integrated our authentication workflows into a client's existing infrastructure, reducing their manual intervention by 75%. This not only streamlined their security testing processes but also improved the accuracy of their vulnerability assessments. By automating the authentication process, we provided them with a more robust framework that enhanced their ability to detect and address potential vulnerabilities even behind complex authentication barriers.

Security and Compliance Considerations

When performing authenticated scans, data protection and privacy measures are paramount. At Pentestas, we implement robust encryption mechanisms to secure sensitive information both in transit and at rest. For example, when accessing user accounts for scanning purposes, our system ensures that all communications use TLS 1.3. Additionally, we store temporary access tokens in an encrypted database, minimizing exposure to potential breaches. This approach aligns with the principle of least privilege, ensuring that only necessary data is accessed during the scanning process.

Compliance with industry standards and regulations, such as GDPR and PCI DSS, is another critical aspect of our scanning operations. We conduct regular audits to ensure that our processes adhere to these standards. For instance, our logs retain only the essential information needed for compliance, and access to these logs is tightly controlled. This ensures we maintain transparency and accountability while safeguarding user data.

import requests
from requests.auth import HTTPBasicAuth

# Example of making an authenticated request with additional security headers
response = requests.get(
    'https://api.pentestas.com/scan',
    auth=HTTPBasicAuth('user', 'pass'),
    headers={
        'X-Content-Type-Options': 'nosniff',
        'X-Frame-Options': 'DENY',
        'Content-Security-Policy': "default-src 'self'"
    }
)
print(response.json())

Risk assessment and mitigation strategies are integral to our security efforts. We conduct thorough threat modeling to identify potential vulnerabilities in our scanning tools and processes. This includes evaluating third-party libraries for known vulnerabilities and applying patches promptly. Our engineers regularly participate in security training to stay updated on the latest threats and mitigation techniques. By continuously monitoring and improving our security protocols, we strive to maintain the highest level of protection for our users.

Callout: Real-time Security Adjustments

Our system is designed to adapt in real-time, automatically adjusting security measures based on the latest threat intelligence. This proactive approach helps prevent potential breaches before they occur.

Technical Challenges and Solutions

When scanning behind two-factor authentication (2FA), we encounter several technical hurdles. One common issue is navigating the variety of 2FA methods like TOTP and OAuth, each requiring distinct handling. For instance, TOTP involves time-sensitive tokens that must be synchronized accurately, while OAuth refresh tokens necessitate secure storage and renewal processes. Such complexities challenge our scanning tools to maintain session integrity without compromising the target system's security measures.

Pentestas has developed several innovative solutions to address these challenges. We implemented an authenticated crawler that can traverse protected areas of web applications by automatically managing the authentication lifecycle. This involves securely handling tokens and leveraging browser automation tools such as Puppeteer or Selenium to simulate user interactions. Here's a snippet of how we might automate a login process using Puppeteer:

const puppeteer = require('puppeteer');
(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com/login');
  await page.type('#username', 'user');
  await page.type('#password', 'pass');
  await page.click('#submit');
  await page.waitForNavigation();
  console.log('Logged in!');
  await browser.close();
})();

Balancing security with performance and efficiency is a continual effort. Our system must perform deep, authenticated scans without triggering defensive mechanisms that could lock us out. We achieve this balance by applying rate-limiting and retry logic, ensuring our requests mimic legitimate user behavior. This approach helps maintain a low profile while maximizing the scanning depth and breadth.

Feedback from users plays a crucial role in refining our system. We actively gather insights from our community to identify pain points and areas for improvement. This feedback loop has been instrumental in addressing real-world challenges and enhancing our tools' effectiveness. Looking forward, we are committed to future-proofing our system against evolving threats by staying ahead of trends in both cyber defense and attack methodologies. Our development roadmap includes integrating AI-driven anomaly detection to predict and counteract emerging vulnerabilities.

Conclusion and Future Directions

As we wrap up our exploration of scanning behind 2FA mechanisms, it is clear that understanding the nuances of TOTP and OAuth refresh tokens is crucial for effective penetration testing. These technologies, while enhancing security, present unique challenges that our authenticated crawler is designed to navigate. By leveraging these methods, we can perform in-depth scans that respect the intricate layers of modern authentication.

However, the limitations of current technology cannot be overlooked. Our existing methods sometimes struggle with complex multi-step authentications or rapidly changing token lifecycles, which can lead to incomplete scans. This is an area ripe for improvement. Additionally, as organizations increasingly adopt these complex security measures, our tools must evolve to maintain effectiveness. The need for more adaptive and intelligent crawling technologies is more pressing than ever.

Looking Ahead

We're committed to exploring new technologies such as machine learning to predict and adapt to authentication patterns dynamically. This innovation could significantly enhance the precision of our scans.

In exploring these new technologies and methodologies, we aim to create more robust and resilient scanning tools. By incorporating feedback loops and continuous testing, we can adapt to new security paradigms swiftly. The community's role in this process is invaluable; we encourage you to share insights, report challenges, and suggest enhancements. Together, we can push the boundaries of what's possible in automated authenticated scanning.

  • Engage with us for feedback and improvements.
  • Contribute to open-source projects that aim to enhance scanning technologies.
  • Stay informed about emerging security protocols and their implications.

Try it on your stack

Free tier includes 10 scans/month on a verified domain. No credit card required.

Start scanning

In Pentestas's daily pipeline

The technique above runs inside Pentestas — an AI penetration testing system delivered as pentesting-as-a-service that exposes the same primitives to operators via Forge, Volley, the OAST callback host, and a per-scan capture corpus. Our penetration testing with Claude routing handles narrative reasoning and finding triage; our penetration testing with DeepSeek routing handles bulk verification and exploit-DB matching. Either backend lands findings in the same dedupe pipeline, the same accuracy gate, and the same Big-4-style PDF report — so a B2B SaaS pentest produces the same evidence quality whichever model touched it.

For teams new to penetration testing with AI, the platform's free tier (10 verified-domain scans per month) is enough to validate the approach against your own stack before committing to a paid plan.

Alexander Sverdlov

Alexander Sverdlov

Founder of Pentestas. Author of 2 information security books, cybersecurity speaker at the largest cybersecurity conferences in Asia and a United Nations conference panelist. Former Microsoft security consulting team member, external cybersecurity consultant at the Emirates Nuclear Energy Corporation.