Gossips on Cryptography – Part 1

In this blog series , We will learn about encryption and decryption basics  in a very casual fashion . We will start discussing from origin of cryptography and then learns about the modern techniques

One of the important and main tech in encryption is Cryptography

Cryptography is a tech which is very very common and which is used in everywhere right now weather any software as follows :

• Routers we use in our homes
• Whatsapp we use to send receive messages
• Any site we are opening on https
• ……. And so on ..

Now , At the same time cryptography is very common , it is extremely complex also . Complex in understanding , implementing and even using it correctly .

Need of Cryptography:

• Why we need password on our phone
• Why to lock the door of our home when going outside and take keys with ourselves

Same is answer to use cryptography – To assures that the sender or receiver is the right one and they can only see the right form of data

To understand how it works let’s start from old age method known as : Caesar Cipher

Caesar Cipher

The Caesar Cipher is a type of substitution cipher in which each letter in a plaintext is shifted a certain number of places down the alphabet. This simple encryption technique is named after Julius Caesar, who used it to encrypt messages sent to his officials. The process of encryption is simple, a shift value ( also known as key) is chosen and each letter of the plaintext is shifted by that number of positions down the alphabet.

For example, with a shift of 3, A would be replaced by D, B would be replaced by E, and so on.

Plaintext: Sahil

Shift(Key): 3

Ciphertext: Vdklo

To decrypt the message, the shift value is used in the opposite direction.

Ciphertext: Vdklo

Shift(Key): -3

Plaintext: Sahil

It is important to note that the Caesar Cipher is very easy to break and should not be used for any serious encryption purposes as it can be broken by simple methods such as frequency analysis.

Here Algorithm is Simple: shift a letter N number of times and then replace it with that letter and Continue this process till our plain text will be Converted into cipher text .

A Caesar Cipher table is a tool that can be used to manually encrypt and decrypt messages using the Caesar Cipher method. It is a table that lists all of the letters of the alphabet and their corresponding encrypted or decrypted letters, based on a chosen shift value (or key).

Here is an example of a Caesar Cipher table with a shift(Key) value of 3:

And so on …

With Caesar cipher, someone can easily reverse engineer this Caesar cipher text and  decode the messages encrypted using this algorithm by identifying the pattern , one can easily determine the key .  This was just an example to get the readers familiar with the cryptography and encryption decryption concepts .

Till Now we have heard about some important terms

• Cryptography
• Algorithm
• Plain Text
• Key
• Cipher Text

Please keep them in mind as these are the generic terms used everywhere in world of encryption and decryption  .

In next Blog we will gossip about some other things like

• Making strong Algo as compare to caesar cipher
• What is latest Algo used now a days
• What type of Cryptography Techniques are present and when one should be used
• and so on …

Stay tuned for next part …

Slowness in Java Application due to increased FullGC events – G1GC

In this Blog, we will see one of the issues and solutions which I found when one one of our production servers , our java application is becoming slow due to more gc pauses .

I will explain this particular approach which can be one of the reasons for initiating more gc pauses .

To understand this article , one must have basic knowledge of G1GC algorithm of java for garbage collection .

Don’t worry if you don’t have knowledge of G1GC , I will make articles on basics of G1GC later and then you can read this article again .

So, Let’s start from what issue we are facing

Issue :  applications become unresponsive in between very frequently .

Analysis :

•  after debugging from jvm level stats from JMX bean dumping  it was clear that GC collection time was increased so much in between
• Heap Also increasing

After that we enabled gc log  by using -Xlog:gc=debug:file=/tmp/gc.log  in jvm arguments while starting application .

Analyzing gc.log , we found Full GC is triggering many times  and whenever FullGC triggers , it generally stop the application for sometime , in java language we call it STW (Stop the World) .

Generally there are following type of events in G1GC :

• Minor: Eden + Survivor From -> Survivor To
• Mixed: Minor + (# reclaimable Tenured regions / -XX:G1MixedGCCountTarget) regions of Tenured
• Full GC: All regions evacuated
• Minor/Mixed + To-space exhaustion: Minor/Mixed + rollback + Full GC

In a smoothly running application, batches of Minor events alternate with batches of Mixed events should be there only . Full GC events and To-space exhaustion are things you absolutely don’t want to see when running G1GC, they need to be detected and eliminated and if they are running they should be run by some external events like (jstack,jmap etc …) .

For in depth details of these events , as already stated I will make a blog series on explaining G1GC concepts   , for now you can search on the net .

Now, coming back to our debugging ,

We checked that no external command for taking thread dump or heap dump or histogram was made  that can possibly initiate Full GC event .

So , the question now was why this full GC is Triggering  .

On Further Researching we found that Humongous objects can be one of the reasons for triggering the Full GC event .

Now what is Humongous objects  ? ? ?

A Brief Definition is  : Any single data allocation ≥ G1HeapRegionSize/2 is considered a Humongous object, which is allocated out of contiguous regions of Free space, which are then added to Tenured. As Humongous objects are allocated out of Free space. Allocation failures trigger GC events. If an allocation failure from Free space triggers GC, the GC event will be a Full GC, which is very undesirable in most circumstances. To avoid Full GC events in an application with lots of Humongous objects, one must ensure the Free space pool is large enough as compared to Eden that Eden will always fill up first  .

So , We started checking if our application is generating Humongous objects .

And from gc.log we found that lots of Humongous objects are created which were the reasons for triggering Full GC events  .

I made following commands to check the Humongous objects specially in linux :

Step 1. : run following command on your gc.log

Command 1 :

`grep "source: concurrent humongous allocation" /tmp/gc.log | sed 's/.*allocation request: \([0-9]*\) bytes.*/\1/' > humoungous_humongoud_size.txt`

Command 2 :

`awk -F',' '{sum+=\$1} END{print sum;}' humoungous_humongoud_size.txt`

It will give you the size of humongous objects generated in my application.

We have java less than Oracle JDK 8u45 version and for java  greater than this , it is written in release notes that these Humongous objects also get collected in Minor events also .

Search for “G1 now collects unreachable Humongous objects during young collections” in the

So then we upgraded our jdk and issue frequency was minimized too much as these objects are now not triggering any major event like FullGC .

But one should also care about generating these large object

So , we also checked and analyzed one of the heaps and corrected the code not to generate these big objects  if not needed .

I hope this blog will be helpful .  Please comment , share and subscribe  .

Introduction

The new Content-Security-Policy HTTP response header helps you reduce XSS risks on modern browsers by declaring, which dynamic resources are allowed to load.

The core functionality of CSP can be divided into three areas:

• Requiring that all scripts are safe and trusted by the application owner (ideally by making sure they match an unpredictable identifier specified in the policy called the CSP nonce),
• Ensuring that page resources, such as images, stylesheets, or frames, are loaded from trusted sources,
• Miscellaneous other security features: preventing the application from being framed by untrusted domains(frame-ancestors), transparently upgrading all resource requests to HTTPS, and others.

Now the major concerns here are as follows :

• What should be the value of csp header to provide utmost security
• What should be the value of csp header so that it will be applicable by all vapt vendors and also no major change required if my application is old .
• What all things need to take care while deciding the header value  , How Strict the header value should be  .
• How to write code (if someone is making a new application) so that it remains CSP Standards compatible .

In this Blog all the above doubts will be cleared .

Example of a Strict Value of Header which can be applied at Production Setups :

``````Content-Security-Policy:
object-src 'none';
script-src 'nonce-{random}' 'unsafe-inline' 'unsafe-eval' 'strict-dynamic' https: http:;
base-uri 'none';
frame-ancestors https://example.com``````

Let’s look at the properties of this policy as interpreted by a modern browser:

• object-src ‘none’ Prevents fetching and executing plugin resources embedded using <object>, <embed> or <applet> tags. The most common example is Flash.
• script-src nonce-{random} ‘unsafe-inline’ The nonce directive means that <script> elements will be allowed to execute only if they contain a nonce attribute matching the randomly-generated value which appears in the policy.
Note: In the presence of a CSP nonce the unsafe-inline directive will be ignored by modern browsers. Older browsers, which don’t support nonces, will see unsafe-inline and allow inline scripts to execute.
• script-src ‘strict-dynamic’ https: http: ‘strict-dynamic’ allows the execution of scripts dynamically added to the page, as long as they were loaded by a safe, already-trusted script (see the specification).
Note: In the presence of ‘strict-dynamic’ the https: and http: whitelist entries will be ignored by modern browsers. Older browsers will allow the loading of scripts from any URL.
• ‘unsafe-eval’ allows the application to use the eval() JavaScript function. This reduces the protection against certain types of DOM-based XSS bugs, but makes it easier to adopt CSP. If your application doesn’t use eval(), you can remove this keyword and have a safer policy.
• base-uri ‘none’ Disables <base> URIs, preventing attackers from changing the locations of scripts loaded from relative URLs. If your application uses <base> tags, base-uri ‘self’ is usually also safe.
• frame-ancestors https://example.com  – This means that your application page can be opened in iframe of application page served by example.com only .

Now , if you have decided some CSP header value and want to check if it is ok to use or not ,

You can check  at the following link : CSP Evaluator

I hope First two concerns listed above are cleared and now moving to next one

Not only setting the correct value makes your application safe , we need to make some changes to client side code also to make the application CSP compatible   .

Code Changes

Random Nonce in Code

• Above we talk about the  random nonce which needs to be set on CSP header , but the question how security can be achieved by setting the random nonce in the header . the answer of this question is as follows :
• We also need to set this same nonce in the parent script tag also and when the browser is requesting a page it checks nonce value from script tag and header and matches it and if it does not match then mark script as unsafe .
• With ’strict-dynamic’, dynamically generated scripts implicitly inherit the nonce from the trusted script that created them. This way, already- executing, legitimate scripts can easily add new scripts to the DOM without extensive application changes. However, an attacker who finds an XSS bug, not knowing the correct nonce, is not able to abuse this functionality because they are prevented from executing scripts in the first place.

Code Before CSP compatibility

``````<script src="/path/to/script.js"></script>
<script>foo()</script>``````

Code After CSP compatibility

``````/path/to/script.js
<script nonce="\${nonce}">foo()</script>``````

Refactor inline event handlers and javascript: URIs

Inline event handlers (onclick=”…”, onerror=”…”) and <a href=”javascript:…”> links can be used to run scripts, so an attacker who finds an XSS bug could inject such HTML and execute malicious JavaScript. CSP requires refactoring those patterns into safer alternatives.

In most cases the changes will be straightforward. To refactor event handlers, rewrite them to be added from a JavaScript block:

Code before CSP compatability

``````<script> function doThings() { ... } </script>
<span onclick="doThings();">A thing.</span>``````

Code after CSP compatability

``````<span id="things">A thing.</span>

<script nonce="\${nonce}">
document.getElementById('things')
.addEventListener('click', function doThings() { ... });
});
</script>
``````

For javascript: URIs, you can use a similar pattern:

Code before CSP compatability

``<a href="javascript:linkClicked()">foo</a>``

Code after CSP compatability

``````<a id="foo">foo</a>

<script nonce="\${nonce}">
document.getElementById('foo')
});
</script>``````

Uses of eval().

If your application uses eval() to convert JSON string serializations into JS objects, you should refactor such instances to JSON.parse().

If you cannot remove all uses of eval() you can still set a CSP policy, but you will have to use the ‘unsafe-eval’ CSP keyword which will make your policy slightly less secure.

There are many frameworks in this time , where we do not write the html and js instead we write coe in java and framework convert it into js for example GWT . Now in these case  the code generated should be CSP compatible is the responsibility of frameworks  .

For more knowledge on CSP you can read the following research paper  and for knowing what all other options  can be added to csp header and browser support  visit content-security-policy.com

Please comment more suggestions if any related to CSP .

You can checkout the following video for explanation of this blog .

Elastic Stack Guide Part – 2 (Heartbeat)

In Our previous blog we have seen mainly the filebeat and metric beat and explored the system module of that . In this blog we will see the usage of heart beat and how to monitor the services using heart beat .

Heartbeat should not be installed on each server you monitor  , it should be installed on some separate servers from which you can monitor all url’s/services . For example we have one server deployed at x.x.x.x:8000 at some server in aws in north region , then we can install heart beat in our four server in each region(north,south,east,west) of aws and can monitor this server from all the servers to check weather services is UP from all india .

From these four server we can monitors all the services url’s .

For setting up the Heartbeat following is the link :

https://www.elastic.co/guide/en/beats/heartbeat/current/heartbeat-installation-configuration.html

https://github.com/elastic/uptime-contrib

And upload it to kibana in kibana objects using (import option avaiable in following url of kibana )

http://127.0.0.1:5601/app/management/kibana/objects

After that  , On Dashboard tab you can see the MetricBeat monitoring  ,

Also you can see the uptime app in kibana to check status and tls expiry time and history of all downtimes :

Followings are some screenshots:

Configuration in heartbeat.yml for setting the name of  machine from where url is pinging in heartbeat.yml

```processors:
# Optional, but recommended geo settings for the location Heartbeat is running in
geo:
# Token describing this location
name: sahil-machine
# Lat, Lon "
#location: "37.926868, -78.024902"
```

Configuration in hearebeat.yml for setting  to monitors urls :

```heartbeat.config.monitors:
# Directory + glob pattern to search for configuration files
path: \${path.config}/monitors.d/*.yml
# If enabled, heartbeat will periodically check the config.monitors path for changes
# How often to check for changes

# Configure monitors inline
heartbeat.monitors:
- type: http
# Set enabled to true (or delete the following line) to enable this example monitor
enabled: false
# ID used to uniquely identify this monitor in elasticsearch even if the config changes
id: my-monitor
# Human readable display name for this service in Uptime UI and elsewhere
name: My Monitor
# List or urls to query
urls: ["http://localhost:9200"]
schedule: '@every 10s'
# Total test connection and data exchange timeout
#timeout: 16s
# Name of corresponding APM service, if Elastic APM is in use for the monitored service.
#service.name: my-apm-service-name

- type: http
# Set enabled to true (or delete the following line) to enable this example monitor
enabled: true
# ID used to uniquely identify this monitor in elasticsearch even if the config changes
id: emerge-gurgaon
# Human readable display name for this service in Uptime UI and elsewhere
name: emerge-gurgaon
# List or urls to query
urls: ["https://app.ameyoemerge.in:8887/"]
schedule: '@every 10s'
# Total test connection and data exchange timeout
#timeout: 16s
# Name of corresponding APM service, if Elastic APM is in use for the monitored service.
#service.name: my-apm-service-name

- type: http
# Set enabled to true (or delete the following line) to enable this example monitor
enabled: true
# ID used to uniquely identify this monitor in elasticsearch even if the config changes
id: emerge-banglore-app24
# Human readable display name for this service in Uptime UI and elsewhere
name: emerge-banglore-app24
# List or urls to query
urls: ["https://app24.ameyoemerge.in:8887/"]
schedule: '@every 10s'
# Total test connection and data exchange timeout
#timeout: 16s
# Name of corresponding APM service, if Elastic APM is in use for the monitored service.
#service.name: my-apm-service-name
```

In the next blog we will explore Logstash with filebeat  . Happy debugging . . .

Elastic Stack Guide Part – 1

As nowadays lots of our servers are deployed on Cloud and many applications are running on these servers , it is impossible to monitor and analyze logs by going to each servers . Central Logging and Monitoring solution is a must in present time  .

In this Bog Series , we will learn about usage of Elastic Stack aka ELK  .

Overview  :

Elastic Stack is a group of open source products from Elastic designed to help users take data from any type of source and in any format and search, analyze, and visualize that data in real time. The product group was formerly known as ELK Stack, in which the letters in the name stood for the products in the group: ElasticSearch, Logstash and Kibana. A fourth product, Beats, was subsequently added to the stack, rendering the potential acronym unpronounceable. Elastic Stack can be deployed on premises or made available as Software as a Service

Architechture :

For a small-sized development environment, the classic architecture will look  as follows :

Here there are many different types of beats you can read them from https://www.elastic.co/beats/ . Each beat have different set of usecases  .

In this blog we will learn about two beats MetricBeat and FileBeat .

Note – LogStash is an options part in the architecture and should not be needed in most of the cases  . Read more about Logstash at https://www.elastic.co/logstash/

Usage Elastic Stack :

I am running experiments on CentOS7 machine and using rpm to setup the elastic stack .

Elastic Search  Installation :

Commands to install Elastic Search :

```curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.14.0-x86_64.rpm
sudo rpm -i elasticsearch-7.14.0-x86_64.rpm
sudo service elasticsearch start
```

How to check if Elastic Search is running :

```[root@localhost elk]# curl http://127.0.0.1:9200
{
"name" : "localhost.localdomain",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "MxKYDoJAQRG9D6krdFThsQ",
"version" : {
"number" : "7.14.0",
"build_flavor" : "default",
"build_type" : "rpm",
"build_hash" : "dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1",
"build_date" : "2021-07-29T20:49:32.864135063Z",
"build_snapshot" : false,
"lucene_version" : "8.9.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}

```

If you are getting output like above , it means elastic search is installed successfully  .

Note : To change listen address and port you can change in the following file : /etc/elasticsearch/elasticsearch.yml

Kibana :

Kibana is the Front end tool which communicates to Elastic search where anyone can monitor and analyze logs .

Commands to install kibana :

```curl -L -O https://artifacts.elastic.co/downloads/kibana/kibana-7.14.0-linux-x86_64.tar.gz
tar xzvf kibana-7.14.0-linux-x86_64.tar.gz
cd kibana-7.14.0-linux-x86_64/
./bin/kibana
```

Access kibana from the url :

http://127.0.0.1:5601/app/home#/

Note :  configure vim config/kibana.yml   for port and ip addressed for listening  settings  .

Beats

These will be installed on all servers from where we want to collect information  . they are like agents which will send data to Elastic Search  .

Enabling Metric Beat :

Every Beats supports different modules , it is up to the use that which module one wnts to enable in each beats  . if we talk about MetricBeat  it has many modules like System,Postgres,Nginx and so on . In this Blog we will see usage of System Module of MetricBeat .

Commands to install MetricBeat  :
```curl -L -O https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-7.14.0-x86_64.rpm
sudo rpm -vi metricbeat-7.14.0-x86_64.rpm
```
Enabling System Module  of Metric Beat :
```sudo metricbeat modules enable system
sudo metricbeat setup -e
sudo service metricbeat start
```

Here we are only enabling system module of metri beats  ,  there are many odule for basic monitoring of aplications like postgresql , nginx , tomcat etc .

Fo list of modules available in metric beats :  command is

metricbeat modules list

Yeipeee  Now we can Monitor System Data in kibana  as follows .

Open [Metricbeat System] Host overview ECS   in Dashboards in kibana UI . There you can apply filter of host of which one wants to see data  .

System Module MetricBeat Uses :   What analysis can be Done by System module of MetricBeat :

Traditionally after accessing linux servers , we gather system information by using many different commands and tools which also takes time , specially when there is some running issue on production .

Following is the list of information :

1. Size information of all partitions
3. InboundOutBound Traffic analysis per Ethernet Port
4. Load Avergae analysis of system
5. Top Proesses consuming High CPU  and RAM

All these type of information now can be seen in seconds for some particular host using kibana UI .

Following are some screenshots  :

Enabling FileBeat

Whether you’re collecting from security devices, cloud, containers, hosts, or OT, Filebeat helps you keep the simple things simple by offering a lightweight way to forward and centralize logs and files.

Commands to install Filebeat :
```curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.14.0-x86_64.rpm
rpm -ivh filebeat-7.14.0-x86_64.rpm
```

Note : For configuring filebeat that where to send data to elastic search or filebeat  configue in /etc/filebeat/filebeat.yml  , cureent as i have only one machine so no need to do an conf as defaut conf will work for me You can check the following lion : https://www.elastic.co/guide/en/beats/filebeat/7.14/configuring-howto-filebeat.html

enabling system logs module in filebeat :
```filebeat modules enable system
(for system logs if we want to set custom paths :   edit the file /etc/filebeat/modules.d/system.yml)  -- Generally no need to change these config in all cases

filebeat setup -e
sudo service filebeat start

```

Like  Metric Beat , FileBeats also have list of modules like postgres,nginx , and it also supports logging of popular framework like spring and can collect logs of these applications and provides ways to analyze them easily .

To check modules list available for filebeat use following command  :

[root@localhost elk]# filebeat modules list | less

System Module Filebeat Uses :

Now you can use Kibana UI to analyze system logs like messages etc  .

Open [Filebeat System] Syslog dashboard ECS  in Dashboard Tab  in Kibana  .

Following are some screen shots which one can see :

Configure filebeat for custom log files  :

Now we may have situation where none of Modules and integration with framework logging work in filebeat for our custom application log then in that case you can configure your input manually to configure path of logs to read and analayse them in logs and stream section in kibana UI

you can watch logs by : http://127.0.0.1:5601/app/logs/stream&nbsp;

Here you can search in logs by hostname , filepath  and can also search in whole message which is fetched .

By default only message column is shown . One can configure then in settings tabs of logs tabs in kibana  .

Following are some screenshot :

By Default logs lines are only one column , if for advance debugging we want to break log tine into columns then we need to use Logstash with Grok Filter .

In next blog we will see the usage of LogStash to break custom logs into columns for better understanding .

ThankYou all

Usage of Practical grep commands examples useful in real world debugging in Linux

In our Daily debugging we need to analyze logs files of various products . Reading those log files are not an easy task , it requires special debugging skills which can only be gained through experience or by god’s grace . Now while debugging we might need to extract some of data or we need to play with a log file which can not be done by just reading , there is need for commands .

There are many commands in linux which are used by debuggers like grep,awk,sed,wc,taskset,ps,sort,uniq,cut,xargs etc . . .

In this blog we will see usage of Practical grep commands examples useful in real world debugging  in Linux . The examples which we will see in this blog are super basic but very useful in real life which a beginner should read to enhance the debugging skills .

Let’s Go to the Practical Part

•  Grep the lines which contains some particular word
```[root@localhost playground]# cat file1.log
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer

[root@localhost playground]# grep 'sahil' file1.log
i am sahil
sahil is a software engineer
```

• Grep number of lines matched for a particualar word in a file
```[root@localhost playground]# cat file1.log
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer

[root@localhost playground]# grep -c 'sahil' file1.log
2

Another way :
[root@localhost playground]# grep 'sahil' file1.log  | wc -l
2
```

• Grep all the lines in which contains some word in a file with case insensitive
```[root@localhost playground]# cat file1.log
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer

[root@localhost playground]# grep -i 'sahil' file1.log
i am sahil
Sahil is a software engineer
sahil is a software engineer
[root@localhost playground]#
```

• Grep the lines in which either of two words are present in a file
```[root@localhost playground]# cat file1.log
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer

[root@localhost playground]# grep  'sahil\|software' file1.log
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer
[root@localhost playground]#

```

• Grep lines in which two words are present
```[root@localhost playground]# cat file1.log
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer

[root@localhost playground]# grep  'sahil' file1.log | grep 'software'
sahil is a software engineer
[root@localhost playground]# ^C
[root@localhost playground]#

```

• Eliminate lines which contains some word
```[root@localhost playground]# cat file1.log
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer

[root@localhost playground]# grep -v 'sahil' file1.log
hello
i am software engineer
Sahil is a software engineer

Eliminate case insensitively
[root@localhost playground]# grep -iv 'sahil' file1.log
hello
i am software engineer
[root@localhost playground]#
```

```[root@localhost playground]# cat file1.log
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer

[root@localhost playground]# grep  '^sahil' file1.log
sahil is a software engineer

```

• Matching the lines that end with a string
```[root@localhost playground]# cat file1.log
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer

[root@localhost playground]# grep  'engineer\$' file1.log
i am software engineer
Sahil is a software engineer
sahil is a software engineer
[root@localhost playground]#
```

• Getting n number of lines after each match
```[root@localhost playground]# cat file1.log
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer
[root@localhost playground]#

[root@localhost playground]# grep  'hello' file1.log
hello

[root@localhost playground]# grep -A 1  'hello' file1.log
hello
i am sahil

[root@localhost playground]# grep -A 2  'hello' file1.log
hello
i am sahil
i am software engineer

```

• Geeting n number of lines before each match
```[root@localhost playground]# cat file1.log
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer
[root@localhost playground]# grep  'i am sahil' file1.log
i am sahil
[root@localhost playground]# grep  -B 1 'i am sahil' file1.log
hello
i am sahil
[root@localhost playground]# grep  -B 2 'i am sahil' file1.log
hello
i am sahil
[root@localhost playground]#

```

• Grep n lines after and m lines before every match
```[root@localhost playground]# cat file1.log
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer
[root@localhost playground]# grep -A 2  -B 1 'i am sahil' file1.log
hello
i am sahil
i am software engineer
Sahil is a software engineer
[root@localhost playground]#
```

• Grep some word in more than one file in current directory
```[root@localhost playground]# cat file1.log
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer

[root@localhost playground]# cat file2.log
hello
i am sahil
i am tech blogger
Sahil is a tech blogger
sahil is a tech blogger

[root@localhost playground]# grep  'sahil' file1.log file2.log
file1.log:i am sahil
file1.log:sahil is a software engineer
file2.log:i am sahil
file2.log:sahil is a tech blogger
```

• Grep some word in all files in current directory
```[root@localhost playground]# grep  'sahil' *
file1.log:i am sahil
file1.log:sahil is a software engineer
file2.log:i am sahil
file2.log:sahil is a tech blogger
[root@localhost playground]#
```

• Check how much lines matched in each file
```[root@localhost playground]# grep -c  'sahil' *
file1.log:2
file2.log:2
file.log:0
```

• Grep using regular expression —
• Suppose the content of files are as follows
```[root@localhost playground]# cat file3.log
time taken by api is 1211 ms
time taken by api is 2000 ms
time taken by api is 3000 ms
time taken by api is 4000 ms
time taken by api is 50000 ms
time taken by api is 123 ms
time taken by api is 213 ms
time taken by api is 456 ms
time taken by api is 1000 ms
```

Now suppose we want to grep all the lines in which time taken by any api is more than 1 second or more than 1000 ms , it means it should have minimum 4 digit number .

Now grep command for this will be as follows :

```[root@localhost playground]# grep -P '[0-9]{4} ms' file3.log
time taken by api is 1211 ms
time taken by api is 2000 ms
time taken by api is 3000 ms
time taken by api is 4000 ms
time taken by api is 50000 ms
time taken by api is 1000 ms

If want to get 5 digit number

[root@localhost playground]# grep -P '[0-9]{5} ms' file3.log
time taken by api is 50000 ms
```

• Recursively grep in a directory  and sub directoies
```[root@localhost playground]# grep -R 'sahil' .
./dir1/file.log:i am sahil
./dir1/file.log:sahil is a software engineer
./file1.log:i am sahil
./file1.log:sahil is a software engineer
./file2.log:i am sahil
./file2.log:sahil is a tech blogger
[root@localhost playground]#
```

All above are basic use cases of grep . One can mix all the command options of grep to achieve the complex use cases and also one can also mix different grep commands using pipe operator to achieve complex use cases .

In future blogs i will explain some complex use cases and example how to achieve that using linux commands which can ease logs debugging .

Stay Tuned . . .

Vulnerability Updates | Week of June 21, 2021

The CISA Vulnerability Bulletin provides a summary of new vulnerabilities that have been recorded by the National Institute of Standards and Technology (NIST) National Vulnerability Database (NVD) in the past week. NVD is sponsored by CISA. In some cases, the vulnerabilities in the bulletin may not yet have assigned CVSS scores. Please visit NVD for updated vulnerability entries, which include CVSS scores once they are available.

Vulnerabilities are based on the Common Vulnerabilities and Exposures (CVE) vulnerability naming standard and are organized according to severity, determined by the Common Vulnerability Scoring System (CVSS) standard. The division of high, medium, and low severities correspond to the following scores:

• High: vulnerabilities with a CVSS base score of 7.0–10.0
• Medium: vulnerabilities with a CVSS base score of 4.0–6.9
• Low: vulnerabilities with a CVSS base score of 0.0–3.9

Entries may include additional information provided by organizations and efforts sponsored by CISA. This information may include identifying information, values, definitions, and related links. Patch information is provided when available. Please note that some of the information in the bulletin is compiled from external, open-source reports and is not a direct result of CISA analysis.

In this Blog , i am writing about High vulnerabilities only and some of Medium and Low if they it feels important to me .

For list of all vulnerabilities you can check CISA Bulletin .

High Vulnerabilities

For the complete list Please visit https://us-cert.cisa.gov/ncas/bulletins/sb21-179

Bulk Updation/Insertion of Database Tables in Java using Hibernate – Optimized Way

Hibernate is the most popular orm framework used to interact with databases in java . In this article  we will see what are the various ways using which bulk selection and updation in any table can be done and what is the most effective way when using the hibernate framework in java .

I  experimented with three ways which are as follows :

• Using Hibernate’s Query.list() method.
• Using ScrollableResults with FORWARD_ONLY scroll mode.
• Using ScrollableResults with FORWARD_ONLY scroll mode in a StatelessSession.

To decide which one gives best performance for our use case, following tests i performed using the above three ways listed.

• Select and update 1000 rows.

Let’s see the Code and results by applying above three ways to the operation stated above one by one.

Using Hibernate’s Query.list() method.

Code Executed :

```   List rows;
Session session = getSession();
Transaction transaction = session.beginTransaction();
try {
Query query = session.createQuery("FROM PersonEntity WHERE id > :maxId ORDER BY id").setParameter("maxId",
MAX_ID_VALUE);
query.setMaxResults(1000);
rows = query.list();
int count = 0;
for (Object row : rows) {
PersonEntity personEntity = (PersonEntity) row;
personEntity.setName(randomAlphaNumeric(30));
session.saveOrUpdate(personEntity);
//Always flush and clear the session after updating 50(jdbc_batch_size specified in hibernate.properties) rows
if (++count % 50 == 0) {
session.flush();
session.clear();
}
}
} finally {
if (session != null && session.isOpen()) {
transaction.commit();
session.close();
}
}
```

Tests Results :

• Time taken:- 360s to 400s
• Heap Pattern:- gradually increased from 13m to 51m(from jconsole).

Using ScrollableResults with FORWARD_ONLY scroll mode.

With this we are expecting that it should consume less memory that the 1st approach . Let’s see the results

Code Executed :

```Session session = getSession();
Transaction transaction = session.beginTransaction();
ScrollableResults scrollableResults = session
.createQuery("FROM PersonEntity WHERE id > " + MAX_ID_VALUE + " ORDER BY id")
.setMaxResults(1000).scroll(ScrollMode.FORWARD_ONLY);
int count = 0;
try {
while (scrollableResults.next()) {
PersonEntity personEntity = (PersonEntity) scrollableResults.get(0);
personEntity.setName(randomAlphaNumeric(30));
session.saveOrUpdate(personEntity);
if (++count % 50 == 0) {
session.flush();
session.clear();
}
}
} finally {
if (session != null && session.isOpen()) {
transaction.commit();
session.close();
}
}
```

Tests Results :

• Time taken:- 185s to 200s
• Heap Pattern:- gradually increased from 13mb to 41mb (measured same using jconsole)

Using ScrollableResults with FORWARD_ONLY scroll mode in a StatelessSession.

A stateless session does not implement a first-level cache nor interact with any second-level cache, nor does it implement transactional write-behind or automatic dirty checking, nor do operations cascade to associated instances. Collections are ignored by a stateless session. Operations performed via a stateless session bypass Hibernate’s event model and interceptors.

These type of session is always recommended in case of bulk updation as we really do not need these overheads of hibernate features in these type of usecases .

Code Executed :

``` StatelessSession session = getStatelessSession();
Transaction transaction = session.beginTransaction();
ScrollableResults scrollableResults = session
.createQuery("FROM PersonEntity WHERE id > " + MAX_ID_VALUE + " ORDER BY id")
.setMaxResults(TRANSACTION_BATCH_SIZE).scroll(ScrollMode.FORWARD_ONLY);
try {
while (scrollableResults.next()) {
PersonEntity personEntity = (PersonEntity) scrollableResults.get(0);
personEntity.setName(randomAlphaNumeric(20));
session.update(personEntity);
}
} finally {
if (session != null && session.isOpen()) {
transaction.commit();
session.close();
}
}
```

Tests Results :

• Time taken:- 185s to 200s
• Heap Pattern:- gradually increased from 13mb to 39mb

Results:-

• Using list():- time taken:- approx 750s, heap pattern:- gradually increased from 13mb to 74 mb
• Using ScrollableResultSet:- time taken:- approx 380s, heap pattern:- gradually increased from 13mb to 46mb
• Using Stateless:- time taken:- approx 380s, heap pattern:- gradually increased from 13mb to 43mb

Blocker Problem with all above approaches Tried

ScrollableResults and Stateless ScrollableResults give almost the same performance which is much better than Query.list(). But there is still one problem with all the above approaches. Locking, all the above approaches select and update the data in same transaction, this means for as long as the transaction is running, the rows on which updates have been performed will be locked and any other operations will have to wait for the transaction to finish.

Solution :

There are two things which we should do here to solve above problem :

•  we need to select and update data in different transactions.
• And updation of these types should be done in Batches

So again I performed the same tests as above but this time update was performed in a different transaction which was commited in batches of 50.

Note:- In case of Scrollable and Stateless we need a different session also, as we need the original session and transaction to scroll through the results.

Results using Batch Processing

• Using list():- time taken:- approx 400s, heap pattern:- gradually increased from 13mb to 61 mb
• Using ScrollableResultSet:- time taken:- approx 380s, heap pattern:- gradually increased from 13mb to 51mb
• Using Stateless:- time taken:- approx 190s, heap pattern:- gradually increased from 13mb to 44mb

Summary and Conclusion

As from all the above experimentation  , in cases where we need to do bulk selection and updation, the best approach in terms of memory consumption and time is as follows :

• Use ScrollableResults in a Stateless Session.
• Perform selection and updation in different transactions in batches of 20 to 50 (Batch Processing) (Note -*-  Batch size  can depend on the case to case basis)

Sample Code with the best approach

```  StatelessSession session = getStatelessSession();
Transaction transaction = session.beginTransaction();
ScrollableResults scrollableResults = session
.createQuery("FROM PersonEntity WHERE id > " + MAX_ID_VALUE + " ORDER BY id")
.setMaxResults(TRANSACTION_BATCH_SIZE).scroll(ScrollMode.FORWARD_ONLY);
int count = 0;
try {
while (scrollableResults.next()) {
PersonEntity personEntity = (PersonEntity) scrollableResults.get(0);
personEntity.setName(randomAlphaNumeric(5));
if (++count % 50 == 0) {
updateTransaction.commit();
}
}
} finally {
if (session != null && session.isOpen()) {
transaction.commit();
session.close();
}
}
```

With the   java frameworks like spring and others this code may be even more smaller , like one not needing to  take care of session closing etc . Above code is written in plain java using hibernate.

Please  try with large data and comment us the results , Also if you have some other better approach to do this please comment .

Thank You for reading the article

Vulnerability Updates | Week of June 14, 2021

The CISA Vulnerability Bulletin provides a summary of new vulnerabilities that have been recorded by the National Institute of Standards and Technology (NIST) National Vulnerability Database (NVD) in the past week. NVD is sponsored by CISA. In some cases, the vulnerabilities in the bulletin may not yet have assigned CVSS scores. Please visit NVD for updated vulnerability entries, which include CVSS scores once they are available.

Vulnerabilities are based on the Common Vulnerabilities and Exposures (CVE) vulnerability naming standard and are organized according to severity, determined by the Common Vulnerability Scoring System (CVSS) standard. The division of high, medium, and low severities correspond to the following scores:

• High: vulnerabilities with a CVSS base score of 7.0–10.0
• Medium: vulnerabilities with a CVSS base score of 4.0–6.9
• Low: vulnerabilities with a CVSS base score of 0.0–3.9

In this Blog , i am writing about High vulnerabilities only and some of Medium and Low if they it feels important to me .

For list of all vulnerabilities you can check CISA Bulletin .

High Vulnerabilities

For the complete list Please visit https://us-cert.cisa.gov/ncas/bulletins/sb21-172

Vulnerability Updates | Week of June 7, 2021

The CISA Vulnerability Bulletin provides a summary of new vulnerabilities that have been recorded by the National Institute of Standards and Technology (NIST) National Vulnerability Database (NVD) in the past week. NVD is sponsored by CISA. In some cases, the vulnerabilities in the bulletin may not yet have assigned CVSS scores. Please visit NVD for updated vulnerability entries, which include CVSS scores once they are available.

Vulnerabilities are based on the Common Vulnerabilities and Exposures (CVE) vulnerability naming standard and are organized according to severity, determined by the Common Vulnerability Scoring System (CVSS) standard. The division of high, medium, and low severities correspond to the following scores:

• High: vulnerabilities with a CVSS base score of 7.0–10.0
• Medium: vulnerabilities with a CVSS base score of 4.0–6.9
• Low: vulnerabilities with a CVSS base score of 0.0–3.9

In this Blog , i am writing about High vulnerabilities only and some of Medium and Low if they it feels important to me .

For list of all vulnerabilities you can check CISA Bulletin .

Medium Vulnerabilities

For the complete list Please visit https://us-cert.cisa.gov/ncas/bulletins/sb21-165