| Steven's profileLunatic ExperimentsPhotosBlogLists | Help |
|
October 25 About The Powershell Host ObjectI got the following query in one of my Google hits today: "pipeline.Invoke() "Read-Host" Cannot invoke this function because the current host does not implement it." I believe that this query was in reference to the lack of host functionality in the default settings for a runspace in Windows Powershell. I don't recall ever actually taking much about this, yet I am known for having abstracted the runspace API for use as a multi threading interface in Powershell. Now is a good time to talk some about this. A runspace in Powershell is a big blob of information that pertains to a particular shell within Powershell. The runspace has several components within it, such as the cmdlets, aliases, functions, providers, etc. The host component specificly is ment to be used as an abstract interface to the console. The default host object used by a runspace doesn't have much functionality, so you may encounter error messages like the one above when you are using a generic host object. It is possible to tell Powershell what host object should be used when the runspace is initialized. I do know that version 1.0 of powershell.exe will use a host that writes to the console used by the powershell.exe process. Since most other instances of Powershell don't have a console the host object will often be the default host object, that doesn't provide any functionality on its own, or a very simple host object, using only implementing a few methods in the PSHostUserInterface class. From the query above it looks like this googler was trying to use Read-Host within an asynchronous runspace. The easy solution to this is to just not do so. It's easier, in most cases, to simply pass the needed information into the asynchronous runspace from the parent runspace. However, there are still some cases where you must use Read-Host from an asynchronous runspace. In that case you'll have to code and compile your own host class, and use that when initializing the runspace. October 22 Powershell, String Encryption, and GPGI finally added support for GnuPG to Library-StringCrypto. The previous functionality to encrypt strings in process is still present and continues to work as expected. Not a couple more parameters have been added to support symmetric and asymmetric encryption using GnuPG. The new parameters are 'gnupg', to enable use of gpg.exe, and 'recipient', to identify the person the encrypted message is for. When using GnuPG passwords/passphrases may be supplied in 'password' parameter or via the console to the gpg.exe process. To use symmetric encryption, with GnuPG just add the gnupg switch parameter. Write-EncryptedString message secret -gnupgYou can also omit the password from the command line and enter it interactivly. Write-EncryptedString message -gnupgTo use asymmetric encryption, omit the password and use the recipient parameter. Write-EncryptedString message -gnupg -recipient 'John Doe'Write-EncryptedString returns the ASCII armored version of the ciphertext. Read-EncryptedString will be able to detect the ASCII armor and automatically process it using gpg.exe. Decryption should just work as expected. The password parameter can be used for both symmetric and asymmetric encrypted messages. Also the passphrase may be interactively entered via the console to gpg.exe. Usage notes: The command 'gpg' is expected to resolve to gpg.exe, so you may need to add a path entry or alias gpg to wherever your copy of gpg.exe is. When the password is given via the password parameter, it is passed to gpg.exe as the first line of its standard input. This means the password will not show up in its command line. This also means that passphrases are limited to one line. No check is performed to ensure the password is not longer than one line. If the passphrase is longer than one line then only the first line will be used as key material, the rest will appear at the start of the decrypted message. Some cryptanalytic stuff: Using GnuPG via this script should be no less secure than using GnuPG by any other method in Powershell. However, because this involves communication with another process, using GnuPG does have a larger attack surface for side channel attacks than simply performing the encryption using the CLR. i.e. Either your installation of the .NET Framework or your installation gpg.exe may be compromised, but gpg.exe alone being compromised does not effect the .NET Framework. Download Library-StringCrypto.ps1 here. October 15 Continuing ACTA Nonsense Every time I hear about ACTA I become violently sick. The fact that it continues to be "negotiated" in secret makes it a violation of the sovereignty of any government that would sign it. I dearly hope that it never goes into effect. August 28 Crond Appears To Hang In XenI recently decided to spend the money to buy a home server. (I got an Atom 330 and a 64GB SSD in case you were wondering.) In the name of security I decided to use Xen to separate the applications that would be running on it. So I installed OpenSSH to control it remotely. Then, I installed Xen. Finally, I initialized a new block device and installed an operating system to act as the first guest OS on the machine. I booted up the guest OS and everything was working fine. That is, until after crond (the equivalent of the task scheduler if you only know Windows) started up. I saw the crond boot message and that was the last of it. Nothing after that. Not even the login prompt. While the reason to this seems obvious to me now, I became quite confused at that time. So I asked Google to find a solution for me. It would appear that quite a number of people had the same problem. I read a few questions about it on forums but not many responses gave a working solution. I then found a couple of blog posts that did work, but I don't quite understand how they worked. http://www.nulynx.com/xen-boot-hangs-at-crond/ http://shell.burgas.org/2009/06/debian-xen-domu-hangs-at-crond/ For a default installation of Debian 5, placing extra = 'console=hvc0 xencons=tty into the guest's configuration file worked. Then it broke when I wanted to use a custom configured kernel. When I changed the kernel, I was back to the original problem.That whole day I worked my brain to figure out what was wrong while I was at work. Why did the default installation not work? Why did that configuration change make it work? Why did it not work when I used a different kernel? I realized the simple truth is that the console you see in xm is not tty1. The Xen console it not a tty at all. It's a separate device called hvc0. The reason why I did not see a login prompt is because getty is only setup to for ttys, and my custom kernel must be interpreting those arguments in a dissimilar manner. So the solution was to simply get getty to start up on the hvc0 device. That was something I knew how to do from experience. Edit the inittab file! Add this line: hvc:2345:respawn:/sbin/getty 38400 hvc0 That's the same line as is used for tty1 with the id and device name changed.Now the system is setup explicitly to startup getty on the xen console, and no need for any hackish kernel arguments. The ttys are actually devoted to interacting with the virtualized frame buffer. If you are not using the vfb you may as well comment out the tty getty lines, but leave them in there if you do intend to use the vfb. June 23 Virtual Memory Management In .NET The hit counter for my blog has just the 10,000 hit milestone today. I had planed to release a comedic themed encryption library for Powershell that shows how some of the functions of GPG can be imitated in Powershell. However, I have not yet finished that library. Instead, I have decided I would release a virtual memory management library I wrote in C#. One of the projects I'm working on right now required a better understanding of the CPU architectures used today. This inevitably lead me to want to be able to use ASM in C# code. So I decided to start studying NASM and ASM in general, and then found that the .NET heap has execution protection. I then found that I can control the protection bits by calling the virtual memory management functions in the kernel32.dll library. This library is a .NET wrapper for those functions. The library is documented, but I don't have any example uses that I can release at this time. While I don't have any examples to release today I can say that this library is useful for executing either precompiled or dynamically generated native code. The library is released under the GNU General Public License. VirtualMemory.7z On the topic of ASM: I have read a few negative comments in the past about how well the .NET Framework is able to optimize the native code result of an assembly, particularly in the use of SIMD instructions. I feel I should comment on my experience while developing this library. I tested the execution time of two algorithms written in both NASM and C#. The execution time of the first algorithm I tested was ~10 times faster in NASM than what I got from the C# version. On the other hand, the execution time of the second algorithm I tested was only ~10% faster in NASM than in C#, and only after a couple hours of work to optimize the NASM code. According to my calculations, the speed result of the second algorithm in C# came very close the theoretical limit of my CPUs capacity, close enough that there would be no way to achieve that speed without the use of wide SIMD instructions. This indicates to me that the JITter in .NET Framework is capable of using SIMD instructions properly, while the same JITter may not always understand the algorithm being JITted. This is actually completely inline of my expectations for any compiler. June 10 "Detainee Photographic Records Protection Act of 2009" I just found something that has reminded me that the U.S.A. is not a democracy. I just discovered that section 1305 of H.R.2346 has the explicit purpose of allowing the Secretary of Defense the unrestricted right to prevent the publication of any photograph taken by US Armed Forces "relating to the treatment of individuals engaged, captured, or detained after September 11, 2001," and this right can not be circumvented by the Freedom of Information Act. From what I can see, section 1305 was removed from the bill by the House of Representatives, however, passed by the Senate. It is argued that the events of September 11, 2001 occurred because of an anti-American mentality. It is clear by the date used that the bill is designed to censor evidence of wrong doing by Bush Administration. Condoning actions made by a previous administration that would reflect negatively against the U.S.A. can only perpetuate this anti-American mentality. I can only suspect that anyone that attempts to prevent the publication of the evidence of any act condones the act represented by the evidence. In summery, I can not see how section 1305 of H.R.2346 could benefit the U.S.A. so I am explicitly stating my dissent to this bill. I hope that anyone with the power to stop ratification of section 1305 does so, and that whoever wrote it is removed from D.C. I am including a copy of section 1305 for your information.
June 01 Google Wave This is a must see. http://wave.google.com/ This is the best collaborative software that I have seen. The best thing is that it will be free to use and organizations will have the ability to set up their own private wave servers. I am very exited about this and, as you might already suspect, I do have plans for this once it moves into the public. May 15 Powershell and String Encryption and Compression Today, I went back over the scripts that I use in Powershell to perform encryption to give them and update. I wanted to have a simple script that will perform encryption over a string, using a string as a password, and return a string, because strings are easy to manage in a shell. (I had been using a really complex but really awesome object based encryption script.) Before I started I took a look at what was available on the web. As it turned out there isn't much out there and what is out there is garbage. In particular I saw http://poshcode.org/116 with it's hard coded salt and weak IV selection. It's key schedule as a whole is worthless. So I open up PSPad and start typing. About an hour of research and 104 lines of code later, I now have a string encryption script that better adheres to cryptoanalytic recommendations, is easier to use, and even supports compression before encryption. I'm normally used to being the first to release any script of any specific genera and don't have any reason to speak down on any other script in that genera. However, in this case, seeing as how poorly the predecessor handles key information, I have to strongly recommend switching to my script as soon as possible if you have been using the script linked to above. To be serious now, since you're data is important enough to encrypt, I would bet you would be rather upset if someone managed to crack your password when your encrypted data does get leaked. The cryptanalysis of my script is quite simple. The stream cipher is Rijndael, and the key schedule is RFC2898 with a random 256 bit salt that is tacked on to the cipher message(just as it should be). Don't understand all that? Don't worry. The only weakness here will likely be the passwords you use. I'm sure you have heard it a hundred times: make passwords that are hard to predict and change passwords as often as possible. You can make the password as long as you want and use any characters you want. Also don't store passwords anywhere someone else might see them. For further cryptanalysis look up Rijndael and RFC2898. I had to add in compression because the moment just before encryption is the last chance you get to do compression. Not only is it the last chance, but generally also the best time to perform compression. Compression works best on uncompressed data and when there is a great amount of it. Compression also will not work at all on encrypted data. Beware that compression will not work for very small parcels of information. You'll want a string of at least 1500 characters before you can get any benefit out of using compression. Library-StringCrypto.ps1 I can't imagine any uncool way to use this so if you find a use for this I would like a comment here or on Twitter @aitsusan so I can hear about it. Important Update: I have already rewritten this script. The changes are wide enough to make the old version and the new version incompatible, but I'm going to use the same script name anyway, because I feel this update to be very important for the security of any person that uses this script. If you have Library-StringCrypto.ps1 version 1.0 please download version 2.0 now. The change is an addition of an HMAC. What this means is that now the script can more effectively detect any corruption of modification to the encrypted data. In version 1.0 no direct method of detecting corruption was implemented. This raises a potential security hole where an attacker could modify the cipher string in a way that would result in garbage being returned in the decrypted string, and not having any exception thrown. The lack of a HMAC before could have caused a script, that did not on it's own perform any authentication, to reveal some information about the encrypted data to an attacker or could have caused the script to otherwise behave in an unexpected manner. Now, with the HMAC in place an exception is thrown before any of the data is decrypted, none of the remaining encrypted data can be leaked and the script should then terminate. March 26 Coolest Thing I Have Seen In Powershell In A Long Time Check out this blog post from the Powershell Team. It shows how you can make variables in Powershell behave as global static properties. Be sure to grab the New-ScriptVariable script. Note that it uses the Add-Type cmdlet available in Powershell 2.0. If you don't have version 2.0 then Add-Type can be replaced with my New-CAssembly script. It's a really easy conversion. In fact I already converted it and is available from my SkyDrive. December 07 Return Of The PSGhost I have just released version 2.0 of PSGhost. Internally PSGhost has many changes, but its behavior is otherwise not changed. The biggest change is that commands are no longer invoked within PSGhost. It interprets its command line arguments as being a command for Powershell.exe. PSGhost encodes the command and hands it over to Powershell.exe, and prevents the console window from appearing. A few benefits of this is that the binary is much smaller, down from 16KB to 5KB. Also, the memory requirement has been significantly reduced. I'm not sure why, but, when loading System.Windows.Forms.dll and showing a simple message box, version 1 of psghost.exe used about two to four times as much memory as powershell.exe. Another thing is that since commands are invoked within a hidden powershell.exe process any console applications called by those commands are also hidden. Version 1 was not able to hide the console windows of console applications used by psghost scripts. The one drawback is that since powershell.exe is used as the actual host then there is no direct way to know whether the console window can be used for user input. Any command going into PSGhost must not attempt to use either the console or the powershell host object to get user input, but must know beforehand that the only way to get user input is to create a GUI using WinForms or WPF. There may still be some room for improvement in PSGhost. Version 2 doesn't use the same profile that was used by version 1. PSGhost definitely has the potential to perform some pre processing on the command to do things like use a separate profile. Otherwise I see version 2.0 as being the completion of the project. The original objectives have be completed. PSGhost can be downloaded from SkyDrive or from my website. November 14 Of Bittorrent & Publisher Signatures As Bittorrent becomes more and more distributed over time I become more and more concerned about the loss of the publisher's identity. Now a few Bittorrent clients have the ability to search for .torrent files over DHT. This completely removes the publishers identity form the torrent and leaves the user to guess about where the .torrent came from and whether it is legit. Something I would suggest is to use a PGP/GPG key to sign the info section of the .torrent file. The resulting signature could then be embedded into the .torrent file as a signature section. Then any client that has the publishers public key and a supporting client would be able to prove with a high degree of certainty where the .torrent originated. This would be a very user friendly way to sign .torrent files, and would also be compatible with non supporting clients and intermediary software since only the info section needs to be maintained to ensure function of the .torrent file. Any software that doesn't support the use of signatures can simply ignore or even delete a signature section of the .torrent file and clients would still be able to open the .torrent and download the content. Trackers and indexers could also support the use of signatures. I would like to be able to search by signature ID, and see signature IDs or UIDs on torrent summery pages. I'm not sure if I would implement this myself since the field is already rich with clients; if I made a client just to test this idea it would not be able to compete with the likes of uTorrent and Vuze. However, if I did persue implementation on my own it would likely first take the shape of a real specification for how signatures are to be embedded into the .torrent files, and then possibly a simple application that would sign and validate .torrent files. October 11 Televised IgnoranceRecently, a person with great influence and is taken seriously by way too many people has made an extremely ignorant statement on national television. It was funny for the first 5 minutes after I saw it. I'm not laughing anymore and, in fact, I'm getting quite annoyed. What you see on television is not factual information! 25% of it is advertising, and the rest of it is either (usually not very good) fiction, or fiction sold to you as fact. It's just entertainment! The not so funny part about it is that you're made to pay for it even when at least 25% of the time you spend watching television you are watching an advertisement! Stop taking things you see on television seriously! October 08 POSH URI 2: The Command! In my last post I talked about using a URI to invoke a Powershell script. I didn't want to get into the details immediately because I didn't want people to follow to closely and get themselves into a position where they could fall into a clever trap. Before posting any details I wanted to be sure that what I did post was going to be resistant to any code injection, since any URI that could exploit any bugs could be on any web page on Earth. Today I'm talking specifically about what command is to be associated with any "PoSH" URIs. One thing that was a big limiter here is that powershell.exe doesn't have any way to explicitly separate code from data. Using Powershell I had to find a way to place the data (i.e. the URI) into the code either without the data ever being parsed or, even if the data is parsed, it is not executed. The way I accomplished that is to place a return statement at the end of the command, followed by a hash mark(#), followed by the URI.
That command makes it so that in 99.9999% of cases the URI is not even parsed by powershell. Powershell thinks that the URI is a comment. In the other 0.0001% of cases the URI may be malformed in a way that it would insert a new line character into the command(yes, I managed to do that) and powershell would then begin parsing the rest of the data. However, we are still safe since powershell.exe will close as soon as it progresses to the return statement.When it comes to the Invoke-Poshuri function, you're on your own. I have not even finished my own yet. But if you do decide to place the above command into the appropriate locations in your system registry, be careful of the behavior of the associated function. I don't want to hear about someones computer going haywire because they navigated to a page that had a malformed URI hidden in a javascript file. As an additional note: I have considered making a powershell host that doesn't treat its command line as code. It would be a simple host that would read its command line and hand it over to some preconfigured script. That would simplify the above problem and may also have other applications. If anyone has any reasonable doubts about the security of the command shown above, or would be interested in having a powershell host like the one I just suggested then just speak up. September 22 "Posh" URIs I'm now in the middle of another lunatic experiment using Powershell. This time I have entered a few registry values to associate the URI schema "posh" with one of my Powershell scripts. Whenever a "posh" URI is invoked on my machine that script receives the URI as its first argument. At this point I believe that I have the process of getting the URI to the script wrapped up fairly well. There shouldn't be any passibility of code injection without being able to alter my registry or the script that ultimately receives the URI, and if someone did manage to pull that off then simple code injection would be the least of my worries. Getting past the issue of safely transporting the URI to the script I can only think of a single caveat: I don't have any way to know for sure where the URI came from. Since I don't know where a URI comes from then I don't know if the source of the URI is authorized to be able commit any actions that are associated with the URI. My solution to that caveat is simple, it allows me to know what generated the URI, it allows me to keep the identity of the object that created the URI to remain anonymous, and, since I know what generated the URI, I can be fairly certain about where the URI invocation is coming from. The idea is to invoke some script on demand and have it generate a new GUID. The script would then generate a series of URIs using the new GUID. (e.g. posh:4fdcd7f0-7b1c-4c82-979e-7d0fa2b4bb0f:args) Then, the script would store those URIs somewhere they would be of use, like in an html file. Finally, the script would register the GUID, a scriptlet that would handle URI invocations with that GUID, and a DateTime of when the GUID should expire. In the case of generating an html file the script may invoke the html file before completion. When the scriptlet that is associated with the GUID is invoked, it can be certain that the URI in someway come from where ever the original script stored the URIs. What is not certain is whether or not the URIs have been copied away or if the arguments were modified by someone unauthorized. If the arguments need to be protected then they too could be made anonymous by the use of GUIDs. (e.g. posh:4fdcd7f0-7b1c-4c82-979e-7d0fa2b4bb0f:20075295-9b78-47da-8ccf-3320db848ccf) The only thing that remains is that valid posh URIs could still be copied from a location where its use is valid to a location where its use would be invalid. The only prevention against this is the expiration date on the GUIDs and the extreme improbability that a GUID may be invoked in an unauthorized manner before the GUID expires. There may also be some use of permanent named scriptlets. In an intranet setting links could be placed on a company web site that invoke scriptlets that invoke local applications relevant to that page, and administrators would be able to easily add the necessary registration information to all machines on the network. Web sites like the script repository could release scripts that help with the download and resigning of scripts from the repository; the script would be associated with some posh URI and the script repository could place special links near each script displayed. This may not be a big deal to everyone but this makes it easier to do more advanced local system management using a simple web interface. August 30 CSharp and Powershell: Take Two I have been using an updated version of New-CAssembly for some time now. To be exact I have been using a Cmdlet version of New-CAssembly for time time now. The cmdlet version improves performance and adds features. I would release the cmdlet version, but I haven't wanted to polish it off yet, partly due to not wanting to document all features, and partly due to there already being an equivalent cmdlet in Powershell V2.0. I might not release the cmdlet, but I will release the updated script. The update includes 1 bug fix, cleaner code, and the ability to specify the compiler version. To maintain compatibility with scripts already written for New-CAssembly it defaults to using the classic compiler, but just set CompilerVersion parameter to v3.5 and suddenly you can use LINQ! Compiler v3.5 should be compatible with v2.0, but I didn't want to risk breaking any mission critical scripts because of some subtle redbit that I didn't know about. You should be able to change the default value yourself from v2.0 to v3.5 on your own without too much fear. I was going to give a few examples of the advantages of being able to use extension methods in post compiled code, show how much easier it is now to use LINQ, and how much fun it is to play with LINQed lists(really they are like magic), but that was going to turn into a very long winded post. A person could go on forever about LINQ and extension methods, but we already know how great that stuff is because we use Powershell and we love the pipe! Link To New-CAssenbly.ps1 May 27 Of Powershell And Compiled EventsAfter writing my last post I realized that I didn't have a script that would simplify the process of generating event handlers down to a single line task. I quickly wrote a script that wraps C# method code into a delegate. The script allows delegates of any signature to be created. The result has four parameters, three of witch have default values, and only 17 lines of actual code. This script should be fairly easy to use. The default values are set to use the EventHandler delegate. I chose this type of delegate because Powershell script blocks can be cast to EventHandler. I wanted to be able to easily generate a compiled delegate and be able to combine it with a script block. Here is an example of its use.
Simple! Right? Think of the compiled delegate as if it were a script block that runs really fast. You can also make delegates of different types.
Here's the cool part. Combine a script block with an event handler to make a new event hander.
There is also a using parameter. Add any namespaces that you want to use there. The default is 'System'. Now, take it, have it, play with it, enjoy it, and remember, the key making this useful is to keep in mind that the result of this script is a reusable function that can do something that is hard much faster than an ordinary script block. Now go find an interesting use for this and post about it! UpdateI expanded the script so that the delegate can receive an object from the script that made it and also be able to remember past invocations. Script blocks store all their context information as variables, but that's not so easy in this case. Here there is no guarantee that there is even a Runspace associated with the thread that invokes the delegate. Now you can provide an initialization object, optional initialization code, and define the types for any values that should remain present between invocations. PS> $eh = get-cdelegate 'context0++; Console.WriteLine(context0);' -contexttypes int -initobject 50 -initcode 'context0 = (int)init.BaseObject;' PS> $eh.invoke($null,$null) 51 PS> $eh.invoke($null,$null) 52 PS> $eh.invoke($null,$null) 53 PS> $eh.invoke($null,$null) 54 May 26 Powershell and Events in WPFI have seen this a few times in my Google referrals. People want to know how to how to add events to PresentationFramework controls in Powershell. May 25 About the "Anti-Counterfeiting Trade Agreement" proposal A document titled "Anti-Counterfeiting Trade Agreement" has just come to my attention. I have never been any more infuriated since I read one particular document that suggested that the department of Homeland Security should have to report every instance of copyright infringement they find to the RIAA! I feel as if I had just watched one of those NWO conspiracy videos. I hope that the many ambiguities in this proposal can be removed because the truth is that all things will be at sometime abused and I can only see an end to the "innovation and creativity" that this document claims to protect when this document becomes subject to abuse. The overall spirit of the document is counter piracy. However, what is being ignored, and has been ignored since the founding of the RIAA, is that a person that intentionally attain a product illegally originally intended to not attain the product legally. That is to say that a person has proven that they can not be considered a potential source of revenue once they have proven to a pirate. It can be further stated that a pirate, in the purely intellectual property sence and only in the intellectual property sence, has technically done no harm as there has been no loss. That does not go to say that a pirate can not cause harm. e.g. Distributing faulty medical equipment(a fake copy of such equipment) will definitely cause harm, however that goes beyond piracy. In short, piracy itself, that is document suggests to place so much resource against, is little more than an annoyance. There is one line that I, even as I am an IP rights holder, particularly do not like. It suggests that an institution be founded with the right to persue legal action against anyone it sees as having infringed on any IP right prior to and without the consent of the IP right holder. Can someone explain to me how any such institution is to know who is authorized to distribute any product without first consulting the rights holder? I like the idea that my copyrights could be protected on a global scale. I don't like anything else about this proposal. This proposal needs to be rewritten! I don't want anyone harassing my, or anyone else's, customers! May 09 Visual Design in JavaScript I would just like to say this is really cool. It makes me want to start focusing on JavaScript again. Go ahead. Click the link and check out the examples. The script Processing.js is actually a go between a visual design language called Processing and the HTML canvas element. April 27 WPF and PowerShell: Updating a window in real time!As you will see the last WPF and PowerShell example is a little over complicated when used with the currently released version of Library-PresentationInterface, but I will likely update the library at some point and then scripts like this could then be made more concise. To make the script easier to read I will list the few basic steps taken by the script.
I have used a modified version of this script to monitor the log file of an IRC chat room in real time, effectively making the resulting window another chat window(minus the ability to respond). Any amount of information could be added like other chat rooms, RSS feeds, or any other information you can get your hands on, and any filters can be added to that information. Requirements:
|
|
|