Advertisement
waliedassar

Reversed ObpCreateSymbolicLinkName

Oct 14th, 2021
1,352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.38 KB | None | 0 0
  1. //Waliedassar
  2. //https://twitter.com/waleedassar
  3.  
  4. //Called by ObCloseHandleTableEntry
  5. void ObpDeleteSymbolicLinkName
  6. (_OBJECT_SYMBOLIC_LINK *SymbolicLink)
  7. {
  8.     ulong var_DosDeviceDriveIndex =
  9.     SymbolicLink->DosDeviceDriveIndex;
  10.    
  11.     // DosDeviceDriveIndex field is 1 if the symbolic link is named "A:"
  12.     // and is zero if it is not within "A:" and "Z:"
  13.    
  14.    
  15.     if(var_DosDeviceDriveIndex)
  16.     {
  17.         _OBJECT_HEADER* pObjHdr =
  18.         SymbolicLink - 0x30;
  19.        
  20.         //Usually 0x20
  21.         ulong Offset =
  22.         ObpInfoMaskToOffset[pObjHdr->InfoMask & 3];
  23.        
  24.        
  25.         _OBJECT_HEADER_NAME_INFO* pNameInfo =
  26.         pObjHdr - Offset;
  27.        
  28.         //The directory, our symbolic link object lives in.
  29.         _OBJECT_DIRECTORY* pParentDir =
  30.         pNameInfo->Directory;
  31.        
  32.         //The device map associated with our process, in case this
  33.         //directory object is our process's device map directory.
  34.         _DEVICE_MAP* pDeviceMap =
  35.         pParentDir->DeviceMap;
  36.        
  37.         if(pDeviceMap)
  38.         {
  39.             _ESERVERSILO_GLOBALS* pServerSiloGlob =
  40.             PsGetCurrentServerSiloGlobals();
  41.            
  42.             ExGetCurrentThread()->??--;
  43.            
  44.             ExAcquirePushLockExclusiveEx
  45.             (&pServerSiloGlob->ObSiloState.DeviceMapLock,0);
  46.            
  47.            
  48.             pDeviceMap->DriveMap &=
  49.             ~(1 << (var_DosDeviceDriveIndex-1));
  50.            
  51.             pDeviceMap->DriveType[var_DosDeviceDriveIndex-1] = 0;
  52.            
  53.             if(pDeviceMap != pServerSiloGlob->ObSiloState.SystemDeviceMap)
  54.             {
  55.                 pServerSiloGlob->ObSiloState.SystemDosDeviceState.LocalDeviceCount[var_DosDeviceDriveIndex-1]--;
  56.             }
  57.             else
  58.             {
  59.                 pServerSiloGlob->ObSiloState.SystemDosDeviceState.GlobalDeviceMap &=
  60.                 (~(1 << (var_DosDeviceDriveIndex-1)));
  61.             }
  62.             //Common_0
  63.             ExReleasePushLockEx
  64.             (&pServerSiloGlob->ObSiloState.DeviceMapLock,0);
  65.            
  66.             KiLeaveGuardedRegionUnsafe(ExGetCurrentThread());
  67.         }
  68.         //Notice this line
  69.         SymbolicLink->DosDeviceDriveIndex = 0;
  70.     }
  71.     return;
  72. }
  73.  
  74. void ObpCreateSymbolicLinkName
  75. (_OBJECT_SYMBOLIC_LINK *SymbolicLink)
  76. {
  77.     //r10 0
  78.    
  79.     _OBP_LOOKUP_CONTEXT LookupContext;
  80.     LookupContext.EntryLink = 0;
  81.     LookupContext.HashValue = 0;
  82.     LookupContext.HashIndex = 0;
  83.     LookupContext.Pad = 0;
  84.    
  85.    
  86.    
  87.     _OBJECT_HEADER* ObjHdr =
  88.     SymbolicLink - 0x30;//r15
  89.    
  90.     //rsi SymbolicLink
  91.    
  92.     uchar var_InfoMask =
  93.     ObjHdr->InfoMask;
  94.    
  95.     ulong var_NumberOfSymLinksToProcess = 0x40;
  96.    
  97.     _OBJECT_HEADER_NAME_INFO* NameInfo = 0;
  98.    
  99.     if(var_InfoMask & 0x2)
  100.     {
  101.         NameInfo =
  102.         ObjHdr - nt!ObpInfoMaskToOffset[var_InfoMask & 0x3];
  103.     }
  104.     //Common_0
  105.    
  106.     if(!NameInfo) return;
  107.    
  108.     _OBJECT_DIRECTORY* NameInfoDirectory =
  109.     NameInfo->Directory;
  110.    
  111.     //Missing check for null pointer here, another bug found.
  112.     if(NameInfoDirectory->DeviceMap == 0) return;
  113.    
  114.     if(NameInfo->Name.Length != 4) return;
  115.    
  116.     //BRANCH_X
  117.     if(NameInfo->Name.Buffer[1] != L':') return;
  118.    
  119.    
  120.     ushort DriveLetter =
  121.     NLS_UPCASE(NameInfo->Name.Buffer[0]);
  122.    
  123.     ushort DriveLetterIndex =
  124.     DriveLetter - 0x41;
  125.    
  126.     if(DriveLetterIndex > 0x19) return;
  127.    
  128.     LookupContext.DirectoryLocked = 0;
  129.    
  130.     SymbolicLink->DosDeviceDriveIndex =
  131.     DriveLetter - 0x40;
  132.    
  133.    
  134.     bool b12 = false;
  135.     bool b13 = false;
  136.    
  137.     LookupContext.Directory = 0;
  138.     LookupContext.Object = 0;
  139.     LookupContext.LockStateSignature = 0xFFFF1234;
  140.    
  141.    
  142.     _ESILO* pSilo =
  143.     PsGetCurrentSilo();
  144.    
  145.     _OBJECT_DIRECTORY* pSiloRootDir =
  146.     OBP_GET_SILO_ROOT_DIRECTORY_FROM_SILO
  147.     (pSilo);
  148.    
  149.     ObfReferenceObject(pSiloRootDir);
  150.    
  151.     _UNICODE_STRING uniLinkTarget;
  152.     memcpy(&uniLinkTarget,
  153.         &SymbolicLink->LinkTarget,
  154.         0x10);
  155.        
  156.    
  157.     _ESERVERSILO_GLOBALS* pServerSiloGlob =
  158.     PsGetCurrentServerSiloGlobals();//rsi
  159.    
  160.     //LoopOuter
  161.     do
  162.     {
  163.         ulong var_LengthTarget;     //edi
  164.         wchar_t* w_TargetBuffer;    //rbx
  165.         _OBJECT_DIRECTORY* pRootDirectory = pSiloRootDir;//r14
  166.        
  167.         if( (uniLinkTarget.Buffer & 7) ||
  168.             ( pServerSiloGlob->ObSiloState.SystemDeviceMap->DosDevicesDirectory == 0)
  169.         )
  170.         {
  171.             var_LengthTarget =  uniLinkTarget.Length;
  172.             w_TargetBuffer = uniLinkTarget.Buffer;
  173.         }
  174.         else
  175.         {
  176.             var_LengthTarget =
  177.             uniLinkTarget.Length =
  178.             uniLinkTarget.Length;
  179.            
  180.             if(var_LengthTarget < 0x8)
  181.             {
  182.                 w_TargetBuffer = uniLinkTarget.Buffer;
  183.             }
  184.             else
  185.             {
  186.                 w_TargetBuffer =
  187.                 uniLinkTarget.Buffer =
  188.                 uniLinkTarget.Buffer;
  189.                
  190.                 //ObpDosDevicesShortNamePrefix => L"\??\"
  191.                 if( memcmp(w_TargetBuffer,ObpDosDevicesShortNamePrefix,8) == 0)
  192.                 {
  193.                     w_TargetBuffer += 4;
  194.                    
  195.                     var_LengthTarget -= 8;
  196.                    
  197.                     uniLinkTarget.Buffer =
  198.                     w_TargetBuffer;
  199.                    
  200.                     uniLinkTarget.Length =
  201.                     var_LengthTarget;
  202.                    
  203.                     pRootDirectory =    pServerSiloGlob->ObSiloState.SystemDeviceMap->DosDevicesDirectory;
  204.                 }
  205.             }
  206.         }
  207.        
  208.        
  209.         //LoopLookup
  210.         do
  211.         {
  212.             if( *w_TargetBuffer == L'\\')
  213.             {
  214.                 w_TargetBuffer++;
  215.                
  216.                 var_LengthTarget -= 2;
  217.                
  218.                 uniLinkTarget.Buffer =
  219.                 w_TargetBuffer;
  220.                    
  221.                 uniLinkTarget.Length =
  222.                 var_LengthTarget;
  223.             }
  224.             //Common_Inside_LoopLookup_0
  225.            
  226.             _UNICODE_STRING uniLinkTargetX;
  227.            
  228.             //XMM Op
  229.             memcpy
  230.             (&uniLinkTargetX,&uniLinkTarget,0x10);
  231.            
  232.             if(var_LengthTarget)
  233.             {
  234.                 //LoopLookupInner
  235.                 while( *w_TargetBuffer != L'\\')//bug here
  236.                 {
  237.                     w_TargetBuffer++;
  238.                     var_LengthTarget -= 2;
  239.                     uniLinkTarget.Length = var_LengthTarget;
  240.                 }
  241.                 //After_LoopLookupInner
  242.                 uniLinkTarget.Buffer = w_TargetBuffer;
  243.             }
  244.             //Common_Inside_LoopLookup_1
  245.             uniLinkTargetX.Length -=
  246.             var_LengthTarget;
  247.            
  248.             if(!uniLinkTargetX.Length)
  249.             {
  250.                 ObpReleaseLookupContext(&LookupContext);
  251.                 ObDereferenceObject(pSiloRootDir);
  252.                 return;
  253.             }
  254.             if(pRootDirectory == NameInfoDirectory)
  255.             {
  256.                 LookupContext.DirectoryLocked = 0x101;
  257.             }
  258.             b12 = false;
  259.             b13 = false;
  260.            
  261.             //Common_Inside_LoopLookup_2
  262.             _OBJECT_DIRECTORY* pTempObject =
  263.             ObpLookupDirectoryEntryEx
  264.             (pRootDirectory,
  265.             &uniLinkTargetX,
  266.             0,0,0,
  267.             &LookupContext);
  268.            
  269.             if(pRootDirectory == NameInfoDirectory)
  270.             {
  271.                 LookupContext.DirectoryLocked = b12;
  272.                 LookupContext.LockedExclusive = b13;
  273.             }
  274.             else
  275.             {
  276.                 b12 = LookupContext.DirectoryLocked;
  277.                 b13 = LookupContext.LockedExclusive;
  278.             }
  279.             //Common_Inside_LoopLookup_3
  280.             if(pTempObject == 0)
  281.             {
  282.                 goto Common_X;
  283.             }
  284.            
  285.             _OBJECT_TYPE* pTempObjectType =
  286.             MacroGetType(pTempObject);
  287.            
  288.            
  289.             if(pTempObjectType == ObpDirectoryObjectType)
  290.             {
  291.                 //Link Target is an Object Directory
  292.                 pRootDirectory = pTempObject;
  293.                 continue;
  294.             }
  295.             else
  296.             {
  297.                
  298.                 if(pTempObjectType == ObpSymbolicLinkObjectType)
  299.                 {
  300.                     //Link target is a symbolic link
  301.                     _OBJECT_SYMBOLIC_LINK* pTempObjectSym =
  302.                     (_OBJECT_SYMBOLIC_LINK*)pTempObject;
  303.                    
  304.                     if(pTempObjectSym->DosDeviceDriveIndex != 0)
  305.                     {
  306.                         goto Common_X;
  307.                     }
  308.                    
  309.                     if(var_NumberOfSymLinksToProcess == 0)
  310.                     {
  311.                         pTempObjectSym =
  312.                         pTempObject =
  313.                         0;
  314.                        
  315.                         goto Common_X;
  316.                     }
  317.                     else
  318.                     {
  319.                         //memcpy
  320.                         //(&uniLinkTarget,
  321.                         //&pTempObjectSym->LinkTarget,
  322.                         //0x10);
  323.                        
  324.                         //var_NumberOfSymLinksToProcess--;
  325.                         break;
  326.                     }
  327.                 }
  328.                 else
  329.                 {
  330.                     goto Common_X;
  331.                 }
  332.             }
  333.         }while(1);
  334.        
  335.         memcpy
  336.         (&uniLinkTarget,
  337.         &pTempObjectSym->LinkTarget,
  338.         0x10);
  339.        
  340.         var_NumberOfSymLinksToProcess--;
  341.     }while(1);
  342.    
  343. Common_X:
  344.     NameInfoDirectory =
  345.     NameInfo->Directory;
  346.    
  347.     //r15 1
  348.     //rbx 1
  349.    
  350.     _DEVICE_MAP* pDeviceMap =
  351.     NameInfoDirectory->DeviceMap;//rdi
  352.    
  353.     uchar DriveType = 1;
  354.     if(pTempObject)
  355.     {
  356.         if( MacroGetType(pTempObject) == IoDeviceObjectType)
  357.         {
  358.             _DEVICE_OBJECT* pTempDeviceObject =
  359.             (_DEVICE_OBJECT*)pTempObject);
  360.            
  361.             register ulong var_DeviceType =
  362.             pTempDeviceObject->DeviceType;
  363.            
  364.             if(var_DeviceType > 0x13)
  365.             {
  366.                 if(var_DeviceType == 0x14)
  367.                 {
  368.                     DriveType = 4;
  369.                 }
  370.                 else if(var_DeviceType == 0x24)
  371.                 {
  372.                     DriveType = 6;
  373.                 }
  374.                 else if(var_DeviceType == 0x28)
  375.                 {
  376.                     DriveType = 4;
  377.                 }
  378.                 else
  379.                 {
  380.                     DriveType = 0;
  381.                 }
  382.             }
  383.             else if(var_DeviceType >= 0x12)
  384.             {
  385.                 DriveType = 4;
  386.             }
  387.             else if(var_DeviceType < 2)
  388.             {
  389.                 DriveType = 0;
  390.             }
  391.             else if(var_DeviceType <= 3)
  392.             {
  393.                 DriveType = 5;
  394.             }
  395.             else if(var_DeviceType <= 6)
  396.             {
  397.                 DriveType = 0;
  398.             }
  399.             else if(var_DeviceType > 9)
  400.             {
  401.                 if(var_DeviceType == 0x10)
  402.                 {
  403.                     DriveType = 4;
  404.                 }
  405.                 else
  406.                 {
  407.                     DriveType = 0;
  408.                 }
  409.             }
  410.             else
  411.             {
  412.                 DriveType =
  413.                 ((~(pTempDeviceObject->Characteristics)) & 1) + 2;
  414.             }
  415.         }
  416.     }
  417. Common_Final:
  418.     ExGetCurrentThread()->???--;
  419.    
  420.     //Good, must have been in the function prologue instead
  421.     ExAcquirePushLockExclusiveEx
  422.     (pServerSiloGlob->ObSiloState.DeviceMapLock,0);
  423.    
  424.     ulong var_DosDeviceDriveIndex =
  425.     SymbolicLink->DosDeviceDriveIndex;
  426.    
  427.     //DosDeviceDriveIndex are 1-based.
  428.     //1 for A letter
  429.     //2 for B letter
  430.    
  431.     var_DosDeviceDriveIndex--;
  432.    
  433.     pDeviceMap->DriveType[var_DosDeviceDriveIndex] = DriveType;
  434.     pDeviceMap->DriveMap |= (1<<var_DosDeviceDriveIndex);
  435.    
  436.     if(pDeviceMap != pServerSiloGlob->ObSiloState.SystemDeviceMap)
  437.     {
  438.         pServerSiloGlob->
  439.         ObSiloState.
  440.         SystemDosDeviceState.
  441.         LocalDeviceCount[var_DosDeviceDriveIndex] |= 1;
  442.     }
  443.     else
  444.     {
  445.         pServerSiloGlob->
  446.         ObSiloState.
  447.         SystemDosDeviceState.
  448.         GlobalDeviceMap |=
  449.         (1<<var_DosDeviceDriveIndex);
  450.     }
  451.     ExReleasePushLockEx
  452.     (pServerSiloGlob->ObSiloState.DeviceMapLock,0);
  453.    
  454.     KiLeaveGuardedRegionUnsafe
  455.     (ExGetCurrentThread());
  456.    
  457.     ObpReleaseLookupContext
  458.     (&LookupContext);
  459.    
  460.     ObDereferenceObject(pSiloRootDir);
  461.    
  462.     return;
  463. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement