Advertisement
Guest User

DiabloHorn

a guest
Nov 8th, 2009
1,668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.21 KB | None | 0 0
  1. //DiabloHorn - steal stuff from vmdk files
  2. //Mainly used sample code from the VDDK itself.
  3. //http://communities.vmware.com/thread/223740 very helpfull link.
  4. //hosted disk = workstation stuff (reminder)
  5. #include <windows.h>
  6. #include <tchar.h>
  7. #include <process.h>
  8.  
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <sstream>
  12. #include <string>
  13. #include <vector>
  14. #include <stdexcept>
  15.  
  16. //most of the vmdk functions
  17. #include "vixDiskLib.h"
  18. //mounting related functions , this needs the driver, so the SDK has to be installed
  19. #include "vixMntapi.h"
  20.  
  21. #pragma comment(lib,"vixDiskLib")
  22. #pragma comment(lib,"vixMntapi")
  23.  
  24. using std::cout;
  25. using std::string;
  26. using std::endl;
  27. using std::vector;
  28.  
  29. #define VIXDISKLIB_VERSION_MAJOR 1
  30. #define VIXDISKLIB_VERSION_MINOR 0
  31.  
  32. void getFile(const char *,char *);
  33. void usage(char *);
  34.  
  35. static struct {
  36.     int command;
  37.     VixDiskLibAdapterType adapterType;
  38.     char *diskPath;
  39.     char *parentPath;
  40.     char *metaKey;
  41.     char *metaVal;
  42.     int filler;
  43.     unsigned mbSize;
  44.     VixDiskLibSectorType numSectors;
  45.     VixDiskLibSectorType startSector;
  46.     uint32 openFlags;
  47.     unsigned numThreads;
  48.     Bool success;
  49.     Bool isRemote;
  50.     char *host;
  51.     char *userName;
  52.     char *password;
  53.     int port;
  54.     char *srcPath;
  55.     VixDiskLibConnection connection;
  56.     std::string vmxSpec;
  57.     char *libdir;
  58.     char *ssMoRef;
  59. } appGlobals;
  60.  
  61. typedef struct {
  62.     VixVolumeHandle volumeHandle;
  63.     VixVolumeInfo* volInfo;
  64. } MountedVolume;
  65.  
  66. #define THROW_ERROR(vixError) \
  67.     throw VixDiskLibErrWrapper((vixError), __FILE__, __LINE__)
  68.  
  69. #define CHECK_AND_THROW(vixError)                                    \
  70.     do {                                                              \
  71.     if (VIX_FAILED((vixError))) {                                  \
  72.     throw VixDiskLibErrWrapper((vixError), __FILE__, __LINE__); \
  73.     }                                                              \
  74.     } while (0)
  75.  
  76. class VixDiskLibErrWrapper
  77. {
  78. public:
  79.     explicit VixDiskLibErrWrapper(VixError errCode, const char* file, int line)
  80.         :
  81.     _errCode(errCode),
  82.         _file(file),
  83.         _line(line)
  84.     {
  85.         char* msg = VixDiskLib_GetErrorText(errCode, NULL);
  86.         _desc = msg;
  87.         VixDiskLib_FreeErrorText(msg);
  88.     }
  89.  
  90.     VixDiskLibErrWrapper(const char* description, const char* file, int line)
  91.         :
  92.     _errCode(VIX_E_FAIL),
  93.         _desc(description),
  94.         _file(file),
  95.         _line(line)
  96.     {
  97.     }
  98.  
  99.     string Description() const { return _desc; }
  100.     VixError ErrorCode() const { return _errCode; }
  101.     string File() const { return _file; }
  102.     int Line() const { return _line; }
  103.  
  104. private:
  105.     VixError _errCode;
  106.     string _desc;
  107.     string _file;
  108.     int _line;
  109. };
  110.  
  111.  
  112.  
  113.  
  114. /*
  115.     All the stuff is done here - ugly needs fixing
  116.     No input validation or error checking on cmd line passed arguments
  117. */
  118. int main(int argc,char *argv[]){
  119.     VixDiskLibConnectParams vxConParams = {0};
  120.     VixDiskLibHandle diskHandle;
  121.     VixDiskLibHandle diskHandles[1];
  122.     VixDiskSetHandle diskSetHandle = NULL;
  123.     size_t numVolumes = 0;
  124.     size_t i = 0;
  125.     VixVolumeHandle *volumeHandles = NULL;
  126.     VixVolumeInfo *volInfo = NULL;
  127.     std::vector<MountedVolume> mountedVolumes;
  128.     appGlobals.openFlags = VIXDISKLIB_FLAG_OPEN_READ_ONLY;
  129.    
  130.     if(argc != 4){
  131.         usage(argv[0]);
  132.     }
  133.     appGlobals.diskPath = argv[1];
  134.  
  135.     try{
  136.         //always needed
  137.         VixError vixError = VixDiskLib_Init(VIXDISKLIB_VERSION_MAJOR,VIXDISKLIB_VERSION_MINOR,NULL,NULL,NULL,appGlobals.libdir);
  138.         CHECK_AND_THROW(vixError);
  139.         vixError = VixMntapi_Init(VIXDISKLIB_VERSION_MAJOR,VIXDISKLIB_VERSION_MINOR,NULL,NULL,NULL,NULL,NULL);
  140.         CHECK_AND_THROW(vixError);
  141.         vixError = VixDiskLib_Connect(&vxConParams,&appGlobals.connection);
  142.         CHECK_AND_THROW(vixError);
  143.  
  144.         //open the disks you want to mount.
  145.         vixError = VixDiskLib_Open(appGlobals.connection,appGlobals.diskPath,appGlobals.openFlags,&diskHandle);
  146.         CHECK_AND_THROW(vixError);
  147.         diskHandles[0] = diskHandle;
  148.         //open them all at ones
  149.         vixError = VixMntapi_OpenDiskSet(diskHandles,1,appGlobals.openFlags,&diskSetHandle);
  150.         CHECK_AND_THROW(vixError);
  151.         vixError = VixMntapi_GetVolumeHandles(diskSetHandle,&numVolumes,&volumeHandles);
  152.         CHECK_AND_THROW(vixError);
  153.         printf("Num Volumes %d\n", numVolumes);
  154.  
  155.         volInfo = NULL;
  156.         for (i = 0; i < numVolumes; ++i) {
  157.             MountedVolume newVolume = {0, 0};
  158.  
  159.             vixError = VixMntapi_MountVolume(volumeHandles[i], TRUE);
  160.             CHECK_AND_THROW(vixError);
  161.  
  162.             vixError = VixMntapi_GetVolumeInfo(volumeHandles[i], &newVolume.volInfo);
  163.             CHECK_AND_THROW(vixError);
  164.  
  165.             printf("\nMounted Volume %d, Type %d, isMounted %d, symLink %s, numGuestMountPoints %d (%s)\n\n",
  166.                 i, newVolume.volInfo->type, newVolume.volInfo->isMounted,
  167.                 newVolume.volInfo->symbolicLink == NULL ? "<null>" : newVolume.volInfo->symbolicLink,
  168.                 newVolume.volInfo->numGuestMountPoints,
  169.                 (newVolume.volInfo->numGuestMountPoints == 1) ? (newVolume.volInfo->inGuestMountPoints[0]) : "<null>" );
  170.  
  171.             string bootPath = newVolume.volInfo->symbolicLink;
  172.             bootPath += argv[3];
  173.             bootPath += argv[2];
  174.             cout << bootPath.c_str() << endl;
  175.             getFile(bootPath.c_str(),argv[2]);
  176.  
  177.             VixMntapi_FreeVolumeInfo(newVolume.volInfo);
  178.         }
  179.        
  180.         //cleanup stuff
  181.  
  182.         std::vector<MountedVolume>::const_iterator iter = mountedVolumes.begin();
  183.         for (; iter != mountedVolumes.end(); ++iter) {
  184.             VixMntapi_FreeVolumeInfo((*iter).volInfo);
  185.             VixMntapi_DismountVolume((*iter).volumeHandle, TRUE);
  186.         }
  187.         if (volumeHandles) {
  188.             VixMntapi_FreeVolumeHandles(volumeHandles);
  189.         }
  190.         if (appGlobals.connection != NULL) {
  191.             VixDiskLib_Disconnect(appGlobals.connection);
  192.         }  
  193.  
  194.         VixDiskLib_Exit();
  195.         free(vxConParams.vmxSpec);
  196.     }catch(const VixDiskLibErrWrapper& e) {
  197.         cout << "Error: [" << e.File() << ":" << e.Line() << "]  " <<
  198.             std::hex << e.ErrorCode() << " " << e.Description() << "\n";
  199.     }
  200.     return 0;
  201. }
  202.  
  203. /*
  204.     Overwrites existing files...so be carefull :)
  205.     Drops files in the current directory
  206. */
  207. void getFile(const char *oFile,char *nFile){
  208.     if(CopyFile(oFile,nFile,FALSE)){
  209.         cout << "copy succeeded\n";
  210.     }else{
  211.         cout << GetLastError() << endl;
  212.     }
  213. }
  214.  
  215. /*
  216.     Usual print usage stuff
  217. */
  218. void usage(char *appName){
  219.     cout << "DiabloHorn - alpha poc" << endl;
  220.     cout << "Steal stuff from vmdk files" << endl;
  221.     cout << appName << " <vmdk> <filetosteal> <pathoftostealfile>" << endl;
  222.     cout << "Ex: " << appName << " c:\\stuf.vmdk boot.ini \"\"";
  223.     exit(0);
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement