-1

Description

At the moment I am trying to call "nmap" through C# Process.Start(). But I can't get it work with my desired input arguments. At Process.Start() it seems to make a false parsing of the arguments. Do you know how I have to write the ArgumentList that my command is running right? In the commandline the same command works like charm...

Thank you!

Example

My simple example: (Edit: Suggestion of Lei Yang)

using System.Diagnostics;


namespace MyApp // Note: actual namespace depends on the project name.
{
    public class Program
    {
        public static void Main(string[] args)
        {
            using var myprocess = Process.Start
            (
                new ProcessStartInfo
                {
                    FileName = "/bin/zsh",
                    ArgumentList = {"-c nmap -p 8000-9000 -oG - 192.168.178.37/24 | awk -F'[/ ]' '{h=$2; for(i=1;i<=NF;i++){if($i==\"open\"){print h,$(i-1)}}}'" }

                }
            );

            myprocess.WaitForExit();
        }
    }
}

Output of this command:

zsh: bad option string: '-c nmap -p 8000-9000 -oG - 192.168.178.37/24 | awk -F'[/ ]' '{h=$2; for(i=1;i<=NF;i++){if($i=="open"){print h,$(i-1)}}}''

Desired output:

192.168.178.1 8089
192.168.178.1 8181
192.168.178.1 8182
192.168.178.1 8183
192.168.178.1 8184
192.168.178.1 8185
192.168.178.1 8186
192.168.178.43 8080
  • 1
    nmap doesn't know what "|" means, pipes require support from the command processor. You'll have to execute cmd.exe /c nmap etcetera – Hans Passant Feb 28 '22 at 13:59
  • The script is running on linux. If I write the same command in the command line nmap has no problem with the pipe.... There is only a problem at parsing with the C# Process() class... Could the problem be the spaces between the arguments? – Alexander Wagner Feb 28 '22 at 15:14
  • try use `bash -c 'nmap ...'`, in C# `FileName = "/bin/bash"` – Lei Yang Feb 28 '22 at 15:23
  • You mean? ` FileName = "/bin/bash", ArgumentList = {"-c 'nmap'"}` Then I have this output /bin/bash: - : ivalid option – Alexander Wagner Feb 28 '22 at 15:37
  • try ArgumentList = {"’-c nmap ... '"}` [ref](https://unix.stackexchange.com/questions/144514/add-arguments-to-bash-c) make sure single quotes are around the parameters? – Lei Yang Feb 28 '22 at 15:43
  • 1
    Please change your question to show the code you are currently trying. `nmap` does not understand pipes, you mush use `/bin/bash` as the process and then pass the proper arguments to it. – NetMage Feb 28 '22 at 21:54
  • @NetMage: I changed my question to the suggestion of Lei Yang. I try to only call "nmap" without any parameters...just for testing. – Alexander Wagner Mar 01 '22 at 16:39
  • `ArgumentList` is a collection of `string`s, not a single `string`. See the [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.argumentlist?view=net-6.0). You need to pass in `ArgumentList = { "-c", "'nmap'" }` – NetMage Mar 01 '22 at 17:15
  • Have you tried to use or to have a look about this nmap wrapper in c# ?https://github.com/thomdixon/SaltwaterTaffy – Gambi Mar 01 '22 at 17:23
  • The code in the example is difficult to follow and should probably be cleaned up. It's not clear why you have 2 commented out lines with `ArgumentList`. The try/catch can be removed; it's not doing anything as it just rethrows any exception. – Reilly Wood Mar 02 '22 at 06:32
  • @ReillyWood Sorry I did not mean to make you angry. Now I deleted the not nessessary lines I hope i fullfill your requirements. – Alexander Wagner Mar 02 '22 at 15:13
  • @Gambi thank you for the link I have already seen the wrapper. Maybe I would try it, but the last change is about 5 years ago... Maybe the wrapper is deprecated... – Alexander Wagner Mar 02 '22 at 15:15
  • @NetMage Thank you your suggestion works! I will update in a short moment! – Alexander Wagner Mar 02 '22 at 15:22

1 Answers1

0

With the help of you I could solve the problem. Thank you!

Solution for someone with the same problem:

Variant 1: Direct way:

using System.Diagnostics;


namespace MyApp // Note: actual namespace depends on the project name.
{
    public class Program
    {
        public static void Main(string[] args)
        {
            using var myprocess = Process.Start
            (
                new ProcessStartInfo
                {
                    FileName = "/bin/zsh",
                    ArgumentList = {  "-c",  "nmap -p 8000-9000 -oG - 192.168.178.37/24 | awk -F'[/ ]' '{h=$2; for(i=1;i<=NF;i++){if($i==\"open\"){print h,$(i-1)}}}'" }

                }
            );

            myprocess.WaitForExit();
        }
    }
}

Variant 2: Detour: Shell script (Maybe the easier way) C#:

using System.Diagnostics;


namespace MyApp // Note: actual namespace depends on the project name.
{
    public class Program
    {
        public static void Main(string[] args)
        {
            using var myprocess = Process.Start
            (
                new ProcessStartInfo
                {
                    FileName = "/bin/zsh",
                    ArgumentList = {"/home/alexander/tst.sh"}
                }
            );

            myprocess.WaitForExit();
        }
    }
}

Shell script:

#!/bin/zsh
nmap -p 8000-9000 -oG - 192.168.178.37/24 | awk -F'[/ ]' '{h=$2; for(i=1;i<=NF;i++){if($i=="open"){print h,$(i-1)}}}'

Output of both variants:

192.168.178.1 8089
192.168.178.1 8181
192.168.178.1 8182
192.168.178.1 8183
192.168.178.1 8184
192.168.178.1 8185
192.168.178.1 8186
192.168.178.43 8080