Guest User

ff

a guest
Jan 27th, 2011
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.42 KB | None | 0 0
  1. /*
  2. MemoryRegions.hpp
  3. This File is a part of Ethonmem, a memory hacking library for linux
  4. Copyright (C) < 2010, Florian Erler (Ethon) >
  5.               < [email protected] - http://ethon-blog.blogspot.com/ >
  6.  
  7. This library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11.  
  12. This library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. Lesser General Public License for more details.
  16.  
  17. You should have received a copy of the GNU Lesser General Public
  18. License along with this library; if not, write to the Free Software
  19. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  20. */
  21.  
  22. #ifndef __ETHON_MEMORYREGIONS_HPP__
  23. #define __ETHON_MEMORYREGIONS_HPP__
  24.  
  25. // POSIX:
  26. #include <unistd.h>
  27.  
  28. // C++ Standard Library:
  29. #include <string>
  30. #include <array>
  31. #include <deque>
  32. #include <cstdint>
  33. #include <algorithm>
  34.  
  35. // Boost Library:
  36. #include <boost/iterator/iterator_facade.hpp>
  37.  
  38. namespace ethon
  39. {
  40.   /**
  41.   * The currently mapped memory regions and their access permissions.
  42.   */
  43.   class MemoryRegion
  44.   {
  45.     friend class MemoryRegionIterator;
  46.  
  47.   private:
  48.  
  49.     enum Perms
  50.     {
  51.       Perm_Read,
  52.       Perm_Write,
  53.       Perm_Execute,
  54.       Perm_Shared
  55.     };
  56.  
  57.     uintptr_t   m_start;    // Begin of the address space in the process.
  58.     uintptr_t   m_end;      // End of the address space in the process.
  59.     std::array<char, 4> m_perms; // A set of permissions.
  60.     uintptr_t   m_offset;   // The offset into the file/whatever.
  61.     uint16_t    m_devMajor; // The device major.
  62.     uint16_t    m_devMinor; // The device minor.
  63.     uint32_t    m_inode;    // The inode on that device.
  64.     std::string m_path;     // The pathname of the mapped file.
  65.  
  66.     /**
  67.     * Special constructor for use by MemoryRegionIterator, processes an entry
  68.     * from a maps-file.
  69.     * @param entryLine The entry of a maps file to parse.
  70.     */
  71.     MemoryRegion(std::string& entryLine);
  72.  
  73.   public:
  74.  
  75.     /**
  76.     * Default constructor creating a completly empty object.
  77.     */
  78.     MemoryRegion();
  79.  
  80.     /**
  81.     * Gets the memory region's virtual start address.
  82.     * @return The memory regions virtual start address.
  83.     */
  84.     uintptr_t getStartAddress() const;
  85.  
  86.     /**
  87.     * Gets the memory region's virtual end address.
  88.     * @return The memory regions virtual end address.
  89.     */
  90.     uintptr_t getEndAddress() const;
  91.  
  92.     /**
  93.     * Gets the memory region's virtual size.
  94.     * @return The memory regions virtual size.
  95.     */
  96.     size_t getSize() const;
  97.  
  98.     /**
  99.     * Checks if reading from the memory regions' memoryspace is allowed.
  100.     * @return True if readable, false otherwise.
  101.     */
  102.     bool isReadable() const;
  103.  
  104.     /**
  105.     * Checks if writing into the memory regions' memoryspace is allowed.
  106.     * @return True if writeable, false otherwise.
  107.     */
  108.     bool isWriteable() const;
  109.  
  110.     /**
  111.     * Checks if the memoryspace of the memory region is executeable.
  112.     * @return True if executeable, false otherwise.
  113.     */
  114.     bool isExecuteable() const;
  115.  
  116.     /**
  117.     * Checks if the memory regions' memoryspace is shared.
  118.     * @return True if shared, false otherwise.
  119.     */
  120.     bool isShared() const;
  121.  
  122.     /**
  123.     * Checks if the memory regions' memoryspace is private.
  124.     * @return True if shared, false otherwise.
  125.     */
  126.     bool isPrivate() const;
  127.  
  128.     /**
  129.     * Returns the memory region's permissions in a format rwx s/p
  130.     * @return The memory region's permissions.
  131.     */
  132.     const std::array<char, 4>& getPermissions() const;
  133.  
  134.     /**
  135.     * Gets the offset into the mapped file.
  136.     * @return The offset into the mapped file.
  137.     */
  138.     uintptr_t getOffset() const;
  139.  
  140.     /**
  141.     * Gets the major number of the device containing the mapped file.
  142.     * @return The major number of the device containing the mapped file.
  143.     */
  144.     uint16_t getDeviceMajor() const;
  145.  
  146.     /**
  147.     * Gets the minor number of the device containing the mapped file.
  148.     * @return The minor number of the device containing the mapped file.
  149.     */
  150.     uint16_t getDeviceMinor() const;
  151.  
  152.     /**
  153.     * Gets the inode on the device containing the mapped file.
  154.     * @return The inode on the device containing the mapped file.
  155.     */
  156.     uint32_t getInode() const;
  157.  
  158.     /**
  159.     * Gets the path of the mapped file.
  160.     * @return The path of the mapped file.
  161.     */
  162.     const std::string& getPath() const;
  163.   };
  164.  
  165.   /**
  166.   * Iterates through all memory regions of a usermode process
  167.   */
  168.   class MemoryRegionIterator
  169.     : public boost::iterator_facade<  MemoryRegionIterator,
  170.                                       MemoryRegion,
  171.                                       boost::forward_traversal_tag >
  172.   {
  173.   public:
  174.  
  175.     /**
  176.     * Default constructor creating an invalid iterator.
  177.     */
  178.     MemoryRegionIterator();
  179.  
  180.     /**
  181.     * Constructor creating an iterator for iterating a process memory regions.
  182.     * @param process The PID of the process.
  183.     */
  184.     MemoryRegionIterator(::pid_t process);
  185.  
  186.     /**
  187.     * Checks if the iterator is (still) valid.
  188.     * @return True if valid, else otherwise.
  189.     */
  190.     bool isValid() const;
  191.  
  192.   private:
  193.  
  194.     friend class boost::iterator_core_access;
  195.  
  196.     /**
  197.     * Increments the iterator to point to the next entry.
  198.     */
  199.     void increment();
  200.  
  201.     /**
  202.     * Checks if both iterators have the same validity-state.
  203.     * @return True if both have the same validity-state, false otherwise.
  204.     */
  205.     bool equal(MemoryRegionIterator const& other) const;
  206.  
  207.     /**
  208.     * Returns a reference to the current entry.
  209.     * @return A reference to the current entry.
  210.     */
  211.     MemoryRegion& dereference() const;
  212.  
  213.     mutable MemoryRegion m_current;
  214.     std::deque<std::string> m_entries;
  215.   };
  216.  
  217.   /**
  218.   * Enumerates all memory regions of a process.
  219.   * @param process The PID of the process.
  220.   * @param f Functor to be called every entry with MemoryRegion as argument.
  221.   * @return The functor f.
  222.   */
  223.   template<typename functor_t>
  224.   functor_t enumMemoryRegions(::pid_t process, functor_t f)
  225.   {
  226.     return std::for_each(
  227.             MemoryRegionIterator(process), MemoryRegionIterator(), f);
  228.   }
  229. }
  230.  
  231. #endif //__ETHON_MEMORYREGIONS_HPP__
Advertisement
Add Comment
Please, Sign In to add comment