Sinisterly
[c] Facebook spreader - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C)
+--- Thread: [c] Facebook spreader (/Thread-c-Facebook-spreader)

Pages: 1 2


[c] Facebook spreader - titbang - 07-18-2017

This program will login to Facebook with the provided username/password, extract the friends list, then message them all.

Requires: curl, and gumbo.

Code:
#include <stdio.h>
#include <curl/curl.h>
#include <iostream>
#include <cstring>
#include <vector>

#include "gumbo.h"

using namespace std;

CURL *curl;
CURLcode res;

string data;
string fb_dtsg;

vector<string> friends;

struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
struct curl_httppost *msgform=NULL;
struct curl_httppost *msglast=NULL;

static size_t curl_write( void *ptr, size_t size, size_t nmemb, void *stream) {
 data.append( (char*)ptr, size*nmemb );
 return size*nmemb;
};

string replace_all(string str, const string& from, const string& to) {
   size_t start_pos = 0;
   while((start_pos = str.find(from, start_pos)) != std::string::npos) {
       str.replace(start_pos, from.length(), to);
       start_pos += to.length();
   }
   return str;
}

string string_between( string str, const string& from, const string& to ) {
   size_t first = str.find(from);
   size_t last = str.find(to);
   return( str.substr ( first+from.size(),last-first-to.size()));
}

int curl_check_cookie_response( )
{
 struct curl_slist *cookies;
 struct curl_slist *nc;
 int i;
 res = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
 if (res == CURLE_OK) {
   nc = cookies, i = 1;
   while (nc) {
       if(strstr( nc->data, "c_user") != NULL )
           return 0;
       nc = nc->next;
       i++;
   }
 }
 curl_slist_free_all(cookies);
 return 1;
}

int authenticate_details( const char* email, const char* password )
{
   curl_easy_setopt(curl, CURLOPT_URL, "https://m.facebook.com/login.php" );
   curl_easy_setopt( curl, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; titbang; Linux i686; rv:26.0) Gecko/20100101 Firefox/26.0");
   curl_easy_setopt( curl, CURLOPT_FOLLOWLOCATION, 2L );
   curl_easy_setopt( curl, CURLOPT_VERBOSE, 0 );
   curl_easy_setopt( curl, CURLOPT_COOKIEFILE, "");
   curl_easy_setopt( curl, CURLOPT_COOKIEJAR, "cookies.txt" );
   curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "email", CURLFORM_COPYCONTENTS, email, CURLFORM_END);
   curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "pass", CURLFORM_COPYCONTENTS, password, CURLFORM_END);
   curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
   curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, curl_write );
   if( curl_easy_perform(curl) == CURLE_OK ) {
       return 0;
   }
   return 1;
}

void gumbo_parse_friend_data( GumboNode* node )
{
   GumboAttribute* url;
   if (node->type != GUMBO_NODE_ELEMENT) {
       return;
   }
   if (node->v.element.tag == GUMBO_TAG_A &&
   (url = gumbo_get_attribute(&node->v.element.attributes, "href"))) {
       if( strstr( url->value, "?uid=" ) != NULL ) {
           data = string_between( url->value, "=", "&" );
           data = replace_all( data, "=", "");
           friends.push_back( data );
       }
   }
   GumboVector* children = &node->v.element.children;
   for (unsigned int i = 0; i < children->length; ++i) {
       gumbo_parse_friend_data(static_cast<GumboNode*>(children->data[i]));
   }
}

void gumbo_parse_session_id ( GumboNode* node )
{
   GumboAttribute* inputName; GumboAttribute* inputValue;
   if (node->type != GUMBO_NODE_ELEMENT) {
       return;
   }
   if (node->v.element.tag == GUMBO_TAG_INPUT ) {
       inputName = gumbo_get_attribute( &node->v.element.attributes, "name" );
       inputValue = gumbo_get_attribute( &node->v.element.attributes, "value" );
       if( inputValue != NULL  && inputName != NULL) {
           std::string val( inputName->value );
           std::size_t match = val.find( "fb_dtsg" );
           if( match == 0 ) {
               fb_dtsg = inputValue->value;
           }
       }
   }
   GumboVector* children = &node->v.element.children;
   for (unsigned int i = 0; i < children->length; ++i) {
       gumbo_parse_session_id(static_cast<GumboNode*>(children->data[i]) );
   }
}

int grab_friends_list_data( )
{
   curl_easy_setopt(curl, CURLOPT_URL, "https://m.facebook.com/friends/center/friends" );
   if( curl_easy_perform(curl) == CURLE_OK ) {
       GumboOutput* output = gumbo_parse(data.c_str());
       gumbo_parse_friend_data( output->root);
       return 0;
   }
   return 1;
}

int grab_friend_session( string friend_id )
{
  char url[512];
  snprintf( url, sizeof( url ), "https://m.facebook.com/messages/thread/%s", friend_id.c_str() );
  curl_easy_setopt( curl, CURLOPT_URL, url );
  if( curl_easy_perform(curl) == CURLE_OK ) {
       GumboOutput* output = gumbo_parse(data.c_str());
       gumbo_parse_session_id( output->root);
       return 0;
  }
  return 1;
}

int send_message_to_friend( string friend_id, string message )
{
   char field[ 32 ], value[ 32 ];
   snprintf( field, sizeof( field ), "ids[%s]", friend_id.c_str() );
   snprintf( value, sizeof( value ), "%s", friend_id.c_str() );
   curl_easy_setopt( curl, CURLOPT_URL, "https://m.facebook.com/messages/send/?icm=1" );
   curl_formadd(&msgform, &msglast, "fb_dtsg", CURLFORM_COPYCONTENTS, fb_dtsg.c_str(), CURLFORM_END);
   curl_formadd(&msgform, &msglast, CURLFORM_COPYNAME, field, CURLFORM_COPYCONTENTS, value, CURLFORM_END);
   curl_formadd(&msgform, &msglast, CURLFORM_COPYNAME, "body", CURLFORM_COPYCONTENTS, message.c_str(), CURLFORM_END);
   curl_easy_setopt( curl, CURLOPT_HTTPPOST, msgform );
   if( curl_easy_perform(curl) == CURLE_OK ) {
       return 0;
   }
   return 1;
}


void cleanup( ) {
   data.clear();
}

int main( int argc, char *argv[] ) {

 curl = curl_easy_init();

 if(curl) {

   if( authenticate_details( "message@allyourfriends.com", "thepassword" ) == 0 ) {

       if( curl_check_cookie_response() == 0 )
       {
           printf("We are logged in.");

           if( grab_friends_list_data() == 0 )
           {
               for(vector<int>::size_type i = 0; i != friends.size(); i++)
               {
                   printf( "Sending message to friend ID: %s\r\n", friends[i].c_str() );

                   if( grab_friend_session( friends[i].c_str() ) == 0 ) {
                       send_message_to_friend( friends[i].c_str(), "hi");
                   }
               }
           }
       }
       else {
           printf("Failed to login.");
       }
   }
 }

 return 0;
}



RE: [c] Facebook Spreader - mothered - 07-18-2017

Nice.

Did you code this yourself?


RE: [c] Facebook Spreader - Mr.Kurd - 07-18-2017

Very nice, actually I want make a one in Java.


RE: [c] Facebook Spreader - titbang - 07-18-2017

@mothered Yeah it's an older sample of mine.

@Mr.Kurd Doing it in Java should be quite easy. Check out Jsoup if you haven't already.


RE: [c] Facebook Spreader - Mr.Kurd - 07-18-2017

(07-18-2017, 09:38 PM)titbang Wrote: @mothered Yeah it's an older sample of mine.

@Mr.Kurd Doing it in Java should be quite easy. Check out Jsoup if you haven't already.

I have background with Jsoup but it want need any Api or any Lib?!


RE: [c] Facebook Spreader - BORW3 - 07-20-2017

LOL, with Java I would use Selenium to automate that stuff.


RE: [c] Facebook Spreader - mothered - 07-20-2017

(07-18-2017, 09:38 PM)titbang Wrote: @mothered Yeah it's an older sample of mine.

Good work.

It's nice to see you've put the time and effort to code It yourself.


RE: [c] Facebook Spreader - Inori - 07-20-2017

It looks like a good step in the right direction for automation, but that's definitely C++. C doesn't have objects, e.g. vectors or strings.


RE: [c] Facebook Spreader - titbang - 07-20-2017

(07-20-2017, 06:17 PM)Inori Wrote: It looks like a good step in the right direction for automation, but that's definitely C++. C doesn't have objects, e.g. vectors or strings.

I have this bad tendency of mixing C and C++.


RE: [c] Facebook Spreader - dark_bit - 08-03-2017

(07-20-2017, 02:26 PM)BORW3 Wrote: LOL, with Java I would use Selenium to automate that stuff.

Quote:me too but using python3 ; (import selenium and call web.driver)