#!/usr/bin/perl use Socket; use Carp; use FileHandle; # (1) use port 80 by default, unless overridden on command line $port = 80; # (2) create local TCP socket and set it to listen for connections socket(S,PF_INET,SOCK_STREAM,getprotobyname('tcp')) || die ("ERROR:Con +nection Creation Failed\n"); setsockopt(S, SOL_SOCKET, SO_REUSEADDR, 1) || die ("ERROR: setsockopt( +) Failed\n"); bind(S, sockaddr_in($port, INADDR_ANY)) || die ("ERROR: bind() Failed\ +n"); listen(S, SOCKMAXCONN) || die ("ERROR: listen() Failed\n"); # (3) print a startup message printf(" <<<Type-O-Serve Accepting on Port % d>>>\n\n",$port); while (1) { # (4) wait for a connection C $cport_caddr = accept(C, S); ($cport,$caddr) = sockaddr_in($cport_caddr); C->autoflush(1); # (5) print who the connection is from $cname = gethostbyaddr($caddr,AF_INET); print "Host: $cname\n"; # (6) read request msg until blank line, and print on screen $temp = <C>; ($method, $request, $http) = split(" ",$temp); # check whether it's / in which case make it index.html if($request eq "/"){ $request = "/index.html"; } # remove .. to filter directory traversal attacks $request =~ s/\.\.//; #check file exists if ( -e "./htdocs/$request" ){ print "200 - $cname $request\n"; $page=""; open FILE, "<./htdocs/$request"; while(<FILE>){ $page .= $_; } close(FILE); print C "HTTP/1.0 200 OK\r\n"; print C "Content-type: text/html\r\n"; print C "\r\n"; print C "$page"; }else{ print "404 - $cname $request\n"; print C "HTTP/1.0 404\r\n"; print C "Content-type: text/html\r\n"; print C "\r\n"; print C "<html><head><title>404 Page Not Found</title><head>"; print C "<body>404 - $request not found</body></html>"; } close(C); }
This is a very simple webserver i know that :s but I am joining a compatition and i want to make a simple WAF(Web Application Firewall) so I made the webserver just to make it as a enviroment to make the firewall work the firewall idea is to filter URL content like if some one wrote http://localhost/index.php?p=' He'll be rejected coz there is a rule in a blacklist file doesn't allow this kind of requets so how to get the rules from a text file and match them with the URL?
the rules are Regex rules the idea is like mod_security and if you couldn't make it get the rules from a file just give me a simple code for doing it directly from the code .
please can anyone help me in doing this i really need it i have to give it to them tomorrow

In reply to webserver - firewall [simple] by -=Mizo=-

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.