welcome back to some more c-sharp the last time we built our fictitious laser program and this one we're taking it to a whole new level i have some really exciting things in store for this playlist actually and now that we've covered most of the basics of the language i mean yeah there are some minor things we skipped over but we'll go back to them on an as needed basis we're truly at a point right now where our options are nearly limitless as far as what we want to learn where we want to go next from this now keep in mind this is not my first programming language and i'm not going to treat it as if it's my first language and if you're learning another language i would recommend you do the same don't restrict yourself to just reading a book and following you know the boring lessons one after the next unless you really want to do that then by all means but if you want to go out and explore different things with the language go and do that go and do what interests you provided that you've covered the very basics like we have up until this point in the series but yeah we have nearly limitless potential here where we can go for next and really quickly let me just say if this is your first language i know a lot of you guys are learning programming for the first time you can still gain a lot of value from taking a similar approach the only adjustment that would make is spend a little bit more time on the basics you know we kind of went through it pretty quickly because we already had a good understanding of what a lot of these things were was just a matter of figuring out the nuance with c-sharp and how it differed from the other languages that i programmed in before but if it's your first time it's going to take you a little bit longer no worries just spend a little bit more time there before moving forward well the exciting announcement that i had actually where i want to take this is i want to actually get into some malware development using c sharp that's one of the pros of the language really easy to interact with you know the native windows api and stuff like that right we have access to the entire. net framework that's an extremely powerful thing and uh there's a lot of really sneaky uh nefarious kind of malware development that we can do we can go down but of course that stuff can get pretty complex so we're going to start with what is going to be pretty simple yet exciting to do we're going to start with ransomware we're going to build out our own ransomware we're going to start simple on it and we can expand on it you know as we go so hopefully you guys are really excited for that now here's the thing there definitely is a few more things we need to cover on this language before we're ready to just quite tackle that right so that's what we're going to do in this video we are going to learn how to do some file i o right some input output right how to write to files how to read uh files how to get your current directory list your you know the files and folders in your directory of choice because all that stuff is going to be extremely important for actually writing the malware where we're recursively going to each fold each file and encrypting it so this is some very necessary requisite knowledge and if you're following along and you're learning the red team tradecraft you're learning the ways of the shadowy hackers out there then eventually it's only a matter of time until you're ready to start applying for jobs so you can do this stuff without getting arrested right so when you are you know about at that point or even when you're gearing up to be you're going to definitely want to arm yourself with the top 10 pen testing interview questions that you absolutely need to know you can check that out down in the description below completely for free and uh yeah let me know what you think hopefully it will be of value i definitely think it will there's no way that it won't be so let's just get right into this let's do some file i o so if i go to my trusty resource of w3schools let me just move that front and center here and uh there was a section for files right so let's just check here it's very straight to the point you see just like any other language there's a lot of different built-in methods that we can call on to do different stuff copy files create files delete them check whether or not a file exists read all the text right to the text replace append let's mostly use write all text and read all text right so they actually have a nice example here looks like we're going to need to use this import here using system i o so let's just start there that'll be the first thing that we do so we'll grab this import and the very next thing is that uh we're going to want to have some text that we want to write and then i'm just going to go ahead and copy this and we'll just modify it as needed that way i don't have to keep looking back and forth and constantly tabbing between the two so we can declare a string called write text and it could be whatever whatever we want it to write right let's say we have written this message to the file right some placeholder text here so that's what that will be and uh well i guess we are writing and then after that it will have written it so we'll say file name dot text let's call this elevate cyber dot text and we're gonna call we're going to pass in the data from the right text variable which is up here and then we want to read that file after it's written so we could just call it here do the read all text and if we want to actually display that on the console then we need a console. writeline on it so we have that with read text and yeah let's just go ahead and run it like this and see how it works so oh let's see can i make this a little bigger for you guys i know that's small even for me to be honest with you there you go we are writing this message to the file so there you go and we could say whatever we wanted to be quite honest completely up to us but this is just to demo it so now we've done something pretty fundamental here now if i wanted to say like i want to append to it we could say we call this variable append text and file dot append all text maybe and what do we want to append adding this extra line and make sure we're passing in the correct variable here and yeah let's see how that works and it doesn't add the new line so that is uh something important to note that there is there was no line break on that so it didn't automatically so adding this on a new line let's try to add a new line so what if we do something like this i think this should appear on the new line if we do the new line character maybe we can even do like carriage return line feed uh i think this should be good we can we can test that out too we're just getting a feel for the language and the nuance and like how everything's working here adding this on a new line what if we were to do um some more text right so what if we did like a carriage return line feed let me just test this out same thing right or no yep either one so there you go and uh yeah that's pretty much all we'll really need for the uh for the ransomware on the file io side of the house now let's look into how we would go about you know seeing the current directory like where did this write the the file to right where is this elevate cyber.
txt file exist because we didn't give it any kind of path right so whatever our current working directory is that is where it wrote it to so let's uh one sec here let's look up um oh here it is let's just look up how we can figure that out so current working directory c-sharp is what i'll search for so here you go directorygetcurrent directory method from systemio which we already have imported so getcurrentdirectory and looks like this is how you write it so let's give that a shot so i'll go ahead and clear this out and i'll say string path is directory dot get current directory and then we'll just do a console. writeline on path and let's see where we're located right now and okay yeah this is well we're pretty nested in here but this is where we're located in just to confirm that if i go all the way drill all the way down there right now i'm in the selfmade. elevate folder if i go into the source folder then repos leviathan leviathan bin debug net 6.
0 we see the elevate cyber file here that we created so awesome and let's just play around with if we wanted to list the files you know so let's see here we'll google that as well so list files in directory c sharp google thinks i'm using python i'm not i am i'm branching out a bit here doing some c sharp along with you guys so get files is a method we can call looks like and that is part of system io namespace so we should be good there let's take a look at an example here so directory um [Music] directory. getfiles okay so yeah let's give that a try and we can get the files in the current directory right so we already defined the current directory as the path variable so we should be able to say console. writeline directory dot get files path and what am i missing something here path is not null so let's see here what did i what did i mess up here so get files target directory it's not liking how i'm using path here let's see what is target directory how did they define that where else is this located uh i'm not sure where it was initialized i don't know if it was initialized on this uh on this thing here and not but we could we could try to pass um a path as well so let's see here what if i say [Music] c drive console.
right line get files all right so we're having a little trouble from this example and that will happen sometimes so if we look like get file c sharp tutorial maybe if we look at a different source we can you know if it's explained a different way maybe we'll understand it a little better that's an approach i like to take if i'm if i'm using one piece of documentation whatever it's not clicking with me then i'll switch to something else so it might be an issue of data type here right so we could do this right this get files here let's try this so if i define an array on the on the get files let's see if we could just read the array array one oops let's see what this would display for us now this is from the c drive i believe oops go ahead and play that and we can play around with those microsoft examples as well okay so we want to drill down and actually read the values of the array so we can say and i think we could probably do this with the for each if not we can google that as well but let's just give that a try first so let's say for each item in array one oops in array one oh uh for each item in array one we'll do console. writeline on and hopefully oh we have to declare say a statically typed language or a strong type of language so we have to say what the data type of item is it should be a string i believe console. writeline item so no we'll try running this and hopefully no errors here and yeah here we go we got it to read out all the files not the directories but the files um and i believe if we wanted the directories we could do directory.