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
729 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
ID: 35179402
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
ID: 35181478
thats what I needed, thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
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…

828 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