Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Get Text of Command


  • Please log in to reply
8 replies to this topic

#1 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:01:28 PM

Posted 30 November 2008 - 09:56 PM

Hello all :thumbsup:

Been working on a function which will get the text of any command passed into it. Was wondering if anyone would like to try make a better way of doing it?

A requirement is that the command called is not allowed to show a console, nor is it allowed to print to the running app's console if it's a console app.
EDIT: It must, however, work correctly when compiled for unicode or multibyte support.

Multithreading need not be supported.

Here's my function:

http://pastebin.com/f653ae4ec

If you're interested in the class to which it belongs:
Header: http://pastebin.com/f3b94c52e
Source: http://pastebin.com/f74ad087f

Billy3

Edited by Billy O'Neal, 30 November 2008 - 09:58 PM.

Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

BC AdBot (Login to Remove)

 


#2 jpshortstuff

jpshortstuff

    WhatTheTech Teacher


  • Members
  • 660 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:UK
  • Local time:09:28 PM

Posted 01 December 2008 - 05:24 AM

Hi Billy :thumbsup:

I've only scanned your code briefly, but would I be correct in saying that you are creating a batch file that runs the command and outputs to another file, then reading the contents of that other file?

I have never tried this, but have a look at something called piping. I believe you can open an IO stream to another process (like CMD.exe or Unix equiv) whereby you could send commands to it and receive the output directly. I keep meaning to give this a try but I never seem to get round to it. Perhaps I will give a go this week, if I do I'll let you know.

Hope that helps :flowers:
Trained at the What The Tech Classroom where you too could learn to help others.

My help is free, however, if you wish to make a small donation to show appreciation and to help me continue the fight against Malware, then click here Posted Image

Posted Image

#3 jpshortstuff

jpshortstuff

    WhatTheTech Teacher


  • Members
  • 660 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:UK
  • Local time:09:28 PM

Posted 01 December 2008 - 05:43 AM

We had a bit of this over at 247:
http://www.247fixes.com/forums/Outputing-c...2318#entry12318

Nebon was originally using the same method you are currently going for, using a temporary file to get the result indirectly.
Trained at the What The Tech Classroom where you too could learn to help others.

My help is free, however, if you wish to make a small donation to show appreciation and to help me continue the fight against Malware, then click here Posted Image

Posted Image

#4 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer

  • Topic Starter

  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:01:28 PM

Posted 01 December 2008 - 10:27 AM

Hi Billy :flowers:

I've only scanned your code briefly, but would I be correct in saying that you are creating a batch file that runs the command and outputs to another file, then reading the contents of that other file?

I have never tried this, but have a look at something called piping. I believe you can open an IO stream to another process (like CMD.exe or Unix equiv) whereby you could send commands to it and receive the output directly. I keep meaning to give this a try but I never seem to get round to it. Perhaps I will give a go this week, if I do I'll let you know.

Hope that helps :trumpet:

Yes, you are correct in how that worked.

I tried doing this but it printed to the console when I did, which I wanted to avoid doing. I could terminate stdout and save that to a file, but then I wouldn't be able to print my own messages to the console.

If you're intereseted, you'd need to open a file using CreateFile (Which may be a memory stream I think)
WriteFile would be needed, as well as GetStdHandle and SetStdHandle

Good luck, :thumbsup:

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#5 jpshortstuff

jpshortstuff

    WhatTheTech Teacher


  • Members
  • 660 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:UK
  • Local time:09:28 PM

Posted 01 December 2008 - 10:49 AM

Could you not use fprintf to print your messages to the console? stdout is a file, so when you want to print to the console you should be able to use fprintf.

What do you want to do with this text once you've got it?
Trained at the What The Tech Classroom where you too could learn to help others.

My help is free, however, if you wish to make a small donation to show appreciation and to help me continue the fight against Malware, then click here Posted Image

Posted Image

#6 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer

  • Topic Starter

  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:01:28 PM

Posted 01 December 2008 - 10:54 AM

No.. the problem is if I wish to run the other command silently, I need to redirect stdout to a file or ram so I can read it later. But if I do that, then my own messages will be printed to redirected stdout :thumbsup:

I've got a bunch of uses for this but right now the major one is:
Header: http://pastebin.com/f7b0eb174
Source: http://pastebin.com/f16d09862

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#7 jpshortstuff

jpshortstuff

    WhatTheTech Teacher


  • Members
  • 660 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:UK
  • Local time:09:28 PM

Posted 01 December 2008 - 10:57 AM

Hmm, I see.

If I get round to it I will have a play.
Trained at the What The Tech Classroom where you too could learn to help others.

My help is free, however, if you wish to make a small donation to show appreciation and to help me continue the fight against Malware, then click here Posted Image

Posted Image

#8 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer

  • Topic Starter

  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:01:28 PM

Posted 04 December 2008 - 10:42 PM

How's this?
http://pastebin.com/f6894f6bf

:thumbsup:

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#9 jpshortstuff

jpshortstuff

    WhatTheTech Teacher


  • Members
  • 660 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:UK
  • Local time:09:28 PM

Posted 05 December 2008 - 02:27 AM

Looks good Billy :thumbsup:

You obviously had time for a play before I did.
Trained at the What The Tech Classroom where you too could learn to help others.

My help is free, however, if you wish to make a small donation to show appreciation and to help me continue the fight against Malware, then click here Posted Image

Posted Image




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users