Click here to Skip to main content
15,890,946 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am trying to get input command from shell Window (not output result). Below is the scenario.
I am trying to get this info using C#, but if it is not possible with C#, any other language like C/C++ will also work.

1- User enter some command in Shell Window.
Command like dr/cd/mkdir etc
Shell window like Command prompt/ PowerShell
2- Whenever user press enter, capture the command which he executed and log it.
3- I need a consistent approach which work on all shell window. There can be more shell windows apart from cmd and powershell.

What I have tried:

1- I tried using keyboard press. Using Keyboard press, I am able to capture key detail but user can use some shortcut keys. so this will not work

2-Trying to get data from Window title. This work for Command prompt but not with powershell.

3- Tried Window API like "getWindowText" but it is giving window Title. I suppose ,there are thousand of WinAPI. If anyone can narrow down the give some suggestion on WinAPI methods. That will also work

Please help !!
Posted
Updated 25-Jan-18 0:04am
Comments
johannesnestler 25-Jan-18 10:26am    
I don't think this will work - no Windows-API function will help here. Key-Logging with a global hook, monitoring target application (Shell) and knowledge about possible shortcuts each target application has could work to some degree. Anyway - I would be just interested what kind of benefit or feature is behind your requirement? At the moment I can not imagine any use for such all "commands" logger for all Shell-Kind applications. What you do then with the logged commands - they will always be application specific.... so in useless without further Knowledge of the Shell and it's commands. Maybe one could help if you describe your target?
saurabh.15in 31-Jan-18 8:36am    
I am trying to create a type of recorder. But taking care that recorder is not capturing sensitive data.
So can i assume,it is not possible?

1 solution

That is not posssible. You can't know what other applications (and these shells are basically just applications) do with there input. All you can do is tracking keyboard and mouse events globally.

But that (and what you are trying to do) are sniffing operations which might be abusive (e.g. for sniffing account information and passwords). Therefore, there should be no public solutions here.

[EDIT]
Quote:
I am trying to create a type of recorder. But taking care that recorder is not capturing sensitive data.
So can i assume,it is not possible?

The command prompt and the PowerShell are recording the history already internally but I don't know of a way to get the history programmatically and I think that it is not possible due to the mentioned reasons.

In a command prompt window press F7. A window will be opened showing the history and you can open a context menu to select all and copy it to the clipboard.

With the PowerShell use Get-History[^] to show the history and optionally save it to file.

So you might try to send corresponding keystrokes to those windows (see How to: Simulate Mouse and Keyboard Events in Code | Microsoft Docs[^]) and get the data from the clipboard resp. the saved file. But I'm not sure if that works for those shell prompt applications.
[/EDIT]
 
Share this answer
 
v2
Comments
saurabh.15in 31-Jan-18 8:36am    
I am trying to create a type of recorder. But taking care that recorder is not capturing sensitive data.
So can i assume,it is not possible?
Jochen Arndt 31-Jan-18 8:58am    
See my updated answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900