Solved

Need native Windows NT API to either delete, rename or move a read-ony file on boot up

Posted on 2011-03-19
2
718 Views
Last Modified: 2013-12-04
I wrote a simple program using DDK that smss.exe fires off. It deletes the specified file and outputs the results to the blue screen before login. It works fine as long as the file is not read-only. From what I have read, this is normal behavior. I figured the work around is to change the attributes of the file before it is deleted, rename the file or move the file. The problem is that I cannot seem to find much info on this. I think I can use NtOpenFile, NtQueryInformationFile and NtSetInformationFile to rename or change attributes, but not sure how. Any help would be appreciated.

Here is the working code for deleting files that are not read-only:
//nt.c

#include "ntddk.h"
#include "nt.h"  

HANDLE Heap;
PWCHAR fileNameStringBuffer;
RTL_HEAP_DEFINITION  heapParams;
UNICODE_STRING filePathString;
OBJECT_ATTRIBUTES ObjectAttributes;
UNICODE_STRING del_SUCCEEDED,del_ERROR;
PWCHAR success_stringBuffer,error_stringbuffer; 

void kill(PWCHAR name){        
	NTSTATUS status;         
	filePathString.Buffer=fileNameStringBuffer;        
	filePathString.Length=wcslen( name ) * sizeof(WCHAR);         
	filePathString.MaximumLength=filePathString.Length + sizeof(WCHAR);        
	ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);        
	ObjectAttributes.RootDirectory=NULL;        
	ObjectAttributes.ObjectName=&filePathString;        
	ObjectAttributes.Attributes = OBJ_CASE_INSENSITIVE| OBJ_INHERIT;         
	ObjectAttributes.SecurityQualityOfService=NULL;        
	ObjectAttributes.SecurityDescriptor=NULL;        
	status=NtDeleteFile(&ObjectAttributes);        

	if ((NTSTATUS)(status) >= 0) {                  
		NtDisplayString(&filePathString);                  
		NtDisplayString(&del_SUCCEEDED);        
	}else{
		NtDisplayString(&del_ERROR);        
	}

}

void NtProcessStartup( PSTARTUP_ARGUMENT Argument){                   
	memset( &heapParams, 0, sizeof( RTL_HEAP_DEFINITION ));        
	heapParams.Length = sizeof( RTL_HEAP_DEFINITION );        
	Heap = RtlCreateHeap( 2, 0, 0x100000, 0x1000, 0, &heapParams );         
	fileNameStringBuffer= RtlAllocateHeap( Heap, 0, 256 );        
	success_stringBuffer=RtlAllocateHeap(Heap,0,20);        
	success_stringBuffer=L"....Deleted!\n";        
	del_SUCCEEDED.Buffer=success_stringBuffer;        
	del_SUCCEEDED.Length=wcslen( success_stringBuffer ) * sizeof(WCHAR);         
	del_SUCCEEDED.MaximumLength = del_SUCCEEDED.Length + sizeof(WCHAR);         
	error_stringbuffer=RtlAllocateHeap(Heap,0,20);        
	error_stringbuffer=L"....ERROR!\n";        
	del_ERROR.Buffer=error_stringbuffer;        
	del_ERROR.Length=wcslen(error_stringbuffer) * sizeof(WCHAR);        
	del_ERROR.MaximumLength = del_ERROR.Length + sizeof(WCHAR);         
	fileNameStringBuffer=L"\\??\\c:\\test\\test.exe";        
	kill(fileNameStringBuffer);          
	RtlFreeHeap( Heap, 0, fileNameStringBuffer );           
	NtTerminateProcess( NtCurrentProcess(), 0 );
}

Open in new window

//nt.h
typedef struct {
	ULONG Unknown[21];
	UNICODE_STRING CommandLine; 
	UNICODE_STRING ImageFile;
} ENVIRONMENT_INFORMATION, *PENVIRONMENT_INFORMATION;


//// This structure is passed as NtProcessStartup's parameter
typedef struct {        
	ULONG Unknown[3];        
	PENVIRONMENT_INFORMATION  Environment;
} STARTUP_ARGUMENT, *PSTARTUP_ARGUMENT;


//// Data structure for heap definition. This includes various
// sizing parameters and callback routines, which, if left NULL,
// result in default behavior
typedef struct {        
	ULONG Length;        
	ULONG Unknown[11];
} RTL_HEAP_DEFINITION, *PRTL_HEAP_DEFINITION;

//Native NT API functions

NTSTATUS NTAPI NtTerminateProcess(HANDLE ProcessHandle, LONG ExitStatus);

NTSTATUS NTAPI NtDisplayString(PUNICODE_STRING String);

HANDLE NTAPI RtlCreateHeap(ULONG Flags, PVOID BaseAddress, ULONG SizeToReserve, ULONG SizeToCommit, PVOID Unknown,PRTL_HEAP_DEFINITION Definition);

PVOID NTAPI RtlAllocateHeap(HANDLE Heap, ULONG Flags, ULONG Size);

BOOLEAN NTAPI RtlFreeHeap(HANDLE Heap, ULONG Flags, PVOID Address);

NTSYSAPI NTSTATUS NTAPI NtDeleteFile(IN POBJECT_ATTRIBUTES ObjectAttributes);

Open in new window

0
Comment
Question by:advcom
2 Comments
 
LVL 19

Accepted Solution

by:
mrwad99 earned 500 total points
Comment Utility
Have you looked at http://www.osronline.com/article.cfm?article=85, which talks about the rename operation in detail?
0
 

Author Closing Comment

by:advcom
Comment Utility
thats what I needed, thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
A short article about problems I had with the new location API and permissions in Marshmallow
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now