Cygwin

 < Day Day Up > 



VMware is a great tool for running multiple operating systems from the same Windows- or Linux-based host, but for those who want to have the best of both Windows and Unix worlds, Cygwin might be a simpler, less expensive alternative. Cygwin is a free Unix subsystem that runs on top of Windows and serves as a shim for Unix system calls to work correctly on a Windows platform. Cygwin uses a single dynamic-link library (cygwin.dll) to implement this subsystem, allowing the developer community to create “Cygwin-ized” Unix tools that use the DLL to run on Windows. In fact, most of the time the transition between Unix and Cygwin is transparent or handled automatically by configuration scripts. Imagine running vi, bash, gcc, tar, sed, and other Unix utilities while still having the power of Windows to run your favorite video game. While some organizations will port these applications or variations of these applications to a native Windows API, Cygwin mostly obviates the need to recode an application.

For system administrators and network professionals, Cygwin is the best alternative to getting some of the more important Unix utilities for system analysis (md5sum, strace, strings, and so on) onto a Windows box.

Download and Installation

The Cygwin environment and its associated tools are all freely available under the GNU General Public License. Begin the installation process by going to http://cygwin.com/ and downloading the setup program (setup.exe). The setup program downloads the files it needs from a Cygwin mirror site of your choice and installs them into a default location. You can choose between Hypertext Transfer Protocol (HTTP), File Transfer Protocol (FTP), and Rsync download methods.

You will be asked a few questions, such as whether or not you want the text files generated by Cygwin applications to be in DOS or Unix format. DOS file lines end with a newline and a carriage return while Unix file lines end with the newline only; if you’ve seen ^M characters at the end of your text files, chances are they were transferred between a Unix and Windows system in binary format rather than ASCII. If you are running on a multi-user Windows box, you will also be asked whether you want to install the application for your user ID alone or for everyone on the system.

The Cygwin installer will also ask you which tools you want to install by presenting you with a screen like the one shown in Figure 3-7.

click to expand
Figure 3-7: Choose packages to install—Category view

You can use the Prev, Curr, and Exp options to have the installer automatically install older, current, or experimental versions of the software. Be careful: If you go through the list and choose to install certain applications and then click one of these buttons, your other selections will get wiped out!

Your best bet is to click the View button. This will cycle between different views of the available packages. Full view is probably the easiest to work with and is shown in Figure 3-8.

click to expand
Figure 3-8: Choose packages to install—Full view

In Full view, you can see all the available packages in an alphabetical list. Clicking the squiggly arrow to the left of the package name will cycle you through the options for a package. You can choose to install the package source code (and compile it yourself later) or choose from one or two binary versions of the package to install. If you install a package binary but for some reason want to download the accompanying source as well, check the Src? checkbox.

Note 

If you choose not to install a package but decide you want to at a later time, you can simply rerun the Cygwin setup program; it will download and install only those packages that you have added to the list since the last time you ran it.

After you select the packages you want to install, Cygwin will retrieve and install them. This can take some time depending on your Internet connection and the number of packages you choose. When you’re done, it’s a good idea to create a Desktop icon for launching Cygwin.

Implementation

Double-click the Cygwin icon. You’ll see a screen similar to the following:

click to expand

The cygwin.bat script runs from a DOS command prompt, sets up the Cygwin environment, and starts a bash shell in Windows. Cygwin does its best to set up intelligent Unix-like environment variables based on your Windows environment. The Cygwin user is exactly the same as the Windows user, although the user ID concept has been translated to a Unix perspective. For example, Cygwin has an /etc/passwd file, whereas Windows uses the SAM database to track users. Rest assured that passwords, encrypted or otherwise, do not appear in the /etc/passwd file. All authentication still relies on the SAM database.

The following window shows some standard Unix commands in Cygwin:

click to expand

Depending on the packages you installed, you can now run Unix utilities with ease. If you’re a Unix user, you’ve undoubtedly wished that Windows had a ps command so that you could see the currently running Windows processes from the command line without bothering with Task Manager. By default, Cygwin lists only processes that have been spawned from one of its shells. If you wish to see native Windows processes, use the –aW flag. The following view shows Windows processes, accessed by running the Cygwin ps command. The extra –l option provides "long" format similar to the –w "wide" option on Linux.

$ ps –aWl | less PID  PPID  PGID  WINPID  TTY  UID    STIME COMMAND   8     0     0       8    ?    0 12:24:48 *** unknown ***  96     0     0     196    ?    0 12:12:56 \SystemRoot\System32\smss.exe 240     0     0     240    ?    0 12:13:21 \??\C:\WINNT\system32\winlogon.exe 268     0     0     268    ?    0 12:13:23 C:\WINNT\system32\services.exe 280     0     0     280    ?    0 12:13:23 C:\WINNT\system32\lsass.exe 448     0     0     448    ?    0 12:13:26 C:\WINNT\system32\svchost.exe 476     0     0     476    ?    0 12:13:27 C:\WINNT\system32\spoolsv.exe 524     0     0     524    ?    0 12:13:28 C:\WINNT\System32\basfipm.exe 576     0     0     576    ?    0 12:13:28 C:\WINNT\System32\svchost.exe 604     0     0     604    ?    0 12:13:29 C:\WINNT\system32\nvsvc32.exe 640     0     0     640    ?    0 12:13:29 C:\WINNT\System32\SCardSvr.exe 664     0     0     664    ?    0 12:13:30 C:\WINNT\system32\MSTask.exe 708     1   708     708    ?   18 12:13:32 /usr/bin/cygrunsrv 824     0     0     824    ?    0 12:13:33 C:\cygwin\usr\sbin\sshd.exe

If you closely examine the output, you’ll notice a few quirks of the marriage between Windows process and Cygwin process representation. The concept of terminal (tty) is valid only for processes spawned out of a Cygwin shell. The format of the path and filename display also depend on the source of the process’s execution. Notice that sshd.exe, the secure shell daemon installed under Cygwin, is shown with its Windows file name format as opposed to /usr/sbin/sshd for Cygwin. This is because the Secure Shell (SSH) daemon is running as a Windows service. This behavior does not have any inherent problems or bugs, it just illustrates the importance of understanding how the Cygwin environment works. A good understanding will be of immense help when you are trying to debug problems or having trouble building Unix applications.

The UID column is populated from the /etc/passwd file. UID 18 corresponds to the SYSTEM account, and UID 500 corresponds to the Administrator account, as does each user’s Windows relative identifier (RID) correspond to the user’s UID. Either check out the /etc/passwd file or use the mkpasswd command to list local accounts in Unix (Cygwin) format:

$ mkpasswd.exe –l SYSTEM:*:18:544:,S-1-3-18:: Administrators:*:544:544:,S-1-3-32-544:: ASPNET:unused_by_nt/2000/xp:1000:513:ASP.NET Machine Account,   U-KAITAIN\ASPNET,S-1-3-21-1573468506-938224170-1106121233-1000:/home/ASPNET:/bin/bash istari:unused_by_nt/2000/xp:500:513:U-KAITAIN\istari,   S-1-3-21-1573468506-938224170-1106121233-500:/home/istari:/bin/bash orc:unused_by_nt/2000/xp:501:513:U-KAITAIN\orc,   S-1-3-21-1573468506-938224170-1106121233-501:/home/orc:/bin/bash sshd:unused_by_nt/2000/xp:1001:513:sshd privsep,U-KAITAIN\sshd,   S-1-3-21-1573468506-938224170-1106121233-1001:/var/empty:/bin/bash __vmware_user__:unused_by_nt/2000/xp:1003:513:__vmware_user__,   U-KAITAIN\__vmware_user__,S-1-3-21-1573468506-938224170-1106121233-   1003:/home/__vmware_user__:/bin/bash 

Notice that the account’s Windows security identifier (SID) is listed for each user.

Directory Structure and File Permissions

Cygwin mounts your local drives under the /cygdrive directory. This includes hard drive partitions, floppy drives, CD drives, and ZIP drives. Let’s do a df on my system and see what the rest of the structure looks like.

$ df Filesystem        1k-blocks      Used Available Use% Mounted on C:\cygwin\usr\X11R6\lib\X11\fonts                   25607609   7236092  18371517  29% /usr/X11R6/lib/X11/fonts C:\cygwin\bin     25607609   7236092  18371517  29% /usr/bin C:\cygwin\lib     25607609   7236092  18371517  29% /usr/lib C:\cygwin         25607609   7236092  18371517  29% / c:                25607609   7236092  18371517  29% /cygdrive/c

This represents a default installation of Cygwin, which places files in the C:\cygwin\ directory. Cygwin has made this directory the root mount point. It then mounts C:\cygwin\ bin on /usr/bin and C:\cygwin\lib on /usr/lib. The /usr/bin, /bin, and /usr/local/bin directories are added to the Cygwin path, but not the Windows path. The directories in the Windows path are imported into the Cygwin path so that you have the same access:

$ echo $PATH /usr/local/bin:/usr/bin:/bin:/cygdrive/c/WINNT/system32:/cygdrive/c/WINNT: /cygdrive/c/WINNT/System32/Wbem:/cygdrive/c/tools:/usr/bin:/cygdrive/c/NTOFW: /cygdrive/c/javacc-3.2/bin:/cygdrive/c/j2sdk1.4.2/bin: /cygdrive/c/home/istari/src/ant/bin:/bin:/usr/X11R6/bin

Tip 

You can cross drive volumes in two manners under Cygwin. For example, to go to the root directory of the D: drive you could type either cd d: or cd /cygdrive/d.

Cygwin also uses sensible file permissions for the “Unix” files. But what does it do for all of your Windows files? Who’s the owner of the files and what are the default permissions? Let’s run an ls –l command on some Windows files to find the answer.

$ ls –l total 8 drwxrwxrwx+   2 Istari Users      233472 Oct 10 11:03 bin/ drwxrwxrwx+   2 Istari None            0 Sep 12 10:48 cygdrive/ -rwxrwxrwx    1 Istari Users          57 Aug  5 14:45 cygwin.bat* -rwxrwxrwx    1 Istari Users        7022 Oct 10 11:09 cygwin.ico* drwxrwxrwx+  20 Istari Users       16384 Oct 10 11:03 etc/ drwxrwxrwx+   2 istari None            0 Aug 19 15:45 home/ drwxrwxrwx+  30 Istari Users       69632 Oct  1 11:43 lib/ drwxrwxrwx+   2 Istari Users           0 Sep  3 10:20 sbin/ drwxrwxrwx+   4 Istari Users        4096 Oct 10 11:33 tmp/ drwxrwxrwx+  22 Istari Users        4096 Oct  1 11:43 usr/ drwxrwxrwx+  10 Istari Users        4096 Aug 20 14:27 var/ 

Cygwin will try to obtain file ownership (user and group) information from Windows. It makes the permissions on all Windows directories and executables 755 (readable and executable by all, writable only by the user) and gives all regular Windows files 644 permissions (readable by all, writable only by the user). Running chmod on Windows files does nothing and is ignored.

Tip 

Cygwin includes several utilities (mkpasswd, mkgroup, and so on) and environment variable values (CYGWIN=ntsec) that try to help map Windows NT/2000 to Unix permissions so you can perform Unix-esque user and file security administration on your Windows box using Cygwin. See http://cygwin.com/cygwin-ug-net/ntsec.html and the Cygwin FAQ (http://cygwin.com/faq/) for more details.

Running Applications

Ultimately, what you can do with Cygwin depends on what packages you choose to install. But let’s take a look at some of the more interesting uses.

Running Windows Applications

Not only can you run Unix/Cygwin apps, but you can run native Windows applications from the command line, as shown here:

click to expand

You can do the same thing with GUI applications.

Building Programs in Windows

What else can you do? If you install gcc, gdb, make, and the Binutils, you now have a free Windows C/C++ development environment. Granted, it lacks the fancy interface of Microsoft’s Visual Studio, but it’s quite a bit less expensive and equally powerful! Here’s an example of compiling and running a simple program in C:

click to expand

Running Perl Scripts

Even though Perl distributions are available for Windows, many of them are not free. Cygwin currently provides Perl 5.8.0. To demonstrate this, take a look at this simple script that takes a number (in any base from 2 to 16) and converts it to another base between 2 and 16. In this example, I took the base-10 number 435 and converted it to hexadecimal:

click to expand

You can install CPAN modules just as you would on any other system. You will find the base directory in /usr/lib/perl5.

Helpful Unix Tools

You now have access to a myriad of useful Unix tools from within Windows, many of which can be helpful to the system administrator or network security professional for system analysis. Here are a few:

  • grep   Search files for regular expressions

  • cut   Extract columns of text from a file based on arbitrary delimiters; perfect for dealing with application logs, CSV files, or similar files

  • sed   Command-line stream editor; good for such things as search and replace

  • strings   Extract printable ASCII strings from a binary file; good for Word documents when you don’t have Office installed

  • strace   Trace system calls and signals; see what system calls and signals an application is making and receiving

  • md5sum   Perform a checksum on a file to ensure its authenticity and protect against tampering

  • diff   Compare two files for differences

  • patch   Use the output from a diff command to make file1 look like file2

  • sshd   Provide encrypted remote command-line access

  • xxd   Powerful hex dump utility; useful for binary file analysis

    Note 

    You can go to http://cygwin.com/ported.html to find other Cygwin packages available for download. You’ll find such popular applications as Apache, smbclient, and even CD-burning software (including the mkisofs utility, which lets you create ISO file images of CD-ROMs).

XFree86 for Cygwin

You can even run the X Window System (X for short) from inside Cygwin. XFree86 version 4.3.0 is one of the options available from the Cygwin setup.exe.

You’re finished! Now type startx at the prompt, and you should see a screen similar to that shown in Figure 3-9.

click to expand
Figure 3-9: Windows under Cygwin

XFree86 has become very stable under Cygwin and you should be able to use several X-based applications from your favorite Linux or BSD environment. If you experience problems, the first step in the debugging process should be to ensure that /usr/X11R6/bin is in your path variable.

One of your first steps will probably be the configuration of a more enjoyable window manager. Window Maker is available from the Cygwin setup. After downloading the file, the ~/.xinitrc file must be modified with a simple, one-line entry:

$ cat .xinitrc # Window Maker default X session startup script     PATH="$PATH:/usr/X11R6/bin"     exec wmaker

A Sourceforge project, CyGNOME, is working on a faithful port of GNOME to Cygwin’s X environment. The project lags the current version of GNOME by a fair amount, but a resurgence of effort brought certain portions of version 2.x to Cygwin.

Tip 

You can cut and paste between the Windows clipboard and the Cygwin X session by supplying extra options to the startx command. If clipboard access is important, start an X session with this command: startx -- -clipboard. The double-dash instructs the startx script to pass subsequent options to the X server, in this case the –clipboard option.

start sidebar
Case Study: Using Unix in a Windows Environment

Ken works for a software development company. He went through college using mostly Solaris and IRIX, but his current company uses all Windows 2000 workstations. He was able to convince the system administrators to allow him to install Cygwin so he could use some of the more useful Unix tools like bash, diff, sleep, vi, lynx, grep, less, md5sum, and NcFTP. In addition, he uses the Cygwin cvs and sshd utilities to set up a secure Concurrent Versions System (CVS) repository for version control on the code he and his department are working on. He also uses Rsync to keep a mirror of the CVS repository on another server in case something happens to his machine. Cygwin's Perl interpreter and GNU build environment also give Ken the ability to compile “Cygwin-ized” Unix code on the Windows system. One of Ken's side interests is network security. He hopes to take Unix-based utilities like nmap and use Cygwin's subsystem to port them to Windows.

Ken realizes that Windows systems seem to dominate the corporate workplace. By helping develop more Unix-based applications that can also run on Windows using Cygwin, he hopes to bridge the gap between the two communities so that Windows and Unix tools can be used seamlessly in combination on a single system (and at no extra cost).

end sidebar



 < Day Day Up > 



Anti-Hacker Tool Kit
Anti-Hacker Tool Kit, Third Edition
ISBN: 0072262877
EAN: 2147483647
Year: 2004
Pages: 189

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net