Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (2)

Powered by Vanilla. Made with Bootstrap.
Perl-CGI Example-02: GET & HTML forms
  • Sh3llc0d3
    Posts: 1,910
    So, in this example we have two scripts... a VERY basic HTML page and our perl-cgi script.

    HTML Page - index.html
    <html>
    <head>
    <title>..::iExploit Rules::..</title>
    </head>

    <body>
    <h1>HTML test page for iExploit perl-cgi example</h1>
    <hr />
    <p><form method=\"GET\" action=\"cgi-bin/example.cgi\">
    Input text to display: <input type=\"text\" size=\"30\" maxlength=\"100\" name=\"input\"> <br />
    <input type=\"submit\" name=\"submit\" value=\"Print value!\">
    </form></p>
    <hr />
    </body>
    </html>


    CGI script - example.cgi (placed in cgi-bin/ or where-ever you have cgi configured to run)
    #!C:/Perl64/bin/Perl.exe -w
    # Semtex-Primed
    # Perl-CGI example2
    # GET method using HTML form

    local ($buffer, @pairs, $pair, $name, $value, %FORM);
    $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
    if ($ENV{'REQUEST_METHOD'} eq \"GET\")
    {
    $buffer = $ENV{'QUERY_STRING'};
    }
    @pairs = split(/&/, $buffer);
    foreach $pair (@pairs)
    {
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%(..)/pack(\"C\", hex($1))/eg;
    $FORM{$name} = $value;
    }
    $input = $FORM{input};

    print \"Content-type:text/html\r\n\r\n\";
    print \"<html>\";
    print \"<head>\";
    print \"<title>..::iExploit Rules::..</title>\";
    print \"</head>\";
    print \"<body>\";
    print \"<h1>HTML test page for iExploit perl-cgi example</h1>\";
    print \"<hr />\";
    print \"<h2>The value you input is: $input</h2>\";
    print \"<hr />\";
    print \"Thanks, Semtex-Primed\";
    print \"</body>\";
    print \"</html>\";


    So let me walk you through whats going on here. In the HTML page your being asked to enter a value/word or whatever you want. The page then using the GET method sends the value to the perl-cgi script (notice action="cgi-bin/example.cgi"). But anyway this isn't a HTML forms tutorial so you should know that already.

    Moving on to the cgi script, the script reads the values and splits the name of the incoming info and the value for the named input. We then put the value we want into a variable within the script. This is done here> "$input = $FORM{input};" 'input' on the html form is the name of that particular input being sent. So that corresponding value is read and saved to the variable $input.

    Next we create a webpage using perl/cgi to output the data you input on index.html.

    This script also demonstrates writing the html code line-by-line instead of using the block style in the last script.

    Hope you've learned something more about the capabilities of perl-cgi and thanks. Again for cgi to work on your web-server you will need to enable it. I'll be writing a tutorial on how to enable/config it within apache2 soon.
  • s1n4
    Posts: 88
    Thanks nice share ;)
  • Sh3llc0d3
    Posts: 1,910
    said:


    Thanks nice share ;)



    No problem :)
  • chroniccommand
    Posts: 1,389
    Might be worth while to mention sanitizing user input. CGI vulnerabilities are disasterous :P
  • Sh3llc0d3
    Posts: 1,910
    said:


    Might be worth while to mention sanitizing user input. CGI vulnerabilities are disasterous :P



    ...I would hope that everyone would look that up anyway. CGI vulnerabilities are no worse than other's, compromising the web-server is common in poorly written scripts, access with httpd privs is rare.
  • Xin
    Posts: 3,251
    Good job semtex keep it up
    Xin
  • Sh3llc0d3
    Posts: 1,910
    Thanks Xin, obviously GET is somewhat of a hazard to use. What I'm going to do is come up with a script to use for sanitising input to these types of scripts.
  • Xin
    Posts: 3,251
    said:


    Thanks Xin, obviously GET is somewhat of a hazard to use. What I'm going to do is come up with a script to use for sanitising input to these types of scripts.



    I wouldnt say GET requests are any more of a hazard than POST or any other times, they all get passed to the server the same way, its just what sanitisation you use.
    Xin