Sponsored Content
Top Forums Programming Linux read and write external memory. Post 302506626 by lilezek on Monday 21st of March 2011 05:55:46 PM
Old 03-21-2011
I'm getting in trouble trying to do this. Firstly, a c++ problem:

This is on memory.cpp (it got compiled):
Code:
template<class T>
bool peekValue(T * value, char * offset, pid_t pid)
{
    // Get /proc/$pid/mem
    char mem_file_name[255];
    sprintf(mem_file_name, "/proc/%d/mem", pid);
    // Opening /proc/$pid/mem
    FILE * mem_fd = fopen(mem_file_name, "r");
    // Tracing the app.
    if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) == -1)
        return false;
    // Force it to wait
    if (waitpid(pid, NULL, 0) == -1)
    {
        ptrace(PTRACE_DETACH, pid, NULL, NULL);
        return false;
    }
    // Move pointer to offset.
    fseek(mem_fd, (long int)offset, SEEK_SET);
    // Read from memory.
    fread(value, 1, sizeof(T), mem_fd);
    // End petrace.
    ptrace(PTRACE_DETACH, pid, NULL, NULL);
    return true;
}

and in main.cpp:

Code:
many includes after:

int main(int argc, char ** argv)
{
    programinfo pnfo;
    int m;
    long int offset = 0;

    ...
    cout << "Enter the PID of the program you want to hack" << endl;
    cin >> pnfo.pid;
    cout << "Enter the pointer to int you want to read" << endl;
    cin >> offset;
    pnfo.mem_blocks = getMemoryMemblocks(pnfo.pid);
    peekValue<int>(&m, (char*)offset, pnfo.pid);
    cout << m << endl;
    return 0;
}

Got error (from linker):
Code:
/home/***/proyectos/xeat_engine/main.cpp|22|undefined reference to `bool peekValue<int>(int*, char*, int)'|

So I changed it to:
Code:
/*template<class T>*/
bool peekValue(int * value, char * offset, pid_t pid)
{
    // Get /proc/$pid/mem
    char mem_file_name[255];
    sprintf(mem_file_name, "/proc/%d/mem", pid);
    // Opening /proc/$pid/mem
    FILE * mem_fd = fopen(mem_file_name, "r");
    // Tracing the app. This fails:
    if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) == -1)
        return false;
    // Force it to wait
    if (waitpid(pid, NULL, 0) == -1)
    {
        ptrace(PTRACE_DETACH, pid, NULL, NULL);
        return false;
    }
    // Move pointer to offset.
    fseek(mem_fd, (long int)offset, SEEK_SET);
    // Read from memory.
    fread(value, 1, sizeof(int), mem_fd);
    // End petrace.
    ptrace(PTRACE_DETACH, pid, NULL, NULL);
    return true;
}

And:

Code:
many includes after:

int main(int argc, char ** argv)
{
    programinfo pnfo;
    int m;
    long int offset = 0;

    ...
    cout << "Enter the PID of the program you want to hack" << endl;
    cin >> pnfo.pid;
    cout << "Enter the pointer to int you want to read" << endl;
    cin >> offset;
    pnfo.mem_blocks = getMemoryMemblocks(pnfo.pid);
    peekValue/*<int>*/(&m, (char*)offset, pnfo.pid);
    cout << m << endl;
    return 0;
}

It "works". Any idea about this?

The second question is that it "works". It means the function got executed but it doesn't do its job. It fails on ptrace attachment. I'm trying to ptrace pid 6000, which is the pid of neverball (a videogame of a ball), but it returns -1.

What are the conditions to do a ptrace? Any idea?

Thank you for read.

---------- Post updated at 04:55 PM ---------- Previous update was at 11:09 AM ----------

I've solved the above problem (super user privileges).

Now I'm wonder a thing. Is to write in /proc/pid/mem possible?

Last edited by lilezek; 03-21-2011 at 01:35 PM..
 

10 More Discussions You Might Find Interesting

1. Programming

shared memory read/write using threads

I am looking for C program source code. Could you please help me in finding the source code required mentioned below. program to create multiple threads (one master thread and rest worker threads) and using the threads write into and read from shared memory Restrictions: Only one thread... (2 Replies)
Discussion started by: kumars
2 Replies

2. UNIX for Advanced & Expert Users

How to Mount a Remote NTFS Share for Read/Write under Linux

I've done this in the past, but I didn't save the syntax. I'm still kicking myself about that... I am trying to mount \\server_name\share_name for read/write under CentOS 5.2 (a "generic" version of RedHat). As I recall, there was a fairly simple (maybe a oneline) command that would allow NTFS... (2 Replies)
Discussion started by: shew01
2 Replies

3. HP-UX

Can I copy a Unix file to a external memory stick?

I'm doing server maintainence to a HP UX server and I have 2 files that someone need for an unknown reason. I was wondering If I can put this files on a memory stick, or is there someother way I have to copy it for them? (3 Replies)
Discussion started by: MarcKim
3 Replies

4. Shell Programming and Scripting

Find all files with group read OR group write OR user write permission

I need to find all the files that have group Read or Write permission or files that have user write permission. This is what I have so far: find . -exec ls -l {} \; | awk '/-...rw..w./ {print $1 " " $3 " " $4 " " $9}' It shows me all files where group read = true, group write = true... (5 Replies)
Discussion started by: shunter63
5 Replies

5. Programming

Urgent!!!! - Write/read to/from shared memory

I created a shared memory and attached it. I also created the appropriate semaphores to sync. my read/write operations. However I do not know how am I supposed to do so. Anyone has got any ideas? I want to write int the form of 1234:23:444:... where each number between : means something to the... (0 Replies)
Discussion started by: tyron
0 Replies

6. UNIX for Dummies Questions & Answers

Write/Read in Shared memory

I have created and attached a shared memory segment. However I do not know how am I supposed to write and read data from it. I want to save various different data on this segment such as process IDs and other. Do you know how am I supposed to write these and be able to read such data in the correct... (1 Reply)
Discussion started by: tyron
1 Replies

7. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

8. Shell Programming and Scripting

How to Read different values from external file?

Hi , I am new to this scripting , I am facing an issue like how to read different values from external file by using different variables, In script I supposed to declare var 1 var 2 var 3 I know how to call this variables from external file (I am using awk command by giving same... (3 Replies)
Discussion started by: chandini
3 Replies

9. Shell Programming and Scripting

Redirect output to memory instead of an external file

I use the following as an example. I find myself always doing this and I believe my scripts would run much faster if I put the sed results into a place in memory rather than writing to files. Again, its not about sed, its about redirecting to a place in memory rather than an external file. ... (5 Replies)
Discussion started by: popeye
5 Replies

10. Shell Programming and Scripting

BASH script to read external file to perform text replacements?

Hi all, I have a moderate size (300 lines) BASH Shell script that performs various tasks on different source reports (CSV files). One of the tasks that it performs, is to use SED to replace 'non-conforming' titles with conformant ones. For example "How to format a RAW Report" needs to become... (3 Replies)
Discussion started by: richardsantink
3 Replies
All times are GMT -4. The time now is 06:28 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy