Admin permission to run netsh in java

Asked

Viewed 723 times

0

In a java program I developed, I need to open connection on port 21 (ftp) but Windows 7 by default blocks this port. I tried to add a rule in the firewall allowing the connection using Runtime.getRuntime(). exec("netsh ..."), but netsh needs admin permission. It is in java to invoke a window requesting permission?

1 answer

2

Well, there are some ways to do this for what you’ve come to understand by your question.

1 - Creating a manifesto

You need to create a manifest file that specifies that your application needs administration permissions. You can include this manifesto within your exe or leave it separate from your file (seuapp.exe.manifest).

https://msdn.microsoft.com/en-us/library/bb756929.aspx

http://mark.koli.ch/uac-prompt-from-java-createprocess-error740-the-requested-operation-requires-elevation

2- Runes

Runes allow a user to run specific tools and programs with different permissions than the current user login provides.

There is a difference between being connected to an account that is part of the administrator group and running (a) high or (b) as the embedded Administrator account.

Whenever you run as an Administrator, you are always high - by definition. Therefore, if you run / user: administrador this window will be raised when it opens, you will not receive a UAC prompt and the netsh command must be executed.

But since the integrated Admin account always runs at high levels and does not generate UAC rune prompts, it is a security risk, especially if you do not have a password. This is why Microsoft disables the administrator account by default and requires you to enable it first:

Under Windows® 7, the integrated administrator account is disabled by default. In previous versions of Windows, an administrator account was automatically created during Out-of-Box-Experience (OOBE) with an empty password.

An administrator account with a blank password is a security risk. To better protect the system, the integrated Admin account is disabled by default in all clean installations and Windows 7 updates.

Because of this, you need to activate the Administrator mode, for this, do the following :

Altere as propriedades da conta de administrador usando a Console de Gerenciamento Microsoft (MMC) de Usuários e Grupos Locais.

 1 - Abra o console do MMC e selecione Usuários e Grupos Locais.

 2- Clique com o botão direito do mouse na conta Administrador e selecione Propriedades. 

3 - A janela Propriedades do administrador é exibida. 

4 - Na guia Geral, desmarque a caixa de seleção Conta desativada. 

5 - Feche a consola MMC.

Also, be aware that runes DO NOT allow you to pass arguments to the executed program:

RUNAS : 

RUNAS [ [/noprofile | /profile] [/env] [/savecred | /netonly] ]
 /user:<UserName> program 

RUNAS [ [/noprofile | /profile] [/env] [/savecred] ] 
 /smartcard [/user:<UserName>] program 

RUNAS /trustlevel:<TrustLevel> program 

Simple example

The following command starts an instance of the command prompt as an administrator on the local computer:

runas /user:<localmachinename>\administrator cmd 

https://technet.microsoft.com/en-us/library/cc771525(v=Ws.10). aspx

3 - Batch files

You can use a Windows program to increase your privileges. The program will show the UAC prompt and then you will have administrator privileges.

http://jpassing.com/2007/12/08/launch-elevated-processes-from-the-command-line/

You can then run for command like this:

Runtime.getRuntime (). Exec ("Elevate.exe yourcommand");

Extra information :

http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html

https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html

http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html

http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec%28java.lang.String%5B%5D%29

NOTE: If you have questions regarding the above topics, just ask.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.