Thursday, December 23, 2010

How to post facebook status via backend in PERL

We know that facebook site is a secured and very strict with regards to their website and other site components. There is so much happening on the site that users doesn't realize it changes constantly and that is also for security reason, why? that is because hackers do some research, checking the site constantly as well and if your site is not changing, they can come up with a plan and boom!! before you know it, you're doom already.

Enough with the applaud to the company coz they were great already.

I'm not here to lecture about hacking, but how to submit facebook status right? so, just follow the steps below and you can have a BOT that will send facebook status without human intervention.

1. First thing you have to do is to know the structure of the wap site, this time the facebook wap site http://m.facebook.com

2. Since we are using PERL, we have to install some libraries for sending HTTP request and cookies. install the libraries below:
  • LWP::UserAgent
  • HTTP::Cookies

3. After installing the 2 libraries into your server, we are now ready to do the coding. The code below is just a standard coding that we do in PERL.

#!/usr/bin/perl

require LWP::UserAgent;

use strict;
use warnings;

use HTTP::Cookies;


4. After we require the libraries, we need to initialize the variables we need.

my $lwpua = LWP::UserAgent->new;

5. Set your facebook username/email, password, and your status. please see below.

my $strUser = "<your facebook username/email>";
my $strPass = "<your facebook password>";
my $strStatus = "<your facebook status>";


6. Set up user agent that we will use to send HTTP request.

my $user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6";
my @header = ( 'Referer' => 'http://m.facebook.com/','User-Agent' => $user_agent);


7. Set up cookie file and jar.

my $cookie_file = "cookies.dat";
my $cookie_jar = HTTP::Cookies->new(
                file => $cookie_file,
                autosave => 1,
                ignore_discard => 1);

$lwpua->cookie_jar($cookie_jar);


8. Setup the code for log-in to facebook. Please take note that you should know the link of the login page of the site which is http://m.facebook.com/login.php. The important thing here is to look for the FORM element in which the site is submitting the credentials. You also have to get all the important INPUT parameters for submission to the FORM. I pasted here the FORM element of the login page source as of today as reference. Below also are the ONLY details you need to login.

=begin
<form method="post" action="https://login.facebook.com/login.php?m=m&amp;refsrc=http%3A%2F%2Fm.facebook.com%2login.php&amp;fbb=rac4881b1&amp;refid=9">
<input class="input" name="email" value="" type="text"/>
<input class="input" name="pass" type="password"/>
<input type="submit" value="Log In" class="btn btnC" name="login"/>
</form>
=cut

# logging in to facebook
my $response = $lwpua->post('http://m.facebook.com/login.php',
                      ['email' => $strUser,
                       'pass' => $strPass,
                       'login' => 'Login'], @header);


9. After logging in, we have to extract the COOKIES and use it on our next SESSION or PAGE.

$cookie_jar->extract_cookies( $response );
$cookie_jar->save;


10. After saving the COOKIES, accessing the facebook home page should direct you to the home page where in you can send your STATUS. You can check that if you print the $response->content. I also pasted the code snippet of the content below as reference.

$response = $lwpua->get('http://m.facebook.com/home.php', @header);
my $form_data = $response->content;

=begin
<form method="post" id="composer_form" action="/a/home.php?fbb=r488fb708&amp;refid=7"><input type="hidden" name="fb_dtsg" value="DdtLy" autocomplete="off" /><input type="hidden" name="post_form_id" value="726fdf3c15403500a70f64960565e305" /><input type="hidden" name="charset_test" value="€,´,€,´,水,Д,Є" /><div>What&#039;s on your mind?<br /><textarea class="composerInput" name="status" rows="2" onfocus="" onblur=""></textarea></div><input type="submit" value="Share" class="btn btnC" name="update" /></form>
=cut


11. Same thing here with the login page but this time, we need to know how the page sends a status message. find the FORM that submits STATUS. please take note that the site is frequently changing so you should know how to do the REGEX to catch all the PARAMETERS you need and for the benefit of this post, the above content is the updated content as of today and my REGEX below should be able to catch parameters such as: FORM ACTION, FB_DTSG, and POST_FORM_ID.

$form_data =~ s/\n//g;
$form_data =~ /form method="post" id="composer_form" action="(.*?)"(.*?)name="fb_dtsg" value="(.*?)"(.*?)name="post_form_id" value="(.*?)"/ig;

my $form_action = $1;
my $form_fbdtsg = $3;
my $form_id = $5;


12. Send now the STATUS message with the PARAMETERS needed below.

@header = ('Referer' => 'http://m.facebook.com/',
           'User-Agent' => $user_agent);

$response = $lwpua->post('http://m.facebook.com'.$form_action,
                           ['fb_dtsg' => $form_fbdtsg,
                            'post_form_id' => $form_id,
                            'status' => $strStatus,
                            'update' => 'Share'], @header);

$form_data = $response->content;
unlink($cookie_file);

1;


13. And, that's it!! below is the whole script. hope you were able to get it. next will be in PHP. yeah men!!

#!/usr/bin/perl

require LWP::UserAgent;

use strict;
use warnings;

use HTTP::Cookies;

my $lwpua = LWP::UserAgent->new;

my $strUser = "<your facebook username>";
my $strPass = "<your facebook password>";
my $strStatus = "<your facebook status>";

my $user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6";
my @header = ( 'Referer' => 'http://m.facebook.com/','User-Agent' => $user_agent);

my $cookie_file = "cookies.dat";
my $cookie_jar = HTTP::Cookies->new(
                file => $cookie_file,
                autosave => 1,
                ignore_discard => 1);

$lwpua->cookie_jar($cookie_jar);

my $response = $lwpua->post('http://m.facebook.com/login.php',
                      ['email' => $strUser,
                       'pass' => $strPass,
                       'login' => 'Login'], @header);
$cookie_jar->extract_cookies( $response );
$cookie_jar->save;
$response = $lwpua->get('http://m.facebook.com/home.php', @header);

my $form_data = $response->content;

$form_data =~ s/\n//g;
$form_data =~ /form method="post" id="composer_form" action="(.*?)"(.*?)name="fb_dtsg" value="(.*?)"(.*?)name="post_form_id" value="(.*?)"/ig;

my $form_action = $1;
my $form_fbdtsg = $3;
my $form_id = $5;

@header = ('Referer' => 'http://m.facebook.com/',
           'User-Agent' => $user_agent);

$response = $lwpua->post('http://m.facebook.com'.$form_action,
                           ['fb_dtsg' => $form_fbdtsg,
                            'post_form_id' => $form_id,
                            'status' => $strStatus,
                            'update' => 'Share'], @header);

$form_data = $response->content;
unlink($cookie_file);

1;

4 comments:

  1. nice... very nice.. keep up the good work sir. :D i might use this on my future projects.. :D
    anyway sir, you can embed \\<\/code\> on the code portion of your blog so to have unique font and visibility in your blog.

    ReplyDelete
  2. thanks.. just new here.. i will post all the codes i know to make my blog informative to everyone.. hehehe!!

    ReplyDelete
  3. please check out my new post - http://paulgonzaga.blogspot.com/2010/12/how-to-submit-facebook-status-via-back_24.html

    ReplyDelete
  4. please check the revised version of this post using oauth the "legay way" - http://paulgonzaga.blogspot.com/2011/01/posting-facebook-status-using-oauth-via.html

    ReplyDelete

Leadership 101


  • Leadership demands sacrifices for the near-term to receive lasting benefits. the longer we wait to make sacrifices, the harder they become. Successful people make important decisions early in their life, then manage those decisions the rest of their lives.
  • Growth does not happen by chance. If you want to be sure to grow, you need a plan something strategic, specific, and scheduled. it's a discipline that would need incredible determination from us.
  • Success comes by going the extra mile, working the extra hours, and investing the extra time. The same is true for us. If we want to get to excel in any segment of life, a little extra effort can help. Our efforts can go a long way if we only work a little smarter, listen a little better, push a little harder, and persevere a little longer.
  • Making a difference in your work is not about productivity; it's about people. When you focus on others and connect with them, you can work together to accomplish great things.
  • Envision a goal you'd like to reach. Make it big enough to scare you a little. Now write down a plan for moving toward it. Create mini-goals within the big goal, to set yourself up for continual progress. And include some risks, too. Set yourself up for success.
  • Leaders build margins, not image. A leader may be forced to take unpopular stands for the good of the company. Popularity isn't bad, but decisions made solely on the basis of popular opinion can be devastating. So take courage and make the right though sometimes painful choices.