Advertisement
Combreal

isEncryptedUsbOsx.cpp

Jun 11th, 2019
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string>
  5. #include <vector>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <sys/wait.h>
  9. #include <errno.h>
  10. #include <dirent.h>
  11. #include <sstream>
  12. #define PIPE_READ 0
  13. #define PIPE_WRITE 1
  14.  
  15. using namespace std;
  16.  
  17. std::string createChild(const char* bashCmd)
  18. {
  19.   int aStdinPipe[2];
  20.   int aStdoutPipe[2];
  21.   int nChild;
  22.   int nResult;
  23.   char buffer[4096];
  24.   char ch;
  25.   std::string commandResult;
  26.   if (pipe(aStdinPipe) < 0)
  27.     {
  28.       exit(1);
  29.     }
  30.   if (pipe(aStdoutPipe) < 0)
  31.     {
  32.       close(aStdinPipe[PIPE_READ]);
  33.       close(aStdinPipe[PIPE_WRITE]);
  34.       exit(1);
  35.     }
  36.   nChild = fork();
  37.   if (0 == nChild)
  38.     {
  39.       if (dup2(aStdinPipe[PIPE_READ], STDIN_FILENO) == -1)//redirect stdin
  40.     {
  41.       exit(1);
  42.     }
  43.       if (dup2(aStdoutPipe[PIPE_WRITE], STDOUT_FILENO) == -1)
  44.     {
  45.       exit(1);
  46.     }
  47.       close(aStdinPipe[PIPE_READ]);
  48.       close(aStdinPipe[PIPE_WRITE]);
  49.       close(aStdoutPipe[PIPE_READ]);
  50.       close(aStdoutPipe[PIPE_WRITE]);
  51.       nResult = execl("/bin/bash", "bash", "-c", bashCmd, (char*)0);
  52.       exit(nResult);
  53.     }
  54.   else if (nChild > 0)
  55.     {
  56.       close(aStdinPipe[PIPE_READ]);
  57.       close(aStdoutPipe[PIPE_WRITE]);
  58.       memset(buffer, 0, sizeof(buffer));
  59.       while (read(aStdoutPipe[PIPE_READ], &ch, 1) > 0)
  60.     {
  61.       if (ch != 0)
  62.         commandResult.push_back(ch);
  63.       else
  64.         {
  65.           commandResult.append(" ");
  66.         }
  67.     }
  68.       wait(NULL);//wait for child proc
  69.       close(aStdinPipe[PIPE_WRITE]);
  70.       close(aStdoutPipe[PIPE_READ]);
  71.     }
  72.   else
  73.     {
  74.       close(aStdinPipe[PIPE_READ]);
  75.       close(aStdinPipe[PIPE_WRITE]);
  76.       close(aStdoutPipe[PIPE_READ]);
  77.       close(aStdoutPipe[PIPE_WRITE]);
  78.     }
  79.   return commandResult;
  80. }
  81.  
  82.  
  83. std::string exeQte(std::string command)
  84. {
  85.   std::string commandResult, reCmd, reCommand;
  86.   char ccommand[4096];
  87.   reCommand = command;
  88.   strcpy(ccommand, reCommand.c_str());
  89.   commandResult = createChild(ccommand);
  90.   return commandResult;
  91. }
  92.  
  93.  
  94. int main(void)
  95. {
  96.   std::string diskName, commandResult;
  97.   std::stringstream scommand;
  98.   std::string path = "/Volumes/encryb/fileb";
  99.   //std::string path = "/Volumes/notencry";
  100.   std::string delimiter = "/";
  101.   size_t pos = 0;
  102.   std::string token;
  103.   int counter = 0;
  104.   while ((pos = path.find(delimiter)) != std::string::npos)
  105.     {
  106.       token = path.substr(0, pos);
  107.       if (counter == 2)
  108.     {
  109.       diskName = token;
  110.     }
  111.       path.erase(0, pos + delimiter.length());
  112.       counter++;
  113.     }  
  114.   scommand.str("");
  115.   scommand << "diskutil cs list | grep -e \"Volume Name:\" | awk '{print$3}'";
  116.   commandResult = exeQte(scommand.str());
  117.   commandResult.pop_back();
  118.   if (diskName.compare(commandResult) == 0)
  119.     {
  120.       scommand.str("");
  121.       scommand << "diskutil cs list | grep -e \"Conversion\" | awk '{print$3}'";
  122.       commandResult = exeQte(scommand.str());
  123.       if(commandResult.find("Complete") != std::string::npos)
  124.     {
  125.       cout<< "Disk is encrypted" <<endl;
  126.     }    
  127.     }
  128.   else
  129.     {
  130.        cout<< "Disk is not encrypted" <<endl;
  131.     }
  132.   return 0;
  133. }
  134. //g++ -g -w -o testEncrypt main.cpp && ./testEncrypt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement