GInfo wrote: ↑28 Sep 2021 17:38
Hi Folks,
I'm trying a script to get the readonly status set by the Diskpart / attr disk set readonly command, but to the disk drive where the script is running, like a flash drive. (current disk)
the result (Readonly: Yes / No) will be used to alert the user that the disk is unprotected for writing.
another method of checking write permissions (NTFS) is already in use, including testing to store a file on disk.
If there is another way or command to acquire the readonly disc status everything is fine, as long as it is the same result acquired with diskpart/attr disk.
the final result of the script, including the other verification methods, will be:
Readonly: Yes/No
NTFS Permission to write: Yes/No
Owner: owner
This script will be used to monitor disk access in real time (every x seconds) as some malware may try to modify readonly disk, NTFS permissions or take over files. if this happens the user will be alerted that the disk has been compromised.
Hi again,
well, as I still haven't got an alternative, I tried a script using the Diskpart command. it works, but...
the problems involved in it are:
1 - this is not accurate in the automatic choice of disk. because it chooses the unit for its equivalent size.
2 - requires administrative rights
3 - requires writing to disk (%temp%)
Code: Select all
set idcd=%cd:~0,+1%
echo list volume>"%Temp%\psmds1.dat"
diskpart /s "%temp%\psmds1.dat" >"%Temp%\psmdd1.dat"
FOR /F "tokens=5 delims= " %%i in ('type "%Temp%\psmdd1.dat" ^| find "%idcd%"')do set disksize=%%i
echo list disk>"%Temp%\psmds1.dat"
diskpart /s "%temp%\psmds1.dat" >"%Temp%\psmdd1.dat"
FOR /F "tokens=2 delims= " %%i in ('type "%Temp%\psmdd1.dat" ^| find "%disksize%"')do set diskn=%%i
(
echo select disk %diskn%
echo attr disk
) >"%Temp%\psmds1.dat"
diskpart /s "%temp%\psmds1.dat" >"%Temp%\psmdd1.dat"
FOR /F "tokens=5 delims= " %%i in ('type "%Temp%\psmdd1.dat" ^| find "Current Read-only State:"')do set readonly=%%i
IF "%readonly%"=="No" echo THE DISC %diskn% - %idcd%: IS NOT PROTECTED!
DEL /Q "%Temp%\psmdd1.dat" >nul 2>&1
DEL /Q "%Temp%\psmds1.dat" >nul 2>&1
pause
Another alternative but that only works with removable drives and has the same problems as the code above, using the wmic command to identify the disk.
*there can only be one removable disk connected
Code: Select all
set idcd=%cd:~0,+2%
wmic logicaldisk get Description, DeviceID, VolumeName >"%Temp%\psmdd1.dat"
FOR /F "tokens=1,4 delims= " %%i in ('type "%Temp%\psmdd1.dat" ^| find "%idcd%"')do (
set disktype=%%i
set disklabel=%%j
)
wmic diskdrive get Index, MediaType >"%Temp%\psmdd1.dat"
FOR /F "tokens=1 delims= " %%i in ('type "%Temp%\psmdd1.dat" ^| find "%disktype%"')do set diskn=%%i
(
echo select disk %diskn%
echo attr disk
) >"%Temp%\psmds1.dat"
diskpart /s "%temp%\psmds1.dat" >"%Temp%\psmdd1.dat"
FOR /F "tokens=5 delims= " %%i in ('type "%Temp%\psmdd1.dat" ^| find "Current Read-only State:"')do set readonly=%%i
IF "%readonly%"=="No" echo THE DISC %diskn% - %idcd%(%disklabel%) IS NOT PROTECTED!
DEL /Q "%Temp%\psmdd1.dat" >nul 2>&1
DEL /Q "%Temp%\psmds1.dat" >nul 2>&1
pause
I'm still looking for an alternative to Diskpart...