How to find current file location in C#? - c#-4.0

I wanted to find where is my .exe file when it runs.
for example it's location is *C:\User* and you need to know this when the app starts.

string path = "";
path = System.AppContext.BaseDirectory;
With this code you are able to find where is your .exe file is running from.
😉

Related

Python - working with unkown directory name versions

I'm using python to download an application from a content distribution network. The application downloads as self extract file cabinet. When executed it creates a directory using a version naming format. For example app-version2/../app.exe, Thus I cannot rely on the folder name as it may change in the future. I'm trying to find the best way to work with the content inside the folder without depending on the actual folder name.
My idea was to rename the folder using os.listdir() and then os.rename(app-version2, myapp) This would work but is not automated. What would be the best automated method to find a folder name that contains version numbers and change that to something more static?
Assuming you want to find the path of the directory which begins with app, you can accomplish this using pathlib.Path:
from pathlib import Path
app_path = next(Path().glob('app*'))
This will give you the path to the first file or directory in your current directory whose name begins with "app".

Node js. Writing directory to the archive, without path to this directory

Got some task, it is not hard, but i have some trouble.
Maybe someone already has similar problem.
My task is writing to zip archive some folder, with files and other folders in it with using NodeJS
I try to use AdmZip pakage
folder project structure:
var AdmZip = require('adm-zip');
let Archive = new AdmZip();
Archive.addLocalFolder('Archive/filesFolder', '');
Archive.writeZip('Archive/newArchive.zip);
I must get archive with 'filesFolder', instead i get archive with 'Archive' folder and in it i have 'filesFolder'.
If anybody know, how to record only target folder, and not the sequence of a way folders?
What happens is that you are providing Archive/filesFolder as value to writeZip and that means include in the zip Archive folder and inside that include filesFolder.
For testing purpose change the value of writeZip() to just filesFolder.zip and it should zip content of Archive as filesFolder.zip in current working directory. See below (you can copy/paste the bit of code and run it and it should work).
var zip = new AdmZip();
// I don't know why you provided a second argument to below, I removed it
// There was nothing about it in the documentation.
zip.addLocalFolder('./Archive');
//rename the value of the following to just filesFolder.zip
zip.writeZip('filesFolder.zip');
The above should output the content of Archive to the current working directory as filesFolder.zip
I did mention this in my comment and your commend seem to indicate that you have path issue, so try the above script.

How to embed, and get resources from Satellite Assembly In .NET MVC site for multiple locales

STEP 1: OK,I know I have read every page on the net about this so I finally broke down and ended up here at SOF.
Here is my resource file code:
using (ResourceWriter rw = new ResourceWriter(#"C:\pathto\errors.resources"))
{
rw.AddResource("String1", "en-US");
}
So obviously, now i have my errors.resources file created w/the string resource named String1, w/the value of "en-US" in it.
STEP 2: So then after that I simply create my satellite assembly with:
c:\>al /t:lib /culture:en-US /embed:C:\pathto\errors.resources /out:C:\pathto\bin\en-US\MyWeb.Ext.errors.resources.dll
Ok...so cool.
STEP 3: So now in my project:
Thread.CurrentThread.CurrentCulture = newCulture;
Thread.CurrentThread.CurrentUICulture = newCulture;
ResourceManager rm = new ResourceManager("MyWeb.Ext.errors", this.GetType().Assembly);
ViewData["String1"] = rm.GetString("String1");
But no matter what I do...I can not access that string resource in the assembly. I can include my errors.resources file in the project and get it that way, but I need it to be in this assembly so I can use this assembly for another project too!
Can someone tell me what I might be doing wrong on step 3?? Or maybe I did something wrong in a prior step??
Where is your resource file located relative to your assembly? You should create a directory containing the name of the locale the resource file is for (en-US in your case), and then place the satellite assembly in there. This directory should be in the same directory as the original assembly.

Exploiting and Correcting Path Traversal Vulnerability

I have a Java Web App running on Tomcat on which I'm supposed to exploit Path traversal vulnerability. There is a section (in the App) at which I can upload a .zip file, which gets extracted in the server's /tmp directory. The content of the .zip file is not being checked, so basically I could put anything in it. I tried putting a .jsp file in it and it extracts perfectly. My problem is that I don't know how to reach this file as a "normal" user from browser. I tried entering ../../../tmp/somepage.jsp in the address bar, but Tomcat just strips the ../ and gives me http://localhost:8080/tmp/ resource not available.
Ideal would be if I could somehow encode ../ in the path of somepage.jsp so that it gets extracted in the web riot directory of the Web App. Is this possible? Are there maybe any escape sequences that would translate to ../ after extracting?
Any ideas would be highly appreciated.
Note: This is a school project in a Security course where I'm supposed to locate vulnerabilities and correct them. Not trying to harm anyone...
Sorry about the downvotes. Security is very important, and should be taught.
Do you pass in the file name to be used?
The check that the server does is probably something something like If location starts with "/tmp" then allow it. So what you want to do is pass `/tmp/../home/webapp/"?
Another idea would be to see if you could craft a zip file that would result in the contents being moved up - like if you set "../" in the filename inside the zip, what would happen? You might need to manually modify things if your zip tools don't allow it.
To protect against this kind of vulnerability you are looking for something like this:
String somedirectory = "c:/fixed_directory/";
String file = request.getParameter("file");
if(file.indexOf(".")>-1)
{
//if it contains a ., disallow
out.print("stop trying to hack");
return;
}
else
{
//load specified file and print to screen
loadfile(somedirectory+file+".txt");
///.....
}
If you just were to pass the variable "file" to your loadfile function without checking, then someone could make a link to load any file they want. See https://www.owasp.org/index.php/Path_Traversal

Load XML from Disk in C#

I am using C# test project .I wish to load a Xml which is available inside the project under a folder Dump . I am able to do
string path =
"C:\APP\FrameworkTest\TestProject\Dump\GetAddressById.xml";
but i don't want to use like this because if the drive changes,my code will fail.
in asp.net we have something like Server.MapPath() . Is there some thing like this ?
For example:
var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var path = Path.Combine(dir, "Dump", "GetAddressById.xml")
Hope this helps.
If you know that folder Dump will always be present in the deployement folder of your application then you certainly don't need to hard code full path.
For ASP.net:
var path = System.IO.Path.Combine(Server.MapPath("/"), "Dump",
"GetAddressById.xml");
For C#:
var path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Dump",
"GetAddressById.xml");

Resources