how to prevent external xml file modification?

2019-06-14 06:10发布

问题:

I'm coding a little library which will handle xml files to store some data, and I need this data to be handled only by the methods I provide in my library.

I know that xml is readable for both human and machine, and that if somebody really wants to modify the xml file he'll probably do it, so... do any of you have an idea that could work?

回答1:

You can store more information in it, such as a hash of the content (before the hash was inserted of course).

When you will reload this file, you can check the hash. If it doesn't match with the current hash of your file, well it has been modified.



回答2:

Well, there is no definitive way to block access to that file. But you can use several measures to make it hard on manual overriding of the file.

First thing you can do is lock the file (need to ensure OS compatibility) for as long as your application is running. Anyone can circumvent an OS file lock, but it is not trivial for an average user.

Second, you can consider encrypting the file on application termination. Restoring the key can be done from application code inspection, but again - a non-trivial effort.



回答3:

As you said above, you have already implented a method that detects file changes, and you want a way how to prevent these modifications.

Usally, that's not possible. I'll explain at the end.

You have a few choices what to do:

  • If you want to prevent modifications while the program is running, you can lock the file. This will prevent applications from accessing it, but when your program exits, the lock will be released. (Example)
  • If you want to prevent access while the program is not running, you'll have to change file system permissions to forbid the user to edit the file. This is way more difficult as it is filesystem-related, and some filesystems like FAT haven't got file permissions at all.
  • You could write a "daemon" script that watches for file changes and revert them.

But all these possibilities have one problem - a program usally has the same permissions as the user, so everything the program does can be undone by the user. If your program has access, the user has too.

If you lock a file, the user could use a tool like Unlocker to release the lock, and edit it anyway. If your program sets file permissions, the user can simply change them back. On some systems, it might be possible to prevent this, but then your program looses access too. Bad. If you write a daemon, the user can kill it.

The only possibility is to have the program running with more rights than the user, and store the data on a place where the user has no access too. As example, on Windows, you can run it as a service. This requries the user to not have Administrator rights (or root, on Unix systems).

If the user is admin or root, you've lost, as he has full access to the system and you can't hide. (on Windows, there is one more level, the SYSTEM user, but an admin user can easily get these rights too).



回答4:

Append a hash of the file concatenated with a secret key to the end of the file. Like an XML comment

<!-- 0123456789abcdefabcdef0123456789 -->

Upon opening the file you hash it again with the appended secret key and verify it.

Some psuedo code to clarify.

# Read
secret  = "Secret key"
file    = get_file_contents("file.xml")
content = strip_trailing_comment(file)
hash    = get_content_hash(file)
if sha1(content + secret) == hash:
    # File is valid

# Write
secret            = "Secret key"
content           = content_to_xml()
hash              = sha1(content + secret)
content_with_hash = append_comment(hash)
write_to_file("file.xml", content_with_hash)

Hope that clears up potential misunderstandings. This way the code is still human readable, if you want that, and hard to tamper with.



回答5:

As I understand from discussions and your question, you want to store the data as xml, and difficult for user to open/modify it.

In that case you will have to do some additional work:

  • Create the xml file with hash information as suggested by Colin HEBERT
  • Zip the file with password protection, the password to which only your app will know

There is a question on stackoverflow on how to password protect your zip file

In this approach, mind you, the xml file does not even become readable.

If you want your files to be readable, then you could probably use a seperate user id for your application (unix user id or windows userid) as owner of the files. and only allow that user to modify the files, but still this won't be a 100% solution.