-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more wrappers and stuff (CI won't work)
- Loading branch information
Showing
20 changed files
with
796 additions
and
197 deletions.
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
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
49 changes: 49 additions & 0 deletions
49
Payload_Type/thanatos/agent/ffiwrappers/src/linux/group.rs
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,49 @@ | ||
use std::{ffi::CStr, ptr::NonNull}; | ||
|
||
use crate::errors::FfiError; | ||
|
||
pub struct GroupInfo(NonNull<libc::group>); | ||
|
||
impl GroupInfo { | ||
pub fn current_group() -> Result<GroupInfo, FfiError> { | ||
Self::lookup_gid(unsafe { libc::getgid() }) | ||
} | ||
|
||
pub fn effective_user() -> Result<GroupInfo, FfiError> { | ||
Self::lookup_gid(unsafe { libc::getegid() }) | ||
} | ||
|
||
pub fn lookup_username(username: &CStr) -> Result<GroupInfo, FfiError> { | ||
let grpasswd = unsafe { libc::getgrnam(username.as_ptr()) }; | ||
Ok(Self( | ||
NonNull::new(grpasswd).ok_or_else(|| FfiError::os_error())?, | ||
)) | ||
} | ||
|
||
pub fn lookup_gid(gid: u32) -> Result<GroupInfo, FfiError> { | ||
let grpasswd = unsafe { libc::getgrgid(gid) }; | ||
Ok(Self( | ||
NonNull::new(grpasswd).ok_or_else(|| FfiError::os_error())?, | ||
)) | ||
} | ||
|
||
pub fn groupname<'a>(&'a self) -> &'a str { | ||
unsafe { | ||
CStr::from_ptr(self.0.as_ref().gr_name) | ||
.to_str() | ||
.unwrap_unchecked() | ||
} | ||
} | ||
|
||
pub fn passwd<'a>(&'a self) -> &'a str { | ||
unsafe { | ||
CStr::from_ptr(self.0.as_ref().gr_passwd) | ||
.to_str() | ||
.unwrap_unchecked() | ||
} | ||
} | ||
|
||
pub fn gid(&self) -> u32 { | ||
unsafe { self.0.as_ref().gr_gid } | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
Payload_Type/thanatos/agent/ffiwrappers/src/linux/ifaddrs.rs
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,66 @@ | ||
use std::{ffi::CStr, marker::PhantomData, ptr::NonNull}; | ||
|
||
use crate::errors::FfiError; | ||
|
||
pub struct IfAddrsList { | ||
ifaddrs: NonNull<libc::ifaddrs>, | ||
_marker: PhantomData<libc::addrinfo>, | ||
} | ||
|
||
impl IfAddrsList { | ||
pub fn new() -> Result<IfAddrsList, FfiError> { | ||
let mut ifap = std::ptr::null_mut(); | ||
|
||
if unsafe { libc::getifaddrs(&mut ifap) } != 0 { | ||
return Err(FfiError::os_error()); | ||
} | ||
|
||
Ok(IfAddrsList { | ||
ifaddrs: NonNull::new(ifap).ok_or(FfiError::os_error())?, | ||
_marker: PhantomData, | ||
}) | ||
} | ||
|
||
pub fn first<'a>(&'a self) -> IfAddr<'a> { | ||
self.ifaddrs.into() | ||
} | ||
} | ||
|
||
impl Drop for IfAddrsList { | ||
fn drop(&mut self) { | ||
unsafe { libc::freeifaddrs(self.ifaddrs.as_ptr()) }; | ||
} | ||
} | ||
|
||
pub struct IfAddr<'a> { | ||
ifaddr: NonNull<libc::ifaddrs>, | ||
_marker: PhantomData<&'a libc::ifaddrs>, | ||
} | ||
|
||
impl<'a> IfAddr<'a> { | ||
pub fn name(&self) -> &str { | ||
unsafe { | ||
CStr::from_ptr(self.ifaddr.as_ref().ifa_name) | ||
.to_str() | ||
.unwrap_unchecked() | ||
} | ||
} | ||
} | ||
|
||
impl<'a> From<NonNull<libc::ifaddrs>> for IfAddr<'a> { | ||
fn from(value: NonNull<libc::ifaddrs>) -> Self { | ||
IfAddr { | ||
ifaddr: value, | ||
_marker: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
impl<'a> Iterator for IfAddr<'a> { | ||
type Item = IfAddr<'a>; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
self.ifaddr = NonNull::new(unsafe { self.ifaddr.as_ref().ifa_next })?; | ||
Some(self.ifaddr.into()) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,10 @@ | ||
mod gethostname; | ||
mod username; | ||
|
||
pub mod addrinfo; | ||
pub mod group; | ||
pub mod ifaddrs; | ||
pub mod socket; | ||
pub mod uname; | ||
pub mod user; | ||
|
||
pub use gethostname::gethostname; | ||
pub use username::username; |
Oops, something went wrong.