-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
159 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* ---------------------------------- | ||
* | libmem - by rdbo | | ||
* | Memory Hacking Library | | ||
* ---------------------------------- | ||
*/ | ||
|
||
/* | ||
* Copyright (C) 2023 Rdbo | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License version 3 | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "osprot.h" | ||
#include <sys/mman.h> | ||
|
||
int | ||
get_os_prot(lm_prot_t prot) | ||
{ | ||
return (int)(prot & LM_PROT_XRW); | ||
} | ||
|
||
lm_prot_t | ||
get_prot(int osprot) | ||
{ | ||
return (lm_prot_t)(osprot & (PROT_EXEC | PROT_READ | PROT_WRITE)); | ||
} |
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,36 @@ | ||
/* | ||
* ---------------------------------- | ||
* | libmem - by rdbo | | ||
* | Memory Hacking Library | | ||
* ---------------------------------- | ||
*/ | ||
|
||
/* | ||
* Copyright (C) 2023 Rdbo | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License version 3 | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/* TODO: Merge 'osprot.h' and 'osprot.c' for Linux and FreeBSD */ | ||
|
||
#ifndef OSPROT_H | ||
#define OSPROT_H | ||
|
||
#include <libmem/libmem.h> | ||
|
||
int | ||
get_os_prot(lm_prot_t prot); | ||
|
||
lm_prot_t | ||
get_prot(int osprot); | ||
|
||
#endif |
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,76 @@ | ||
/* | ||
* ---------------------------------- | ||
* | libmem - by rdbo | | ||
* | Memory Hacking Library | | ||
* ---------------------------------- | ||
*/ | ||
|
||
/* | ||
* Copyright (C) 2023 Rdbo | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License version 3 | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include <libmem/libmem.h> | ||
#include "osprot.h" | ||
#include <unistd.h> | ||
#include <sys/user.h> | ||
#include <sys/sysctl.h> | ||
#include <libprocstat.h> | ||
|
||
LM_API lm_bool_t LM_CALL | ||
LM_EnumSegmentsEx(const lm_process_t *process, | ||
lm_bool_t (LM_CALL *callback)(lm_segment_t *segment, | ||
lm_void_t *arg), | ||
lm_void_t *arg) | ||
{ | ||
lm_bool_t result = LM_FALSE; | ||
struct procstat *ps; | ||
struct kinfo_proc *proc; | ||
struct kinfo_vmentry *vmmap; | ||
unsigned int count; | ||
unsigned int i; | ||
lm_segment_t segment; | ||
|
||
if (!process || !callback) | ||
return result; | ||
|
||
ps = procstat_open_sysctl(); | ||
if (!ps) | ||
return result; | ||
|
||
proc = procstat_getprocs(ps, KERN_PROC_PID, process->pid, &count); | ||
if (!proc) | ||
goto CLOSE_EXIT; | ||
|
||
vmmap = procstat_getvmmap(ps, proc, &count); | ||
procstat_freeprocs(ps, proc); | ||
if (!vmmap) | ||
goto CLOSE_EXIT; | ||
|
||
for (i = 0; i < count; ++i) { | ||
segment.base = vmmap[i].kve_start; | ||
segment.end = vmmap[i].kve_end; | ||
segment.size = segment.end - segment.base; | ||
segment.prot = get_prot(vmmap[i].kve_protection); | ||
|
||
if (callback(&segment, arg) == LM_FALSE) | ||
break; | ||
} | ||
|
||
result = LM_TRUE; | ||
|
||
procstat_freevmmap(ps, vmmap); | ||
CLOSE_EXIT: | ||
procstat_close(ps); | ||
return result; | ||
} |
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