Re: Check that uploaded photo is a JPEG




"Chris Mahoney" <chrismahoney@xxxxxxxxx> wrote in message
news:1170900403.182356.290710@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi

I'm setting up a site where users will be able to upload photos. I'd
like to be able to ensure that they're uploading JPEGs, and not
malicious code. I've tried checking the MIME type, but that doesn't
seem to be reliable; for example if you rename an .exe to .jpg and
upload using Firefox, it returns "image/jpeg" (IE 6 returns
"application/octet-stream").

I understand that there probably isn't a surefire solution to this,
but a little security is better than none. Any advice? I'm using VB
2005 but I can read C# if I need to :)

Thanks
Chris


Hi Chris

First check it with HttpPostedFile.ContentType

Then try to create a System.Drawing.Image object from a given source. If
this succeeds, you can be fairly certain the source is a valid image. In
addition, check Img.RawFormat

Sample code:

Try
Dim Img as System.Drawing.Image =
System.Drawing.Image.FromFile("C:\MyImage.gif") 'FromStream(...)

if (Img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg)) then
... ok
Else
... wrong
End if
Catch
... wrong
End Try


.



Relevant Pages

  • [NEWS] Upload Lite Allows Remote Code Execution
    ... The following security advisory is sent to the securiteam mailing list, and can be found at the SecuriTeam web site: http://www.securiteam.com ... attackers to upload malicious code to the server, ... the second temporary file does not. ...
    (Securiteam)
  • Re: Check that uploaded photo is a JPEG
    ... I'm setting up a site where users will be able to upload photos. ... like to be able to ensure that they're uploading JPEGs, ... I understand that there probably isn't a surefire solution to this, ... don't believe FromFilecan cause malicious code to execute. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Check that uploaded photo is a JPEG
    ... I'm setting up a site where users will be able to upload photos. ... like to be able to ensure that they're uploading JPEGs, ... malicious code. ...
    (microsoft.public.dotnet.framework.aspnet)
  • java applet and browser session state - HELP!
    ... I have a java applet that is being used to upload photos to a webserver. ... assuming the java applet creates a new session when connection to the web server.. ...
    (comp.lang.java.programmer)
  • Re: Uploading photos is suddenly impossible
    ... > Out of the blue I am now unable to upload photos to my favorite on-line ... > album site. ... I have a suspicious feeling that my 3-year-old grandson may ... > want to upload, and then wait, and wait and wait and nothing happens. ...
    (microsoft.public.windowsxp.newusers)