Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SMB] Massive Fixes, Features and Refactoring #1894

Open
wants to merge 24 commits into
base: master
Choose a base branch
from

Conversation

covertivy
Copy link
Contributor

@covertivy covertivy commented Feb 8, 2025

Hello!

I did some research regarding some annoying STATUS_SHARING_VIOLATION and STATUS_ACCESS_DENIED errors.
I know for a fact that some files that cannot be read with smbclient can be copied aside with a command / the file explorer.
This means that theoretically it should be possible to do so over SMB!

I opened up Wireshark and played around a bit - it seems this is caused by over-restrictive share access permissions on impacket's side.
I then dug deeper and saw some mismatching flag usage in the SMBv1 implementation of the protocol so I fixed those too.

To sum up, I added the ability to READ FILES WITH OPEN HANDLES WITH (ALMOST) NO RESTRICTION!!!
The only restriction is of course for some system files (eg. SAM, SECURITY, SYSTEM and basically all files that require a ShadowCopy to allow reading them).
This means that files with "weak handles" can be read remotely WITH ABSOLUTELY NO LIMITATION!

The list contains:

  • Event Logs - *.evt / *.evtx
  • Files open by Local Users
  • Browser Files - Chrome Logins & History
  • And much much more!

Glad to suffer for all y'alls pleasure!

…, write and delete.

I found it out when capturing network traffic and seeing I can manage to read files with open handles.
This fixes SHARE_ACCESS_DENIED errors when trying to read files with handles on them.
This is the logical thing to do when we open files for reading.
We do not want to block other processes from interacting with the file.
This may raise problems when reading files that are being written into / being deleted.
I still think this is the right move since we usually want to read the files no matter what.
This is related to my older commits on the subject.
@covertivy covertivy changed the title Add Ability to Read Files with Handles Add Ability to Read Files with Open Handles over SMB Feb 8, 2025
@covertivy covertivy changed the title Add Ability to Read Files with Open Handles over SMB Fix: Add Ability to Read Files with Open Handles over SMB Feb 10, 2025
@yuri2o1o
Copy link

Really important fix that makes SMB more intuitive! Please merge!

@laxa
Copy link
Contributor

laxa commented Feb 26, 2025

Hello, interesting PR, however, it does not seem to be working on my side, am I missing something?

$ smbclient.py 'User:Password'@192.168.122.111
# use C$
# cd users/user/appdata/local/microsoft/edge/user data/default/network
# get cookies
[-] SMB SessionError: code: 0xc0000043 - STATUS_SHARING_VIOLATION - A file cannot be opened because the share access flags are incompatible.

@covertivy
Copy link
Contributor Author

The problem is with your test case.
On chromium based browsers the Cookies file is locked with a "strong handle" which restricts read access to other processes, therefore in this case the file cannot be read by us.
However, the other sensitive chrome files such as Local State, Login Data, History and others can be read with no issue!

I am happy to help with any other questions you may have 😊

covertivy and others added 12 commits March 1, 2025 04:37
This is done to assist future development of SetInfo operations on files and directories.

```
Date Conversion Example - Year Component:
-----------------------------------------

    2009 - 1980 = 29

            | (convert to binary)
            V

    0001 1101

            | (position data correctly by shifting)
            V

                    0001 1101
    <<                     9
        ---------------------
    =  0 0011 1010 0000 0000

            | (trim to correct size with bitwise AND of correct mask)
            V

        0 0011 1010 0000 0000
    &   1111 1110 0000 0000
        ---------------------
    =   0011 1010 0000 0000
```
By doing so I also fixed a bad structure definition in the SMBSetFileBasicInfo structure.
Now we can modify file information remotely ;)
Also converted the SMB DATETIME methods to use my SMB_DATE and SMB_TIME implementations.
Also implemented setInfo method to use for setting file information.
fileInformationClasses other than the default one.
…s using SMB.

for now only implemented query.
@covertivy covertivy changed the title Fix: Add Ability to Read Files with Open Handles over SMB [SMB] Massive Fixes, Features and Refactoring Mar 2, 2025
@covertivy
Copy link
Contributor Author

covertivy commented Mar 2, 2025

Hi again, I am changing the title because this is slowly growing into something much bigger.

I will soon add a better description and list all of the current changes I have made to the SMB libraries but the work is far from over 😅

Changes done so far:

  • Add better constant definitions for shareAccessMode, createDisposition and desiredAccess in smb.py
  • Fix bad shareAccessMode given in all readFile/retr_file implementations.
  • Add set_file_info method in SMBv1.
  • Add SMB_DATE and SMB_TIME class definitions.
  • Replace poorly implemented getSMBDate and getSMBTime methods from smbserver.py to now use the newly implemented class definitions.
  • Moved & Renamed FileTime conversion methods from smbserver.py to smb.py for ease of use and access.
  • Fix broken SMBSetFileBasicInfo struct.
  • Add setInfo method to the SMBConnection wrapper class.
  • Add type hinting all over the SMBConnection class in order to allow easier programming for the user.
  • Add example script attrib.py to showcase newly implemented setInfo method.

I believe that is all for now, I will make sure to keep you posted!

Have a good one 😉

@anadrianmanrique anadrianmanrique added the in review This issue or pull request is being analyzed label Mar 6, 2025
@anadrianmanrique anadrianmanrique self-assigned this Mar 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in review This issue or pull request is being analyzed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants