Enable Windows Firewall (with Advanced Security) logging - c#

Which is the equivalent of:
netsh advfirewall set currentprofile logging filename %systemroot%\system32\LogFiles\Firewall\pfirewall.log
in C/C++ and VBScript ?
I found nothing in official: Windows Firewall with Advanced Security Interfaces
Note:
I am interested only in enabling/disabling logging, not in changing the log filename.

Don't know about C/C++, but in VBScript you'd simply shell out to run the netsh commandline:
logfile = "%systemroot%\system32\LogFiles\Firewall\pfirewall.log"
Function qq(str) : qq = Chr(34) & str & Chr(34) : End Function
Set sh = CreateObject("WScript.Shell")
sh.Run "netsh advfirewall set currentprofile logging filename " & qq(logfile)

Related

Service installer with a different name

I am using below cmd code to install a windows service
#echo off
:: BatchGotAdmin
::-------------------------------------
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"="
echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
::--------------------------------------
::ENTER YOUR CODE BELOW:
cmd /k "C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil -i C:\fgbk\WindowsService\installer\OurIntegrationService.exe"
So this creates a service with name OurIntegrationService and i want to create another instance of same service with a different name OurIntegrationServiceStage
How can i do this ?
The ServiceName identifies the service to the Service Control Manager.
The value of this property must be identical to the name recorded for
the service in the ServiceInstaller.ServiceName property of the
corresponding installer class. In code, the ServiceName of the service
is usually set in the main() function of the executable.
https://learn.microsoft.com/en-us/dotnet/api/system.serviceprocess.servicebase.servicename?view=netframework-4.7.2
To give a different name, I suggest you change the service (if you have the code) to read name from app.config, or any other config location.
You can also change the display name (not the service name) by directly changing it in registry.

windows remote login api using c#/c++

I want to write a program to login into remote machine using domain admin user credential and do following tasks
Get some system information like OS, IIS version, .net version etc
Transfer some files from remote to local machine and vice versa.
Is there any windows API to do this?
Task I am trying to do can done manually using remote desktop application, but I don't want to use GUI as I would like to automate this, to get machine info periodically from our environment and display in a Dashboard.
you can use winrm
and automate it using powershell
https://msdn.microsoft.com/en-us/library/aa384426%28v=vs.85%29.aspx
here is a vb example (you can use the same COM objects in c#)
Const RemoteComputer = "ComputerName.domain.com"
Set objWsman = CreateObject("Wsman.Automation")
Set objConnectionOptions = objWsman.CreateConnectionOptions
objConnectionOptions.UserName = "Username"
objConnectionOptions.Password = "Password"
iFlags = objWsman.SessionFlagUseKerberos Or _
objWsman.SessionFlagCredUserNamePassword
Set objSession = objWsman.CreateSession("https://" & RemoteComputer, _
iFlags, objConnectionOptions)
strResource = "http://schemas.microsoft.com/wbem/wsman/1/" & _
"wmi/root/cimv2/Win32_OperatingSystem"
Set objResponse = objSession.Enumerate(strResource)
While Not objResponse.AtEndOfStream
DisplayOutput(objResponse.ReadItem)
Wend
'****************************************************
' Displays WinRM XML message using built-in XSL
'****************************************************
Sub DisplayOutput(strWinRMXml)
Dim xmlFile, xslFile
Set xmlFile = CreateObject("MSXml2.DOMDocument.3.0")
Set xslFile = CreateObject("MSXml2.DOMDocument.3.0")
xmlFile.LoadXml(strWinRMXml)
xslFile.Load("WsmTxt.xsl")
Wscript.Echo xmlFile.TransformNode(xslFile)
End Sub

netsh interface set interface name="Local Area Connection 2" admin=DISABLED

I want to disable all network adapters:
I saw that it can be done via system command netsh.
When I run the following command i got an error.
C:\Windows\system32>netsh interface set interface name="Local Area Connection 2" admin=DISABLED
Area is not an acceptable value for connect.
The parameter is incorrect.
It seems that the command line parser could not get the spacing. The same happens if using single quote (instead of double quotes).
i need your help gurus.
By the way, my code is C#, I tried many examples from the web and none of worked.
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo(
"netsh",
"interface set interface name=\"" + interfaceName + "\" admin=enabled");
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = psi;
bool result = p.Start();
I use
netsh interface set interface "Local Area Connection 2" ENABLED
it works fine for me and enables "Local Area Connection 2"
probably you might use this way with your code, it should work for you.
echo ----------------------------------------------
echo Restarting network interfaces
echo ----------------------------------------------
netsh interface ipv4 show interfaces
for /F "tokens=1-3,4*" %%I in ('netsh interface ipv4 show interfaces ^| find /I "CONNECTED" ^| find /I /V "DISCONNECTED" ^| find /I /V "LOOPBACK"') do (
echo.&echo - Restarting #%%I [%%M] ...
netsh interface set interface name="%%M" admin=disabled >nul 2>&1
ping -n 1 127.0.0.1 >nul 2>&1
netsh interface set interface name="%%M" admin=enabled >nul 2>&1
ping -n 1 127.0.0.1 >nul 2>&1
if %ERRORLEVEL% EQU 0 (
echo +++ OK +++
) else (
echo --- NOK ---
)
)

Is there a built-in function in ASP.NET to resolve a MAC address from an IP address

I'm about to start developing a simple internal web application to log visitors to my office. Basically, they will read an AUP, then enter their name to indicate acknowledgement. The application will also log the visitor's IP using Request.UserHostAddress(). In addition to logging the IP, I'd like to run a script to make a DHCP reservation with the IP I get from the application, but I need to have the MAC address as well. This application will only be used within one subnet, and I'm planning to use a small DHCP range for the visitors.
Is there any built-in function I can use to resolve the MAC address from the IP via ARP? I'd prefer not to have to use a workaround if there's an existing function.
Okay, for reference, this is what I did:
Private Function get_MAC(ByVal ip As String) As String
Dim proc As New System.Diagnostics.Process
Try
proc.StartInfo.UseShellExecute = False
proc.StartInfo.RedirectStandardOutput = True
proc.StartInfo.CreateNoWindow = True
proc.StartInfo.FileName = Environment.ExpandEnvironmentVariables("%WINDIR%\system32\ping.exe")
proc.StartInfo.Arguments = ip
proc.Start()
proc.StartInfo.Arguments = "-a " & ip
proc.StartInfo.FileName = Environment.ExpandEnvironmentVariables("%WINDIR%\system32\arp.exe")
proc.Start()
Dim output As String = proc.StandardOutput.ReadToEnd
Dim rgx As New Regex("\w{2}-\w{2}-\w{2}-\w{2}-\w{2}-\w{2}")
If rgx.IsMatch(output) Then
Return rgx.Match(output).Value
Else
Return "ERROR No MAC address found."
End If
Catch ex As Exception
Return "ERROR " & ex.Message
End Try
End Function
I then called it with
get_MAC(Request.UserHostAddress())
Note that this only works because the web server is guaranteed to be in the same subnet as the host. Also if you decide to test this with Visual Web Developer, note that the test server will return UserHostAddress as ::1 (the IPv6 loopback address).
But back to the main question: Is there any other way to do it?
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/3435b288-a48f-46cc-b247-0f4c3deb5f89/
" How to retrieve IP and MAC address from DHCP Server using C# "

How to change the registry value of remote system using C#

Hai every one
I am developing an windows application in which i have to block the removable storage devices such as pendrives.I found that its possible by changing the registry value of HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\UsbStor,start value to 4.But the problem is I have to block it on remote systems too.Can any one suggest me how to change the value of registry of remote system using c# with a code or sites where i can i find the code for this.
The .net way is to use Microsoft.Win32.RegistryKey.OpenRemoteBaseKey.
An alternative would be to use WMI. There are lots of examples on Google for reading values; replacing GetStringValue with SetStringValue (or SetDWORDValue, etc.) should do what you want.
You probably want to take a look at the Remote Registry Service and make an RPC call.
MSDN description: http://msdn.microsoft.com/en-us/library/aa940121(WinEmbedded.5).aspx
MSDN example using RegistryKey.OpenRemoteBaseKey: http://msdn.microsoft.com/en-us/library/8zha3xws.aspx
You need to have Remote Registry service running on remote machine. Then you can use WMI to connect the registry. Here is a code sample script from this site:
Dim strComputer
Dim strUserName
Dim strPassword
Dim objLocator
Dim objService
Dim objRegistry
strComputer = "somesys"
strUserName = "somename"
strPassword = "somepw"
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objService = objLocator.ConnectServer( strComputer, _
"Root\Default", strUserName, strPassword )
objService.Security_.impersonationlevel = 3
Set objRegistry = objService.Get( "StdRegProv" )
'Do something here like retrieving or setting values.
Set objRegistry = Nothing
Set objLocator = Nothing
Set objService = Nothing
you can get many valuable results by googling for "using WMI to modify remote registry"

Categories