📄 ch19_05.htm
字号:
CLASS="programlisting"><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Insecure dependency in open while running with -T switch at ...</I></CODE></B></CODE></PRE><PCLASS="para">This is because <CODECLASS="literal">$ARGV[0]</CODE> (having come from outside your program) is not trustworthy. The only way to change tainted data into untainted data is by using regular expression backreferences:</P><PRECLASS="programlisting">$file = $ARGV[0]; # $file taintedunless ($file =~ m#^([\w.-]+)$#) { # $1 is untainted die "filename '$file' has invalid characters.\n";}$file = $1; # $file untainted</PRE><PCLASS="para">Tainted data can come from anything outside your program, such as from your program arguments or environment variables, the results of reading from filehandles or directory handles, and <CODECLASS="literal">stat</CODE> or locale information. Operations considered insecure with tainted data include <CODECLASS="literal">system(STRING)</CODE>, <CODECLASS="literal">exec(STRING)</CODE>, backticks, <CODECLASS="literal">glob</CODE>, <CODECLASS="literal">open</CODE> with any mode except read-only, <CODECLASS="literal">unlink</CODE>, <CODECLASS="literal">mkdir</CODE>, <CODECLASS="literal">rmdir</CODE>, <CODECLASS="literal">chown</CODE>, <CODECLASS="literal">chmod</CODE>, <CODECLASS="literal">umask</CODE>, <CODECLASS="literal">link</CODE>, <CODECLASS="literal">symlin</CODE>k, the <BCLASS="emphasis.bold">-s</B> command-line switch, <CODECLASS="literal">kill</CODE>, <CODECLASS="literal">require</CODE>, <CODECLASS="literal">eval</CODE>, <CODECLASS="literal">truncate</CODE>, <CODECLASS="literal">ioctl</CODE>, <CODECLASS="literal">fcntl</CODE>, <CODECLASS="literal">socket</CODE>, <CODECLASS="literal">socketpair</CODE>, <CODECLASS="literal">bind</CODE>, <CODECLASS="literal">connect</CODE>, <CODECLASS="literal">chdir</CODE>, <CODECLASS="literal">chroot</CODE>, <CODECLASS="literal">setpgrp</CODE>, <CODECLASS="literal">setpriority</CODE>, and <CODECLASS="literal">syscall</CODE>.</P><PCLASS="para">A common attack exploits what's known as a <EMCLASS="emphasis">race condition</EM><ACLASS="indexterm"NAME="ch19-idx-1000005439-0"></A>. That's a situation where, between two actions of yours, an attacker can race in and change something to make your program misbehave. A notorious race condition occurred in the way older Unix kernels ran setuid scripts: between the kernel reading the file to find which interpreter to run, and the now-setuid interpreter reading the file, a malicious person could substitute their own script.</P><PCLASS="para">Race conditions crop up even in apparently innocuous places. Consider what would happen if not one but many copies of the following code ran simultaneously.</P><PRECLASS="programlisting">unless (-e $filename) { # WRONG! open(FH, "> $filename"); # ...}</PRE><PCLASS="para">There's a race between testing whether the file exists and opening it for writing. Still worse, if someone replaced the file with a link to something important, like one of your personal configuration files, the above code would erase that file. The correct way to do this is to do a non-destructive create with the <CODECLASS="literal">sysopen</CODE> function, described in <ACLASS="xref"HREF="ch07_02.htm"TITLE="Opening a File">Recipe 7.1</A>.</P><PCLASS="para">A setuid CGI script runs with different permissions than the web server does. This lets the CGI script access resources (files, shadow password databases, etc) that it otherwise could not. This can be convenient, but it can also be dangerous. Weaknesses in setuid scripts may let crackers access not only files that the low-privilege web server user can access, but also any that could be accessed by the user the script runs as. For a poorly written setuid root script, this could let anyone change passwords, delete files, read credit card records, and other malicious acts. This is why you should always make sure your programs run with the lowest possible privilege, normally the user the web server runs as: <CODECLASS="literal">nobody</CODE>.</P><PCLASS="para">Finally (and this recommendation may be the hardest to follow) be conscious of the physical path your network traffic takes. Are you sending passwords over an unencrypted connection? Do these unencrypted passwords travel through insecure networks? A form's PASSWORD input field only protects you from someone looking over your shoulder. Always use SSL when real passwords are involved. If you're serious about security, fire up your browser and a packet sniffer to see how easily your traffic is decoded. <ACLASS="indexterm"NAME="ch19-idx-1000005427-0"></A><ACLASS="indexterm"NAME="ch19-idx-1000005427-1"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-432">See Also</A></H3><PCLASS="para">The section on <ACLASS="olink"HREF="../prog/ch06_03.htm"> "Cooperating with Strangers"</A> in <ACLASS="olink"HREF="../prog/ch06_01.htm">Chapter 6</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; <ICLASS="filename">perlsec </I>(1); the CGI and HTTP specs and the CGI Security FAQ, all mentioned in the Introduction to this chapter; the section on "Avoiding Denial of Service Attacks" in the standard CGI module documentation; <ACLASS="xref"HREF="ch19_07.htm"TITLE="Executing Commands Without Shell Escapes">Recipe 19.6</A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_04.htm"TITLE="19.3. Fixing a 500 Server Error"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 19.3. Fixing a 500 Server Error"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_06.htm"TITLE="19.5. Making CGI Scripts Efficient"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 19.5. Making CGI Scripts Efficient"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">19.3. Fixing a 500 Server Error</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">19.5. Making CGI Scripts Efficient</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -