您的位置 首页 php

每个程序员都应该警惕的文件上传漏洞

文件上传漏洞是指网络攻击者上传了一个可执行的文件到服务器并执行。这里上传的文件可以是木马,病毒,恶意脚本或者WebShell等。

网站上传文件并且存储的过程:

假如某网站有一个允许用户上传图片的功能,用户上传的图片最终会被存储到 /upload/ 目录下,并且以上传文件时的时间日期命名,上传文件的后缀为后缀。小明这时上传了一个1.jpg的文件,当服务器收到用户上传的数据时,会在 /upload/ 目录下新建一个以当前时间日期命名的文件,后缀为.jpg 。然后将接收到的用户上传的1.jpg图片里的数据写入到新建的文件里面。

漏洞成因:

由于程序员在对用户文件上传部分的控制不足或者处理缺陷,而导致用户可以越过其本身权限向服务器上传可执行的动态脚本文件。打个比方来说,如果你使用 php 作为服务器端的动态网站环境,那么在你的网站的上传功能处,就一定不能让用户上传 php 类型的文件,否则他上传一个一句话木马,你服务器上的文件就可以被他任意更改了。因此文件上传漏洞带来的危害常常是毁灭性的,Apache、Tomcat、Nginx等都曝出过文件上传漏洞。

一般我们会利用文件上传漏洞上传一句话木马,然后用菜刀连接获取 webshell。但是这里有两个问题:

  • 第一你的文件能上传到web服务器
  • 第二你的文件能被当成脚本文件执行,所以要想让上传文件被当成脚本执行,我们经常会和文件包含漏洞和文件解析漏洞一起利用

服务器上传文件命名规则:

  • 第一种:上传文件名和服务器命名一致
  • 第二种:上传文件名和服务器命名不一致(随机,时间日期命名等)

服务器对上传文件的过滤

  1. 前端脚本检测文件扩展名。当客户端选择文件点击上传的时候,客户端还没有向服务器发送任何消息,前端的 js 脚本就对文件的扩展名进行检测来判断是否是可以上传的类型
#前端脚本检测,只允许上传 .jpg格式的文件	<script type="text/javascript">	 function selectFile(fnUpload) {	 var filename = fnUpload.value; 	 var mime = filename.toLowerCase().substr( file name.lastIndexOf(".")); 	 if(mime!=".jpg") 	 { 	 alert("请选择jpg格式的照片上传"); 	 fnUpload.outerHTML=fnUpload.outerHTML; 	 }	 }	</script> 

2.后端脚本检测文件扩展名,数据提交到后端,后端的函数对上传文件的后缀名进行检测,比如黑名单检测不允许上传 .php 、.asp 后缀格式的文件; 白名单 检测只允许上传 .jpg 格式的文件

#后端php检测	$info=pathinfo($_FILES["file"]["name"]);	 $ ext =$info['extension'];// 得到文件扩展名	 if (strtolower($ext) == "php") { #黑名单检测,不允许上传php格式的文件	 exit("不允许的后缀名");	 } 

3.后端通过对上传文件的 Content-Type 类型进行黑白名单检测过滤

#后端对上传文件的 Content-Type类型进行检测,只允许上传 image/gif、image/ jpeg 、image/pjpeg格式的文件	if (($_FILES["file"]["type"] != "image/gif") && ($_FILES["file"]["type"] != "image/jpeg") 	 && ($_FILES["file"]["type"] != "image/pjpeg")){	 exit($_FILES["file"]["type"]);	 exit("不允许的格式"); 

4.通过函数比如 getimagesize() 函数检测你上传的图片的大小是否是正常的图片大小,防止上传一句话木马。通过分析图片头部来判断这个是不是一个有效的图片格式,比如 jpg 格式图片头部是 JFIF ,gif头部是GIF89a,png头部是%PNG

#后端检测上传的文件是否是正常大小的图片	if(!getimagesize($_FILES["file"]["tmp_name"])){	 exit("不允许的文件");	 } 

注意:在生产环境中的过滤,往往是这些方法都会结合的,而不只是单单的某一个过滤方法。生产环境中的过滤是很严格的

上传文件过滤的绕过

  • 对于第一种前端脚本检测过滤,上传的时候上传 jpg 后缀的文件,然后用butpsuite进行 抓包 修改为.php的即可绕过。
  • 对于第二种后端过滤,如果是后端黑名单过滤的话,我们可以想尽任何办法绕过黑名单进行上传。比如如果目标服务器是windows系统的话,我们可以利用windows系统的解析漏洞,用burpsuite抓包,将 文件名后缀 改为 .php. 或者 .php ,因为在windows系统内是不允许文件以 . 或者空格结尾的。所以在绕过上传之后windows系统会自动去掉 点和空格。所以,该文件最终还是会被解析成 .php 。或者还可以将php三个字母变换大小写,因为在windows系统里面是不区分大小写的。如果是白名单检测的话,我们可以采用00截断绕过。00截断利用的是php的一个漏洞。在 php<5.3.4 版本中,存储文件时处理文件名的函数认为0x00是终止符。于是在存储文件的时候,当函数读到 0x00(%00) 时,会认为文件已经结束。

例如:我们上传 1.php%00.jpg 时,首先后缀名是合法的jpg格式,可以绕过前端的检测。上传到后端后,后端判断文件名后缀的函数会认为其是一个.jpg格式的文件,可以躲过白名单检测。但是在保存文件时,保存文件时处理文件名的函数在遇到%00字符认为这是终止符,于是丢弃后面的 .jpg,于是我们上传的 1.php%00.jpg 文件最终会被写入 1.php 文件中并存储在服务端。

  • 对于第三种过滤,可以使用burpsuite进行抓包修改 Content-Type 类型
  • 对于第四种过滤,可以将一句话木马写入到正常的图片中:copy /b 1.jpg+1.php 2.jpg 。然后在利用burpsuite修改后缀为 .php ,或者利用文件包含漏洞或者文件解析漏洞,将其解析成 php脚本即可
  • 还有其他的过滤,比如多文件上传时,有时服务器只对第一个上传的文件进行了检查,这时通过上传多个文件并将恶意文件掺杂进其中也可绕过服务器的过滤。

00截断实验:

这个实验对用户上传文件是这样处理的,首先会对用户上传文件的后缀名进行检测,只能上传 jpg/gif/png 格式的文件,然后会对上传后的文件路径进行判断,如果是以 php 为后缀的就会返回flag,如果是以 jpg/gif/png 为后缀就会显示存储路径为固定的:./uploads/8a9e5f6a7a789acb.php

文件上传的防御

  1. 客户端检测,使用 js 对上传图片检测,包括文件大小、文件扩展名、文件类型等
  2. 服务端检测,对文件大小、文件路径、文件扩展名、文件类型、文件内容检测、对文件重命名等
  3. 其他限制,如服务器端上传目录设置不可执行权限
  4. 检查网站有没有文件解析漏洞和文件包含漏洞

文章来源:智云一二三科技

文章标题:每个程序员都应该警惕的文件上传漏洞

文章地址:https://www.zhihuclub.com/34191.shtml

关于作者: 智云科技

热门文章

评论已关闭

32条评论

  1. It is all I can do to work all day The mean C max of anastrozole decreased by 16 and the median Tmax was delayed from 2 to 5 hours when anastrozole was administered 30 minutes after food

  2. Benzatropine is an anticholinergic drug used to treat Parkinson s disease PD and extrapyramidal symptoms, except tardive dyskinesia Nakajima Y, Sato Y, Konishi T

  3. For electrophoresis of BSA TAMNLC and EPO TAMNLC, 8 and 12 acrylamide gels were casted, respectively But there must have been a few qualms not about whether they would be good parents, but simply over what they were letting themselves, and their unborn child, in for

  4. Although Biktarvy has been linked to weight gain, it s important to understand that every person s experience is different

  5. We did not find any articles that evaluated safety of systemic therapy from a population perspective

  6. com 20 E2 AD 90 20Sildenafil 20Versus 20Viagra 20 20Viagra 20Natural 20Feminino 20Receita sildenafil versus viagra U Ackerman MG, Shapiro PA, Coe A, Trivedi MS, Crew KD

  7. Keywords Brain macrophages; MCAo model; Microglia; Muscarinic acetylcholine receptor; Stroke

  8. cabgolin levaquin epididymitis reviews An indictment charged the Jewish men using stolen prescription sheets and other means to obtain 23, 000 oxycodone pills with a street value of 460, 000

  9. antivert ivermectin watsons While Google can make money by allowing small businesses ina country to use its self serve advertising platform, Twitter sself serve ad product remains in its infancy and is onlyavailable in the U

  10. It caught all the sun, was not so sheltered and didn t have quite the same view, but it felt right Randomised placebo controlled trial of primaquine for prophylaxis of falciparum and vivax malaria

  11. There are big gaps in the therapeutic armementarium for these infections Haywood ZJVApGlTwSiBJHvfI 6 27 2022

  12. For instance, if your son is playing in a soccer game this afternoon that you really want to go to, take it easy in the morning and conserve energy for the afternoon event

  13. After the initial cases of Conn syndrome, multiple patients demonstrated similar features

  14. CTX was measured using a sandwich enzyme linked immunosorbent assay Serum Crosslaps; IDS Ltd, Fountain Hills, AZ with an interassay variability of 11

  15. The ErbB family also comprises three other homologous proteins HER1 EGFR or ErbB1, HER3 ErbB3, and HER4 ErbB4 This is the full article,

  16. PGI 2, like NO, induces vasodilatation whilst thromboxane A 2 is a potent vasoconstrictor

  17. Peripheral neuropathy, mainly of sensory type has been reported and is characterized by numbness or paresthesia of an extremity Adaptation to the Spanish language was performed using the forward backward translation method, followed by a pretesting study on five representative patients

  18. I am only on Test at the moment but plan to start HCG soon Alginate coating on tamoxifen loaded chitosan nanoparticles was done with an aim of protecting chitosan from stomach environment and release nanoparticles in the intestine

  19. Although we calculated delivery costs for multiples according to the birthweight of each neonate, the costs of singleton deliveries may not be an accurate reflection of the costs associated with multiples of the same birthweight

  20. All preparations including low dose, nonoral routes of estrogen are effective in preserving bone mass

网站地图