C# ReadProcessMemory
I'm basically trying to figure out how to search for a value in a process without giving an exact offset. The process can be anything (notepad, iexplorer, msword, etc.). Just looking for search a value between the first and last memory address of a process instead of giving a specific offset, which is I had to find from another application like ollydbg.
Here's what I have
const int PROCESS_WM_READ = 0x0010;
[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
[DllImport("kernel32.dll")]
public static extern bool ReadProcessMemory(int hProcess,
Int64 lpBaseAddress, byte lpBuffer, int dwSize, ref int lpNumberOfBytesRead);
public static string search = "somestring";
static void Main(string args)
{
Process process = Process.GetProcessById(15728);
IntPtr processHandle = OpenProcess(PROCESS_WM_READ, false, process.Id);
int bytesRead = 0;
byte buffer = new byte[16];
ReadProcessMemory((int)processHandle, 0x20BC4ADE4C8, buffer, buffer.Length, ref bytesRead);
Console.WriteLine(Encoding.Unicode.GetString(buffer) +
" (" + bytesRead.ToString() + "bytes)");
if (Encoding.Unicode.GetString(buffer).Contains(somestring))
Console.WriteLine("Match");
else
Console.WriteLine("Didint Match");
Console.ReadLine();
}
c# memory readprocessmemory
|
show 3 more comments
I'm basically trying to figure out how to search for a value in a process without giving an exact offset. The process can be anything (notepad, iexplorer, msword, etc.). Just looking for search a value between the first and last memory address of a process instead of giving a specific offset, which is I had to find from another application like ollydbg.
Here's what I have
const int PROCESS_WM_READ = 0x0010;
[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
[DllImport("kernel32.dll")]
public static extern bool ReadProcessMemory(int hProcess,
Int64 lpBaseAddress, byte lpBuffer, int dwSize, ref int lpNumberOfBytesRead);
public static string search = "somestring";
static void Main(string args)
{
Process process = Process.GetProcessById(15728);
IntPtr processHandle = OpenProcess(PROCESS_WM_READ, false, process.Id);
int bytesRead = 0;
byte buffer = new byte[16];
ReadProcessMemory((int)processHandle, 0x20BC4ADE4C8, buffer, buffer.Length, ref bytesRead);
Console.WriteLine(Encoding.Unicode.GetString(buffer) +
" (" + bytesRead.ToString() + "bytes)");
if (Encoding.Unicode.GetString(buffer).Contains(somestring))
Console.WriteLine("Match");
else
Console.WriteLine("Didint Match");
Console.ReadLine();
}
c# memory readprocessmemory
2
Is there an actual question there?
– MickyD
Dec 31 '18 at 14:10
See pinvoke. IntPtr are 32 bit pointer. You have lpBaseAddress defined as a Int64 which will not work. You also have to move the byte in c# from managed memory to unmanaged memory before passing to dll : pinvoke.net/default.aspx/user32/ReadProcessMemory.html
– jdweng
Dec 31 '18 at 14:11
ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte lpBuffer, IntPtr nSize, out IntPtr lpNumberOfBytesRead)
– GSerg
Dec 31 '18 at 14:12
a resource for this kind of work pinvoke.net as @jdweng provide the specific case.
– kenny
Dec 31 '18 at 14:13
Code work without any problem. but i dont wanna give specific memory offset
– lstngl
Dec 31 '18 at 14:20
|
show 3 more comments
I'm basically trying to figure out how to search for a value in a process without giving an exact offset. The process can be anything (notepad, iexplorer, msword, etc.). Just looking for search a value between the first and last memory address of a process instead of giving a specific offset, which is I had to find from another application like ollydbg.
Here's what I have
const int PROCESS_WM_READ = 0x0010;
[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
[DllImport("kernel32.dll")]
public static extern bool ReadProcessMemory(int hProcess,
Int64 lpBaseAddress, byte lpBuffer, int dwSize, ref int lpNumberOfBytesRead);
public static string search = "somestring";
static void Main(string args)
{
Process process = Process.GetProcessById(15728);
IntPtr processHandle = OpenProcess(PROCESS_WM_READ, false, process.Id);
int bytesRead = 0;
byte buffer = new byte[16];
ReadProcessMemory((int)processHandle, 0x20BC4ADE4C8, buffer, buffer.Length, ref bytesRead);
Console.WriteLine(Encoding.Unicode.GetString(buffer) +
" (" + bytesRead.ToString() + "bytes)");
if (Encoding.Unicode.GetString(buffer).Contains(somestring))
Console.WriteLine("Match");
else
Console.WriteLine("Didint Match");
Console.ReadLine();
}
c# memory readprocessmemory
I'm basically trying to figure out how to search for a value in a process without giving an exact offset. The process can be anything (notepad, iexplorer, msword, etc.). Just looking for search a value between the first and last memory address of a process instead of giving a specific offset, which is I had to find from another application like ollydbg.
Here's what I have
const int PROCESS_WM_READ = 0x0010;
[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
[DllImport("kernel32.dll")]
public static extern bool ReadProcessMemory(int hProcess,
Int64 lpBaseAddress, byte lpBuffer, int dwSize, ref int lpNumberOfBytesRead);
public static string search = "somestring";
static void Main(string args)
{
Process process = Process.GetProcessById(15728);
IntPtr processHandle = OpenProcess(PROCESS_WM_READ, false, process.Id);
int bytesRead = 0;
byte buffer = new byte[16];
ReadProcessMemory((int)processHandle, 0x20BC4ADE4C8, buffer, buffer.Length, ref bytesRead);
Console.WriteLine(Encoding.Unicode.GetString(buffer) +
" (" + bytesRead.ToString() + "bytes)");
if (Encoding.Unicode.GetString(buffer).Contains(somestring))
Console.WriteLine("Match");
else
Console.WriteLine("Didint Match");
Console.ReadLine();
}
c# memory readprocessmemory
c# memory readprocessmemory
edited Dec 31 '18 at 15:28
Kit
8,91323268
8,91323268
asked Dec 31 '18 at 14:05
lstngllstngl
41
41
2
Is there an actual question there?
– MickyD
Dec 31 '18 at 14:10
See pinvoke. IntPtr are 32 bit pointer. You have lpBaseAddress defined as a Int64 which will not work. You also have to move the byte in c# from managed memory to unmanaged memory before passing to dll : pinvoke.net/default.aspx/user32/ReadProcessMemory.html
– jdweng
Dec 31 '18 at 14:11
ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte lpBuffer, IntPtr nSize, out IntPtr lpNumberOfBytesRead)
– GSerg
Dec 31 '18 at 14:12
a resource for this kind of work pinvoke.net as @jdweng provide the specific case.
– kenny
Dec 31 '18 at 14:13
Code work without any problem. but i dont wanna give specific memory offset
– lstngl
Dec 31 '18 at 14:20
|
show 3 more comments
2
Is there an actual question there?
– MickyD
Dec 31 '18 at 14:10
See pinvoke. IntPtr are 32 bit pointer. You have lpBaseAddress defined as a Int64 which will not work. You also have to move the byte in c# from managed memory to unmanaged memory before passing to dll : pinvoke.net/default.aspx/user32/ReadProcessMemory.html
– jdweng
Dec 31 '18 at 14:11
ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte lpBuffer, IntPtr nSize, out IntPtr lpNumberOfBytesRead)
– GSerg
Dec 31 '18 at 14:12
a resource for this kind of work pinvoke.net as @jdweng provide the specific case.
– kenny
Dec 31 '18 at 14:13
Code work without any problem. but i dont wanna give specific memory offset
– lstngl
Dec 31 '18 at 14:20
2
2
Is there an actual question there?
– MickyD
Dec 31 '18 at 14:10
Is there an actual question there?
– MickyD
Dec 31 '18 at 14:10
See pinvoke. IntPtr are 32 bit pointer. You have lpBaseAddress defined as a Int64 which will not work. You also have to move the byte in c# from managed memory to unmanaged memory before passing to dll : pinvoke.net/default.aspx/user32/ReadProcessMemory.html
– jdweng
Dec 31 '18 at 14:11
See pinvoke. IntPtr are 32 bit pointer. You have lpBaseAddress defined as a Int64 which will not work. You also have to move the byte in c# from managed memory to unmanaged memory before passing to dll : pinvoke.net/default.aspx/user32/ReadProcessMemory.html
– jdweng
Dec 31 '18 at 14:11
ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte lpBuffer, IntPtr nSize, out IntPtr lpNumberOfBytesRead)
– GSerg
Dec 31 '18 at 14:12
ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte lpBuffer, IntPtr nSize, out IntPtr lpNumberOfBytesRead)
– GSerg
Dec 31 '18 at 14:12
a resource for this kind of work pinvoke.net as @jdweng provide the specific case.
– kenny
Dec 31 '18 at 14:13
a resource for this kind of work pinvoke.net as @jdweng provide the specific case.
– kenny
Dec 31 '18 at 14:13
Code work without any problem. but i dont wanna give specific memory offset
– lstngl
Dec 31 '18 at 14:20
Code work without any problem. but i dont wanna give specific memory offset
– lstngl
Dec 31 '18 at 14:20
|
show 3 more comments
1 Answer
1
active
oldest
votes
You cannot avoid passing an address into ReadProcessMemory
as it is required, and I don't believe there are any other APIs out there that allow you to read a process's memory.
So, what you have to do is pass in the base address. Rather than get the base address, you can calculate it yourself. This question can help.
Next you will need to find the size of the process's memory and pass that to the nSize
parameter. But... that might be a bad idea because
you have to determine what that value is (I'm not sure how; you could brute force it by doing a binary search across the largest possible value and finding the largest value that doesn't force
ReadProcessMemory
to returnfalse
or perhaps using a performance counter or some other mechanism).Deal with memory constraints of having to allocate a huge chunk of memory for your buffer.
So instead of reading all of the memory, make multiple calls to ReadProcessMemory
with smaller buffer sizes. The algorithm could be something like
while not an error
read into a buffer, scanning it for your string
if found
return true;
bump the offset
If the above loop does not find your string, you're still not done because the string could have spanned the boundary between two buffers. To deal with this, create another loop that scan each boundary from boundary offset - string size
to boundary offset + string size
, returning true if found.
Thank you for your time sir. Will check diffrent way to code it as u said. if i success to do it proper way i will write down here.
– lstngl
Jan 2 at 21:34
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988352%2fc-sharp-readprocessmemory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You cannot avoid passing an address into ReadProcessMemory
as it is required, and I don't believe there are any other APIs out there that allow you to read a process's memory.
So, what you have to do is pass in the base address. Rather than get the base address, you can calculate it yourself. This question can help.
Next you will need to find the size of the process's memory and pass that to the nSize
parameter. But... that might be a bad idea because
you have to determine what that value is (I'm not sure how; you could brute force it by doing a binary search across the largest possible value and finding the largest value that doesn't force
ReadProcessMemory
to returnfalse
or perhaps using a performance counter or some other mechanism).Deal with memory constraints of having to allocate a huge chunk of memory for your buffer.
So instead of reading all of the memory, make multiple calls to ReadProcessMemory
with smaller buffer sizes. The algorithm could be something like
while not an error
read into a buffer, scanning it for your string
if found
return true;
bump the offset
If the above loop does not find your string, you're still not done because the string could have spanned the boundary between two buffers. To deal with this, create another loop that scan each boundary from boundary offset - string size
to boundary offset + string size
, returning true if found.
Thank you for your time sir. Will check diffrent way to code it as u said. if i success to do it proper way i will write down here.
– lstngl
Jan 2 at 21:34
add a comment |
You cannot avoid passing an address into ReadProcessMemory
as it is required, and I don't believe there are any other APIs out there that allow you to read a process's memory.
So, what you have to do is pass in the base address. Rather than get the base address, you can calculate it yourself. This question can help.
Next you will need to find the size of the process's memory and pass that to the nSize
parameter. But... that might be a bad idea because
you have to determine what that value is (I'm not sure how; you could brute force it by doing a binary search across the largest possible value and finding the largest value that doesn't force
ReadProcessMemory
to returnfalse
or perhaps using a performance counter or some other mechanism).Deal with memory constraints of having to allocate a huge chunk of memory for your buffer.
So instead of reading all of the memory, make multiple calls to ReadProcessMemory
with smaller buffer sizes. The algorithm could be something like
while not an error
read into a buffer, scanning it for your string
if found
return true;
bump the offset
If the above loop does not find your string, you're still not done because the string could have spanned the boundary between two buffers. To deal with this, create another loop that scan each boundary from boundary offset - string size
to boundary offset + string size
, returning true if found.
Thank you for your time sir. Will check diffrent way to code it as u said. if i success to do it proper way i will write down here.
– lstngl
Jan 2 at 21:34
add a comment |
You cannot avoid passing an address into ReadProcessMemory
as it is required, and I don't believe there are any other APIs out there that allow you to read a process's memory.
So, what you have to do is pass in the base address. Rather than get the base address, you can calculate it yourself. This question can help.
Next you will need to find the size of the process's memory and pass that to the nSize
parameter. But... that might be a bad idea because
you have to determine what that value is (I'm not sure how; you could brute force it by doing a binary search across the largest possible value and finding the largest value that doesn't force
ReadProcessMemory
to returnfalse
or perhaps using a performance counter or some other mechanism).Deal with memory constraints of having to allocate a huge chunk of memory for your buffer.
So instead of reading all of the memory, make multiple calls to ReadProcessMemory
with smaller buffer sizes. The algorithm could be something like
while not an error
read into a buffer, scanning it for your string
if found
return true;
bump the offset
If the above loop does not find your string, you're still not done because the string could have spanned the boundary between two buffers. To deal with this, create another loop that scan each boundary from boundary offset - string size
to boundary offset + string size
, returning true if found.
You cannot avoid passing an address into ReadProcessMemory
as it is required, and I don't believe there are any other APIs out there that allow you to read a process's memory.
So, what you have to do is pass in the base address. Rather than get the base address, you can calculate it yourself. This question can help.
Next you will need to find the size of the process's memory and pass that to the nSize
parameter. But... that might be a bad idea because
you have to determine what that value is (I'm not sure how; you could brute force it by doing a binary search across the largest possible value and finding the largest value that doesn't force
ReadProcessMemory
to returnfalse
or perhaps using a performance counter or some other mechanism).Deal with memory constraints of having to allocate a huge chunk of memory for your buffer.
So instead of reading all of the memory, make multiple calls to ReadProcessMemory
with smaller buffer sizes. The algorithm could be something like
while not an error
read into a buffer, scanning it for your string
if found
return true;
bump the offset
If the above loop does not find your string, you're still not done because the string could have spanned the boundary between two buffers. To deal with this, create another loop that scan each boundary from boundary offset - string size
to boundary offset + string size
, returning true if found.
edited Jan 2 at 22:59
answered Jan 2 at 18:06
KitKit
8,91323268
8,91323268
Thank you for your time sir. Will check diffrent way to code it as u said. if i success to do it proper way i will write down here.
– lstngl
Jan 2 at 21:34
add a comment |
Thank you for your time sir. Will check diffrent way to code it as u said. if i success to do it proper way i will write down here.
– lstngl
Jan 2 at 21:34
Thank you for your time sir. Will check diffrent way to code it as u said. if i success to do it proper way i will write down here.
– lstngl
Jan 2 at 21:34
Thank you for your time sir. Will check diffrent way to code it as u said. if i success to do it proper way i will write down here.
– lstngl
Jan 2 at 21:34
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988352%2fc-sharp-readprocessmemory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Is there an actual question there?
– MickyD
Dec 31 '18 at 14:10
See pinvoke. IntPtr are 32 bit pointer. You have lpBaseAddress defined as a Int64 which will not work. You also have to move the byte in c# from managed memory to unmanaged memory before passing to dll : pinvoke.net/default.aspx/user32/ReadProcessMemory.html
– jdweng
Dec 31 '18 at 14:11
ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte lpBuffer, IntPtr nSize, out IntPtr lpNumberOfBytesRead)
– GSerg
Dec 31 '18 at 14:12
a resource for this kind of work pinvoke.net as @jdweng provide the specific case.
– kenny
Dec 31 '18 at 14:13
Code work without any problem. but i dont wanna give specific memory offset
– lstngl
Dec 31 '18 at 14:20