Video (Part 1)
|
Video (Part 2)
|
Please read the written tutorial because it shows with the new code, the videos are old and use the old code.
I have updated the code since the tutorial so there are some new things not shown in the tutorial. You will need to download the source code to see the changes.
Remote Shutdown Application Via Pastebin - Using Webrequest
In this tutorial I show you how you can create a shutdown application in VB.NET. I made the videos quite a while ago and it was my first programming tutorial so I was quite slow and the code isn't that clever. Some of the imports aren't even used so, excuse me. I should have probably have used Sockets and Ports instead of web clients, It would probably would be a lot less intensive. I updated the code a lot since then, but cannot find it anymore, so I'll will work with the one from the tutorial. *Edit I have re-written the code somewhat, so it is more reliable and clean.
Written Tutorial
First what we do is import Microsoft.Win32, System.Threading and System.IO library's and we must put them at the very top of the script. This will stop us from having to put that extension before each bit code that requires it, that is all. So, for example, instead of putting IO.File.Exists, we would only have to put if File.Exists.
Now we need to create seven Global Variables of type Boolean. We do this by simply by writing Private bShutdown As Boolean = False & Private Cancel As Boolean = False. We set them to false initially because we don't want our script doing things before we activate anything. We put this just underneath the class declaration where it says Private Class <ClassName>. You also need to add the rest. We have to make a string variable to a folder than may or may not exist for where will use to save backups and miscellaneous things. I have made it to be in Program Files (x86) > RGSoftware > Remote Shutdown. Now we need to create a variable we will use to get the Pastebin paste text from. Initially we set it has "PUT_YOUR_COMMAND_PASTE_URL_HERE" for those who are looking at the source code. However, If they don't put their link there, we create a input box to get the URL later on.
Now we just create two helper strings that will stop us from having to type this long link out every time we want to use it, so I only have to reference the variable name.
Now we just create two helper strings that will stop us from having to type this long link out every time we want to use it, so I only have to reference the variable name.
The reason I put in Shown instead of Form1_Load is because VB.NET won't run certain codes in the load. If you try to hide the form when it is loading, it will ignore it. Putting it in Shown will make it run as soon as the form in 'Shown' whereas Activated will wait until you 'Activate' the form.
Now we must add code to the Form Load.
Now we must add code to the Form Load.
I use a Try-Catch Statement to avoid any unprofessional errors and instead MsgBox the error. I set Me.Opacity = 0 just to make it transparent so when it does load it will be totally transparent, so you 'shouldn't' get a flicker of the form before it get's hidden. Me.ShowInTaskBar() = False will make it so the form does not show in the taskbar.
Now we run a sub that doesn't exist yet because we have yet to create it. so add runAtStartUp(Application.ProductName, Application.ExecutablePath) to the script and we will create the sub later. Next we check if that folder we Globally declared exists. If it doesn't we will create it.
As I mentioned, you will see how we check if that variable is still same, because of the user looking at the source code forgot to add their own Pastebin URL. If it is still the same, we check if a setting is False, (you must also create this setting) and if it is, we now set it to TRUE. Now this msgbox will not show up again when this application is started. We continue by checking if the MsgBox we show in the current line has the result of the YES. If it was yes, we run a URL with process.start. This will open my own personal paste for you to copy for your own version. We now set that variable to the resulting text from the text field on the InputBox. If the user clicks Cancel instead of OK we will get an error which get handled by the Try-Catch Statement we used.
We initially set the interval to a safe 5000ms (5 seconds) so you won't get banned by Pastebin for two many requests. Then we start Timer1.
Now we can add the code in the Timer1_Tick.
Now we run a sub that doesn't exist yet because we have yet to create it. so add runAtStartUp(Application.ProductName, Application.ExecutablePath) to the script and we will create the sub later. Next we check if that folder we Globally declared exists. If it doesn't we will create it.
As I mentioned, you will see how we check if that variable is still same, because of the user looking at the source code forgot to add their own Pastebin URL. If it is still the same, we check if a setting is False, (you must also create this setting) and if it is, we now set it to TRUE. Now this msgbox will not show up again when this application is started. We continue by checking if the MsgBox we show in the current line has the result of the YES. If it was yes, we run a URL with process.start. This will open my own personal paste for you to copy for your own version. We now set that variable to the resulting text from the text field on the InputBox. If the user clicks Cancel instead of OK we will get an error which get handled by the Try-Catch Statement we used.
We initially set the interval to a safe 5000ms (5 seconds) so you won't get banned by Pastebin for two many requests. Then we start Timer1.
Now we can add the code in the Timer1_Tick.
We use another Try-Cast to avoid script crashes and then check if _error is TRUE and if it is, we just Exit the Sub.
Now we create create a a HttpWebRequest and use our URL variable we set in the InputBox (which was prompted if we didn't change the code). Then we make a variable to get a response from the request we just sent and then pass that into a stream reader that reads the response stream. Then we create a string variable and set it to the stream and read all of it from start to end.
Following that, we create a string variable and use a function we have yet to create; to get the string between "<run Request_Interval=" and "ms".
Forthwith, we use the opposite of .ToString which is Parse, which converts a string to a number (integer) and check if that number is < 5000, and if it is, we reset to 5000 to stop the interval from being too quick and getting you banned from Pastebin, if it is 5000 or is greater than 5000, we set the interval to what was read from the ReqInterval variable.
At this time, we create a string variable that will be used as the file name to be the backup file we will create every tick. If Pastebin decides to delete the paste or it is deleted by accident you will have a backup.
So to stop it creating a backup of a bad stream we check if it doesn't contain 403 or if the response contains the words "THIS PASTE HAS BEEN REMOVED!". Then we check if the directory still exists and hasn't been deleted by the user. If it exists, we create a new StreamWriter and specify where we want it to write it by concatenating the directory variable with the string variable we created; that was file-name we want it to be. We write the whole stream, which should be just the paste text if we used the RAW link from Pastebin. If you didn't use the RAW link, you will will have a stream with the whole HTML code of the website which is not good. We then close the StreamWriter and dispose it, which will release the file it was just writing to, so we can interact with the backup file with out warnings and restrictions about how it is still open in another program. We use and ELSE that runs if that directory didn't exist.
Now we check if the stream contains 403 and if it does, we display and notifying MsgBox that there is an error and then close the application because we cannot continue. We also check if the stream contained "THIS PASTE HAS BEEN REMOVED!" and if it does, we also display a MsgBox saying so and that we made a backup of the script which they removed, so you can re-upload. Then we close the script.
We proceed to make a filter for the stream that will test just between the "-->" and "<--" and replace the "<run>" with and empty string then trim off any whitespace that may exist.
We check if that filtered stream contains one of our 'event' codes and if the Boolean we made at the beginning is FALSE, if it is, we then set that Boolean to TRUE so it won't run it again and set the Cancel Boolean to FALSE if it was made TRUE by the ElseIf below. Then we create a variable to read the stream for the shutdown delay and use our not yet created function to get the string between the two delimiters. We then start a process "shutdown" with the parameters of -s -t and using the parsed variable that reads the delay.
We make an ElseIf to check if the stream contained the Cancel code and if the Cancel Boolean = FALSE, it does we set it to TRUE to stop the if being ran again and set the Shutdown Boolean to FALSE because we are cancelling now and not shutting down anymore. We then run the shutdown abort code by running a process of "shutdown" and with parameters of "-a".
Now we use an ElseIf, if the stream contained the code to kill the application and you guessed it, if it does we close the program.
After our Catch ex as Exception we set our error Boolean to true so errors won't be spammed at us because of it being in a tick event. We display a MsgBox of the error and close the application. You may then close the Try with End Try.
That function we have been using to get the string between words is what we create now.
Now we create create a a HttpWebRequest and use our URL variable we set in the InputBox (which was prompted if we didn't change the code). Then we make a variable to get a response from the request we just sent and then pass that into a stream reader that reads the response stream. Then we create a string variable and set it to the stream and read all of it from start to end.
Following that, we create a string variable and use a function we have yet to create; to get the string between "<run Request_Interval=" and "ms".
Forthwith, we use the opposite of .ToString which is Parse, which converts a string to a number (integer) and check if that number is < 5000, and if it is, we reset to 5000 to stop the interval from being too quick and getting you banned from Pastebin, if it is 5000 or is greater than 5000, we set the interval to what was read from the ReqInterval variable.
At this time, we create a string variable that will be used as the file name to be the backup file we will create every tick. If Pastebin decides to delete the paste or it is deleted by accident you will have a backup.
So to stop it creating a backup of a bad stream we check if it doesn't contain 403 or if the response contains the words "THIS PASTE HAS BEEN REMOVED!". Then we check if the directory still exists and hasn't been deleted by the user. If it exists, we create a new StreamWriter and specify where we want it to write it by concatenating the directory variable with the string variable we created; that was file-name we want it to be. We write the whole stream, which should be just the paste text if we used the RAW link from Pastebin. If you didn't use the RAW link, you will will have a stream with the whole HTML code of the website which is not good. We then close the StreamWriter and dispose it, which will release the file it was just writing to, so we can interact with the backup file with out warnings and restrictions about how it is still open in another program. We use and ELSE that runs if that directory didn't exist.
Now we check if the stream contains 403 and if it does, we display and notifying MsgBox that there is an error and then close the application because we cannot continue. We also check if the stream contained "THIS PASTE HAS BEEN REMOVED!" and if it does, we also display a MsgBox saying so and that we made a backup of the script which they removed, so you can re-upload. Then we close the script.
We proceed to make a filter for the stream that will test just between the "-->" and "<--" and replace the "<run>" with and empty string then trim off any whitespace that may exist.
We check if that filtered stream contains one of our 'event' codes and if the Boolean we made at the beginning is FALSE, if it is, we then set that Boolean to TRUE so it won't run it again and set the Cancel Boolean to FALSE if it was made TRUE by the ElseIf below. Then we create a variable to read the stream for the shutdown delay and use our not yet created function to get the string between the two delimiters. We then start a process "shutdown" with the parameters of -s -t and using the parsed variable that reads the delay.
We make an ElseIf to check if the stream contained the Cancel code and if the Cancel Boolean = FALSE, it does we set it to TRUE to stop the if being ran again and set the Shutdown Boolean to FALSE because we are cancelling now and not shutting down anymore. We then run the shutdown abort code by running a process of "shutdown" and with parameters of "-a".
Now we use an ElseIf, if the stream contained the code to kill the application and you guessed it, if it does we close the program.
After our Catch ex as Exception we set our error Boolean to true so errors won't be spammed at us because of it being in a tick event. We display a MsgBox of the error and close the application. You may then close the Try with End Try.
That function we have been using to get the string between words is what we create now.
What we do is, declare three parameters, StartID, EndID, TxtSource and set the return type of the function to type string. We then create a variable to hold the length of StartID and create a string variable and set it to the TxtSource parameter.
We used another Try-Catch to avoid errors and set s to the substring of s plus the length of the StartID. Next we set s to the substring of s to the index of the EndID then return s which will send back the value of s when this function is called.
Now we just need to create the sub that will add the program to the start up so that it will run again without the user having to do it manually.
We used another Try-Catch to avoid errors and set s to the substring of s plus the length of the StartID. Next we set s to the substring of s to the index of the EndID then return s which will send back the value of s when this function is called.
Now we just need to create the sub that will add the program to the start up so that it will run again without the user having to do it manually.
We create the sub and give it two parameters, the ApplicationName and ApplicationPath. We used this back in the Load of the script and used "RemoteShutdown" as the ApplicationName and used Application.ExecutablePath which will get the path of application currently running. This is good because you can set it from where ever the program is be running from.
We also use a Try in this sub to avoid errors and create a variable CU as RegistryKey. Then we set CU to the subkey 'run' which we get to by going down each key and set the "writeable" parameter to TRUE so we can write to the registry. If for some reason we can't the Try will create a clean exception. We then close the Try-Catch statement.
You have completed this tutorial on how to create a Remoted Shutdown Program via Pastebin in VB.NET. I hope you enjoy this program as much as I did coding it.
Downloads at the top.
We also use a Try in this sub to avoid errors and create a variable CU as RegistryKey. Then we set CU to the subkey 'run' which we get to by going down each key and set the "writeable" parameter to TRUE so we can write to the registry. If for some reason we can't the Try will create a clean exception. We then close the Try-Catch statement.
You have completed this tutorial on how to create a Remoted Shutdown Program via Pastebin in VB.NET. I hope you enjoy this program as much as I did coding it.
Downloads at the top.
Author: Riccardo Geraci