Please note that This attack differs from Code Injection, in that code injection allows the attacker to add their own code that is then executed by the application. In Command Injection, the attacker extends the default functionality of the application, which execute system commands, without the necessity of injecting code.
user_supplied_path
they could do this:exec(command)
, syscall(command)
, system(command)
and command
. You will have to be especially careful with these functions if the user may enter the whole command, or a part of it. This is because in most shells, you can execute another command at the end of the first one, concatenating them with a semicolon (;
) or a vertical bar (|
).What can I do against ImageMagick Command Injection in Rails? When you’re dealing with a list of known parameters, as here, it’s easiest to validate user input against a regex or set of known secure responses. The Dragonfly library, for example, does a good job of checking user arguments (e.g. resize requests) to make sure they’re safe:
`RESIZE_GEOMETRY = /\\A\\d*x\\d*[><%^!]?\\z|\\A\\d+@\\z/ # e.g. '300x200!'`
demo
system(command, parameters)
method which passes command line parameters safely.
system("ls -a -l -@ -1 #{path}") #No
system("ls", "-a", "-l", "-@", "-1", path) #Yes
CSS Injection happens when a malicious party is able to alter your webpage by making use of user-defined styles. If your Rails application allows users to define a color which is then served back through CSS using a view:
<div style="background: <%= user.background_color %>;">
Then a user could supply a value which alters the page layout or content. A major risk of CSS injection is abuse of the content directive to rewrite a page’s content. Additionally, if a user is able to edit the style of forms seen by others, they could trick those users into putting personal data in the public.
CSS Injection is explained best by the well-known MySpace Samy worm. This worm automatically sent a friend request to Samy (the attacker) simply by visiting his profile. Within several hours he had over 1 million friend requests, which created so much traffic that MySpace went offline. The following is a technical explanation of that worm.
This example, again, showed that a restricted list filter is never complete. However, as custom CSS in web applications is a quite rare feature, it may be hard to find a good permitted CSS filter. If you want to allow custom colors or images, you can allow the user to choose them and build the CSS in the web application. Use Rails' sanitize() method as a model for a permitted CSS filter, if you really need one.
The easiest way to prevent injection attacks is to validate user-provided values. Instead of giving end users the ability to set their own values, you can also give users a pre-defined list which you’ve already validated.
Asynchronous Javascript and XML (AJAX) is one of the latest techniques used by web application developers to provide a user experience similar to that of a traditional (i.e., “pre-web”) application. Since AJAX is still a new technology, there are many security issues that have not yet been fully researched.
AJAX uses the XMLHttpRequest(XHR) object for all communication with a server-side application, frequently a web service. A client sends a request to a specific URL on the same server as the original page and can receive any kind of reply from the server. In the case of accessing an AJAX page on a non-SSL connection, the subsequent XMLHttpRequest calls are also not SSL encrypted. Hence, the login data is traversing the wire in clear text. Using secure HTTPS/SSL channels, which the modern day browsers support, is the easiest way to prevent such attacks from happening. XMLHttpRequest(XHR) objects retrieve the information of all the servers on the web. This could lead to various other attacks such as SQL Injection, Cross Site Scripting (XSS), etc.
Countermeasures - CLIENT SIDE
The use of .innerText
will prevent most XSS problems as it will automatically encode the text.
eval()
function is evil, never use it. Needing to use eval usually indicates a problem in your design.
When using data to build HTML, script, CSS, XML, JSON, etc. make sure you take into account how that data must be presented in a literal sense to keep its logical meaning. Data should be properly encoded before used in this manner to prevent injection style issues, and to make sure the logical meaning is preserved.
Least ye have forgotten the user controls the client side logic. I can use a number of browser plugging to set breakpoints, skip code, change values, etc. Never rely on client logic.
Just like the security one, make sure any interesting business rules/logic is duplicated on the server side less a user bypass needed logic and do something silly, or worse, costly.
This is hard and even a small mistake can cause large security issues. There are already a lot of frameworks to provide this functionality.
Just like building HTML or SQL you will cause XML injection bugs, so stay way from this or at least use an encoding library or safe JSON or XML library to make attributes and element data safe.
Anything the client knows the user will also know, so keep all that secret stuff on the server please.
Use TLS/SSL and encrypt on the server!
This is the overall one that gets me out of trouble in case I missed something :)
Countermeasures - SERVER Side
We will learn more in the CSRF section! (Next section)
Protect against JSON Hijacking for Older Browsers
Always have the outside primitive be an object for JSON strings:
[{"object": "inside an array"}]
{"object": "not inside an array"}
{"result": [{"object": "inside an array"}]}
Avoid writing serialization code Server Side
Remember ref vs. value types! Look for an existing library that has been reviewed.
Even though you only expect your AJAX client side code to call those services the users can too. Make sure you validate inputs and treat them like they are under user control (because they are!).
Use the framework and be safe, do it by hand and have security issues.
You need to use a third-party library to validate web services.