Basic Authentication

Cyberizm

Member
Joined
21 May 2026
Messages
22
Reaction score
56
Points
13

Objective-C:


Vulnerable Code:

C++:
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    NSString *user = @"user";
    NSString *pass = @"pass";
    if ([[challenge protectionSpace] receivesCredentialSecurely] == YES && [[[challenge protectionSpace] host] isEqualToString:@"myhost.com"]) {
        NSURLCredential *credential = [NSURLCredential credentialWithUser:user password:pass persistence:NSURLCredentialPersistenceForSession];
        [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
    }
}
Description: This vulnerable code implements HTTP basic authentication using NSURLConnection. However, it hardcodes the username and password directly in the code, which poses a security risk.

Patched Code:

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    NSURLCredential *credential = [self retrieveCredentialFromKeychainForChallenge:challenge];
    if (credential) {
        [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
    } else {
        // Handle authentication failure or prompt user for credentials
    }
}

- (NSURLCredential *)retrieveCredentialFromKeychainForChallenge:(NSURLAuthenticationChallenge *)challenge {
    // Retrieve stored credentials from Keychain based on protection space
    // Implement this method to securely fetch credentials from Keychain
    // Return NSURLCredential object or nil if not found
}
Description: The patched code retrieves the username and password from the Keychain instead of hardcoding them, enhancing security. It delegates the responsibility of securely fetching credentials from the Keychain to a separate method, reducing the risk of exposing sensitive information.

Swift:

Vulnerable Code:

func connection(_ connection: NSURLConnection, willSendRequestFor challenge: URLAuthenticationChallenge) {
    let user = "user"
    let pass = "pass"
    if challenge.protectionSpace.receivesCredentialSecurely == true && challenge.protectionSpace.host == "myhost.com" {
        let credential = URLCredential(user: user, password: pass, persistence: .forSession)
        challenge.sender?.use(credential, for: challenge)
    }
}
Description: Similar to the Objective-C code, this vulnerable Swift code implements HTTP basic authentication with hardcoded credentials, posing a security risk.

Patched Code:

func connection(_ connection: NSURLConnection, willSendRequestFor challenge: URLAuthenticationChallenge) {
    guard let credential = retrieveCredentialFromKeychain(for: challenge) else {
        // Handle authentication failure or prompt user for credentials
        return
    }
    challenge.sender?.use(credential, for: challenge)
}

func retrieveCredentialFromKeychain(for challenge: URLAuthenticationChallenge) -> URLCredential? {
    // Retrieve stored credentials from Keychain based on protection space
    // Implement this method to securely fetch credentials from Keychain
    // Return URLCredential object or nil if not found
}

Description: The patched Swift code also retrieves credentials from the Keychain instead of hardcoding them, improving security. It separates the credential retrieval logic into a dedicated method, promoting code readability and maintainability.





---


Professional Hacking Services Available


We offer ethical security assessments and penetration testing for iOS and Android mobile devices, websites, web apps, data servers, cryptography, operational security (OPSEC) posture reviews, digital footprint decontamination, account takeover (ATO) resistance audits, and adversarial reputation risk mitigation.





For inquiries:


E-mail: cyberizm@proton.me / cyberizm@dnmx.cc


Telegram: @cyb3rizm


Signal: cyberizm.88





Contact us for discreet, professional consultation and assistance.