Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid race condition in the extension callback on windows beacons #1159

Merged

Conversation

DominicBreuker
Copy link
Contributor

Card

Beacons on Windows can crash if you run too many extensions at the same time.
I believe this is because of a race condition in WindowsExtenions.Call, which stores the function onFinish referred to in the callback on the struct.
The change is to use a closure and don't reference the struct.

Details

I've noticed the following behaviour: Write yourself an extension or install one from the armory. For example armory install sa-whoami. Get a Windows HTTP beacon and call sa-whoami slowly, only once per check in. Everything will be fine (well, the first time there is no output, but I don't yet know why). Now run sa-whoami lots of times so that they all execute on the next check in. There is a good chance your implant will die with exit code 2, probably a panic.

In the code I saw the following:

Now my thinking is that because all calls to extensions are created in their own Goroutine they may overwrite the onFinish callback stored on the struct, which is stored before a lock is acquired. Moreover, in beacon mode it seems that each call to the extension also loads the extension. Not sure about it but I wonder if the entire extension struct may be garbage-collected by the time the DLL calls back.

The change is to decouple the DLL callback from the extension struct. This should ensure the reference to the callback survives until it is called.

In my experiments the beacon did not crash a single time anymore after this change no matter the number of extension calls. Still, you probably want to give it a try yourself before merging this!!! I only tried with sa-whoami and an extension I wrote myself, with HTTP baecon.

@DominicBreuker DominicBreuker requested a review from a team as a code owner March 20, 2023 21:22
@rkervella
Copy link
Member

Thanks @DominicBreuker, that's a good analysis, and indeed, it looks like we're overwriting the callback between each call. I'll test this PR when I get some time, but it looks good after a first look at the code.

@github-advanced-security
Copy link

You have successfully added a new CodeQL configuration .github/workflows/codeql-scanning.yml:CodeQL-Build. As part of the setup process, we have scanned this repository and found 20 existing alerts. Please check the repository Security tab to see all alerts.

@moloch-- moloch-- merged commit 2ece520 into BishopFox:master Mar 21, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants