Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Javascript 

HTML/JS code.Detect OS and download right payload

I have a locally hosted web page with a link that downloads a payload.The code right now is something like this:
Code
<h2><p>&nbsp; &nbsp; &nbsp; &nbsp;<a href="./login/guest.apk">Guest Key Authentication</a></p></h2>

I am trying to change what file the link corresponds to depending on OS detection.I am clueless in HTML and JS.

I found this piece of JS code:
var OSName = "Unknown OS";
Code
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Android")!=-1) OSName="Android";

if (OSName ==  "Windows"){
    window.location = "./login/guest.apk"
    };

if (OSName == "Android"){
     window.location = "./login.guest.apk"
    };



What I want is a way to call the script when the link is clicked.
[Image: VCytj45.jpg]
Place your code in a(n anonymous) function and call said function when the element is clicked. For this, you will need to add an event listener to the element. This can be achieved using the addEventListener function on an element.

More information about adding an event listener can be found here:
https://developer.mozilla.org/en-US/docs...ntListener

For a list of possible events, please take a look at the following page:
https://developer.mozilla.org/en-US/docs/Web/Events

For example, the function addEventListener can wait for a "click" event to occur:
Code
element.addEventListener("click", () => {
// Place your code here that needs to be executed after a click occurs
});

This is all very basic HTML and JavaScript. A live jsfiddle can be found here:
https://jsfiddle.net/cyLszejn/

This example uses an anonymous function, which has been possible since ES6:
https://jsfiddle.net/f19sv4ma/

Since I'm not doing anything with the event parameters, I have removed them from my example. But if you want, you can obviously work with event parameters like in this example:
https://jsfiddle.net/ke04nj15/

Code
element.addEventListener("click", (event) => {
 // Work with event parameters, for example a simple: console.log(event);
});

I strongly recommend that you look into modern JavaScript development as well. More information about arrow functions can be found here:
https://developer.mozilla.org/en-US/docs..._functions

When you're using
Code
<a></a>
elements without a href attribute, you should not be using a a element in the first place. This is an extremely bad practice. Use other element types instead, like the button that I showed you in the example or a span. You can use CSS to change their appearance.

If instead, you want the link to change, when the site has finished loading, you could use something like this:
Code
const link = document.getElementById("aElementHere");
link.setAttribute("href", "http://google.com");

This will effectively set the href attribute to be something else. A live example can be found here:
https://jsfiddle.net/ke04nj15/1/

Noted, this can seriously impact SEO, due to some search engines not being very clever in this regard.

More information regarding the setAttribute function can be found here:
https://developer.mozilla.org/en-US/docs...tAttribute

You should also be aware that what you're trying to achieve (retrieving the operating system information) is not always possible and you might be faced with issues using your code. For example, some mobile browsers will provide you with incorrect information when it comes to the operating system and said information can also be modified by third parties. So all in all, if you wait for the DOM to be ready, and then 'correctly' identify the operating system and change the target URL, you should have what you're looking for. You should be able to do this on your own now with the information that was given to you in this post.




Some more information about JavaScript itself that you probably missed:

You should also look into truthy and falsy in JavaScript:
https://www.sitepoint.com/javascript-truthy-falsy/

One more thing. I noticed that you're using the var keyword. ES6 has introduced let and const. You should learn when to use which identifier. More information can be found here, for modern JavaScript development:
https://tylermcginnis.com/var-let-const/
[Image: vp6iypF.gif]
Thanks, that was a mini lesson and I really appreciate it.I've learnt quite a few things but I had to experiment for hours because it's my first touch with html and js code.I had to assume and test many things related to structure and syntax.I have some ultra newbie C++ knowledge and that helped a little.
So final code for a button that downloads different files depending on OS detection is this:
html:
<button onclick="visitPage();">Download</button>
js:
function visitPage(){
if (navigator.appVersion.indexOf("Win")!=-1) window.location='./login/test.zip';
if (navigator.appVersion.indexOf("Android")!=-1) window.location='./login/test.apk';
}
[Image: VCytj45.jpg]
(Jan 2, 2020 04:02 PM)skidhackz Wrote:  Thanks, that was a mini lesson and I really appreciate it.I've learnt quite a few things but I had to experiment for hours because it's my first touch with html and js code.I had to assume and test many things related to structure and syntax.I have some ultra newbie C++ knowledge and that helped a little.
So final code for a button that downloads different files depending on OS detection is this:
html:
<button onclick="visitPage();">Download</button>
js:
   function visitPage(){
if (navigator.appVersion.indexOf("Win")!=-1) window.location='./login/test.zip';
if (navigator.appVersion.indexOf("Android")!=-1) window.location='./login/test.apk';
   }

You're welcome. If I can make a couple more suggestions based on the code you've shared with us now:

I'd recommend using JavaScript to add an event listener to the button instead of the HTML onclick functionality. The reason why I suggest this is because you're limited to only a single in-line function using the onclick attribute whereas in pure vanilla JavaScript you can easily add multiple listeners to the same element. In-line event handlers can be overwritten as well, which can make it very obscure what the code is actually doing. It also makes sure that there's a clear separation between design and code, which is the main reason why I'm telling you this. Of course, arrow functions, scoping and usage of anonymous functions is also guaranteed this way, even though using onclick in-line can be considered an anonymous function.

One more thing to note would be that you don't have a default case for what the link is supposed to be if the operating system fails to match any of the patterns you described. Imagine if a Linux user visits your page, they'll be greeted with a button that does absolutely nothing when they click on it due to the code not handling this particular OS.

Something like this (switch statement) should do the trick:
Code
switch(os) {
 case x:
   // link for os x
   break;
 case y:
   // link for os y
   break;
 default:
   // default link when OS isn't known
}
https://developer.mozilla.org/en-US/docs...nts/switch

Take note of the default keyword which points to the block of code that will be executed if the specified condition doesn't match any of the specified cases.

Happy coding.
[Image: vp6iypF.gif]
Thanks for the suggestions.Yeah I thought about it but don't care outside of Android and windows.I'll add it though.

I tried making it work using event listener and Js code using your links and quite a few other examples but couldn't do it.

The problem was that I didn't know where was I supposed to create the button, inside the event listener?Outside but in the js script?Or I'm html code?If I did make it in the HTML code (the only way it actually created a button) but it wouldn't trigger the listener events inside the js code.I hope I am understood.
Can you post an example of that solution?
[Image: VCytj45.jpg]
(Jan 3, 2020 01:49 AM)skidhackz Wrote:  Thanks for the suggestions.Yeah I thought about it but don't care outside of Android and windows.I'll add it though.

I tried making it work using event listener and Js code using your links and quite a few other examples but couldn't do it.

The problem was that I didn't know where was I supposed to create the button, inside the event listener?Outside but in the js script?Or I'm html code?If I did make it in the HTML code (the only way it actually created a button) but it wouldn't trigger the listener events inside the js code.I hope I am understood.
Can you post an example of that solution?

Absolutely. It's important to understand where your JavaScript is being loaded and executed as well. There are various places where you can put your JavaScript code. For example in the head of a page, the top of the body or the bottom of the body to name a few. Each one will have a different loading cycle.

Let's take a look at the following example:
https://jsfiddle.net/0vqfp3jo/

As you can see, the JavaScript is being loaded in the top of the body tag. This will essentially make sure that when our browser reaches that part in the page rendering process, that it will execute the JavaScript. Because the DOM (Document Object Model) hasn't actually finished loading yet, the JavaScript itself won't be able to find the element that it is supposed to select and add an event listener to. This means that the JavaScript code won't be able to add the event listener, because the element it's trying to find doesn't exist yet in its current context.

The error message in the browser developer tools is quite clear about this as well:
Quote:TypeError: document.getElementById(...) is null


If, however, we place the same JavaScript code at the end of the body tag, you'll notice that it does indeed work:
https://jsfiddle.net/0vqfp3jo/1/

That's because by the time the JavaScript code is executed, the element is indeed available to the JavaScript code. For a little more information regarding these browser processes, you will probably want to read up more about this:
https://www.homeandlearn.co.uk/javascrip...ement.html
https://teamtreehouse.com/community/best...cript-tags

So, how could we make it so that even if our JavaScript is placed at the top of the body tag, that it will select the proper element and work as intended? Well, there are a couple of solutions to that problem. The best way to solve this would be to wait until the DOM is ready. This can be done by adding an event listener on the global document variable for the DOMContentLoaded event:
Code
document.addEventListener("DOMContentLoaded", callback);
callback being a reference to the function that should be executed when the DOM content has loaded. Here's a live example using JSFiddle and anonymous functions:
https://jsfiddle.net/0vqfp3jo/2/

As you can see, the JavaScript is located at the top of the body tag, but works flawlessly when compared to the other example above that doesn't wait for the DOM to be ready.

In regards to the placement of your HTML: You cannot place HTML elements inside JavaScript code the way you do and expect them to be rendered into the DOM. But you can definitely insert HTML in a document by appending them to DOM elements. Here's an example on how to do just that in JavaScript:
Code
const node = document.createElement("li");                 // Create a <li> node
const textnode = document.createTextNode("Water");         // Create a text node
node.appendChild(textnode);                              // Append the text to <li>
document.getElementById("myList").appendChild(node);     // Append <li> to <ul> with id="myList"

I do notice that you're struggling with some of the basics of both HTML and JavaScript, which is why I would recommend that you look at both individually first. Learning HTML and CSS is one of the first steps in webdevelopment that you should take. After which, you should probably start with JavaScript. If I can recommend you some good material to start your journey, I'd recommend the following playlists on YouTube, which are free of charge:
https://www.youtube.com/watch?v=hu-q2zYw...XWzLlPAm6G
https://www.youtube.com/watch?v=iWOYAxln...RB_ZkFrQAc

The man in the video (Shaun) also offers courses, which I highly recommend you give a try if you're serious about learning webdevelopment:
https://www.Udemy (warez disallowed).com/course/modern-javascript-from-novice-to-ninja/?couponCode=NINJADEC19
His courses are modern, straight to the point and filled with practical examples. For theory related information, I'd recommend that you buy an actual book.

Happy coding.
[Image: vp6iypF.gif]
I've tried that code before (script placed on botton of body) and I didn't know how to call the download function inside the click event.How can I do this?

Thanks a lot for the suggestions but I only wanted to create a functional phishing page.I'm not interested in web development but I've started python seminars and I'm impressed with the structure of this language.Experimenting with python after only knowing how to program in VB6 is extremely satisfactory :D
[Image: VCytj45.jpg]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Javascript]  by flexing_99Apr 29, 2023 12:02 AM 3 270 Apr 29, 2023 12:02 AM
Last Post: Zgh600
[Javascript]  by flexing_99Jul 4, 2020 04:11 PM 0 134 Jul 4, 2020 04:11 PM
Last Post: flexing_99
[PHP] by The One Who KnocksApr 23, 2020 10:57 PM 3 271 Apr 23, 2020 10:57 PM
Last Post: Pronouncer