January 31, 2021

Malware Counter Measure Standard Operation Procedure in Old Corporations

featured image and summary
I have a few experience in playing around with cyber attacks, but it was merely a curiosity and I did this on my own PCs and virtual machines. Back then I tried exploiting the vulnerability on port 445 SMB using an exploit prepared by Metasploit. Metasploit also provided a tool to make backdoors (Trojan) which I practice after hijacking. The result was I can remotely control the victim's PC and after I activate the backdoor, I can connect whenever I want. Then after the vulnerability is patched (system updated) exploiting port 445 no longer works as explained on this lecture but for as long the backdoor is still implanted I can still remotely connect through there. If the vulnerability is exploited once and the system got infected by a virus, just patching it will not be enough. The infection must be healed. This is the fourth assignment from my Masters Advanced Network Security Course which has never been published anywhere and I, as the author and copyright holder, license this assignment customized CC-BY-SA where anyone can share, copy, republish, and sell on condition to state my name as the author and notify that the original and open version available here.

1. Cyber Attacks Can Come From Based on This Lecture

  1. Kernel and server side of OS.
  2. Application vulnerabilities.
  3. Before and after infection of worm or virus.

Most famous attack is buffer overflow. PCs for file sharing are susceptible to remote buffer overflow. What happens if vulnerability is announced

  1. With patch or update available.
  2. No patch or update available.

The first case automatically or manually apply the patch. The second case disable the application or port that is vulnerable. Vulnerability can be:

  1. Vulnerability due to design and programming error.
  2. Vulnerability due to misconfiguration.
  3. Vulnerability due to modules.

In case of design, the service must be disabled and reprogram. Case of misconfiguration, edit the configuration file, and as for modules disable and edit the module. To handle the vulnerabilities above the following common steps are taking.

  1. Identify the vulnerability and plan the modification beforehand.
  2. Isolate the system from the network.
  3. Backup the system and make sure it can be restored.
  4. It's suggested to perform simulation beforehand on similar environment, using virtual machine for example.
  5. Finally perform modification. For beginners the step usually taken is update the system to the latest, after this process experts tends to manually modify. It's suggested to leave explanations on modified parts, for example leaving a comment on a configuration script that contain informations of what the modification is and when it was modified.

If not taking the steps above then there's a risked in rebuilding the system from the scratch since there's a chance in breaking the system when performing modification.

2. International Standard Organization Information Security Management

As written on the first part if it is already infected by a virus or worm then the infection must be cleaned. A software called antivirus had been developed which detects the infection based on patterns of the virus. By installing this software it can prevent and also clean the infections, but even this software is not perfect. If the antivirus cannot detect the infection then it's suggested to take the 5 steps above in dealing with the vulnerability. Worst case we must backup the data, destroy and rebuild the system.

The term “policy” is a set of rules and procedures that is agreed by involving parties, and then carried out. In my opinion the strategy build this information security policy must be carried out not in a hasty pace, because it includes parties that is not professional in IT and some are less aware of the cyber attacks that occurs. Even as a information security professional, if given a large book of the information security policy will respond as “this will take time to review”. So it is recommend to take it step by step as in some standards from International Standard Organization (ISO).

  1. The first thing is to explain the current situation that many personal information is leaked, the network is swarming with worms and virus, and ultimately the damages cause a lost in profit of over millions of Yens. We should state as a national, if possible global crisis, or any other to convince that this is important.
  2. Then create an overview of the important part of the security policy which includes the counter measures of the cyber attacks, for example what the system should be equipped with (antivirus, firewall, IDS, vulnerabilities patch, etc), and clearly separate private data and public data. Private data should not go into public data which we decided to be completely open, also measures to protect the private data should be stated for example the use of authorization and backup. Another important point that should be added is the procedure that should be taken if a threat is detected which in general it is isolating the PC from the network, reporting, then take action to fix the issue.
  3. The first draft should only be the overview and contains only most important points, because this determines whether the policy be implemented or not. It's an important turning point and the strategy above is one of the best ways to open the door. When it's established then we can submit even more details of the security policy. For example security policy 2nd chapter should state about private informations, what data are important information, why is it important, and how's the impact if it's leaked, altered, or destroyed. 3nd chapter is about the system's equipment (what softwares should be installed, how the configuration should be, not to forget the physical secuirty as well, etc). 4Rd chapter of how to confront the network when holding private data, in other words our behavior that we should be careful when receiving attachments, suspicious files, dangerous site, and what we should do if we're attacked.

Mirrors

January 28, 2021

My Comments on Information Security Policy when I was a young student

featured image and summary
The information security policies that were explained on this lecture are policies when handling vulnerabilities. I strongly agree to this policy due to my past experience in modifying systems. Most of the time I broke the system and the best way after that is to rebuild the system from the beginning. In summary the policies on this lecture is mainly about identifying the vulnerabilities and plan the action that must be done, backup the system and make sure it can be restored, and finally we may modify the system. This lectures explained the policies when facing vulnerabilities in the kernel, server program, and server common gateway interface (CGI). This is the third assignment from my Masters Advanced Network Security Course which has never been published anywhere and I, as the author and copyright holder, license this assignment customized CC-BY-SA where anyone can share, copy, republish, and sell on condition to state my name as the author and notify that the original and open version available here.

1. Kernel Vulnerabilities

kernel module
  • Design of the kernel itself.
  • Drivers embedded in the kernel.
  • The driver's module.
If it is the kernel design then we have no choice but to modify the whole system. The same applies if the driver is directly embedded in the kernel. If it is only the driver module then we can only focus on the module.

2. Server Program and CGI Vulnerabilities

server CGI
  • Program's and Common Gateway Interface (CGI)'s design.
  • Misconfiguration.
  • Problem on some modules.
For the design itself we must reprogram the whole thing. If it's due to misconfiguration for example allows code injections, and unintentionally leave an open port, we reconfigure the configuration usually by editing the configuration file. If there's problem on some modules then just focus on the modules.

3. Common Steps For Handling Previous Vulnerabilities

  1. Identify the vulnerability and plan the modification beforehand.
  2. Backup the system and make sure it can be restored.
  3. It's suggested to perform simulation beforehand on similar environment, using virtual machine for example.
  4. Finally perform modification. For beginners the step usually taken is update the system to the latest, after this process experts tends to manually modify. It's suggested to leave explanations on modified parts, for example leaving a comment on a configuration script that contain informations of what the modification is and when it was modified.

If not taking the steps above then there's a risked in rebuilding the system from the scratch since there's a chance in breaking the system when performing modification.

4. Some of My Comments and Experiences

My comment is that information security policy becomes necessary at the era of where we live know. As my first experience working at a corporation called Toshiba Tec, informations are very crucial. Just leaking a bit information outside the company can greatly damage both its image and income. There are informations that are highly classified that only the top management have authorization to access. Informations can be in form of physical substance (paper for example), but mostly today in form of electronics (word processor, spread sheet, image, video), that it is forbidden the use of hard drive on the companies private computer. With today's technology it is not unusual that access and exchanges of electronic information relies on computer networks.

In my company those in the information system division are responsible in handling the technology of electronic exchange at the same time the privacy of the exchange that informations must not leak outside of the company. They create a set of information security rule that was approved by the top managements of the company (a policy was created). To defend against outside threats, firewall, intrusion detection system, and antivirus were implemented. The networks were also strictly configure of who have the authentication to access. The division is also responsible to protect from the inside whether to prevent information leakage or to prevent information damage. In the policy is stated that employees are forbidden the use of outside electronic devices to connect to the network. A strict monitoring is installed on the IT infrastructure to record the time, and the person who access the information. Also the division is responsible in socializing the information security policy, especially of what both employers and employees are allowed to do and not do.

Mirrors

Dealing with Malware for Ordinary Users

featured image and summary
The countermeasures of Malicious Ware (malware) are mitigation (prevention) or treatment like any other measures to cope against harmful substances. To prevent virus/worm infection is either we put up security to defend ourselves or avoid interacting with malwares. If we are already infected then we have cure the infection. This is the second assignment from my Masters Advanced Network Security Course which has never been published anywhere and I, as the author and copyright holder, license this assignment customized CC-BY-SA where anyone can share, copy, republish, and sell on condition to state my name as the author and notify that the original and open version available here.

1. Mitigation

antivirus firewall update
This step is the most commonly used and mostly for those with the preference who are not expert in the area of malware. People usually know of the existence of malware and follows advices given by the media, experts, or computer companies to install firewalls, antivirus and antimalware softwares, and other preventive softwares. Finally tells them to update their Operating System (OS) (windows update for example), macrowares (word, spreadsheet), and other softwares to their latest version. Analogically equipped themselves with weapons and armors.

2. Evasion

beware of phisings
This step if you have better knowledge of malwares. Initially know the overview of how malwares works. (1) Like virus needs a host to run, resides on infected files, and can only initiate if you activate it. For this we will be careful when handling files by checking its size, its origin, its name, especially its format which virus resides in executeable formats. If it seems suspicious we should delete the file. If the system is modified, deleted, or a process occur without your approval is likely virus infected. (2) Other than virus a malware called worm exists, unlike virus it can infect a host without us executing it. In real life a worm can get inside of our body just getting contact in contaminated water. Virus cannot live without a host but worm can. In computer network is similar, even without your presence it keeps reproducing itself throught the network. Back then just by visiting a site or a mail server can already be infected with a worm. The network traffic could feel really heavy, or you might send informations to addresses you don't even recognize. If the site seems suspicious or receive a suspicious email, don't visit or open it. (3) Another dangerous malware that could take full control of your PC is a trojan horse. Back in the ancient Greeks this method was described as giving a large horse figure statue to the city of Troy during the Trojan war. Without any suspicion the city accepted the horse statue. What leads to the city's subjugation they didn't know there were soldiers inside the horse, and during the night those soldiers came out and took over the city. So in computers don't accept and execute suspicious files. (4) If already an expert, some says no need for antivirus. For example just use Linux or other OS other than Windows since most malwares are coded to attack Windows, using other system won't initiate the malware since the code doesn't match. This isn't everything and more measures are necessary.

3. Treatment

scan and safemode
if already infected then we have to treat the infection or remove it. First is identification to make sure if it really is an infection and it's not a hardware problem. When it is sure that is not hardware problem, identify the symptoms, whether the process is heavy, high memory consumption, heavy traffic, a program runs without your approval, etc. Don't forget to disconnect from the network (unless performing an online scan). For common people usually install an antivirus, perform a scan and clean. But this method is not guaranteed since it is perform on an infected host. The best way to do this is to get another clean PC with the latest software and antivirus update and perform a scan on the disk of the infected host. If you're really an expert then you can remove it manually by booting into safemode or input the disk into a different operating system, then delete the files and registries of the infection.

4. Last Resort

erase and reinstall
If all else fails the extreme resort is to destroy everything (along with the malware) and start again from a scratch. Reinstall your OS and have a fresh start (you may backup your data beforehand).

Mirrors

January 27, 2021

My Comments on Network and Bots and Pentest when I was a young student

featured image and summary
This is the first assignment from my Masters Advanced Network Security Course which has never been published anywhere and I, as the author and copyright holder, license this assignment customized CC-BY-SA where anyone can share, copy, republish, and sell on condition to state my name as the author and notify that the original and open version available here. Since it is the first meeting, this only my comments about the first lecture but hopefully you get the picture about it.

1. Network Consists of Clients and Servers

clients and servers
About network servers I capture that the network consist of clients and servers. The clients requests informations and servers gives the information. I have tried using some server which includes web server, file server, mail server, dns server, and dhcp server but I'm still new especially on the security aspects of this servers.

2. There Are Many Bots on The Network

hacker evolution bots
I have heard bot network before, I played a game called hacker evolution that gives a picture of bots but I never tried them before. If I were on the hacker side I will hijack into other clients PC using malware. That malware will inform back to me and I can use the hijacked clients to do something for me for example sending spam mails, or make new bot (find other victims). Usually as an amateur I would directly hack to the targeted server, but with this method is more effective because instead of using my own PC, I can use other PCs as well. I wonder if I can practice this in virtual machine. By the way some links on the buttom of the page are dead.

3. Popular Hacking Methods

ddos commview windows
DDOS or Denial of Service is very famous in hacking event among newcomers, which is to disable the service of a server. I have only practice this by flooding the traffic of a Wifi, the other I tried is on a server on client using many computer. The way I tried is very suitable using statement in number 2 using bots. By having many PC increases the computing power. About web defacement I have seen some sites have their contents hacked, either by modifying the contents or inserting new contents. Today there are many free tools available to scan for vulnerability, Nessus for example, and tools to exploit those vulnerability like Metasploit Framework. I'm only user of this tools so I still don't know the concept behind it. There also exist simple vulnerabilities due to misconfiguration for example MySQL injection that allows to use MySQL commands on login to bypass, or another one which I can modify the html page which allows to enter without password.

4. My Prior Knowledge About Penetration Testing

4 phases of pentest
In the past of learning network security the first thing I was asked to remember the OSI layers which consists of physical, datalink, network, transport, session, presentation, and application. Buffer overflow is very famous but the concept behind it for me is more tricky that others. I have read a book called The Basics of Hacking and Penetration Testing. The contents told me that there is a difference between hacker (black hat) for ill purposes and penetration tester (white hat) to find security holes in order to cover them. I on the other hand am a grey hat (only serve as a hobby and mere curiosity, and only finds satisfaction if able to hack a system and won't go further than that). Main the contents consist of 4 important steps of hacking which is highly recommend to follow in order. First and the most important part is information gathering which affect the outcomes of the other processes. Second is scanning, while information gathering gathers informations about the target, scanning scan this information like port scanning on certain found IP from information gathering. Then the third, the main step becomes affective which is exploitation. Finally the fourth is maintaining access for permanent access.

Mirrors

January 26, 2021

7,4 Hamming Codes

7,4 Hamming Code Venn Diagram
Figure 0. 7,4 Hamming Code Venn Diagram

0. Note

This is the fifth assignment from my Masters Applied Digital Information Theory Course which has never been published anywhere and I, as the author and copyright holder, license this assignment customized CC-BY-SA where anyone can share, copy, republish, and sell on condition to state my name as the author and notify that the original and open version available here.

1. Introduction

The previous 4th assignment demonstrate 1 bit error detection using 3,4 parity codes. On this assignment will be demonstrating 1 bit error correction using 7,4 hamming codes. On 3,4 parity codes we group 3 bits per block and perform exclusive or on each blocks to get a bit called the parity code bit and add it into the 4th bit of the blocks. A different approach for the 7,4 hamming codes we first group 4 bits per block, and then obtain the 3 hamming bit codes from the 4 bits for each blocks and add them which makes each blocks contained 7 bits. Suppose there are 4 bits as follows:

b1,b2,b3,b4

To get the hamming bit codes we do the following calculation:

b5=b1⊕b2⊕b3, b6=b1⊕b2⊕b4, b7=b1⊕b3⊕b4

Those bits will be added to the block:

b1,b2,b3,b4,b5,b6,b7

Following Table 1 are the complete list:

Table 1. Coding Table

Info Code Info Code
0000 0000000 1000 1000110
0001 0001011 1001 1001101
0010 0010101 1010 1010011
0011 0011110 1011 1011000
0100 0100111 1100 1100001
0101 0101100 1101 1101010
0110 0110010 1110 1110100
0111 0111001 1111 1111111

On the receiver side the hamming codes is also constructed using the received first 4 bits of each blocks, then compare them with the hamming codes produced on the receiver side. Since the hamming codes were produced using the above equation (each codes uses different elements) we can know on which bit the error occurs, and a correction is performed. For example if receiver's b5, b6, b7 is different from transmitter's then an error on b1, if b5, b6 is different then an error occur on b2, if b5,b7 is different then an error occur on b3, if b6, b7 then b4, if b5 only then b5, if b6 only then b6, if b7 only then b7. Following Table 2 are the complete syndromes:

Table 2. Syndrome and Correction List

Syndrome Correction Syndrome Correction Syndrome Correction Syndrome Correction
No error None b6 error b6⊕1 b6b7 error b4⊕1 b5b6 error b2⊕1
b7 error b7⊕1 b5 error b5⊕1 b5b7 error b3⊕1 all error b1⊕1

2. Memoryless Errors (Skew Tent Map)

On this simulation we will (1) generate a chaotic binary sequence from memoryless source (2) perform hamming coding (3) simulate through a noisy channel (4) perform error correction on receiver. We will calculate the practical probabilities of incorrect decoding and compare them theoretically defined by PI=1–PC where PC=7p(1-p)6 + (1-p)7 is the probability of correct decoding. The formula is derived from the probability of 1 bit error out of all possible error. Also we will compare the error probability of the binary sequence before correction and after correction. We will use critical point of c=0.499999 for generating the memoryless source, while we use c=1-p for generating the error sequence, and initial chaotic value for both is x(1)=0.333333 and various error probability p for the error sequence (noise). Generating the source will be the same as assignment 2, and generating the hamming codes is similar to 4th assignment except we input 3 extra bits in each blocks based on the initial 4 bits, which will make 7 bits. Like the previous assignment we perform exclusive or between the generated memoryless source and the error sequence to obtain the received sequence. On Table 3 is the simulation result of 1000000 (million) blocks (N) (7000000 bits) with error probability up to 0.4.

Table 3. simulation result of 1000000 (million) blocks (N) (7000000 bits) with error probability up to 0.4

Error Probability 0.01 0.05 0.1 0.2 0.3 0.4
Error Before Decoding 69185 310155 651846 1489329 2095701 2817931
Error After Decoding 5661 103819 413866 1456118 2253571 2871364
Probability Error Before Decoding 0.009884 0.044308 0.093121 0.212762 0.299386 0.402562
Probability Error After Decoding 0.008087 0.014831 0.059123 0.208017 0.321939 0.410195
Incorrect Decoding 1887 33173 132352 444091 672462 810696
Probability of Incorrect Decoding 0.001887 0.033173 0.132352 0.444091 0.672462 0.810696
Theoretical Probability of Incorrect Decoding 0.002031 0.044381 0.149695 0.423283 0.670583 0.841369

3. Markov-type Errors (PWL Map)

Similar to section 2 but this time we generate the error sequence through Markov type or Piece Wise Linear (PWL) map. The theoretical incorrect error decoding is PI=1–PC and correct one:

PC = P(1)p2 (1-p)5 + 5P(0)p1 p2 (1-p1)4 + P(0)(1-p1)5 p1 + P(0)(1-p1)6

We use the same critical point and initial chaotic value. To generate the error sequence we use the PWL map with various p2 values and P1 = (1-c)/c P2, with c=1-p. On Table 4 is the theoretical result of 1000000 (million) blocks (N) (7000000 bits) with error probability up to 0.4, Table 5 is the simulation result, Table 6 is error probability before decoding, Table 7 is error probability after decoding.

Table 4. Theoretical Result

p2\p 0.01 0.05 0.1 0.2 0.3 0.4
0.1 0.013497 0.067421 0.134663 0.268465 0.400994 0.531546
0.2 0.015996 0.079888 0.159498 0.317444 0.472536 0.622384
0.3 0.017509 0.087704 0.175712 0.351731 0.525154 0.690342
0.4 0.018048 0.091159 0.184430 0.375393 0.566651 0.746271

Table 5 Simulation Result

p2\p 0.01 0.05 0.1 0.2 0.3 0.4
0.1 0.014407 0.074791 0.165392 0.288812 0.409343 0.518025
0.2 0.039309 0.084100 0.125731 0.363730 0.463654 0.633573
0.3 0.013653 0.083021 0.179077 0.365025 0.567390 0.702815
0.4 0.011958 0.100293 0.248337 0.361628 0.607297 0.741535

Table 6. Error Probability Before Decoding

p2\p 0.01 0.05 0.1 0.2 0.3 0.4
0.1 0.019189 0.050632 0.121454 0.215809 0.310955 0.387244
0.2 0.0310312 0.050779 0.073711 0.239916 0.287656 0.390070
0.3 0.0064253 0.045953 0.095724 0.199656 0.351083 0.400005
0.4 0.0073853 0.051241 0.124822 0.196768 0.334634 0.393053

Table 7. Error Probability After Decoding

p2\p 0.01 0.05 0.1 0.2 0.3 0.4
0.1 0.0182 0.050889 0.120800 0.215805 0.311266 0.387078
0.2 0.0310312 0.050020 0.071017 0.236854 0.285676 0.395473
0.3 0.0065111 0.044599 0.095300 0.196479 0.336207 0.407214
0.4 0.0055779 0.050823 0.127047 0.189369 0.317022 0.398460

4. Results

On Figure 1 shows that the error probability before and after decoding fluctuates on different error probability values (p) and type of sources (memoryless and p2s), but had one thing in common that belowp < 0.3 the error after decoding decreases, and unfortunately rises above that. The incorrect coding for memoryless is preferable when p < 0.1 but not recommended when above. For PWL with p2=0.1 shows low incorrect decoding amongst other p2. For other values there is a cutting point on p=0.2, below lower p2 shows lower incorrect decoding and viceversa.

Error vs Error Before and After Decoding
Figure 1. Error vs Error Before and After Decoding
Error vs Incorrect Decoding
Figure 2. Error vs Incorrect Decoding

5. Source Code

The source code is written in Fortran95 which is said to be a good programming language for mathematics in the old days, the first one is for memoryless, and the first one is for PWL (markov).

program Memoryless_Error_Hamming

! For safe purposes
 implicit none

! Type declarations
 integer :: m, n1, n2, i, j, s1, s2, s3
 real :: c, c_error, p, practical_error_before, p_practical_error_before, practical_error_after
 real :: p_practical_error_after, incorrect_decoding, p_incorrect_decoding
 real :: p_theory_correct_decoding, p_theory_incorrect_decoding
 real, dimension(40000000) :: x
 integer, dimension(40000000) :: bit
 integer, dimension(70000000) :: bit_hamming, bit_error, bit_receiver, bit_hamming_uncorrected, bit_corrected
 character(len=20) :: firstname, lastname, text
  
 write (*,'(A)',advance='no') 'Enter number of blocks: '
 read (*,*) m
 n1 = m*4
 n2 = m*7
 c = 0.499999

! Memoryless Generated Source (here n is number of bits)
 x(1) = 0.333333
 do i = 1, n1-1
  if (x(i) >= 0 .and. x(i) < c) then
   x(i+1) = x(i)/c
   bit(i) = 0
  else
   x(i+1) = (1-x(i))/(1-c)
   bit(i) = 1
  end if
 end do

! Hamming Codes
 j = 1
 do i = 1, n2, 7
  bit_hamming(i) = bit(j)
  bit_hamming(i+1) = bit(j+1)
  bit_hamming(i+2) = bit(j+2)
  bit_hamming(i+3) = bit(j+3)
  bit_hamming(i+4) = xor(xor(bit(j),bit(j+1)),bit(j+2))
  bit_hamming(i+5) = xor(xor(bit(j),bit(j+1)),bit(j+3))
  bit_hamming(i+6) = xor(xor(bit(j+1),bit(j+2)),bit(j+3))
  j = j+4
 end do

! Error Sequence
 write (*,'(A)',advance='no') 'Enter error probability: '
 read (*,*) p
 c_error = 1 - p
 do i = 1, n2
  if (x(i) >= 0 .and. x(i) < c_error) then
   x(i+1) = x(i)/c_error
   bit_error(i) = 0
  else
   x(i+1) = (1-x(i))/(1-c_error)
   bit_error(i) = 1
  end if
 end do

! Receiver Side (hamming encoded sequence + error sequence), calculate error probability before decoding, prefill future corrected bits
 practical_error_before = 0;
 do i = 1, n2
  bit_receiver(i) = xor(bit_hamming(i), bit_error(i))
  bit_corrected(i) = bit_receiver(i)
  if (bit_receiver(i) /= bit_hamming(i)) then
   practical_error_before = practical_error_before + 1
  end if
 end do
 p_practical_error_before = practical_error_before/n2
 
! Perform Hamming coding again on the received bits
 do i = 1, n2, 7
  bit_hamming_uncorrected(i) = bit_receiver(i)
  bit_hamming_uncorrected(i+1) = bit_receiver(i+1)
  bit_hamming_uncorrected(i+2) = bit_receiver(i+2)
  bit_hamming_uncorrected(i+3) = bit_receiver(i+3)
  bit_hamming_uncorrected(i+4) = xor(xor(bit_receiver(i),bit_receiver(i+1)),bit_receiver(i+2))
  bit_hamming_uncorrected(i+5) = xor(xor(bit_receiver(i),bit_receiver(i+1)),bit_receiver(i+3))
  bit_hamming_uncorrected(i+6) = xor(xor(bit_receiver(i+1),bit_receiver(i+2)),bit_receiver(i+3))
 end do

! Error Correction based on symptoms, "&" is placed to continue next line (fortran95 cannot read long lines)
 do i = 1, n2, 7
  if ((bit_hamming_uncorrected(i+4) /= bit_receiver(i+4)) .and. (bit_hamming_uncorrected(i+5) /= bit_receiver(i+5)) &
.and. (bit_hamming_uncorrected(i+6) /= bit_receiver(i+6))) then
   bit_corrected(i) = xor(bit_receiver(i),1)
  else if ((bit_hamming_uncorrected(i+4) /= bit_receiver(i+4)) .and. (bit_hamming_uncorrected(i+5) /= bit_receiver(i+5)) &
.and. (bit_hamming_uncorrected(i+6) == bit_receiver(i+6))) then
   bit_corrected(i+1) = xor(bit_receiver(i+1),1)
  else if ((bit_hamming_uncorrected(i+4) /= bit_receiver(i+4)) .and. (bit_hamming_uncorrected(i+5) == bit_receiver(i+5)) &
.and. (bit_hamming_uncorrected(i+6) /= bit_receiver(i+6))) then
   bit_corrected(i+2) = xor(bit_receiver(i+2),1)
  else if ((bit_hamming_uncorrected(i+4) == bit_receiver(i+4)) .and. (bit_hamming_uncorrected(i+5) /= bit_receiver(i+5)) &
.and. (bit_hamming_uncorrected(i+6) /= bit_receiver(i+6))) then
   bit_corrected(i+3) = xor(bit_receiver(i+3),1)
  else if ((bit_hamming_uncorrected(i+4) /= bit_receiver(i+4)) .and. (bit_hamming_uncorrected(i+5) == bit_receiver(i+5)) &
.and. (bit_hamming_uncorrected(i+6) == bit_receiver(i+6))) then
   bit_corrected(i+4) = xor(bit_receiver(i+4),1)
  else if ((bit_hamming_uncorrected(i+4) == bit_receiver(i+4)) .and. (bit_hamming_uncorrected(i+5) /= bit_receiver(i+5)) &
.and. (bit_hamming_uncorrected(i+6) == bit_receiver(i+6))) then
   bit_corrected(i+5) = xor(bit_receiver(i+5),1)
  else if ((bit_hamming_uncorrected(i+4) == bit_receiver(i+4)) .and. (bit_hamming_uncorrected(i+5) == bit_receiver(i+5)) &
.and. (bit_hamming_uncorrected(i+6) /= bit_receiver(i+6))) then
   bit_corrected(i+6) = xor(bit_receiver(i+6),1)  
  end if
 end do

! Error Probability After Decoding
 practical_error_after = 0;
 do i = 1, n2
  if (bit_corrected(i) /= bit_hamming(i)) then
   practical_error_after = practical_error_after + 1
  end if
 end do
 p_practical_error_after = practical_error_after/n2

! Probability of Correct, and incorrect decoding
 p_theory_correct_decoding = (7*p*(1-p)*(1-p)*(1-p)*(1-p)*(1-p)*(1-p))+((1-p)*(1-p)*(1-p)*(1-p)*(1-p)*(1-p)*(1-p))
 p_theory_incorrect_decoding = 1 - p_theory_correct_decoding
 incorrect_decoding = 0
 do i = 1, n2, 7
  if ((bit_corrected(i) /= bit_hamming(i)) .or. (bit_corrected(i+1) /= bit_hamming(i+1)) .or. &
(bit_corrected(i+2) /= bit_hamming(i+2)) .or. (bit_corrected(i+3) /= bit_hamming(i+3)) .or. &
(bit_corrected(i+4) /= bit_hamming(i+4)) .or. (bit_corrected(i+5) /= bit_hamming(i+5)) .or. &
(bit_corrected(i+6) /= bit_hamming(i+6))) then
   incorrect_decoding = incorrect_decoding + 1
  end if
 end do
 p_incorrect_decoding = incorrect_decoding/m

! Results
 write (*,'(A)',advance='no') 'Practical error before decoding: '
 write (*,*) practical_error_before
 write (*,'(A)',advance='no') 'Practical error after decoding: '
 write (*,*) practical_error_after
 write (*,'(A)',advance='no') 'Probability practical error before decoding: '
 write (*,*) p_practical_error_before
 write (*,'(A)',advance='no') 'Probability practical error after decoding: '
 write (*,*) p_practical_error_after
 write (*,'(A)',advance='no') 'Incorrect decoding: '
 write (*,*) incorrect_decoding
 write (*,'(A)',advance='no') 'Probability of incorrect decoding: '
 write (*,*) p_incorrect_decoding
 write (*,'(A)',advance='no') 'Theoretical probability of incorrect decoding: '
 write (*,*) p_theory_incorrect_decoding


! Debugging purposes, uncomment them to see binary sequences, max 18 blocks
! do i = 1, n2
!  write(*,'(1i1.1)',advance='no') bit_hamming(i)
! end do
! write (*,*) ' '
! do i = 1, n2
!  write(*,'(1i1.1)',advance='no') bit_error(i)
! end do
! write (*,*) ' '
! do i = 1, n2
!  write(*,'(1i1.1)',advance='no') bit_receiver(i)
! end do
!  write (*,*) ' '
!  do i = 1, n2
!  write(*,'(1i1.1)',advance='no') bit_corrected(i)
! end do
!  write (*,*) ' '

end program Memoryless_Error_Hamming
program Piece_Wise_Linear_Error

! For safe purposes
 implicit none

! Type declarations
 integer :: m, n1, n2, i, j, s1, s2, s3
 real :: c, c_error, p, practical_error_before, p_practical_error_before, practical_error_after
 real :: p_practical_error_after, incorrect_decoding, p_incorrect_decoding
 real :: p_theory_correct_decoding, p_theory_incorrect_decoding
 real :: p1, p2, a, a1, a2, a3, c1, c2, d1, d2
 real, dimension(40000000) :: x
 integer, dimension(40000000) :: bit
 integer, dimension(70000000) :: bit_hamming, bit_error, bit_receiver, bit_hamming_uncorrected, bit_corrected
 character(len=20) :: firstname, lastname, text
  
 write (*,'(A)',advance='no') 'Enter number of blocks: '
 read (*,*) m
 n1 = m*4
 n2 = m*7
 c = 0.499999
 x(1) = 0.333333

! Uncomment to use memoryless source
! Memoryless Generated Source (here n is number of bits)
! x(1) = 0.333333
! do i = 1, n1-1
!  if (x(i) >= 0 .and. x(i) < c) then
!   x(i+1) = x(i)/c
!   bit(i) = 0
!  else
!   x(i+1) = (1-x(i))/(1-c)
!   bit(i) = 1
!  end if
! end do

! PWL Generated Source
 p1 = (1-c)*p2/c
 a = 1/(1-(p1+p2))
 if (p1+p2 < 1) then
  c1 = c-(c/a)
  c2 = c+((1-c)/a)
  d1 = c1*(1-c)
  d2 = 1-((1-c2)*c)
  a1 = -c/(c1-d1)
  a2 = a
  a3 = (c-1)/(d2-c2)
  do i = 1, n2
   if (x(i) >= 0 .and. x(i) < c1) then
    x(i+1) = (a1*(x(i)-d1))+c
   else if (x(i) >= c1 .and. x(i) < c2) then
    x(i+1) = a2*(x(i)-c1)
   else
    x(i+1) = (a3*(x(i)-c2))+1
   end if
   if (x(i) >= 0 .and. x(i) < c) then
    bit(i) = 0
   else
    bit(i) = 1
   end if
  end do
 else
  c1 = c-((c-1)/a)
  c2 = c-(c/a)
  d1 = c1*(1-c)
  d2 = 1-((1-c2)*(1-c))
  a1 = -c/(c1-d1)
  a2 = a
  a3 = c/(d2-c2)
  do i = 1, n2
   if (x(i) >= 0 .and. x(i) < c1) then
    x(i+1) = (a1*(x(i)-d1))+c
   else if (x(i) >= c1 .and. x(i) < c2) then
    x(i+1) = (a2*(x(i)-c1))+1
   else
    x(i+1) = a3*(x(i)-c2)
   end if
   if (x(i) >= 0 .and. x(i) < c) then
    bit(i) = 0
   else
    bit(i) = 1
   end if
  end do
 end if

! Hamming Codes
 j = 1
 do i = 1, n2, 7
  bit_hamming(i) = bit(j)
  bit_hamming(i+1) = bit(j+1)
  bit_hamming(i+2) = bit(j+2)
  bit_hamming(i+3) = bit(j+3)
  bit_hamming(i+4) = xor(xor(bit(j),bit(j+1)),bit(j+2))
  bit_hamming(i+5) = xor(xor(bit(j),bit(j+1)),bit(j+3))
  bit_hamming(i+6) = xor(xor(bit(j+1),bit(j+2)),bit(j+3))
  j = j+4
 end do

! Error Sequence
 write (*,'(A)',advance='no') 'Enter error probability: '
 read (*,*) p
 c_error = 1 - p
 write (*,'(A)',advance='no') 'Enter p2: '
 read (*,*) p2
 p1 = (1-c_error)*p2/c_error
 a = 1/(1-(p1+p2))
 if (p1+p2 < 1) then
  c1 = c_error-(c_error/a)
  c2 = c_error+((1-c_error)/a)
  d1 = c1*(1-c_error)
  d2 = 1-((1-c2)*c_error)
  a1 = -c_error/(c1-d1)
  a2 = a
  a3 = (c_error-1)/(d2-c2)
  do i = 1, n2
   if (x(i) >= 0 .and. x(i) < c1) then
    x(i+1) = (a1*(x(i)-d1))+c_error
   else if (x(i) >= c1 .and. x(i) < c2) then
    x(i+1) = a2*(x(i)-c1)
   else
    x(i+1) = (a3*(x(i)-c2))+1
   end if
   if (x(i) >= 0 .and. x(i) < c_error) then
    bit_error(i) = 0
   else
    bit_error(i) = 1
   end if
  end do
 else
  c1 = c_error-((c_error-1)/a)
  c2 = c_error-(c_error/a)
  d1 = c1*(1-c_error)
  d2 = 1-((1-c2)*(1-c_error))
  a1 = -c_error/(c1-d1)
  a2 = a
  a3 = c_error/(d2-c2)
  do i = 1, n2
   if (x(i) >= 0 .and. x(i) < c1) then
    x(i+1) = (a1*(x(i)-d1))+c_error
   else if (x(i) >= c1 .and. x(i) < c2) then
    x(i+1) = (a2*(x(i)-c1))+1
   else
    x(i+1) = a3*(x(i)-c2)
   end if
   if (x(i) >= 0 .and. x(i) < c_error) then
    bit_error(i) = 0
   else
    bit_error(i) = 1
   end if
  end do
 end if

! Receiver Side (hamming encoded sequence + error sequence), calculate error probability before decoding, prefill future corrected bits
 practical_error_before = 0;
 do i = 1, n2
  bit_receiver(i) = xor(bit_hamming(i), bit_error(i))
  bit_corrected(i) = bit_receiver(i)
  if (bit_receiver(i) /= bit_hamming(i)) then
   practical_error_before = practical_error_before + 1
  end if
 end do
 p_practical_error_before = practical_error_before/n2
 
! Perform Hamming coding again on the received bits
  
 do i = 1, n2, 7
  bit_hamming_uncorrected(i) = bit_receiver(i)
  bit_hamming_uncorrected(i+1) = bit_receiver(i+1)
  bit_hamming_uncorrected(i+2) = bit_receiver(i+2)
  bit_hamming_uncorrected(i+3) = bit_receiver(i+3)
  bit_hamming_uncorrected(i+4) = xor(xor(bit_receiver(i),bit_receiver(i+1)),bit_receiver(i+2))
  bit_hamming_uncorrected(i+5) = xor(xor(bit_receiver(i),bit_receiver(i+1)),bit_receiver(i+3))
  bit_hamming_uncorrected(i+6) = xor(xor(bit_receiver(i+1),bit_receiver(i+2)),bit_receiver(i+3))
 end do

! Error Correction based on symptoms, "&" is placed to continue next line (fortran95 cannot read long lines)
 do i = 1, n2, 7
  if ((bit_hamming_uncorrected(i+4) /= bit_receiver(i+4)) .and. (bit_hamming_uncorrected(i+5) /= bit_receiver(i+5)) &
.and. (bit_hamming_uncorrected(i+6) /= bit_receiver(i+6))) then
   bit_corrected(i) = xor(bit_receiver(i),1)
  else if ((bit_hamming_uncorrected(i+4) /= bit_receiver(i+4)) .and. (bit_hamming_uncorrected(i+5) /= bit_receiver(i+5)) &
.and. (bit_hamming_uncorrected(i+6) == bit_receiver(i+6))) then
   bit_corrected(i+1) = xor(bit_receiver(i+1),1)
  else if ((bit_hamming_uncorrected(i+4) /= bit_receiver(i+4)) .and. (bit_hamming_uncorrected(i+5) == bit_receiver(i+5)) &
.and. (bit_hamming_uncorrected(i+6) /= bit_receiver(i+6))) then
   bit_corrected(i+2) = xor(bit_receiver(i+2),1)
  else if ((bit_hamming_uncorrected(i+4) == bit_receiver(i+4)) .and. (bit_hamming_uncorrected(i+5) /= bit_receiver(i+5)) &
.and. (bit_hamming_uncorrected(i+6) /= bit_receiver(i+6))) then
   bit_corrected(i+3) = xor(bit_receiver(i+3),1)
  else if ((bit_hamming_uncorrected(i+4) /= bit_receiver(i+4)) .and. (bit_hamming_uncorrected(i+5) == bit_receiver(i+5)) &
.and. (bit_hamming_uncorrected(i+6) == bit_receiver(i+6))) then
   bit_corrected(i+4) = xor(bit_receiver(i+4),1)
  else if ((bit_hamming_uncorrected(i+4) == bit_receiver(i+4)) .and. (bit_hamming_uncorrected(i+5) /= bit_receiver(i+5)) &
.and. (bit_hamming_uncorrected(i+6) == bit_receiver(i+6))) then
   bit_corrected(i+5) = xor(bit_receiver(i+5),1)
  else if ((bit_hamming_uncorrected(i+4) == bit_receiver(i+4)) .and. (bit_hamming_uncorrected(i+5) == bit_receiver(i+5)) &
.and. (bit_hamming_uncorrected(i+6) /= bit_receiver(i+6))) then
   bit_corrected(i+6) = xor(bit_receiver(i+6),1)  
  end if
 end do

! Error Probability After Decoding
 practical_error_after = 0;
 do i = 1, n2
  if (bit_corrected(i) /= bit_hamming(i)) then
   practical_error_after = practical_error_after + 1
  end if
 end do
 p_practical_error_after = practical_error_after/n2

! Probability of Correct, and incorrect decoding
 p_theory_correct_decoding = (((1-c_error)*p2*((1-p1)**5)) + (5*c_error*p1*p2*((1-p1)**4)) + (c_error*((1-p1)**5)*p1) + &
(c_error*((1-p1)**6)))
 p_theory_incorrect_decoding = 1 - p_theory_correct_decoding
 incorrect_decoding = 0
 do i = 1, n2, 7
  if ((bit_corrected(i) /= bit_hamming(i)) .or. (bit_corrected(i+1) /= bit_hamming(i+1)) .or. &
(bit_corrected(i+2) /= bit_hamming(i+2)) .or. (bit_corrected(i+3) /= bit_hamming(i+3)) .or. &
(bit_corrected(i+4) /= bit_hamming(i+4)) .or. (bit_corrected(i+5) /= bit_hamming(i+5)) .or. &
(bit_corrected(i+6) /= bit_hamming(i+6))) then
   incorrect_decoding = incorrect_decoding + 1
  end if
 end do
 p_incorrect_decoding = incorrect_decoding/m

! Results
 write (*,'(A)',advance='no') 'Practical error before decoding: '
 write (*,*) practical_error_before
 write (*,'(A)',advance='no') 'Practical error after decoding: '
 write (*,*) practical_error_after
 write (*,'(A)',advance='no') 'Probability practical error before decoding: '
 write (*,*) p_practical_error_before
 write (*,'(A)',advance='no') 'Probability practical error after decoding: '
 write (*,*) p_practical_error_after
 write (*,'(A)',advance='no') 'Incorrect decoding: '
 write (*,*) incorrect_decoding
 write (*,'(A)',advance='no') 'Probability of incorrect decoding: '
 write (*,*) p_incorrect_decoding
 write (*,'(A)',advance='no') 'Theoretical probability of incorrect decoding: '
 write (*,*) p_theory_incorrect_decoding


! Debugging purposes, uncomment them to see binary sequences, max 18 blocks
! do i = 1, n2
!  write(*,'(1i1.1)',advance='no') bit_hamming(i)
! end do
! write (*,*) ' '
! do i = 1, n2
!  write(*,'(1i1.1)',advance='no') bit_error(i)
! end do
! write (*,*) ' '
! do i = 1, n2
!  write(*,'(1i1.1)',advance='no') bit_receiver(i)
! end do
!  write (*,*) ' '
!  do i = 1, n2
!  write(*,'(1i1.1)',advance='no') bit_corrected(i)
! end do
!  write (*,*) ' '

end program Piece_Wise_Linear_Error

Mirrors