Encrypt resources in Cocoa app? - objective-c

I have a shell script stored in the resources folder of my Cocoa app. If used improperly it could be dangerous (even though I have taken precautions to reduce exploits, such as using the absolute path to commands) so is there any way to encrypt the script in binary format, then decrypt it when it needs to be used?
Thanks

It seems as if your concern is about people getting write access to the script and modifying it to run arbitrary code. You could keep a checksum for the script in the binary and compare that with the checksum of the script before you run it. Now, how do you stop people from editing the binary too? Code signing. In fact, if you keep the shell script in the app bundle then editing the script will break the signature of the bundle anyway.

This does not make a lot of sense. If an attacker has access to edit this script file, then they likely have access to edit any number of files, your application is less likely to be a security risk than any number of other things would be attacker could do.

No. If the user will decrypt it to use it, then she can see (and intercept) the clear text at some point. If you think you have "shell-like" things to do, do them in C/ObjC... This can be your friend.

What you're asking for is essentially DRM. A different purpose (“security” instead of thwarting copyright infringement), but the same approach, with the same problems.
In order for the user to be able to normally use the (music|video|script), they must be able to decrypt it. You would do this for them under only the right conditions in your (player|app), but that doesn't matter: no matter how well you hide it, you still have to provide the user with all the technology and keys necessary to decrypt the (music|video|script), so that your (player|app) can do that.
And then, since the user has all the technology and keys necessary to decrypt it, an attacker can and eventually will uncover them all and decrypt the (music|video|script) on their own.
I second Massa's suggestion of switching away from a shell script. This doesn't completely eliminate risk: If an attacker can gain access to write to your shell script, they can gain access to write to a Mach-O executable just as easily. But editing a Mach-O executable is not nearly as easy, so you are at least raising the bar that way.

Related

Excel, create an automatic save/change log, how to?

I teach at a university and one assignment i have developed tests some quite specific modelling skill sets in Excel. Because of this, and because all assignments need to be fairly weighted, they end up having largely the same content (in terms of the final code) with different variables so that they are not exact copies - so it is fairly easy for students to take another persons completed file and input their own variables, change a few cosmetic items (drag data locations, colours, borders etc) and hand the work in as their own. i am confident that this is occurring and most of the time i can identify the suspects easily but proving the collusion (not so much detecting it), this is difficult.
I am trying to work out how i can include somewhere on the worksheet (which i will provide as a template) a function that logs the user ID and time each incidence that the save button is pressed (or at a set interval)? This way i can have this info logged in the background (hopefully where nobody can find or goes looking for it) and it will be quite easy for me to see if at any point along the way the student working on the sheet changed - indicating collusion. Of course there are other ways around this, and i am not looking for a fail safe system, just an easy way to catch lazy cheats.
I can find functions that save the last user to a cell, but none that can provide an ongoing log. Ideally, i would like it to be as covert as possible, i.e., avoiding obvious macros would be handy but i can understand this might be a problem. Does anyone have any tricks up their sleeve to help me in my quest to wipe out student copying?
Type sign in Excel's help. Look at digital signatures. There may be some way of using them?
From Help
After you have installed your digital certificate, you can sign files and macro projects.
When you digitally sign a file, you certify that the information in the file is valid and that it has not been modified since the file was signed. As long as a file is unchanged, reviewers can attach signatures to it. You might use a digital signature with important files. When you digitally sign a macro project, your digital signature says that you guarantee that the project is safe. Just as signed files remain signed until the file is modified, signed macro projects remain signed until the macro code is altered.
Or perhaps turn on track changes and make an initial change yourself.

Secure data in VB.NET?

My VB.NET application has a very important boolean variable. I need to be able to save it "somewhere" and read it back in the future (even if the application is closed).
Currently, I store the variable as a .bin file somewhere. But I fear that the user can simply grab the file and do some magic to edit the value.
I need to make this value completely unavailable from the user's eyes. Or at least, make it impossible to edit. What is the best way to hide such value?
I managed to store the variable in my online MySQL database. But honestly, that doesn't work very well for my purposes. I need to store it locally...
Any ideas?
Theoretically, you can never hide this variable at all. However, you can make it harder to find or read. To know how to hide the variable, you must first know some common ways of retrieving it.
Typical problems and solutions
How others could theoretically retrieve your variable value
Decompiling your program with a single click using .NET Reflector or any other .NET Decompilation program.
Analyze the memory of the PC while the program is running, and retrieve the value from there.
If your value was saved in a file, the user could easily find it by analyzing harddrive IO activity through a hook or a dump comparison.
If your value was saved in the registry, a simple registry hook or registry dump comparison tool could figure out where the value is stored.
If your value is encrypted, method #1 (decompiling the program) could be used to figuring out how a decryption could be done.
Solving the possible issues above
A general obfuscation program can be used to make it harder to decompile applications. For this, I suggest SmartAssembly. Other than that, there's a tool called Spoon Studio (previously called PostBuild) which will recompile your application into assembly code (and also make it run without the .NET Framework installed).
The SecureString class could be used to make it harder to find and decrypt the value while it's in the memory. This class also cleans itself up after usage, but is generally slightly slower to use than a normal string.
Storing things in a file is not nescessarily a bad idea (even if people can sniff file activity), since you can always store the variable in a non-pretty way. For instance, you could have a file called IsFullScreen.bin, that contained the value of your boolean variable (1 or 0, or true or false), even though it has nothing to do with full-screen rendering. This would make it a bit confusing, but also not very pretty programming-wise.
For the registry, everything in solution #3 still applies.
Encryption is not a bad idea either, and it is hard to decrypt some encryption types (for instance public/private key encryption if you have a server), or hashing (such as MD5 or SHA1).
In your scenario, could you store the value on a server instead?
So to summarize ...
You can't protect your application entirely. But you can use some of the solutions above (or combine them) for a better protection making it harder.
Of course, premature security is bad. If that boolean isn't VERY important, then some simple encryption would be fine too in my opinion.
There's more ...
Edit 1
I just noticed that you've commented on your own answer saying that the file should not be "valid" after copying it to another computer and reading it from there.
If that's the case, you could use some key-based encryption such as XOR encryption, and then use the MAC-address of the PC or the motherboard serial number as key for that encryption.
Being on the computer that the file was created on would then be needed to read the file as well. If you're interested in this, add a comment and I'll give you a code example.
I would store it in the registry in a non-obvious way. For example, make the name of the registry entry appear to be important and store a random numeric value then ensure it ends in 0 or 1 or contains a 0 or 1 embedded at a fixed position in the number.
You can then read the value from the registry and extract the 0 or 1 from the appropriate location.
Encrypt the value before storing it. Use .NET Cryptography Model. For more on how to implement something using that model see

iOS Download Security

I would like to update my app to support the downloading of PDF documents, but am worried primarily about 2 things:
1: that the user will attempt to use my app to download non-PDF, or virus-riddled items.
2: that said virus riddled-items could infect the device.
I may just be paranoid, as I assume the sandbox would prevent most malware from escaping and screwing with the OS, Kernel or the Lower Level Processes but you can never be too sure. So my question is twofold: How can I (at least mostly) guarantee the download of only PDF's and do it as securely as possible.
(P.S. the download will be done through any generic website in a webView, not a server seeing as I shudder at the thought of web development).
Based on ozdrgnaDiies' answer, I have found this sample code; which I will be modifying to suit my needs, thank you. http://iphoneincubator.com/blog/server-communication/how-to-download-a-file-only-if-it-has-been-updated
From what I've gathered from your question, I assume you want to check if the file you are downloading is a pdf and not anything else? If so, you could check the header of the file to verify it is a pdf.
Example, the first thing in every .pdf (That I have myself) is:
%PDF-*.*
Where *.* is a version(?) number. For example, 1.2, 1.3, 1.5, etc.
Therefore, reading the first 5 characters from a downloaded pdf should result in "%PDF-", otherwise it is not a pdf.
As more examples, the first thing in a jpeg image is:
リ・
And for an .exe it's:
MZ・
etc
iOS apps are sandboxed. Meaning, anything you do within the app should only affect your app. Unless you attempt to somehow run the downloaded content (which I believe is against the rules for the App Store). The only thing left would be maliciously crafted PDFs…
I personally don't think it would be a big issue.

Automatic screenshot uploading on Mac like Cloud App

Cloud App has this neat feature wherein it automatically uploads new screenshots as they are added to the Desktop. Any ideas how this is done?
You can do similar things yourself without much in the way of programming. In OSX, you can configure "Folder Actions" to run a script, for example, when a new item appears in a folder, including the Desktop. You can then use the script to do whatever you want with the new files.
This article at TUAW includes an example of uploading files to a web server when they hit a particular folder.
So, basically, the answer is "Folder Actions", or "something's keeping an eye on the folder and sending notifications", at some level. Whether Cloud App uses Folder Actions or watches the folder itself at a lower level, using FSEvents/NSWorkspace, or the kqueue mechanisms (for which there's a nice wrapper class called UKKQueue, if I remember correctly -- don't know how current my knowledge is on that one though!) is another matter...
You could implement this at several different levels, depending on the outcome you want, how you want to design whatever it is you're actually doing, and even what kind of filesystem you're targeting. Fundamentally, in Cocoa/Objective C, I think you probably want to start looking at FSEvents.
Once you've got notifications of the file changes, I'd probably use something like ConnectionKit to do the uploading -- any library at all, really, that means you don't have to bother with the sockets level yourself -- but again, there's a lot of different ways.
Depends, really, what level you're looking to solve the problem at, and whether you want to build something for other people or get something working for yourself. If I just wanted to bash something together for myself, I could probably have something cobbled together using Coda's Transmit app, and Folder Actions, or maybe Hazel, and a minimal bit of Applescript, in a half-hour at most, that would do the job well enough for me...
I am not sure what you are asking for exactly. If you are asking for a way to take a screenshot programmatically in MacOSX, I suggest you have a look at the "screencapture" command (in the terminal, type "man screencapture" for doc).
If you want to do it the "hard" way, you should look at this.

is it possible to automatically purge a Word document of all scripts and other malicious threats?

reputedly, it is possible to make a "malicious" Word document. Maybe using embedded VB script? Anyway, not sure. My question is, is it possible to make an app that safely scrubs all such insertions from a .doc file? Of course, preferably this app should work without actually opening that file in Word application since presumably that may be sufficient for the machine to get damaged.
Is there something like that out there already? Is this even a problem worthy of discussion or in reality there is nothing really malicious that can be done using the Word documents distributed online?
ADDED LATER: johnnyArt, yes, and when you get dirt on your clothes, make sure to go to mommy and tell her about it. Mommy knows best! As a computer programmer, I am interested in learning more about how the world works, including how the world of .doc files and their embedded malicious scripts works. As for using the antivirus and anti-spyware, I will handle these issues without your precious advice. As will, probably, most other users of this forum.
You should scan the file with your antivirus/spyware of choice.
My advice is, if it has malware in it, it's not worth "cleaning" it for use.
Get yourself a clean copy somewhere else.