Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading PuTTY output with c#

Tags:

c#

io

windows

putty

What i want: I want to read the Output of the PuttY Window with C#

What I've got: Our company has several hundreds of servers with at least 2-3 technical users (which are running applications). We got a database of all the users and passwords. So it's basically search, copy and paste to login.

What i want to do: Write a piece of software that does this automatically.

How far i am: Wrote a tool, that reads the logfile of a single PuTTY instance and looks for the password prompt. Determinates target user and server (based on current server and su - [username]). Retrieves the password and sends it via PostMessage to a selected PuTTY.

The Problem or what i want to change:

I want to be able to read the PuTTY output directly from the PuTTY window, because the logfile is kind of unreliable (TAB, ESCAPE, etc which scrambles the output). I have used UISpy and other tools to get a control, but no luck.

I don't want to use a keylogger mechanism or something like that.

Maybe a hook or something, but have never done that before.

Additonal Info:

  • Connection is made over a JumpServer, only ssh connections from there to the target servers are allowed.
  • Direct login with the tech. user is disabled.
  • The PuTTY window is already opened and used to work on the server.
  • Writing a whole new Connection Manager is no option. Coworkers are familar with PuTTY and this solution should be some sort of standalone background worker.

Additonal Info 2: The goal is to write a strict 3rd party software. Not to use other SSH libs, modify PuTTY source or other approaches. The question is: how to read text from the PuTTY window, beside the logfiles.

like image 223
mazer Avatar asked Oct 15 '12 08:10

mazer


2 Answers

I don't know why this hasn't been suggested yet, but plink (which is part of the PuTTY suite) is the command-line version of PuTTY, you'll just need to redirect stdin and stdout to get a relatively powerful (as in features, you'll still need to interpret telnet stuff yourself) SSH client.

like image 93
Aurelia Avatar answered Oct 12 '22 22:10

Aurelia


Have you considered using OCR?

Sketch of solution would be:

1 - Agent runs waiting to notice a Putty Window (either register a callback with OS for new processes or periodically check the list of running processes)

2 - When a putty is noticed, agent takes screenshot and extracts portion of screen occupied by putty. You would need to extract window location, but can be done via OS calls assuming you have a handle from step 1

3 - Pump this image data into tesseract or something, and get text output back. check to see if the password prompt is there

4 - If prompt is there, it sounds like you had the rest done after this (send info needed via PostMessage)

like image 20
davec Avatar answered Oct 13 '22 00:10

davec