libcurl post username and password - c++

I think I have read every libcurl post related question on stackoverflow, yet I still can not seem to get my login code to work. I have copied code from other sample post programs as well as code from https://www.hackthissite.org/articles/read/1078
and it still doesn't work. There are no errors in the code itself, however the final get keeps redirecting to https://www.masteringbiology.com/site/notloggedin.html.
I am trying to login and use the cookies to access a specific page on the site.
I am 100% self taught and would appreciate help.
#include <stdio.h>
#include <iostream>
#include <curl\curl.h>
#include<fstream>
#include <cstdio>
#include <stdlib.h>
using namespace std;
int main(void)
{
CURLcode res;
// string URL= "https://session.masteringbiology.com/myct/assignmentPrintViewassignmentID=6607103";
CURL* curly= curl_easy_init();
curl_global_init(CURL_GLOBAL_ALL);
if(curly){
cout<<"go!!!"<<endl;
curl_easy_setopt(curly, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curly, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_easy_setopt(curly, CURLOPT_USERAGENT, "Mozilla/4.0");
curl_easy_setopt(curly, CURLOPT_AUTOREFERER, 1 );
curl_easy_setopt(curly, CURLOPT_FOLLOWLOCATION, 1 );
curl_easy_setopt(curly, CURLOPT_COOKIEFILE, "");
curl_easy_setopt(curly, CURLOPT_URL,
"https://www.masteringbiology.com/site/login.html");
curl_easy_perform( curly );
// Now, can actually login. First we forge the HTTP referer field
curl_easy_setopt(curly, CURLOPT_REFERER,
"https://www.masteringbiology.com/site/login.html");
// Next we tell LibCurl what HTTP POST data to submit
char *data="nme=xxx&pwd=yyy";
curl_easy_setopt(curly, CURLOPT_POSTFIELDS, data);
curl_easy_perform( curly );
//todoc
curl_easy_setopt(curly, CURLOPT_HTTPGET, 1L);
//curl_easy_setopt(curly, CURLOPT_URL, URL.c_str());
curl_easy_setopt(curly, CURLOPT_URL,
"https://session.masteringbiology.com/myct/mastering#/");
FILE * filename1;
filename1=fopen("filetest.txt","w+");
if(filename1) {
// write the page body to this file handle
curl_easy_setopt(curly, CURLOPT_WRITEDATA, filename1);
// get it!
res=curl_easy_perform(curly);
// close the header file
fclose(filename1);
}
if (res==CURLE_OK){
cout<<"Yeah!!!";
}
else{cout<<"oh no!!! "<<res;}
}
else{
fprintf(stderr, "curl initialization failure");
return 0;
}
curl_easy_cleanup(curly);
return 0;
}
Here is the html for the login form
<!-- Mastering hidden form -->
<form name="hiddenForm" id="hiddenForm" class="hidden"
method="POST" action="https://session.masteringbiology.com/login"
target="_top">
<input type="hidden" id="authProvider" name="authProvider"
value="SMS" />
<input type="hidden" id="username" name="username" value=""
/>
<input type="hidden" id="password" name="password" value=""
/>
<input type="hidden" id="passwordEncType" name="passwordEncType" value="" />
</form>
<!-- end Mastering hidden form -->
<form class="form-stacked has-validation" name="loginForm"
id="loginForm" method="post" action="#" target="_top" autocomplete="off"
data-errorMsg="Please supply the information for all required fields marked
below.">
<label for="nme">Username</label>
<div class="group nowrap">
<input type="text" name="nme" id="nme" class="medium-
width required-field">
</div>
<label for="pwd">Password</label>
<div class="group nowrap">
<input type="password" name="pwd" id="pwd"
class="medium-width password required-field">
</div>
<button type="submit" class="button button-big-icon bg-
color-match uppercase mar-top-x1Half">
<span aria-hidden="true" data-icon="" class="left">
</span>Sign In</button>

Related

Uploading picture camera file to absolute path folder

I'm new to ColdFusion and was trying to attempt a simple process to where you take a picture of something with your IOS camera device and it sends it to a drive on your computer in a absolute path temporary folder. I don't know if this is the right approach to doing this?
However, when I hit submit it returns a fatal error I'm not sure why here is what I attempted. Also, when i tested to see if the uploadfile was set but it says it isn't and yet it is in the name field?
test.cfm
<form name="uploadform" action="testaction.cfm" method="POST" enctype="multipart/form-data">
<input type="file" capture="camera" accept="image/*" id="cameraInput" name="uploadfile">
<input type="submit" name="uploadsubmit" value="Upload">
</form>
testaction.cfm
<cfif isDefined("form.uploadfile")>
<cffile
action = "upload"
fileField= "uploadfile"
destination= "T:\cftest"
nameConflict= "overwrite">
<cfelse>
<form name="uploadform" action="test.cfm" method="POST"
enctype="multipart/form-data">
<input type="file" capture="camera" accept="image/*" id="cameraInput"
name="uploadfile">
<input type="submit" name="uploadsubmit" value="Upload">
</form>
</cfif>

brute force simple authentication

Hi to all I am new to python intermediate level and I am trying to develop simple authentication brute force tool, I am really unaware where I missed please someone correct my mistake I am trying this code in mutillidea and bwapp. code is listed below
!/usr/bin/python
import mechanize
import itertools
br = mechanize.Browser()
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
combos = itertools.permutations("pwa",3)
r = br.open("http://127.0.0.1/bwapp/login.php")
for x in combos:
new_form = '''
<<form action="/bwapp/login.php" method="POST">
<p><label for="login">Login:</label><br />
<input type="text" id="login" name="login" size="20" autocomplete="off"></p>
<p><label for="password">Password:</label><br />
<input type="password" id="password" name="password" size="20" autocomplete="off"></p>
<p><label for="security_level">Set the security level:</label><br />
<select name="security_level">
<option value="0">low</option>
<option value="1">medium</option>
<option value="2">high</option>
</select>
</p>
<button type="submit" name="form" value="submit">Login</button>
</form>
'''
r.set_data(new_form)
br.set_response(r)
br.select_form( nr = 0 )
br.form['login'] = 'bee'
br.form['password'] = ''.join(x)
br.form['security_level'] = 0
print "Checking ",br.form['password']
response=br.submit()
if response.geturl()=="http://127.0.0.1/bwapp/portal.php":
#url to which the page is redirected after login
print "Correct password is ",''.join(x)
break
Error I am getting is I cannot get correct password

Use form information in external POST request

I've built a simple form to open up a JIRA ticket based on user input. I've almost got all of it, except I don't know how to use the form element in the POST request. Here's what I have so far:
<form target="_blank" action='http://baseurl.com/secure/CreateIssueDetails!init.jspa?pid=10517&issuetype=3&summary=Change+application+name+to+{{new_name}}&reporter={{request.user}}&priority=5&assignee=xxx' method='post'>
<label for="new_name">New name: </label>
<input id="new_name" type="text" name="new_name" value="{{item.name}}">
<input type="submit" value="Create JIRA ticket">
</form>
So I just need the value the user puts in the new_name element to be passed into the appropriate spot in the URL. How do I access that?
It sounds like you're getting POST and GET mixed. POST data would not be included in the URL itself, but rather in the request payload itself.
So, your URL would be http://baseurl.com/secure/CreateIssueDetails!init.jspa
The payload would be separately put in the body of the HTTP request.
If you need to use a GET method, the URL itself would be the same as above, but the URL that eventually gets hit would be http://baseurl.com/secure/CreateIssueDetails!init.jspa?new_name=WHATEVERVALUE.
If you need additional key-value pairs to get passed, just add them as hidden fields and pass them that way.
Your code, edited:
<form target="_blank" action='http://baseurl.com/secure/CreateIssueDetails!init.jspa' method='post'> <!-- ARE YOU SURE IT'S A POST REQUEST AND NOT A GET? -->
<label for="new_name">New name: </label>
<input id="new_name" type="text" name="new_name" value="{{item.name}}">
<input type="hidden" value="10517" name="pid">
<input type="hidden" value="3" name="issuetype">
<input type="hidden" value="5" name="priority">
<input type="hidden" value="Change application name to {{new_name}}" name="summary">
<input type="hidden" value="{{request.user}}" name="reporter">
<input type="hidden" value="xxx" name="assignee">
<input type="submit" value="Create JIRA ticket">
</form>
Makes sense?

angularJS : validation pattern should allow ? but should not save to server

Plz check example where i need to show "??" into textbox initially.
but those "??" should not save to the server means it should save data validated by regEx.
For that i am assigning "??" initially its fire the validation however
"??" not showing to the textbox.
So what is work around for that ?
Below is the HTML
<div ng-app ng-controller="formCtrl">
<form name="myForm" ng-submit="onSubmit()">
<input type="text" ng-model="price" name="price_field" ng-pattern="/^[0-9]{1,7}$/" required>
<span ng-show="myForm.price_field.$error.pattern">Not a valid number!</span>
<input type="submit" value="submit"/></form></div>
Below is the Javascript
function formCtrl($scope){
$scope.price= "??";
$scope.onSubmit = function(){
alert("form submitted");
}}
Plz check on JsFiddle sample
-Thanks,
Yogesh
It seems input doesn't show up until it matches the regex
So try this,
Html file,
<div ng-app ng-controller="formCtrl">
<form name="myForm" ng-submit="onSubmit(price)">
<input type="text" ng-model="price" required />
<span ng-hide="validater">Not a valid number!</span><br>
<input type="submit" value="submit"/>
</form>
</div>
Js file,
function formCtrl($scope){
$scope.price= "??";
$scope.onSubmit = function(price){
$scope.validater= new RegExp("^\[0-9]{1,7}$").test(price);
//alert("validater "+ $scope.validater);
if(!$scope.validater){
$scope.price= "??";
}else{
alert("form submitted");
}
return $scope.validater;
}
}
Hope this solves your problem :)
Here is the working demo jsfiddle
Try using like this.
<div ng-app ng-controller="formCtrl">
<form name="myForm" ng-submit="onSubmit()">
<input type="text" ng-model="price" name="price_field" placeholder="Enter a number" ng-pattern="/^[0-9]{1,7}$/" required>
<span ng-show="myForm.price_field.$error.pattern">Not a valid number!</span>
<input type="submit" ng-disabled="myForm.$invalid" value="submit"/></form></div>
And in your JS
function formCtrl($scope){
$scope.price= "-1";
$scope.onSubmit = function(){
alert("form submitted");
}}
Take a look at this.
Working Demo
html
<div ng-app ng-controller="formCtrl">
<form name="myForm" ng-submit="onSubmit()">
<input type="text" ng-model="price" name="price_field" ng-pattern="/^[0-9]{1,7}$/" placeholder="??" required >
<span ng-show="myForm.price_field.$error.pattern">Not a valid number!</span>
<input type="submit" value="submit"/>
</form>
</div>
script
function formCtrl($scope){
$scope.price= "";
$scope.onSubmit = function(){
alert("form submitted");
}
}

Regex and POST in same connection

please bear with me, i'm brand new to Python!
I'm trying to login to a website which uses PHP. The form contains two hidden fields, the value of one and the name of another are generated on page load.
My code below successfuly accesses the page and using regex manages to return the values - great!
The problem I am having is that I then generate my querystring that will be used for the POST (this contains the two values obtained earlier) and opens the url again. This generates brand new tokens/values and my originals are of no use.
Can someone shed some light on how I can connect to a site, use regex to get the values and then POST all in the same connection.
I hope i've made myself clear, if not please let me know.
Thanks in advance for your help.
import urllib2,urllib,re,cookielib
url='http://www.example.com/index.php'
req = urllib2.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3 Gecko/2008092417 Firefox/3.0.3')
response = urllib2.urlopen(req)
link=response.read()
response.close()
token1=re.compile('<input type="hidden" name="return" value="(.+?)" />').findall(link)
token2=re.compile('<input type="hidden" name="(.+?)" value="1" />').findall(link)
print token1[0]
print token2[0]
username = 'username'
password = 'password'
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'username' : username, 'password' : password, 'return' : token1[0], token2[0] : '1', 'Submit' : 'Log in', 'option' : 'com_users', 'task' : 'user.login'})
opener.open('http://www.example.com/index.php', login_data)
resp = opener.open('http://www.example.com/index.php')
FORM:
<form action="/index.php/welcome2" method="post" id="login-form" >
<fieldset class="userdata">
<p id="form-login-username">
<label for="modlgn-username">User Name</label>
<input id="modlgn-username" type="text" name="username" class="inputbox" size="18" />
</p>
<p id="form-login-password">
<label for="modlgn-passwd">Password</label>
<input id="modlgn-passwd" type="password" name="password" class="inputbox" size="18" />
</p>
<p id="form-login-remember">
<label for="modlgn-remember">Remember Me</label>
<input id="modlgn-remember" type="checkbox" name="remember" class="inputbox" value="yes"/>
</p>
<input type="submit" name="Submit" class="button" value="Log in" />
<input type="hidden" name="option" value="com_users" />
<input type="hidden" name="task" value="user.login" />
<input type="hidden" name="return" value="aW5kZXgucGhwP0l0ZW1pZD0xMjc=" />
<input type="hidden" name="c813c34837e4e48e8e3268c0a42912a2" value="1" />
</fieldset>
<ul>
<li>
<a href="/index.php/my-account/my-details?view=reset">
Forgot your password?</a>
</li>
<li>
<a href="/index.php/my-account/my-details?view=remind">
Forgot your username?</a>
</li>
<li>
<a href="/index.php/register">
Create an account</a>
</li>
</ul>
</form>
When you write...
opener.open('http://www.example.com/index.php', login_data)
resp = opener.open('http://www.example.com/index.php')
Why not just this?
resp = opener.open('http://www.example.com/index.php', login_data)
I've never used this Python library, but my first reaction is that this would give you the response text all in one request, with which you can get the new token, wouldn't it?
Update based on form: It looks like your problem is you're POSTing the login info to index.php rather than index.php/welcome.