Senin, 02 Desember 2013

Xathrya Sabertooth

Xathrya Sabertooth


Managing Windows Service via Command Line Interface

Posted: 01 Dec 2013 09:02 PM PST

Service is also known as background process, a program which run in background and similar concept to a Unix daemon. A Windows service must conform to the interface rules and protocols of the Service Control Manager, the component responsible for managing Windows services.

As title suggests, we will discuss about how to manage Windows service using Command Line Interface (CLI) not Graphical User Interface (GUI). Specifically we will use two CLI: command prompt and Windows PowerShell. Using a command prompt, we can invoke two different program to fulfill our need: sc.exe and net.exe.

We will use a fictional service serv.exe registered as serv48 as our example.

All examples are done using Administrator privilege, no user privilege involved.

Get Service Status

Get status from a registered service, such as state (running, paused, suspended, stopped), name, etc.

Using sc (command prompt)

sc query serv48

Sample response:

SERVICE_NAME: serv48          TYPE               : 10  WIN32_OWN_PROCESS          STATE              : 1  STOPPED          WIN32_EXIT_CODE    : 0  (0x0)          SERVICE_EXIT_CODE  : 0  (0x0)          CHECKPOINT         : 0x0          WAIT_HINT          : 0x0

Using PowerShell

Get-Service mysql56serv48

Sample response:

Status   Name               DisplayName  ------   ----               -----------  Stopped  Serv48             serv48

Register New Service

Creates a service entry in the registry and service database. In other word, it register the service to windows service component.

Using sc (command prompt)

sc create serv48 binPath=C:\ImportantApp\serv48d.exe DisplayName=serv48

Using PowerShell

New-Service -name serv48 -binaryPathName C:\ImportantApp\serv48d.exe -displayName serv48

Restart Service

This section will restart a service. Actually, a restart means stopping and starting the same service.

Using sc (command prompt)

sc stop serv48  sc start serv48

Using net (command prompt)

net stop serv48  net start serv48

Using PowerShell

Stop-Service serv48  Start-Service serv48

There is also a single command in Powershell to restart a service:

Restart-Service serv48

Resume Service

Resuming a service after the service is suspended.

Using sc (command prompt)

sc continue serv48

Using net (command prompt)

net continue serv48

Using PowerShell

Resume-Service serv48

Set Service

Change or set state of services with some options.

Using sc (command prompt)

sc config serv48 [option=value]

with options available (and possible value) are:

  • type= own,share,interact,kernel,filesys,rec,adapt
  • start= boot,system,auto,demand,disabled,delayed-auto
  • error= normal,servere,critical,ignore
  • binPath= binary pathname to the .exe file
  • tag= yes,no
  • depend= service it’s depended on, separated by / (forward slash)
  • DisplayName= name used to display the service

Using PowerShell

Set-Service serv48 [-option value]

with options available (and possible value) are:

  • ComputerName – specifies one or more computers, the default is local computer.
  • Description – new description for service which will appear in Computer Management.
  • DisplayName – New display name for the service
  • Name – new service name (in our case: serv48)
  • StartupType – Automatic, Manual, Disabled
  • Status – Running, Stopped, Paused

Start Service

Start a service, change the state from stopped to running.

Using sc (command prompt)

sc start serv48

Using net (command prompt)

net start serv48

Using PowerShell

Start-Service serv48

Stop Service

Stopping a service, change state from running to stopped.

Using sc (command prompt)

sc stop serv48

Using net (command prompt)

net stop serv48

Using PowerShell

Stop-Service

Suspend Service

Also known as pause. Pause a service for a moment until it is resumed.

Using sc (command prompt)

sc pause serv48

Using net (command prompt)

net pause serv48

Using PowerShell

Suspend-Service

Build Apache HTTPD for Windows from Source

Posted: 01 Dec 2013 05:47 PM PST

Apache HTTP Server, commonly referred to as Apache, is a popular web server application. It is notably having a key role in initial growth of World Wide Web. Originally based on the NCSA HTTPd server, development of Apache began in early 1995 after work on the NCSA code stalled. Apache quickly overtook NCSA HTTPd as the dominant HTTP server, and has remained the most popular HTTP server in use since April 1996.

In this article we will bring ourselves to build Apache HTTP Server for Windows. Some material used here are:

    1. Windows 8, 64-bit
    2. Apache HTTPD 2.4.7 (latest per November 28th, 2013)
    3. Windows 8 Platform SDK, February 2003 or later
    4. Microsoft Visual Studio 2010
    5. Perl 5.16.3
    6. awk
    7. nasm 2.11.rc1

Also for material building the Apache

    1. apr, apr-util, apr-iconv
    2. PCRE (Perl Compatible Regular Expressions)
    3. zlib library (optional)
    4. OpenSSL libraries (optional)

You should also provide free disk space at least 200MB when compiling. After installation, Apache needs approximately 80 MB of disk space.

In this article, we will use Visual Studio Command Prompt instead of common Command Prompt.

Instead of compiling to x64 code, we will target the x86 architecture.

Opening Visual Studio Command Prompt

As stated before, we use Visual Studio Command Prompt instead of common Command Prompt. I use Visual Studio 2010 (Visual Studio 10) on Windows 8 64-bit. To activate Visual Studio Command Prompt, there are two methods: search & run Visual Studio Command Prompt from Start Screen, run Command Prompt then execute script to set environment variable.

If you want to use first method, make sure you use Visual Studio Command Prompt for amd64 or 64-bit instead of 32-bit.

If you want to do second method, you can do:

"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" amd64

Here we execute vcvarsall.bat and set the argument to amd64 to obtain necessary environment variables for the toolchain basically.

Grab the Materials

Source code of Apache HTTPD can be downloaded freely from here. Choose the closest mirror for you.

There are two Perl implementation for Windows: Strawberry Perl and ActiveState perl. I leave you to choose which one. Both can be downloaded from here.

Windows Software Developer Kit (SDK) for Windows 8 can be downloaded freely from here. Download the sdksetup.exe then run it. Choose Windows SDK from options available.

AWK is standard feature of most Unix-like operating system. For Windows, there is an alternative: Brian Kernighan’s http://www.cs.princeton.edu/~bwk/btl.mirror/ site has a compiled native Win32 binary, http://www.cs.princeton.edu/~bwk/btl.mirror/awk95.exe which you must save with the name awk.exe (rather than awk95.exe). It should be installed in environment path or known by Visual Studio.

NASM or Netwide Assembler is the assembler targeting x86 family processor. We can download nasm from it’s official site. The latest (version 2.11rc1 per November 28th, 2013) can be downloaded here. The one I use here is nasm-2.11rc1-installer.exe. Make sure nasm can be executed (the path is in the environment path).

APR (Apache Project Runtime) is used for building Apache. The project is a separated project from Apache HTTPD therefore we need to download it manually. Download it here http://apr.apache.org/download.cgi, select the appropriate mirror for you. The three we should download are: apr 1.5, apr-util 1.5.3, apr-iconv 1.2.1. Download the win32 version source code.

Perl Compatible Regular Expressions is regular expression pattern matching using the same syntax and semantics as Perl 5. It can be downloaded from www.pcre.org. The latest version is 8.33 which can be downloaded from ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/.

The zlib library is optional, used for mod_deflate. The current version is 1.2.8 and can be download from http://www.zlib.net/. In this case, the filename is zlib-1.2.8.tar.xz.

OpenSSL libraries is optional, used for mod_ssl and ab.exe with ssl support. You can obtain the OpenSSL for Windows from http://www.openssl.org/source/. Assuming we have downloaded it. In this case, the file name is openssl-1.0.1e.tar.gz.

Pre-Compilation Stage

In this article, I assume awk is installed as C:\Windows\awk.exe which should on the environment path.

Extract the Apache source code. Once it’s extracted we have “httpd-2.4.7” directory (for example: D:\httpd-2.4.7).

Extract the apr package to Apache’s srclib and rename them to apr, apr-iconv, apr-util respectively. Therefore we have three subdirectories apr, apr-iconv, and apr-util inside of “httpd-2.4.7/srclib”.

Extract the PCRE package to Apache’s srclib and rename it to pcre. Therefore we have “httpd-2.4.7/srclib/pcre”.

If you want to include zlib support, extract zlib source code inside Apache’s srclib sub directory and rename the directory to zlib. Therefore, we have “httpd-2.4.7/srclib/zlib”.

If you want to include openssl support, extract openssl source code inside Apache’s srclib sub directory and rename the directory to openssl. Therefore, we have “httpd-2.4.7/srclib/openssl”.

Compilation

The makefile script for Windows is defined as Makefile.win. In this article, we will build all the optional package first, manually.

To compile & build zlib, enter the Apache’s srclib for zlib and invoke the Makefile. Assuming Apache source code is in D:\httpd-2.4.7

cd D:\httpd-2.4.7\srclib\zlib  nmake -f win32\Makefile.msc AS=ml64 LOC="-DASMV -DASMINF -I." OBJA="inffasx64.obj gvmat64.obj inffas8664.obj"  nmake -f win32\Makefile.msc test  copy zlib.lib zlib1.lib

The last command is used to copy zlib.lib as zlib1.lib. We do this because when we compile OpenSSL we need library with this name.

To compile & build openssl, enter the Apache’s srclib for zlib and invoke the Makefile. Assuming Apache source code is in D:\httpd-2.4.7. To prepareOpenSSL to be linked to Apache mod_ssl or the abs.exe project, we can use following commands:

cd D:\httpd-2.4.7\srclib\openssl  perl Configure disable-idea enable-camellia enable-mdc2 enable-zlib VC-WIN64A -ID:\httpd-2.4.7\srclib\zlib -LD:\httpd-2.4.7\srclib\zlib  ms\do_win64a.bat  nmake -f ms\ntdll.mak

The above command configures OpenSSL with Visual C++ Win64 AMD. We disable the IDEA algorithm since this is by default disabled in the pre-distributions and really shouldn’t be missed. If you do however require this then go ahead and remove disable-idea.

Because we use OpenSSL 1.0.1e, we should invoke following command:

echo. > inc32\openssl\store.h

Now go to the top level directory of our Apache HTTPD source code. We will invoke the makefile to build Apache HTTPD.

nmake /f Makefile.win installr

Finally compile and install HTTPd, you can specify INSTDIR= to specify a path of where to install HTTPd as well. You can also specify database bindings by adding DBD_LIST=”mysql sqlite” etc. Also as it points out, don’t forget to add the libraries and includes from the databases to the INCLUDE and LIB

Tidak ada komentar:

Posting Komentar