Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Intercepting file operations (Madshi?)

Posted on 2003-11-18
24
Medium Priority
?
698 Views
Last Modified: 2010-08-05
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.
0
Comment
Question by:TJ_The_Dude
  • 7
  • 5
  • 5
  • +4
22 Comments
 
LVL 5

Accepted Solution

by:
DeerBear earned 500 total points
ID: 9777148
Hi,

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

HTH,

Andrew
0
 

Author Comment

by:TJ_The_Dude
ID: 9778071
Copy hooks only work for folders, not files.
0
 
LVL 5

Expert Comment

by:DeerBear
ID: 9778184
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 6

Assisted Solution

by:GloomyFriar
GloomyFriar earned 500 total points
ID: 9778431
The best way to solve the task is writing driver.
0
 
LVL 5

Expert Comment

by:DeerBear
ID: 9778518
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
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9778589
;-)
So, TJ_The_Dude even don't try to do it! ;-)))
0
 
LVL 5

Expert Comment

by:DeerBear
ID: 9778619
LOL

U nasty! ;-)

Andrew
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9778634
>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;-)
0
 
LVL 5

Expert Comment

by:DeerBear
ID: 9778809
LOL

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

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

Andrew
0
 

Expert Comment

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

Still waiting...
0
 

Expert Comment

by:TJHaeser
ID: 9779556
I think my problem is that Explorer probably uses kernel mode functions like NtDeleteFile and ZwDeleteFile
0
 
LVL 5

Expert Comment

by:DeerBear
ID: 9779572
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
0
 

Author Comment

by:TJ_The_Dude
ID: 9780579
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.
0
 

Author Comment

by:TJ_The_Dude
ID: 9780650
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.
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9781227
>copy hook extensions can only monitor folders.
Then write you own hook ;-)
0
 

Author Comment

by:TJ_The_Dude
ID: 9781259
What do you mean?

BTW, are 500 points not enough?
0
 
LVL 5

Expert Comment

by:DeerBear
ID: 9785160
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
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9787994
>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

0
 
LVL 20

Assisted Solution

by:Madshi
Madshi earned 500 total points
ID: 9797720
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?   :-)
0
 

Author Comment

by:TJ_The_Dude
ID: 9798429
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.
0
 

Expert Comment

by:easysoft_studios
ID: 10426424
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
0
 
LVL 3

Assisted Solution

by:Bijith
Bijith earned 500 total points
ID: 10438330
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.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

824 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