Intercepting file operations (Madshi?)

Short question: How can I prevent the user from deleting a file (system wide)?

More elaborate question: This is for a program that's going to be used in kiosk mode (lan house, etc). There's an interface which limits the programs that the user can open, and he can't use alt+tab or ctrl+alt+del or the Windows key. However, the user can just open any available program (Internet Explorer, the Office suite) that has a file open/save menu and he will have access to the explorer shell (enough access to move stuff around and delete files). What I want to do is to intercept file operations (especially deletion) and deny the action. I tried doing this with hooks (DeleteFileA, DeleteFileW, SHFileOperation), but it didn't work. Works fine for programs, but the explorer shell can still delete the files.

The intended platform is Windows XP, but in the future I'd like to make it work with Win98 also, so setting user permissions doesn't seem like a very good idea.
TJ_The_DudeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DeerBearCommented:
Hi,

Use a copy hook. You can find an example in the DelphiX/Demos folder, where X stands for the version.

HTH,

Andrew

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TJ_The_DudeAuthor Commented:
Copy hooks only work for folders, not files.
DeerBearCommented:
Hi,

It's been a long time since I last used COM that way, but are you sure they're not suited?
I remember having written a copy hook which required a password to copy a certain
file... anyway, if you're really sure...

Cheers,

Andrew
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

GloomyFriarCommented:
The best way to solve the task is writing driver.
DeerBearCommented:
Gloomy,

To say the truth there's *no* real way to completely solve the issue.
Even with a driver, it would be enough to use a LiveCD edition of Linux and
anyone would be able to do basically anything on the computer, through
terminal and/or GUI.

Even if NTFS partitions can't be written to from Linux as to now( which I
beleive won't last long yet ), it's however possible( if I'm not wrong ) to
delete files from them.

Thus the issue has no definite solution.

But since the PC is already heavily patched in order to avoid some tasks,
I beleive a driver would really be overkill.

All this, obviously, imho.

HTH,

Andrew
GloomyFriarCommented:
;-)
So, TJ_The_Dude even don't try to do it! ;-)))
DeerBearCommented:
LOL

U nasty! ;-)

Andrew
GloomyFriarCommented:
>To say the truth there's *no* real way to completely solve the issue.
By the way. You can write the you own FS. ;-) It's possible;-)
DeerBearCommented:
LOL

Well, if you wanna know, I'm thinking of writing a new OS <g>.

But that's another matter... <g>.

Andrew
TJHaeserCommented:
So... no cigar? I'm off to trying to monitor API calls made by Explorer.exe when deleting a file...

Still waiting...
TJHaeserCommented:
I think my problem is that Explorer probably uses kernel mode functions like NtDeleteFile and ZwDeleteFile
DeerBearCommented:
LOL,

I told you: if you have D6/7, try looking at the Sample components.
I found an API some time ago that allowed this kind of monitoring.

HTH,

Andrew

P.S. Maybe it's the fact I am a bit tired, but I felt your message as
rude... again, I may be misunderstanding, but that's the way I felt it.

It's been given LOTS of informations on file deletions, along with
lots of useless( for you ) infos. :-)

HTH,

Andrew
TJ_The_DudeAuthor Commented:
I didn't mean to be rude in my previous comments, nor could I sense any offensiveness by re-reading my posts. Anyway, I've checked and double checked, and copy hook extensions can only monitor folders.
TJ_The_DudeAuthor Commented:
And the TShellChangeNotifier (or anything using the SHChangeNotify API) will only notify you of changes or deletions AFTER they've taken place - no use for me.
GloomyFriarCommented:
>copy hook extensions can only monitor folders.
Then write you own hook ;-)
TJ_The_DudeAuthor Commented:
What do you mean?

BTW, are 500 points not enough?
DeerBearCommented:
Hi,

Use API SHChangeNotify :-)

That api notifies you when a file's being deleted, among other things, but I'm unsure if you can
stop the deletion process.

HTH,

Andrew
GloomyFriarCommented:
>What do you mean?
I mean, that you need
1. System wide hook on all file API that allow to delete file.
OR
2. Driver

MadshiCommented:
Yep, either write a driver. Or do system wide API hooking. GloobyFriar is right there.

I'm not sure how the XP's explorer deletes files. I would start by hooking DeleteFileA/W and SHFileOperation. But it seems you've already tried that. The next try would be to hook NtCreateFile/NtOpenFile + NtSetFileInformation. This is the more low level way of deleting files. With NtSetFileInformation you can do all sorts of things, including renaming and deleting files.

Btw, are you talking about deleting files or about moving files to the recycle bin? What I've talked about was really deleting files. Moving files to the recycle bin is a different beast again...

P.S: Since you mentioned me in your question title, I guess you're already using madCodeHook, so I don't need to mention it, right?   :-)
TJ_The_DudeAuthor Commented:
Yup :-)

The thing is, if I block all those deletion functions, every other program I've tried is unable to delete files, EXCEPT Explorer.exe (= the shell).

I did some monitoring and it indeed uses the NtSetFileInformation function. I'll take a look at it. Oh, and if it turns out to be the one I need to hook, I'll give ya the points.
easysoft_studiosCommented:
Hi

I've was wondering how you do this in code? I'm in the excact same problem, and would like to know if your solution could help me too :)

Best regards
 Jonas
BijithCommented:
try this


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TFileLockType = (ltNone, ltQuery, ltReadOnly, ltReadWrite);
  TForm1 = class(TForm)
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  FFileLockHandle: THandle;
  FFileLockType: TFileLockType;

implementation

{$R *.DFM}


function LockFile(FileName: string; var AHandle: THandle): TFileLockType;
begin
  Result := ltReadWrite;
  AHandle := CreateFile(PChar(FileName), GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ, nil,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if AHandle = INVALID_HANDLE_VALUE then
  begin
    Result := ltReadOnly;
    AHandle := CreateFile(PChar(FileName), GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE, nil,
      OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
    if AHandle = INVALID_HANDLE_VALUE then
    begin
      Result := ltQuery;
      AHandle := CreateFile(PChar(FileName), 0, FILE_SHARE_READ or FILE_SHARE_WRITE or FILE_SHARE_DELETE, nil,
        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
      if AHandle = INVALID_HANDLE_VALUE then
        Result := ltNone;
    end;
  end;
end;

procedure UnLockFile;
begin
 if FFileLockHandle <> 0 then
    CloseHandle(FFileLockHandle);
  FFileLockHandle := 0;
  FFileLockType := ltNone;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
    FFileLockType := LockFile(OpenDialog1.FileName, FFileLockHandle);
end;

end.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.