### Category: Labs

Home / Category: Labs

### Programming Problem Set: 99 Problems Chapter 2

December 9, 2015 | Labs | No Comments

Ninety-nine Problems is generalized version to famous P-99: Ninety-Nine Prolog Problems collection used for teaching programming. The problems initially set for prolog but later many solutions come from various programming language. The purpose of this problem is to give us opportunity to practice our skills in logic programming. The goal is to find the most elegant solution of the given problem. Efficiency is important, but logical clarity is even more crucial.

The problem set are divided into seven categories / chapters: Lists, Arithmetic, Logic and Codes, Binary Trees, Multiway Trees, Graphs, and Miscellaneous.

In this chapter you will be only given a problem set. The solution might come however it would be on different page.

This chapter will cover about Arithmetic. A list is either empty or it is composed of a first element (head) and a tail, which is a list itself. As a continuation from previous chapter, the problem will be started from last previous number.

#### 29. Determine whether a given integer number is prime.

`Example: is_prime_p( 7 ) -> Yes`

#### 30. Determine the prime factors of a given positive integer.

```Construct a list containing the prime factors in ascending order

Example: prime_factor_p( 315 ) -> [ 3, 3, 5, 7 ]```

#### 31. Determine  the prime factors of a given positive integer (2)

```Construct a list containing the prime factors and their multiplicity.

Example: prime_factor2_p( 315 ) -> [ [3,2], [5,1], [7,1] ]

Hint: The solution of problem 10 may be helpful.```

#### 32. A list of prime number

```Given a range of integers by its lower and upper limit, construct
a list of all prime numbers in that range.

Example: prime_list_p( 3, 15 ) -> [3, 5, 7, 11, 13 ]```

#### 33. Goldbach’s conjecture

```Goldbach's conjecture says that every positive even number greater
than 2 is the sum of two prime numbers. Example: 28 = 5 + 23.
It is one of the most famous facts in number theory that has not
been proved to be correct in the general case. It has been numerically
confirmed up to very large numbers.
Find the two prime numbers that sum up to a given even integer

Example: goldbach_p( 28 ) -> [ 5, 23]```

#### 34. A list of Goldbach compositions

```Given a range of integers by its lower and upper limit, print a
list of all even numbers and their Goldbach composition

Example: goldbach_list_p( 9, 20 )
10 = 3 + 7
12 = 5 + 7
14 = 3 + 11
16 = 3 + 13
18 = 5 + 13
20 = 3 + 17

In most case, if an even number is written as the sum of two prime
numbers, one of them is very small. Very rarely, the primes are
both bigger than say 50. Try to find out how many such cases
there are in the range 2..3000.```

#### 35. Determine the greatest common divisor of two positive integer number

```Use Euclid's algorithm

Example: gcd_p( 36, 63 ) -> 9```

#### 36. Determine whether two positive integer numbers are coprime

```Two numbers are coprime if their greates common divisor equals 1

Example: coprime_p( 35, 64 ) -> Yes```

#### 37. Calculate Euler’s totient function phi(m)

```Euler's so-called totient phi(m) is defined as the number of pisitive
integers r (1 <= r < m) that are coprime to m.
If m = 10 then r = 1, 3, 7, 9; thus phi(m) = 4. Note the special case
phi(1) = 1

Example: phi_p( 10 ) -> 4```

#### 38. Calculate Euler’s totient function phi(m) (2)

```See the previous problem for definition of Euler's totient function.
If the list of the prime factors of a number m is known in the form
of problem 32 then the function phi(m) can be efficiently calculated
as follows:

Let [[p1, m1], [p2, m2], [p3, m3], ...] be the list of prime factors
(and their multiplicities) of a given number m. Then phi(m) can be
calculated with following formula:

phi(m) = (p1-1)* p1^(m1-1) *(p2-1)* p2^(m2-1)*(p3-1)* p3^(m3-1)

Note that a^b stands for the b'th power of a.```

#### 39. Compare the two methods of calculating Euler’s totient function.

```Use the solution of problem 37 and 38 to compare algorithm. Take
the number of logical inferences as a measure for efficiency. Try to
calculate phi(10090) as an example```

## Solution:

2. Lisp
3. Prolog
4. Python

### Nmap Video Tutorials and Trainings

December 9, 2015 | Labs | No Comments

Nmap or Network Mapper is the tools to conduct a networking mapping. Using Nmap, one can know whether computer or host active and obtain further information about the target. Nmap is available in a variety of operating system such as Linux and Windows.

These video is password protected and archived with 7zip. Once you download it, rename the extension from .xar to .7z and extract it. As the archive is password protected, to be able to extract the file you should enter the correct password. The password is: “xathrya.id” without quotes.

The tutorials consists of eleven modules.

These modules require a web browser with working adobe flash installed. Please make sure your browser support it.

## Module 1 – Getting Started with Nmap

Overview of the entire course, along with a sneak peek of the secrets that will be uncovered along the way.

## Module 2 – Nmap Basics

Overview of network protocols, the Nmap scan process, and secrets for increasing the speed of this process.

## Module 3 – Scans for Every Occasion

Introduces four of the most popular, most useful, and most versatile Nmap scanning methods: TCP SYN scan, TCP connect() scan, Ping scan, and UDP scan.

## Module 4 – “Back Pocket” Scans

Sometimes, you’ll run into a situation where a normal Nmap scan isn’t providing you with all of the information you need. This may be a situation where remote devices aren’t responding, or perhaps you aren’t able to identify any available ports on a remote device – even though you can easily connect to its web server! In these situations, it’s useful to have a few tricks in your back pocket.

## Module 5 – Useful Scanning Options

Concentrate on some useful scanning options that will assist in building Nmap scan sessions that are effective and efficient.

## Module 6 – Nmap “Pings” : The Search for Hosts

There’s a lot to Nmap’s ping process, and we’ll start with defining a ping. From there, we’ll move to Nmap’s default pings – the ARP ping and the ICMP and TCP ACK ping combo. To really make the most of Nmap’s pings, we’ll also investigate the details of the TCP SYN ping and the UDP ping.

## Module 7 – Recon Scanning

Investigae the secrets of network reconnaissance and take the details of two major Nmap features – operating system fingerprinting and version detection.

## Module 8 – Ninja Scanning

Introduce to Nmap’s art of invisibility. Learn the secrets of using Nmap on network in stealth mode, where you can go come and go like the wind.

## Module 9 – Output Options

Nmap includes a number of output options, and this module takes us through them all. We’ll show you the differences in the output options, including how to convert Nmap’s XML output into some great HTML-based reports.

## Module 10 – Windows Nmap

Learn about the history of Nmap and Windows – both the good, and the bad. Although there have been some significant operational issues with Windows-related issues that exist today are easy to work around.

## Module 11 – Real-World Nmap Scanning

Discuss six Nmap scanning techniques that can get you through some pretty nasty security problems.

### Programming Problem Set: 99 Problems Chapter 1

December 9, 2015 | Labs | No Comments

Ninety-nine Problems is generalized version to famous P-99: Ninety-Nine Prolog Problems collection used for teaching programming. The problems initially set for prolog but later many solutions come from various programming language. The purpose of this problem is to give us opportunity to practice our skills in logic programming. The goal is to find the most elegant solution of the given problem. Efficiency is important, but logical clarity is even more crucial.

The problem set are divided into seven categories / chapters: Lists, Arithmetic, Logic and Codes, Binary Trees, Multiway Trees, Graphs, and Miscellaneous.

In this chapter you will be only given a problem set. The solution might come however it would be on different page.

This chapter will cover about Lists. A list is either empty or it is composed of a first element (head) and a tail, which is a list itself.

#### 01. Find the last element of a list

`Example: last_p( [a, b, c, d] ) -> d`

#### 02. Find the last but one element of a list

`Example: last_one_p( [a, b, c, d] ) -> c`

#### 03. Find the n’th element of a list

```The index is started from 1.

Example: at_p( [a, b, c, d], 3) -> c```

#### 04. Find the number of elements of a list

`Example: length_p( [a, b, c, d] ) -> 4`

#### 05. Reverse a list

`Example: reverse_p( [a, b, c, d] ) -> [d, c, b, a]`

#### 06. Find out whether a list is a palindrome

```A palindrome ca be read forward of backward.

Example: palindrom_p( [a, b, c, b, a] ) -> true```

#### 07. Flatten a nested list structure

```Transform a lists as elements into a 'flat'
list by replacing each list with its element (recursively)

Example: flatten_p( [a, [b, [c, d], e]] ) -> [a, b, c, d, e]```

#### 08. Eliminate consecutive duplicates of list elements

```If a list contains repeated elements they should be replaced
with a single copy of the element. The order of elements
should not be changed.

Example: compress_p( [a, a, a, a, b, c, c, a, a, d, e, e, e, e] ) -> [a, b, c, a, d, e]```

#### 09. Pack consecutive duplicates of list elements into sublists

```If a list contains repeated elements, they should be placed
in separate sublist.

Example: pack_p( [a, a, a, a, b, c, c, a, a, d, e, e, e, e])
-> [ [a, a, a, a], [b], [c, c], [a, a], [d], [e, e, e, e,] ]```

#### 10. Run-length encoding of a list

```Consecutive duplicates of elements are encoded as terms [N, E]
where N is the number of duplicates of the element E.

Example: encode_p1( [a, a, a, a, b, c, c, a, a, d, e, e, e, e] )
-> [ [4,a], [1,b], [2,c], [2,a], [1,d], [4,e]]
```

#### 11. Modified run-length encoding

```Consecutive duplicates of elements are encoded as terms [N, E]
where N is the number of duplicates of the element E. If an
element has no duplicates, it is simply copied into the result
list Only duplicates are transferred as [N, E] terms.

Example: encode_p2( [a, a, a, a, b, c, c, a, a, d, e, e, e, e] )
-> [ [4,a], b, [2,c], [2,a], d, [4,e] ]```

#### 12. Decode a run-length encoding list

```Given a run-length code list generated, construct its uncompressed
version.

Example: encode_p3( [4,a], [1,b], [2,c], [2,a], [1,d], [4,e])
-> [a, a, a, a, b, c, c, a, a, d, e, e, e, e] ]
```

#### 13. Run-length encoding of a list (direct solution)

```Implement the so-called run-length encoding data compression method
directly. Don't explicitly create the sublists containing the
duplicates but only count them. Simplify the result list by
replacing the singleton terms [1,X] by X.

Example: encode_p4([a, a, a, a, b, c, c, a, a, d, e, e, e, e])
-> [ [4,a], b, [2,c], [2,a], d, [4,e]]
```

#### 14. Duplicate the elements of a list

```Example: duplicate_p([a, b, c, d, e]) -> [a, a, b, b, c, c, d, d, e, e]
```

#### 15. Duplicate the elements of a list a given number of times.

```Example: duplicate_px([a, b, c, d, e], 3)
-> [a, a, a, b, b, b], c, c, c, d, d, d, e, e, e]```

#### 16. Drop everh N’th element from a list

```Example: drop_p([a, b, c, d, e, f, g, h, i, k], 3)
-> [a, b, d, e, g, h, k]
```

#### 17. Split a list into two parts; the length of the first part is given

```Do not use any predefined predicates / function.

Example: split_p([a, b, c, d, e, f, g, h, i, k], 3, L1, L2)
-> L1 = [a, b, c] ; L2 = [d, e, f, g, h, i, k]```

#### 18. Extract a slice from a list

```Given two indices, I and K, the slice is the list containing the
elements between the I'th and K'th element of the original list
(both limits included). Start counting the element with 1.

Example: slice_p([a, b, c, d, e, f, g, h, i, k], 3, 7) -> [c, d, e, f, g]
```

#### 19. Rotate a list N places to the left

```Example:
rotate_p([a, b, c, d, e, f, g, h], 3) -> [d, e, f, g, h, a, b, c]
rotate_p([a, b, c, d, e, f, g, h],-2) -> [g, h, a, b, c, d, e, f]
```

#### 20. Remove the K’th element from a list

```Example: remove_p([a, b, c, d], 2) -> [a, c, d]
```

#### 21. Insert an element at a given position into a list

```Example: insert_p(x, [a, b, c, d], 2) -> [a, x, b, c, d]
```

#### 22. Create a list containing all integers withing a given range.

```Example: range_p(4, 9) -> [4, 5, 6, 7, 8, 9]
```

#### 23. Extract a given number of randomly selected elements from a list.

```The selected items shall be put into a result list.

Example: rnd_select_p1([a, b, c, d, e, f, g, h], 3) -> [g, a, c]
```

#### 24. Draw N different random numbers from the set 1..M.

```The selected numbers shall be put into a result list.

Example: rnd_select_p2(6, 49) -> [23, 1, 33, 21, 37, 17]
```

#### 25. Generate a random permutation of the elemnts of a list.

`Example: rnd_permut_p([a, b, c, d, e]) -> [b, a, d, c, e, f]`

#### 26. Generate the combinations of K distinct objects chosen from the N elements of a list.

```In how many ways can a committee of 3 be chosen from a group of
12 people? There are C(12,3) = 220 possibilities denotes the
well-known binomial coefficients.

Example: combination_p([a, b, c, d, e, f])
[a, b, c]
[a, b, d]
[a, b, e]
...
```

#### 27. Group the elements of a set into disjoint subsets.

```(a) In how many ways can a group of 9 people work in 3 disjoint subgroups of 2, 3, and 4 persons?
Example: group3_p([aldo,beat, carla, david, evi, flip, gary, hugo, ida])

G1 = [aldo, beat], G2 = [carla, david, evi], G3 = [flip, gary, hugo, ida]

(b) Generalize the above function in a way that we can specify a list of group sizes and the
predicate will return a list of groups

Example: group_p([aldo, beat, arla, david, evi, flip, gary, hugo, ida],[2, 2, 5])
-> [[aldo,beat],[carla,david],[evi,flip,gary,hugo, ida]]
[/sourcecode]```

#### 28. Sorting a list of lists according to length of sublists

```(a) Suppose that a list (InList) contains elements that are lists themselves.
The objective is to sort the elements of InList according to their length.
E.g. short lists first, longer lists later, or vice versa.

Example: lsort_p([[a, b, c], [d,e],[f,g,h], [d,e], [i,j,k,l], [m,n],[o]])
-> [[o],[d, e],[d, e],[m, n],[a, b, c],[f, g, h],[i, j, k, l]]

(b) Suppose a list (InList) contains elements that are list themselves.
But this time the objective is to sort the elements of InList according to
their length frequency. i.e. in the default, where sorting is done ascendingly,
lists with rare lengths are placed first, others with a more frequent length
come later.

Example: lfsort_p([[a, b, c], [d,e],[f,g,h], [d,e], [i,j,k,l], [m,n],[o]])
-> [[i, j, k, l],[o],[a, b, c],[f, g, h],[d, e],[d, e],[m, n]]

Note that in the above example, the first two list in the result have length
4 and 1, both lengths appear just one. The third and forth list have length
3, there are two list of this length. And finally the last three lists have
length 2. This is the most frequent length
[/sourcecode]```

## Solution:

2. Lisp
3. Prolog
4. Python

### Installing Windows XP on QEMU

December 5, 2015 | Article, Labs | No Comments

On other article we have discussed about running Debian ARM on QEMU, and also Slackware ARM on QEMU. Now,  in this article we will attempt install Windows XP on QEMU.  In this article I use:

1. Slackware64 14.0 as host OS
2. QEMU
3. Windows XP ISO / CD

## Obtain the Materials

Make sure you have install QEMU. If you have not installed it yet, you can read article here.

Another thing we need is Windows XP ISO or CD for installation. Make sure you have product key for activation. This article won’t give you link to obtain any of them so you should find them by yourself. Either ISO or CD is OK, just pick one.

## Preparing the Environment

First we need to set the environment before do installation.

### Make Working Directory

All progress we did will be stored on a single directory. You can create any directory but for this article I assume you create it on ~/QEMU/WinXP directory. Also, save ISO file, if you do installation by ISO file, to this directory.

### Create Disk

We need to create a raw hard disk. At minimum, 4GB disk is sufficient. In this article I use 6GB disk. You don;t need to partitioning your disk. All work will be done on an image disk. The image can be created by qemu:

`qemu-img create -f raw WinXP.img 6G`

On above command, we create a disk with 6GB capacity. The -f switch tell qemu to create a raw disk. The disk then stored as an image file with filename WinXP.img

## Installation

First we need to boot the ISO. On our working directory, invoke following command:

`qemu-system-i386 -m 512 -hda WinXP.img -cdrom winxp_n_sp2.ISO -boot d`

The platform we use is i386 or generic Intel x86 CPU therefore we use qemu-system-i386 to boot. Here are the explanation of all arguments we put:

• -m 512: allocate 512 MB of RAM for virtual machine
• -hda WinXP.img: Search for WinXP.img on current directory and use it as hard disk.
• -cdrom winxp_n_sp2.ISO: Use ISO file with filename winxp_n_sp2.ISO on current directory. The name may be vary and you should adjust it with your ISO’s name. If you use installation CD, insert your CD and replace winxp_n_sp2.ISO with /dev/cdrom or whatever your CD device file node is.
• -boot d: instructs QEMU to let the virtual machine boot from the (virtual) CD-ROM drive we’ve specified using -cdrom

A window might appear and then you can see the installation CD is now booting. You can continue with usual installation procedure, it’s not different compared to when you install to actual machine. And it still ask you about product key so you better enter the correct code.

Once the installation finish, you will got you Windows XP running on QEMU. Now, whenever you want to boot Windows XP, you can at minimum invoke following command:

`qemu-system-i386 -m 512 -hda WinXP.img`

Now, happy hacking 😀

Here are some screenshots    