Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
How would i strip bad codes between good codes?
Old 08-15-2009, 05:11 PM How would i strip bad codes between good codes?
Average Talker

Posts: 22
Name: David
Trades: 0
I am making a small website and i am going to allow people to make profiles, in the profiles i want to allow them to use html codes to fix their profiles up like with music players, and images and scroll boxes stuff like that.

And i am having a hard time understanding how to do it, i was going to make 2 arrays one with bad codes and replace them with good codes in the other array with preg_replace() but i am not understanding how do i make it look for bad code that could be written in many way.

Could someone give me an example of how i would do this?
davidphp is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-15-2009, 06:45 PM Re: How would i strip bad codes between good codes?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
strip out
HTML Code:
<iframe> </iframe>
and
HTML Code:
<script></script>
tags
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-15-2009, 08:08 PM Re: How would i strip bad codes between good codes?
Average Talker

Posts: 22
Name: David
Trades: 0
Are those the only two codes that people could use for xss attacks?

Because i was looking on some websites and they were showing all types of different codes that people put xss in and i want to make sure no one will put that stuff into the codes they put on my site.
davidphp is offline
Reply With Quote
View Public Profile
 
Old 08-15-2009, 08:18 PM Re: How would i strip bad codes between good codes?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
pretty much. Everything else is just markup code.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-15-2009, 11:28 PM Re: How would i strip bad codes between good codes?
Average Talker

Posts: 22
Name: David
Trades: 0
What would happen if someone put in a code like this below?

HTML Code:
<IMG SRC=javascript:alert("XSS")>
What can i do to stop codes like that?
davidphp is offline
Reply With Quote
View Public Profile
 
Old 08-16-2009, 05:39 AM Re: How would i strip bad codes between good codes?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
replace "javascript" with the equivalent ASCII entities

106 97 118 97 115 99 114 105 112 116

prepend "&#" and append ";" to each number

or just add a space

PHP Code:
str_replace("javascript:","java script:",$input
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?

Last edited by chrishirst; 08-16-2009 at 05:40 AM..
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-16-2009, 03:15 PM Re: How would i strip bad codes between good codes?
alexxxl's Avatar
Average Talker

Posts: 18
Name: Alex S. K.
Location: Bishkek, KG
Trades: 0
You can provide all problems only if you will use BB code!
alexxxl is offline
Reply With Quote
View Public Profile Visit alexxxl's homepage!
 
Old 08-17-2009, 01:18 AM Re: How would i strip bad codes between good codes?
Average Talker

Posts: 22
Name: David
Trades: 0
I have never heard of ASCII entities before how do they help against xss?
davidphp is offline
Reply With Quote
View Public Profile
 
Old 08-17-2009, 02:08 PM Re: How would i strip bad codes between good codes?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Nope;

they just replace ASCII character with a browser renderable equivalent but "break" the "executable" aspect of the markup/source.

so j looks like "j" but will break a script tag (view the source code to see what I mean) )

http://www.w3schools.com/tags/ref_entities.asp
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-17-2009, 03:14 PM Re: How would i strip bad codes between good codes?
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
strip_tags() will strip all HTML tags by default, but you can make exceptions in the optional second parameter. I suggest you use this function, then make a limited list of what you will allow your users to utilize.
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 08-17-2009, 03:35 PM Re: How would i strip bad codes between good codes?
NullPointer's Avatar
Will Code for Food

Posts: 2,784
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by wayfarer07 View Post
strip_tags() will strip all HTML tags by default, but you can make exceptions in the optional second parameter. I suggest you use this function, then make a limited list of what you will allow your users to utilize.
Make sure to keep in mind that strip_tags does not remove attributes from allowed tags. A user can still insert a tag with onclick, mouseover, etc attributes.

Also, if you want to convert characters to their ASCII equivalent you can use the ord() function. Ex:

PHP Code:
echo '&#' ord('j');
//will output &#106 and will appear as the character j in the browser 
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 08-17-2009, 03:56 PM Re: How would i strip bad codes between good codes?
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Quote:
Originally Posted by NullPointer View Post
Make sure to keep in mind that strip_tags does not remove attributes from allowed tags. A user can still insert a tag with onclick, mouseover, etc attributes.
Someone on the strip_tags page wrote a simple function that does just that, however: http://us2.php.net/manual/en/functio...tags.php#91498
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 08-17-2009, 08:54 PM Re: How would i strip bad codes between good codes?
Average Talker

Posts: 22
Name: David
Trades: 0
Ok this is what i have so far


PHP Code:
<?php
// below is the string to searh for bad words in
$text '<IMG SRC=javascript:alert("XSS")>';
// below is the words to look for
$find = array("javascript:",
              
"alert",
              
"onload");
// below is the words that they will replace          
$replace = array("javascript"// this will be ASCII characters
                 
"alert"// this will be ASCII characters
                 
"onload"); // this will be ASCII characters         
$filtered str_ireplace($find,$replace,$text);
echo 
$filtered;
?>
now the above code does work but i have seen xss that uses other numbers in it and the above code does not do nothing to those xss attacks like below

PHP Code:
<A HREF="http://%77%77%77%2E%67%6F%6F%67%6C%65%2E%63%6F%6D">XSS</A>

or 

<
A HREF="http://0x42.0x0000066.0x7.0x93/">XSS</A
all that the above codes do is makes links, and i am assuming that when the person clicks that link something will happen.

below are all the codes that i want to allow people to have, any other code they use will be stripped out.

HTML Code:
<b><i><a><ul><li><hr><img><br><center><font><table><tr><td>
<p<div<object><param><embed><bgsound>
Now my question is what words do i filter out? or what characters should i replace with the ascii characters?
davidphp is offline
Reply With Quote
View Public Profile
 
Old 08-18-2009, 04:59 AM Re: How would i strip bad codes between good codes?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
in reality the ONLY character you would need to replace to disable ANY tag is the first "<"

use &lt ( add a ";")
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-18-2009, 07:58 AM Re: How would i strip bad codes between good codes?
Average Talker

Posts: 22
Name: David
Trades: 0
If i str_ireplace < to &lt/; without the slash it just shows all the code, it does not filter it out.
davidphp is offline
Reply With Quote
View Public Profile
 
Old 08-18-2009, 09:51 AM Re: How would i strip bad codes between good codes?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Yep, but it does prevent it executing.
So for things that may be difficult to set a regular expression for removal (due to the "greedy" nature of reg exp) is protects your system.
And if anyone is trying to inject scripts it hardly matters if their pages do look "broken".
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-18-2009, 06:05 PM Re: How would i strip bad codes between good codes?
Average Talker

Posts: 22
Name: David
Trades: 0
I am lost, if i put the &lt/; tag they will not be able to make a profile because all their code will be shown on the page i want members to be able to have their tables or whatever they choose show up on their profiles as tables not code.

I am trying to figure out everything i need to strip out of the codes so no one can use xss on my site, and i am just not understanding because their are so many thing people can do.

If no one can give me a thorough answer i will take any links you have on this subject because i can not really find anything myself.
davidphp is offline
Reply With Quote
View Public Profile
 
Old 08-18-2009, 06:10 PM Re: How would i strip bad codes between good codes?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
You remove the leading "<" from code YOU CONSIDER TO BE DANGEROUS!!!!!
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-18-2009, 06:55 PM Re: How would i strip bad codes between good codes?
Average Talker

Posts: 22
Name: David
Trades: 0
I understand that part but how am i supposed to know if it is a good code or a bad code? If a person was to put in say 3 different codes if i replaced one "<" i would replace them all, and they all would show up on the profile as code.
davidphp is offline
Reply With Quote
View Public Profile
 
Old 08-18-2009, 07:15 PM Re: How would i strip bad codes between good codes?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
You don't know and nobody can tell you ALL the possible code variations that can be employed, so you do not allow <iframe or <script element open tags to be "live" on the pages, as they are the two elements that can be easily used for malicious code.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to How would i strip bad codes between good codes?

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.55275 seconds with 12 queries