SlideShare a Scribd company logo
web coding security
Huỳnh Hải Âu
Công ty ISePRO
Đơn vị tổ chức:

Đơn vị tài trợ:
Contents
• SQL Injection
• XSS
• File upload
SQL Injection
• Introduction
• Bad codes
• Preventing solutions
Security Bootcamp 2013 - Lap trinh web an toan
Security Bootcamp 2013 - Lap trinh web an toan
• Types: 3 types
– Union base
• Inject a union query to extract data from database

– Error base
• Inject SQL characters, queries to make the application
query fail and raise errors
• Use sql errors to extract data from database

– Blind SQLi
• Inject sql characters to ask the database true or false
questions and determines the answer based on the
applications response
– Bad code 1:
if(isset($_POST['user']) && isset($_POST['password']))
{
$u = $_POST['user'];
$p = $_POST['password'];
$u = preg_replace('/union|select|from|where|and|or/i','',$u);
$p = preg_replace('/union|select|from|where|and|or/i','',$p);
$q = mysql_query("select * from user where username='$u' and
password='$p'");
if($r=mysql_fetch_assoc($q))
{
echo "<br>Log in successfully !";
echo "<br>Hello $r[username]";
}
else
echo "<br>Invalid login !";
}
– Bad code 2:
if(isset($_POST['user']) && isset($_POST['password']))
{
$u = $_POST['user'];
$p = $_POST['password'];
$u = preg_replace(“/’/i”,””,$u);
$p = preg_replace(“/’/i”,””,$p);
$q = mysql_query("select * from user where username='$u' and
password='$p'");
if($r=mysql_fetch_assoc($q))
{
echo "<br>Log in successfully !";
echo "<br>Hello $r[username]";
}
else
echo "<br>Invalid login !";
}
– Bad code 3:
if(isset($_GET['id']))
{
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$q = mysql_query("select * from user where id=$id");
if($r=mysql_fetch_assoc($q))
{
echo "<br>Profile: $r[username]";
echo "<br>Age: $r[Age]";
echo "<br>Phone: $r[Phone]";
echo "<br>Mail: $r[Mail]";
echo "<br>Address: $r[Address]";
}
else
echo "<br>Invalid id !";
}
– Bad code 4:
if(isset($_POST['user']) && isset($_POST['password']))
{
$u = mysql_real_escape_string($_POST['user']);
$p = mysql_real_escape_string($_POST['password']);
$p = hash("whirlpool", $p, true);
$q = mysql_query("select * from user where username='$u' and password='$p'");
if($r=mysql_fetch_assoc($q))
{
echo "<br>Log in successfully !";
echo "<br>Hello $r[username]";
}
else
Echo "<br>Invalid login !";
}
• Preventing solutions:
– Prepare statement
• aka parameterized statement
• Template of sql query structure
– INSERT INTO PRODUCT (name, price) VALUES (?, ?)

• Separation of control flow & data flow
if(isset($_POST['user']) && isset($_POST['password']))
{
$u = $_POST['user'];
$p = $_POST['password'];
$q = $sql->prepare("select * from user where username=? and password=?");
$q->bind_param("ss", $u, $p);
$q->execute();
$res = $q->get_result();
if($r=$res->fetch_assoc())
{
echo "<br>Log in successfully !";
echo "<br>Hello $r[username]";
}
else
echo "<br>Invalid login !";
$q->close();
}
Cross Site Scripting (XSS)
• Introduction
• Bad codes
• Preventing solutions
Security Bootcamp 2013 - Lap trinh web an toan
Security Bootcamp 2013 - Lap trinh web an toan
Security Bootcamp 2013 - Lap trinh web an toan
• TYPES:
1.
2.

Non-Persistent
Persistent
• Non-Persistent:
In this type of XSS vulnerability an attacker is able to
execute his own code into a webpage but no changes
can be done in that website.
•

Persistent:
In this case attacker stores his executable script in the
vulnerable website database which is being executed every
time webpage is showing the data.

•

Common targets are:
–
–
–
–

Comments
Chat messages
E-mail messages
Wall posts, etc.
• Bad codes
– Bad code 1:
if (isset($_GET['color']))
{
$color = $_GET['color'];
$color = htmlspecialchars($color);
echo "<body bgcolor='". $color."'></body>";
}
– Bad code 2:
if (isset($_GET['color']))
{
$color = $_GET['color'];
$color = htmlspecialchars($color, ENT_QUOTES);
echo "<body bgcolor=$color></body>";
}
• Preventing solutions:
– Input validation
– Output encoding
• Input validation
– whitelist of acceptable inputs
– Consider potential input properties: length, type, range
of acceptable values, syntax
function validate($input)
{
if(!is_string($input))
die(“input must be string !”);
if(strlen($input) > 10)
die(“input length must lower than 10”);
if(!pregmatch(“/^city/”,$input))
die(“input must begin with city word”);
$whitelist={“red”, “green”, “blue”};
if(!in_array($input, $whitelist))
die(“bad input”);

}
• Output encoding
– Sanitizing all values before outputing to browser
– Output encoding functions:
• htmlentities: convert all applicable characters to
HTML entities
function encoding($output)
{
return htmlenties($output);

}
$safe_value = encoding($value);
echo $safe_value;
File Upload Attack
• Introduction
• Bad codes
• Preventing solutions
• Allow attacker to upload malicious files to server
• Most of time, it’s web shell to take control over web
server
• Risk:
–
–
–
–
–
–

Web-shell upload
Website deface
XSS
Phishing
Malware upload
…
• Bad codes
– Bad code 1:
if (isset($_POST['submit']))
{
if($_FILES['userfile']['type'] != "image/gif")
{
echo "Sorry, we only allow uploading GIF images";
exit;
}
$uploaddir = 'uploads/';
$uploadfile= $uploaddir.basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
echo "File is valid, and was successfully uploaded.n";
else
echo "File uploading failed.n";
}
– Bad code 2:
if (isset($_POST['submit']))
{
$imageinfo = getimagesize($_FILES['userfile']['tmp_name']);
if($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg')
{
echo "Sorry, we only accept GIF and JPEG imagesn";
exit;
}
$uploaddir = 'uploads/';
$uploadfile= $uploaddir.basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
echo "File is valid, and was successfully uploaded.n";
else
echo "File uploading failed.n";
}
– Bad code 3:
if (isset($_POST['submit']))
{
$uploaddir = ‘D:/uploads/';
$uploadfile= $uploaddir.basename($_FILES['userfile']['name']);

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
{
echo "File is valid, and was successfully uploaded.n";
echo "<IMG SRC='" . $uploadfile . "'>";
}
else
echo "File uploading failed.n";
}
• Preventing solutions
– Keep uploaded files where they cannot be directly accessed
by the users via a direct URL
• Outside of webroot
• Or configure web server to deny access to upload directory

– Use system-generated file names instead of the names
supplied by users when storing files
if (isset($_POST['submit']))
{
$uploaddir = ‘D:/uploads/';
$new_file_name = rand(1,1000);
$uploadfile = $uploaddir . $new_file_name;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
{
echo "File is valid, and was successfully uploaded.n";
echo "<IMG SRC='" . $uploadfile . "'>";
}
else
echo "File uploading failed.n";
}
The end

Thank you !

More Related Content

DOC
PDF
PhoneGap: Local Storage
PDF
2014 database - course 3 - PHP and MySQL
PDF
Pemrograman Web 8 - MySQL
PDF
Pemrograman Web 9 - Input Form DB dan Session
PDF
OWASP Top 10 at International PHP Conference 2014 in Berlin
TXT
PhoneGap: Local Storage
2014 database - course 3 - PHP and MySQL
Pemrograman Web 8 - MySQL
Pemrograman Web 9 - Input Form DB dan Session
OWASP Top 10 at International PHP Conference 2014 in Berlin

What's hot (17)

PDF
Http and security
PDF
jQuery%20on%20Rails%20Presentation
PDF
Andreas Roth - GraphQL erfolgreich im Backend einsetzen
PDF
php plus mysql
PPTX
User registration and login using stored procedure in php
PPTX
preventing sqli and xss by ravi rajput in owasp meet ahmedabad
TXT
Daily notes
PPTX
Web security
PDF
Hidden in plain site – joomla! hidden secrets for code monkeys
PDF
Coding website
DOCX
logic321
KEY
Potential Friend Finder
PDF
The Future of JavaScript (SXSW '07)
PPTX
15. CodeIgniter editarea inregistrarilor
PDF
Puppet Camp Amsterdam 2015: Manifests of Future Past
PDF
Drush. Secrets come out.
Http and security
jQuery%20on%20Rails%20Presentation
Andreas Roth - GraphQL erfolgreich im Backend einsetzen
php plus mysql
User registration and login using stored procedure in php
preventing sqli and xss by ravi rajput in owasp meet ahmedabad
Daily notes
Web security
Hidden in plain site – joomla! hidden secrets for code monkeys
Coding website
logic321
Potential Friend Finder
The Future of JavaScript (SXSW '07)
15. CodeIgniter editarea inregistrarilor
Puppet Camp Amsterdam 2015: Manifests of Future Past
Drush. Secrets come out.
Ad

Viewers also liked (16)

PPT
Bài 1: Web Cơ Bản - Lập Trình Mạng Nâng Cao
DOCX
Sức mạnh của jsf 2, phần 3 xử lý sự kiện, java script và ajax
PPT
Bài 10: Custom Tag - Lập Trình Mạng Nâng Cao
PPT
Bài 12: JSF-2 - Lập Trình Mạng Nâng Cao
PPT
Bài 11: JSF-1 - Lập Trình Mạng Nâng Cao
PDF
[Cntt] bài giảng java khtn hcm
PPT
Bài 3: Servlet - Lập Trình Mạng Nâng Cao
PPT
Bài 2: J2EE - Lập Trình Mạng Nâng Cao
PDF
Vận dụng kiến thức lập trình web vào môi trường thực tế
PPT
Bài 3: Servlet&Cookie&Session - Lập Trình Mạng Nâng Cao
PPT
Bài 4: JSP Cơ Bản - Lập Trình Mạng Nâng Cao
PDF
VCP 21- VMWare VPC 6
PDF
Linux LPI Bacis
PDF
tài liệu Mã nguồn mở Lap trình shells
PPTX
Presentation on leadership
PPTX
Leadership concepts and theories
Bài 1: Web Cơ Bản - Lập Trình Mạng Nâng Cao
Sức mạnh của jsf 2, phần 3 xử lý sự kiện, java script và ajax
Bài 10: Custom Tag - Lập Trình Mạng Nâng Cao
Bài 12: JSF-2 - Lập Trình Mạng Nâng Cao
Bài 11: JSF-1 - Lập Trình Mạng Nâng Cao
[Cntt] bài giảng java khtn hcm
Bài 3: Servlet - Lập Trình Mạng Nâng Cao
Bài 2: J2EE - Lập Trình Mạng Nâng Cao
Vận dụng kiến thức lập trình web vào môi trường thực tế
Bài 3: Servlet&Cookie&Session - Lập Trình Mạng Nâng Cao
Bài 4: JSP Cơ Bản - Lập Trình Mạng Nâng Cao
VCP 21- VMWare VPC 6
Linux LPI Bacis
tài liệu Mã nguồn mở Lap trình shells
Presentation on leadership
Leadership concepts and theories
Ad

Similar to Security Bootcamp 2013 - Lap trinh web an toan (20)

PPT
Php Security By Mugdha And Anish
PPTX
Security: Odoo Code Hardening
PDF
Take Data Validation Seriously - Paul Milham, WildWorks
PDF
Application Security around OWASP Top 10
PDF
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
PDF
Php Security
PDF
Take Data Validation Seriously - Paul Milham, WildWorks
PPT
Php security
ODP
My app is secure... I think
PDF
Security in Node.JS and Express:
PDF
Spot the Web Vulnerability
PDF
Applications secure by default
PDF
Applications secure by default
PDF
My app is secure... I think
PPS
Php security3895
PPS
PHP Security
PPTX
PCI Security Requirements - secure coding
ODP
My app is secure... I think
ODP
My app is secure... I think
ODP
My app is secure... I think
Php Security By Mugdha And Anish
Security: Odoo Code Hardening
Take Data Validation Seriously - Paul Milham, WildWorks
Application Security around OWASP Top 10
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Php Security
Take Data Validation Seriously - Paul Milham, WildWorks
Php security
My app is secure... I think
Security in Node.JS and Express:
Spot the Web Vulnerability
Applications secure by default
Applications secure by default
My app is secure... I think
Php security3895
PHP Security
PCI Security Requirements - secure coding
My app is secure... I think
My app is secure... I think
My app is secure... I think

More from Security Bootcamp (20)

PDF
Ẩn mình kết nối C&C - Xu hướng tấn công và cách phòng thủ
PPTX
AI-ttacks - Nghiên cứu về một số tấn công vào các mô hình học máy và AI
PPTX
Human and AI - Balancing Innovation and Data Privacy in the Age of Cyber Threats
PPTX
Robustness of Deep learning mode ls.pptx
PPTX
DLL Sideloading cho mọi nhà - Security Bootcamp 2024
PDF
Let the Hunt Begin - Security Bootcamp 2024
PDF
Detection as Code - Effective Approach to manage & optimize SOC Development
PDF
Quản trị rủi ro nguồn mở tại các doanh nghiệp phần mềm Việt Nam
PDF
Phân tích một chiến dịch ransomware: Từ lan truyền đến tống tiền
PDF
CyberJutsu - The Joern-ey of Static Code Analysis.pdf
PPTX
Security in the AI and Web3 era - Veramine
PDF
ĐỂ AI ĐƯỢC AN TOÀN, MINH BẠCH, CÓ TRÁCH NHIỆM VÀ ‘NHÂN TÍNH’ HƠN
PDF
Modern Security Operations - Building and leading modern SOC
PDF
Humanity and AI: Balancing Innovation and Data Privacy in the Age of Cyber Th...
PPTX
SBC2024_AI TRONG CYBER SECURITY_final.pptx
PPTX
Cyber GenAI – Another Chatbot? - Trellix
PDF
Akamai_ API Security Best Practices - Real-world attacks and breaches
PPTX
How to steal a drone Drone Hijacking - VNPT Cyber Immunity
PDF
Empowering Malware Analysis with IDA AppCall
PDF
Detection of Spreading Process on many assets over the network
Ẩn mình kết nối C&C - Xu hướng tấn công và cách phòng thủ
AI-ttacks - Nghiên cứu về một số tấn công vào các mô hình học máy và AI
Human and AI - Balancing Innovation and Data Privacy in the Age of Cyber Threats
Robustness of Deep learning mode ls.pptx
DLL Sideloading cho mọi nhà - Security Bootcamp 2024
Let the Hunt Begin - Security Bootcamp 2024
Detection as Code - Effective Approach to manage & optimize SOC Development
Quản trị rủi ro nguồn mở tại các doanh nghiệp phần mềm Việt Nam
Phân tích một chiến dịch ransomware: Từ lan truyền đến tống tiền
CyberJutsu - The Joern-ey of Static Code Analysis.pdf
Security in the AI and Web3 era - Veramine
ĐỂ AI ĐƯỢC AN TOÀN, MINH BẠCH, CÓ TRÁCH NHIỆM VÀ ‘NHÂN TÍNH’ HƠN
Modern Security Operations - Building and leading modern SOC
Humanity and AI: Balancing Innovation and Data Privacy in the Age of Cyber Th...
SBC2024_AI TRONG CYBER SECURITY_final.pptx
Cyber GenAI – Another Chatbot? - Trellix
Akamai_ API Security Best Practices - Real-world attacks and breaches
How to steal a drone Drone Hijacking - VNPT Cyber Immunity
Empowering Malware Analysis with IDA AppCall
Detection of Spreading Process on many assets over the network

Recently uploaded (20)

PDF
project resource management chapter-09.pdf
PDF
Hybrid model detection and classification of lung cancer
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Getting Started with Data Integration: FME Form 101
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PDF
STKI Israel Market Study 2025 version august
PPTX
The various Industrial Revolutions .pptx
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PPTX
TLE Review Electricity (Electricity).pptx
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PPT
Module 1.ppt Iot fundamentals and Architecture
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Zenith AI: Advanced Artificial Intelligence
project resource management chapter-09.pdf
Hybrid model detection and classification of lung cancer
cloud_computing_Infrastucture_as_cloud_p
Getting Started with Data Integration: FME Form 101
1 - Historical Antecedents, Social Consideration.pdf
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
OMC Textile Division Presentation 2021.pptx
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
STKI Israel Market Study 2025 version august
The various Industrial Revolutions .pptx
Final SEM Unit 1 for mit wpu at pune .pptx
TLE Review Electricity (Electricity).pptx
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Module 1.ppt Iot fundamentals and Architecture
Univ-Connecticut-ChatGPT-Presentaion.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
NewMind AI Weekly Chronicles – August ’25 Week III
A comparative study of natural language inference in Swahili using monolingua...
Zenith AI: Advanced Artificial Intelligence

Security Bootcamp 2013 - Lap trinh web an toan

  • 1. web coding security Huỳnh Hải Âu Công ty ISePRO
  • 2. Đơn vị tổ chức: Đơn vị tài trợ:
  • 3. Contents • SQL Injection • XSS • File upload
  • 4. SQL Injection • Introduction • Bad codes • Preventing solutions
  • 7. • Types: 3 types – Union base • Inject a union query to extract data from database – Error base • Inject SQL characters, queries to make the application query fail and raise errors • Use sql errors to extract data from database – Blind SQLi • Inject sql characters to ask the database true or false questions and determines the answer based on the applications response
  • 8. – Bad code 1: if(isset($_POST['user']) && isset($_POST['password'])) { $u = $_POST['user']; $p = $_POST['password']; $u = preg_replace('/union|select|from|where|and|or/i','',$u); $p = preg_replace('/union|select|from|where|and|or/i','',$p); $q = mysql_query("select * from user where username='$u' and password='$p'"); if($r=mysql_fetch_assoc($q)) { echo "<br>Log in successfully !"; echo "<br>Hello $r[username]"; } else echo "<br>Invalid login !"; }
  • 9. – Bad code 2: if(isset($_POST['user']) && isset($_POST['password'])) { $u = $_POST['user']; $p = $_POST['password']; $u = preg_replace(“/’/i”,””,$u); $p = preg_replace(“/’/i”,””,$p); $q = mysql_query("select * from user where username='$u' and password='$p'"); if($r=mysql_fetch_assoc($q)) { echo "<br>Log in successfully !"; echo "<br>Hello $r[username]"; } else echo "<br>Invalid login !"; }
  • 10. – Bad code 3: if(isset($_GET['id'])) { $id = $_GET['id']; $id = mysql_real_escape_string($id); $q = mysql_query("select * from user where id=$id"); if($r=mysql_fetch_assoc($q)) { echo "<br>Profile: $r[username]"; echo "<br>Age: $r[Age]"; echo "<br>Phone: $r[Phone]"; echo "<br>Mail: $r[Mail]"; echo "<br>Address: $r[Address]"; } else echo "<br>Invalid id !"; }
  • 11. – Bad code 4: if(isset($_POST['user']) && isset($_POST['password'])) { $u = mysql_real_escape_string($_POST['user']); $p = mysql_real_escape_string($_POST['password']); $p = hash("whirlpool", $p, true); $q = mysql_query("select * from user where username='$u' and password='$p'"); if($r=mysql_fetch_assoc($q)) { echo "<br>Log in successfully !"; echo "<br>Hello $r[username]"; } else Echo "<br>Invalid login !"; }
  • 12. • Preventing solutions: – Prepare statement • aka parameterized statement • Template of sql query structure – INSERT INTO PRODUCT (name, price) VALUES (?, ?) • Separation of control flow & data flow
  • 13. if(isset($_POST['user']) && isset($_POST['password'])) { $u = $_POST['user']; $p = $_POST['password']; $q = $sql->prepare("select * from user where username=? and password=?"); $q->bind_param("ss", $u, $p); $q->execute(); $res = $q->get_result(); if($r=$res->fetch_assoc()) { echo "<br>Log in successfully !"; echo "<br>Hello $r[username]"; } else echo "<br>Invalid login !"; $q->close(); }
  • 14. Cross Site Scripting (XSS) • Introduction • Bad codes • Preventing solutions
  • 19. • Non-Persistent: In this type of XSS vulnerability an attacker is able to execute his own code into a webpage but no changes can be done in that website.
  • 20. • Persistent: In this case attacker stores his executable script in the vulnerable website database which is being executed every time webpage is showing the data. • Common targets are: – – – – Comments Chat messages E-mail messages Wall posts, etc.
  • 21. • Bad codes – Bad code 1: if (isset($_GET['color'])) { $color = $_GET['color']; $color = htmlspecialchars($color); echo "<body bgcolor='". $color."'></body>"; }
  • 22. – Bad code 2: if (isset($_GET['color'])) { $color = $_GET['color']; $color = htmlspecialchars($color, ENT_QUOTES); echo "<body bgcolor=$color></body>"; }
  • 23. • Preventing solutions: – Input validation – Output encoding
  • 24. • Input validation – whitelist of acceptable inputs – Consider potential input properties: length, type, range of acceptable values, syntax
  • 25. function validate($input) { if(!is_string($input)) die(“input must be string !”); if(strlen($input) > 10) die(“input length must lower than 10”); if(!pregmatch(“/^city/”,$input)) die(“input must begin with city word”); $whitelist={“red”, “green”, “blue”}; if(!in_array($input, $whitelist)) die(“bad input”); }
  • 26. • Output encoding – Sanitizing all values before outputing to browser – Output encoding functions: • htmlentities: convert all applicable characters to HTML entities
  • 28. File Upload Attack • Introduction • Bad codes • Preventing solutions
  • 29. • Allow attacker to upload malicious files to server • Most of time, it’s web shell to take control over web server • Risk: – – – – – – Web-shell upload Website deface XSS Phishing Malware upload …
  • 30. • Bad codes – Bad code 1: if (isset($_POST['submit'])) { if($_FILES['userfile']['type'] != "image/gif") { echo "Sorry, we only allow uploading GIF images"; exit; } $uploaddir = 'uploads/'; $uploadfile= $uploaddir.basename($_FILES['userfile']['name']); if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) echo "File is valid, and was successfully uploaded.n"; else echo "File uploading failed.n"; }
  • 31. – Bad code 2: if (isset($_POST['submit'])) { $imageinfo = getimagesize($_FILES['userfile']['tmp_name']); if($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg') { echo "Sorry, we only accept GIF and JPEG imagesn"; exit; } $uploaddir = 'uploads/'; $uploadfile= $uploaddir.basename($_FILES['userfile']['name']); if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) echo "File is valid, and was successfully uploaded.n"; else echo "File uploading failed.n"; }
  • 32. – Bad code 3: if (isset($_POST['submit'])) { $uploaddir = ‘D:/uploads/'; $uploadfile= $uploaddir.basename($_FILES['userfile']['name']); if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { echo "File is valid, and was successfully uploaded.n"; echo "<IMG SRC='" . $uploadfile . "'>"; } else echo "File uploading failed.n"; }
  • 33. • Preventing solutions – Keep uploaded files where they cannot be directly accessed by the users via a direct URL • Outside of webroot • Or configure web server to deny access to upload directory – Use system-generated file names instead of the names supplied by users when storing files
  • 34. if (isset($_POST['submit'])) { $uploaddir = ‘D:/uploads/'; $new_file_name = rand(1,1000); $uploadfile = $uploaddir . $new_file_name; if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { echo "File is valid, and was successfully uploaded.n"; echo "<IMG SRC='" . $uploadfile . "'>"; } else echo "File uploading failed.n"; }