r/ExploitDev • • Dec 17 '24

Secure context from http page

hey guys, I have the following snippet here where I can try to execute a javascript payload in a new window that regains secure context if the origin page was http:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Secure Script Execution</title>
    <script>
        window.onload = function () {
            // URL of a secure blank page (use your own HTTPS domain)
            const secureWindowUrl = 'https://your-https-domain.com/secure_blank.html';

            // Open the secure window
            const secureWindow = window.open(secureWindowUrl, '_blank', 'noopener,noreferrer');

            // JavaScript payload to execute
            const scriptPayload = `
                console.log('Running script in a secure context');
                alert('This script is running securely!');
            `;

            // Send the payload to the new window
            window.addEventListener('message', function(event) {
                if (event.data === 'ready') {
                    secureWindow.postMessage({ script: scriptPayload }, '*'); // Replace '*' with specific origin for security
                }
            });
        };
    </script>
</head>
<body>
    <h1>Secure Script Execution</h1>
    <p>Opening a secure window to execute JavaScript independently.</p>
</body>
</html>

I was wondering if there is a way to modify this payload, or use a different technique that would allow me to execute an https page in a secure context THAT ORIGINATED from an http page, without opening a new popup window

7 Upvotes

10 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Dec 17 '24

Ok thanks for clarifying.

Off the top of my head I don't think it would normally be possible without opening a new window. And even if you're allowed to, your solution only works because there is a vulnerability on the https page: there is an insecure message handler that will run arbitrary js from untrusted origins. Is that the case? Does it has other vulnerabilities that you could exploit?

Edit: of course you can avoid opening a new window if you already have access to another window, through window.opener for example.

1

u/ansolo00 Dec 17 '24

is there a way to open the window headless? where I do not require it to be opening a new tab for code to execute?